~jfi/ubuntu/quantal/psensor/new-upstream

« back to all changes in this revision

Viewing changes to src/plib/plib_luatpl.c

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Philippe Orsini
  • Date: 2011-02-20 15:15:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110220151530-5xzhb3o39yaegrn0
Tags: upstream-0.6.1.5
ImportĀ upstreamĀ versionĀ 0.6.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010-2011 wpitchoune@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
#include <locale.h>
 
20
#include <libintl.h>
 
21
#define _(str) gettext(str)
 
22
 
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#include <lualib.h>
 
27
#include <lauxlib.h>
 
28
 
 
29
#include "plib_luatpl.h"
 
30
 
 
31
char *
 
32
luatpl_generate(const char *lua,
 
33
                int (*init) (lua_State *, void *),
 
34
                void *init_data,
 
35
                struct luatpl_error *err)
 
36
{
 
37
        lua_State *L;
 
38
        char *page = NULL;
 
39
 
 
40
        L = lua_open();
 
41
        if (!L) {
 
42
                err->code = LUATPL_ERROR_LUA_STATE_OPEN;
 
43
                return NULL;
 
44
        }
 
45
 
 
46
        luaL_openlibs(L);
 
47
 
 
48
        if (!init || init(L, init_data)) {
 
49
 
 
50
                if (!luaL_loadfile(L, lua)) {
 
51
                        if (!lua_pcall(L, 0, 1, 0)) {
 
52
                                if (lua_isstring(L, -1))
 
53
                                        page = strdup(lua_tostring(L, -1));
 
54
                                else
 
55
                                        err->code =
 
56
                                                LUATPL_ERROR_WRONG_RETURN_TYPE;
 
57
                        } else {
 
58
                                err->code = LUATPL_ERROR_LUA_EXECUTION;
 
59
                                err->message = strdup(lua_tostring(L, -1));
 
60
                        }
 
61
                } else {
 
62
                        err->code = LUATPL_ERROR_LUA_FILE_LOAD;
 
63
                }
 
64
        } else {
 
65
                err->code = LUATPL_ERROR_INIT;
 
66
        }
 
67
 
 
68
        lua_close(L);
 
69
 
 
70
        return page;
 
71
}
 
72
 
 
73
int
 
74
luatpl_generate_file(const char *lua,
 
75
                     int (*init) (lua_State *, void *),
 
76
                     void *init_data,
 
77
                     const char *dst_path,
 
78
                     struct luatpl_error *err)
 
79
{
 
80
        FILE *f;
 
81
        int ret;
 
82
        char *content;
 
83
 
 
84
        ret = 1;
 
85
 
 
86
        content = luatpl_generate(lua, init, init_data, err);
 
87
 
 
88
        if (content) {
 
89
                f = fopen(dst_path, "w");
 
90
 
 
91
                if (f) {
 
92
                        if (fputs(content, f) == EOF)
 
93
                                ret = 0;
 
94
 
 
95
                        if (fclose(f) == EOF)
 
96
                                ret = 0;
 
97
                } else {
 
98
                        ret = 0;
 
99
                }
 
100
 
 
101
                free(content);
 
102
        } else {
 
103
                ret = 0;
 
104
        }
 
105
 
 
106
        return ret;
 
107
}
 
108
 
 
109
void
 
110
luatpl_fprint_error(FILE *stream,
 
111
                    const struct luatpl_error *err,
 
112
                    const char *lua,
 
113
                    const char *dst_path)
 
114
{
 
115
        if (!err || !err->code)
 
116
                return ;
 
117
 
 
118
        switch (err->code) {
 
119
        case LUATPL_ERROR_LUA_FILE_LOAD:
 
120
                fprintf(stream,
 
121
                        _("LUATPL Error: failed to load Lua script: %s.\n"),
 
122
                        lua);
 
123
                break;
 
124
 
 
125
        case LUATPL_ERROR_INIT:
 
126
                fprintf(stream,
 
127
                        _("LUATPL Error: failed to call init function: %s.\n"),
 
128
                        lua);
 
129
                break;
 
130
 
 
131
        case LUATPL_ERROR_LUA_EXECUTION:
 
132
                fprintf(stream,
 
133
                        _("LUATPL Error:"
 
134
                          "failed to execute Lua script (%s): %s.\n"),
 
135
                        lua, err->message);
 
136
                break;
 
137
 
 
138
        case LUATPL_ERROR_WRONG_RETURN_TYPE:
 
139
                fprintf(stream,
 
140
                        _("LUATPL Error:"
 
141
                          "lua script (%s) returned a wrong type.\n"),
 
142
                        lua);
 
143
                break;
 
144
 
 
145
        case LUATPL_ERROR_LUA_STATE_OPEN:
 
146
                fprintf(stream,
 
147
                        _("LUATPL Error:"
 
148
                          "failed to open lua state.\n"));
 
149
                break;
 
150
 
 
151
 
 
152
        default:
 
153
                fprintf(stream,
 
154
                        _("LUATPL Error: code: %d.\n"),
 
155
                        err->code);
 
156
        }
 
157
}