switched from wpitchoune@gmail.com to jeanfi@gmail.com (my usual email)
[psensor.git] / src / plib / plib_luatpl.h
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #ifndef _P_LUATPL_H
21 #define _P_LUATPL_H
22
23 #include <lua.h>
24
25 #define LUATPL_ERROR_LUA_FILE_LOAD 1
26 #define LUATPL_ERROR_INIT 2
27 #define LUATPL_ERROR_LUA_EXECUTION 3
28 #define LUATPL_ERROR_WRONG_RETURN_TYPE 4
29 #define LUATPL_ERROR_LUA_STATE_OPEN 5
30
31 struct luatpl_error {
32         unsigned int code;
33
34         char *message;
35 };
36
37 /*
38   Generates a string which is the result of a Lua script execution.
39
40   The string is retrieved from the top element of the Lua stack
41   after the Lua script execution.
42
43   If not 'NULL' the 'init' function is called after Lua environment
44   setup and before Lua script execution. This function typically puts
45   input information for the Lua script into the stack.
46
47   'init_data' is passed to the second parameter of 'init' function
48
49   Returns the generated string on success, or NULL on error.
50  */
51 char *luatpl_generate(const char *lua,
52                       int (*init) (lua_State *, void *),
53                       void *init_data,
54                       struct luatpl_error *err);
55
56 /*
57   Generates a file which is the result of a Lua script execution.
58
59   See luatpl_generate function for 'init', 'init_data', and 'err'
60   parameters.
61
62   'dst_path' is the path of the generated file
63
64   Returns '1' on success, or '0' on error.
65
66  */
67 int luatpl_generate_file(const char *lua,
68                          int (*init) (lua_State *, void *),
69                          void *init_data,
70                          const char *dst_path,
71                          struct luatpl_error *err);
72
73 void luatpl_fprint_error(FILE *stream,
74                          const struct luatpl_error *err,
75                          const char *lua,
76                          const char *dst_path);
77
78 #endif