author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 05 Mar 2016 15:51:12 +0200 | |
changeset 62 | d92358eafead |
parent 54 | 6bef2082e5f9 |
child 66 | a40beb82130c |
permissions | -rw-r--r-- |
23 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This file is part of lua-lm. |
|
5 |
||
6 |
lua-lm is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 2 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
||
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 18 |
|
19 |
#include <lua.h> |
|
20 |
#include <lauxlib.h> |
|
21 |
#include <loudmouth/loudmouth.h> |
|
22 |
||
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
23 |
#include "config.h" |
0 | 24 |
#include "lm_types.h" |
25 |
||
26 |
/// lm.message_node |
|
27 |
/// Object, representing xml node of the message. |
|
28 |
/// Cannot be created directly, only as a part of message tree. |
|
29 |
||
30 |
/// lm.message_node.bless |
|
31 |
/// Blesses given pointer to lm message node object. |
|
32 |
/// A: lightuserdata (C lm message node object) |
|
33 |
/// R: lm message node object |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
34 |
static int bless_lua_lm_node (lua_State *L) |
0 | 35 |
{ |
36 |
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth message node lightuserdata expected"); |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
37 |
bless_lm_node (L, lua_touserdata (L, 1)); |
0 | 38 |
return 1; |
39 |
} |
|
40 |
||
41 |
/// message_node:name |
|
42 |
/// Gets a name of message node. |
|
43 |
/// R: string |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
44 |
int name_lm_node (lua_State *L) |
0 | 45 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
46 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
47 |
lua_pushstring (L, node->name); |
0 | 48 |
return 1; |
49 |
} |
|
50 |
||
51 |
/// message_node:next |
|
52 |
/// Gets next message node. |
|
53 |
/// R: lm message node object or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
54 |
int next_lm_node (lua_State *L) |
0 | 55 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
56 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
57 |
LmMessageNode *next = node->next; |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
58 |
if (next) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
59 |
bless_lm_node (L, next); |
0 | 60 |
else |
61 |
lua_pushnil (L); |
|
62 |
return 1; |
|
63 |
} |
|
64 |
||
65 |
/// message_node:prev |
|
66 |
/// Gets previous message node. |
|
67 |
/// R: lm message node object or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
68 |
int prev_lm_node (lua_State *L) |
0 | 69 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
70 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
71 |
LmMessageNode *prev = node->prev; |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
72 |
if (prev) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
73 |
bless_lm_node (L, prev); |
0 | 74 |
else |
75 |
lua_pushnil (L); |
|
76 |
return 1; |
|
77 |
} |
|
78 |
||
79 |
/// message_node:parent |
|
80 |
/// Gets parent message node. |
|
81 |
/// R: lm message node object or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
82 |
int parent_lm_node (lua_State *L) |
0 | 83 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
84 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
85 |
LmMessageNode *parent = node->parent; |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
86 |
if (parent) |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
87 |
bless_lm_node (L, parent); |
0 | 88 |
else |
89 |
lua_pushnil (L); |
|
90 |
return 1; |
|
91 |
} |
|
92 |
||
93 |
/// message_node:value |
|
94 |
/// Gets or sets a value of message node. |
|
95 |
/// A: string (optional value) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
96 |
/// R: string (if called without arguments) or lm message node object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
97 |
int value_lm_node (lua_State *L) |
0 | 98 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
99 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
0 | 100 |
if (lua_gettop (L) > 1) { // Set |
101 |
const char *value = luaL_checkstring (L, 2); |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
102 |
lm_message_node_set_value (node, value); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
103 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
104 |
} else // Get |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
105 |
lua_pushstring (L, lm_message_node_get_value (node)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
106 |
return 1; |
0 | 107 |
} |
108 |
||
109 |
/// message_node:child |
|
110 |
/// Gets or creates child node object with given name. |
|
14
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
111 |
/// If name is omitted, first child node is returned. |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
112 |
/// A: string (name, optional), string (optional value) |
0 | 113 |
/// R: lm message node object or nil |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
114 |
int child_lm_node (lua_State *L) |
0 | 115 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
116 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
0 | 117 |
LmMessageNode *child; |
14
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
118 |
int top = lua_gettop (L); |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
119 |
if (top > 1) { |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
120 |
const char *name = luaL_checkstring (L, 2); |
0 | 121 |
|
14
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
122 |
if (top > 2) // Add |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
123 |
child = lm_message_node_add_child (node, name, |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
124 |
luaL_checkstring (L, 3)); |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
125 |
else // Get |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
126 |
child = lm_message_node_get_child (node, name); |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
127 |
} else |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
128 |
child = node->children; |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
129 |
|
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
130 |
if (child) |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
131 |
bless_lm_node (L, child); |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
132 |
// XXX lm_message_node_unref (child); // may be necessary on _get/_add'ed nodes |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
133 |
else |
4cb715fed99b
Merge children and child node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
134 |
lua_pushnil (L); |
0 | 135 |
return 1; |
136 |
} |
|
137 |
||
39
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
138 |
static int children_iterator_lm_node (lua_State *L) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
139 |
{ |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
140 |
const char *name = lua_tostring (L, lua_upvalueindex (1)); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
141 |
LmMessageNode *node; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
142 |
if (lua_type (L, 2) == LUA_TUSERDATA) { |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
143 |
node = luaL_checkLmMessageNode (L, 2); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
144 |
node = node -> next; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
145 |
} else { |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
146 |
node = luaL_checkLmMessageNode (L, 1); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
147 |
node = node -> children; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
148 |
} |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
149 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
150 |
if (name) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
151 |
while (node && g_strcmp0 (node -> name, name)) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
152 |
node = node -> next; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
153 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
154 |
if (node) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
155 |
bless_lm_node (L, node); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
156 |
// XXX lm_message_node_unref (child); // may be necessary on _get/_add'ed nodes |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
157 |
else |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
158 |
lua_pushnil (L); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
159 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
160 |
return 1; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
161 |
} |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
162 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
163 |
/// message_node:children |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
164 |
/// Returns iterator over child nodes of given node. Optionally filters childs by name. |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
165 |
/// A: string (name, optional) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
166 |
/// R: iterator function, lm message node object, nil |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
167 |
int children_lm_node (lua_State *L) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
168 |
{ |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
169 |
if (lua_type (L, 2) != LUA_TSTRING) |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
170 |
lua_pushnil (L); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
171 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
172 |
lua_pushcclosure (L, children_iterator_lm_node, 1); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
173 |
lua_pushvalue (L, 1); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
174 |
lua_pushnil (L); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
175 |
|
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
176 |
return 3; |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
177 |
} |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
178 |
|
0 | 179 |
/// message_node:find_child |
180 |
/// Searches for node with a given name over all node subtree. |
|
181 |
/// A: string (name) |
|
182 |
/// R: lm message node object or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
183 |
int find_child_lm_node (lua_State *L) |
0 | 184 |
{ |
39
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
185 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
186 |
const char *name = luaL_checkstring (L, 2); |
0 | 187 |
LmMessageNode *child; |
188 |
||
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
189 |
child = lm_message_node_get_child (node, name); |
0 | 190 |
if (!child) |
191 |
lua_pushnil (L); |
|
192 |
else { |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
193 |
bless_lm_node (L, child); |
0 | 194 |
// XXX lm_message_node_unref (child); |
195 |
} |
|
196 |
return 1; |
|
197 |
} |
|
198 |
||
199 |
/// message_node:raw |
|
200 |
/// Gets or sets raw mode flag for node. |
|
201 |
/// When set, value of node will not be escaped. |
|
202 |
/// A: boolean (optional) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
203 |
/// V: boolean (if called with no apguments) or lm message node object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
204 |
int raw_lm_node (lua_State *L) |
0 | 205 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
206 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
0 | 207 |
if (lua_gettop (L) > 1) { // Set |
208 |
luaL_checktype (L, 2, LUA_TBOOLEAN); |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
209 |
lm_message_node_set_raw_mode (node, lua_toboolean (L, 2)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
210 |
lua_pop (L, 1); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
211 |
} else // Get |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
212 |
lua_pushboolean (L, lm_message_node_get_raw_mode (node)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
213 |
return 1; |
0 | 214 |
} |
215 |
||
216 |
/// message_node:attribute |
|
217 |
/// Gets or sets value of node attribute with a given name. |
|
218 |
/// A: string (name), string (optional value) |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
219 |
/// R: string (when called with one aprgument) or lm message node object |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
220 |
int attribute_lm_node (lua_State *L) |
0 | 221 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
222 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
39
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
223 |
const char *name = luaL_checkstring (L, 2); |
0 | 224 |
if (lua_gettop (L) > 2) { // Set |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
225 |
lm_message_node_set_attribute (node, name, luaL_checkstring (L, 3)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
226 |
lua_pop (L, 2); |
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
227 |
} else // Get |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
228 |
lua_pushstring (L, lm_message_node_get_attribute (node, name)); |
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
229 |
return 1; |
0 | 230 |
} |
231 |
||
232 |
/// message_node:xml |
|
233 |
/// Returns node representation in xml. |
|
234 |
/// R: string |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
235 |
int xml_lm_node (lua_State *L) |
0 | 236 |
{ |
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
237 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
238 |
lua_pushstring (L, lm_message_node_to_string (node)); |
0 | 239 |
return 1; |
240 |
} |
|
241 |
||
8 | 242 |
/// message_node:path |
243 |
/// Returns node with specified path to it. |
|
244 |
/// If any element in a path cannot be found, it returns nil. |
|
245 |
/// A: string (node name), ... |
|
246 |
/// R: lm message node object or nil |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
247 |
int path_lm_node (lua_State *L) |
8 | 248 |
{ |
249 |
LmMessageNode *node = luaL_checkLmMessageNode (L, 1); |
|
250 |
int i = 1; |
|
251 |
while (i++ < lua_gettop (L)) { |
|
252 |
node = lm_message_node_get_child (node, luaL_checkstring (L, i)); |
|
253 |
if (node == NULL) { |
|
254 |
lua_pushnil (L); |
|
255 |
return 1; |
|
256 |
} |
|
257 |
} |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
258 |
bless_lm_node (L, node); |
8 | 259 |
// XXX lm_message_node_unref (child); |
260 |
return 1; |
|
261 |
} |
|
262 |
||
0 | 263 |
/// message_node:pointer |
264 |
/// Returns pointer to underlying C structure. |
|
265 |
/// R: lightuserdata |
|
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
266 |
static int pointer_lm_node (lua_State *L) |
0 | 267 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
268 |
llm_node_t *object = luaL_checklm_node (L, 1); |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
269 |
lua_pushlightuserdata (L, object->node); |
0 | 270 |
return 1; |
271 |
} |
|
272 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
273 |
static int gc_lm_node (lua_State *L) |
0 | 274 |
{ |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
275 |
llm_node_t *object = luaL_checklm_node (L, 1); |
38
34a2b880615c
Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
276 |
D ("Message node %p gc called", object); |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
277 |
lm_message_node_unref (object->node); |
0 | 278 |
return 0; |
279 |
} |
|
280 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
281 |
static const luaL_Reg reg_f_lm_node[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
282 |
{ "bless", bless_lua_lm_node }, |
12 | 283 |
{ NULL, NULL }, |
0 | 284 |
}; |
285 |
||
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
286 |
static const luaL_Reg reg_m_lm_node[] = { |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
287 |
{ "name", name_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
288 |
{ "next", next_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
289 |
{ "prev", prev_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
290 |
{ "parent", parent_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
291 |
{ "value", value_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
292 |
{ "child", child_lm_node }, |
39
c5a487f2fd7f
Add message/message_node children iterator method
Myhailo Danylenko <isbear@ukrpost.net>
parents:
38
diff
changeset
|
293 |
{ "children", children_lm_node }, |
11
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
294 |
{ "find_child", find_child_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
295 |
{ "attribute", attribute_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
296 |
{ "raw", raw_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
297 |
{ "xml", xml_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
298 |
{ "path", path_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
299 |
{ "pointer", pointer_lm_node }, |
a8c6460d612b
Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
300 |
{ "__gc", gc_lm_node }, |
12 | 301 |
{ NULL, NULL }, |
0 | 302 |
}; |
303 |
||
304 |
int luaopen_lm_message_node (lua_State *L) |
|
305 |
{ |
|
306 |
luaL_newmetatable (L, "loudmouth.message_node"); |
|
19
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
307 |
lua_pushvalue (L, -1); |
d775d7289fe4
Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents:
18
diff
changeset
|
308 |
lua_setfield (L, -2, "__index"); |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
309 |
luaL_setfuncs (L, reg_m_lm_node, 0); |
0 | 310 |
lua_pop (L, 1); |
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
14
diff
changeset
|
311 |
lua_newtable (L); // XXX we can specify here exact amount of fields |
54
6bef2082e5f9
v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents:
45
diff
changeset
|
312 |
luaL_setfuncs (L, reg_f_lm_node, 0); |
0 | 313 |
return 1; |
314 |
} |
|
315 |