lm_message_node.c
author Myhailo Danylenko <isbear@ukrpost.net>
Tue, 19 May 2009 02:12:02 +0300
changeset 23 13f03e604c8a
parent 19 d775d7289fe4
child 38 34a2b880615c
permissions -rw-r--r--
License notes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     1
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     2
/* Copyright 2009 Myhailo Danylenko
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     3
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     4
This file is part of lua-lm.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     5
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     6
lua-lm is free software: you can redistribute it and/or modify
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     7
it under the terms of the GNU General Public License as published by
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     8
the Free Software Foundation, either version 2 of the License, or
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     9
(at your option) any later version.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    10
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    11
This program is distributed in the hope that it will be useful,
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    14
GNU General Public License for more details.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    15
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    16
You should have received a copy of the GNU General Public License
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    17
along with this program.  If not, see <http://www.gnu.org/licenses/>. */
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
#include <lua.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
#include <lauxlib.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
#include <loudmouth/loudmouth.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    23
#include "config.h"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
#include "lm_types.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
/// lm.message_node
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
/// Object, representing xml node of the message.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
/// Cannot be created directly, only as a part of message tree.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
/// lm.message_node.bless
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
/// Blesses given pointer to lm message node object.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
/// A: lightuserdata (C lm message node object)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
/// message_node:name
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
/// Gets a name of message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
/// message_node:next
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
/// Gets next message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
/// message_node:prev
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
/// Gets previous message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
/// message_node:parent
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
/// Gets parent message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
/// message_node:value
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
/// Gets or sets a value of message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    99
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
/// message_node:child
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   116
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   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
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
/// message_node:find_child
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
/// Searches for node with a given name over all node subtree.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
/// A: string (name)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
/// 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
   142
int find_child_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   144
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
	const char *name = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
	LmMessageNode *child;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   148
	child = lm_message_node_get_child (node, name);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
	if (!child)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
	else {
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   152
		bless_lm_node (L, child);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
		// XXX lm_message_node_unref (child);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
/// message_node:raw
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
/// Gets or sets raw mode flag for node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
/// When set, value of node will not be escaped.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
/// A: boolean (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   162
/// 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
   163
int raw_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   165
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
		luaL_checktype (L, 2, LUA_TBOOLEAN);
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   168
		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
   169
		lua_pop (L, 1);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   170
	} else // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   171
		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
   172
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
/// message_node:attribute
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
/// Gets or sets value of node attribute with a given name.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
/// A: string (name), string (optional value)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   178
/// 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
   179
int attribute_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   181
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
	const char *name = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
	if (lua_gettop (L) > 2) { // Set
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   184
		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
   185
		lua_pop (L, 2);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   186
	} else // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   187
		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
   188
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
/// message_node:xml
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
/// Returns node representation in xml.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
/// R: string
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   194
int xml_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   196
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   197
	lua_pushstring (L, lm_message_node_to_string (node));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
8
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   201
/// message_node:path
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   202
/// Returns node with specified path to it.
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   203
/// If any element in a path cannot be found, it returns nil.
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   204
/// A: string (node name), ...
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   205
/// 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
   206
int path_lm_node (lua_State *L)
8
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   207
{
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   208
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   209
	int i = 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   210
	while (i++ < lua_gettop (L)) {
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   211
		node = lm_message_node_get_child (node, luaL_checkstring (L, i));
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   212
		if (node == NULL) {
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   213
			lua_pushnil (L);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   214
			return 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   215
		}
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   216
	}
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   217
	bless_lm_node (L, node);
8
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   218
	// XXX lm_message_node_unref (child);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   219
	return 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   220
}
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   221
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
/// message_node:pointer
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
/// Returns pointer to underlying C structure.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
/// R: lightuserdata
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   225
static int pointer_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
{
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   227
	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
   228
	lua_pushlightuserdata (L, object->node);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   232
static int gc_lm_node (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
{
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   234
	llm_node_t *object = luaL_checklm_node (L, 1);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   235
	D ("Message node %X gc called", (int) object);
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   236
	lm_message_node_unref (object->node);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
	return 0;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   240
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
   241
	{ "bless", bless_lua_lm_node },
12
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   242
	{ NULL,    NULL              },
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   245
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
   246
	{ "name",       name_lm_node       },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   247
	{ "next",       next_lm_node       },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   248
	{ "prev",       prev_lm_node       },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   249
	{ "parent",     parent_lm_node     },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   250
	{ "value",      value_lm_node      },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   251
	{ "child",      child_lm_node      },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   252
	{ "find_child", find_child_lm_node },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   253
	{ "attribute",  attribute_lm_node  },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   254
	{ "raw",        raw_lm_node        },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   255
	{ "xml",        xml_lm_node        },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   256
	{ "path",       path_lm_node       },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   257
	{ "pointer",    pointer_lm_node    },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   258
	{ "__gc",       gc_lm_node         },
12
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   259
	{ NULL,         NULL               },
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
int luaopen_lm_message_node (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
	luaL_newmetatable (L, "loudmouth.message_node");
19
d775d7289fe4 Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
   265
	lua_pushvalue (L, -1);
d775d7289fe4 Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents: 18
diff changeset
   266
	lua_setfield (L, -2, "__index");
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 8
diff changeset
   267
	luaL_register (L, NULL, reg_m_lm_node);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   268
	lua_pop (L, 1);
16
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 14
diff changeset
   269
	lua_newtable (L); // XXX we can specify here exact amount of fields
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 14
diff changeset
   270
	luaL_register (L, NULL, reg_f_lm_node);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   271
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   272
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   273