lm_message_node.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Feb 2009 16:10:28 +0200
changeset 8 951f92c66821
parent 7 5db1448eb857
child 11 a8c6460d612b
permissions -rw-r--r--
Path method
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
#include <lua.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
#include <lauxlib.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
#include <loudmouth/loudmouth.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
     6
#include "config.h"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
#include "lm_types.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
/// lm.message_node
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
/// Object, representing xml node of the message.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
/// Cannot be created directly, only as a part of message tree.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
/// lm.message_node.bless
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
/// Blesses given pointer to lm message node object.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
/// A: lightuserdata (C lm message node object)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
/// R: lm message node object
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
static int llm_message_node_bless_lua (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth message node lightuserdata expected");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
	llm_message_node_bless (L, lua_touserdata (L, 1));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
/// message_node:name
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
/// Gets a name of message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
/// R: string
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    27
int llm_message_node_name (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    29
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    30
	lua_pushstring (L, node->name);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
/// message_node:next
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
/// Gets next message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    37
int llm_message_node_next (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    39
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    40
	LmMessageNode *next = node->next;
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    41
	if (next)
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    42
		llm_message_node_bless (L, next);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
/// message_node:prev
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
/// Gets previous message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    51
int llm_message_node_prev (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    53
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    54
	LmMessageNode *prev = node->prev;
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    55
	if (prev)
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    56
		llm_message_node_bless (L, prev);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
/// message_node:children
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
/// Gets first child node (raw access used).
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    65
int llm_message_node_children (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    67
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    68
	LmMessageNode *child = node->children;
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    69
	if (child)
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    70
		llm_message_node_bless (L, child);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
/// message_node:parent
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
/// Gets parent message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    79
int llm_message_node_parent (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    81
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    82
	LmMessageNode *parent = node->parent;
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    83
	if (parent)
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    84
		llm_message_node_bless (L, parent);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
	else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
/// message_node:value
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
/// Gets or sets a value of message node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
/// A: string (optional value)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    93
/// R: string (if called without arguments) or lm message node object
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    94
int llm_message_node_value (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    96
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
		const char *value = luaL_checkstring (L, 2);
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    99
		lm_message_node_set_value (node, value);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   100
		lua_pop (L, 1);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   101
	} else // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   102
		lua_pushstring (L, lm_message_node_get_value (node));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   103
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
/// message_node:child
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
/// Gets or creates child node object with given name.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
/// A: string (name), string (optional value)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   110
int llm_message_node_child (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   112
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
	const char *name = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
	LmMessageNode *child;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   116
	if (lua_gettop (L) > 2) // Add
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   117
		child = lm_message_node_add_child (node, name,
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
						   luaL_checkstring (L, 3));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   119
	else { // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   120
		child = lm_message_node_get_child (node, name);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
		if (!child) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
			lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
			return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
		}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
	llm_message_node_bless (L, child);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
	// XXX lm_message_node_unref (child);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
/// message_node:find_child
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
/// Searches for node with a given name over all node subtree.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
/// A: string (name)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
/// R: lm message node object or nil
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   135
int llm_message_node_find_child (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   137
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
	const char *name = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
	LmMessageNode *child;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   141
	child = lm_message_node_get_child (node, name);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
	if (!child)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
		lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
	else {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
		llm_message_node_bless (L, child);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
		// XXX lm_message_node_unref (child);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
/// message_node:raw
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
/// Gets or sets raw mode flag for node.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
/// When set, value of node will not be escaped.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
/// A: boolean (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   155
/// V: boolean (if called with no apguments) or lm message node object
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   156
int llm_message_node_raw (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   158
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
		luaL_checktype (L, 2, LUA_TBOOLEAN);
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   161
		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
   162
		lua_pop (L, 1);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   163
	} else // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   164
		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
   165
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
/// message_node:attribute
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
/// Gets or sets value of node attribute with a given name.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
/// A: string (name), string (optional value)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   171
/// R: string (when called with one aprgument) or lm message node object
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   172
int llm_message_node_attribute (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   174
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
	const char *name = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
	if (lua_gettop (L) > 2) { // Set
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   177
		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
   178
		lua_pop (L, 2);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   179
	} else // Get
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   180
		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
   181
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
/// message_node:xml
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
/// Returns node representation in xml.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
/// R: string
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   187
int llm_message_node_xml (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
{
7
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   189
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
5db1448eb857 Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   190
	lua_pushstring (L, lm_message_node_to_string (node));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
8
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   194
/// message_node:path
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   195
/// Returns node with specified path to it.
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   196
/// If any element in a path cannot be found, it returns nil.
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   197
/// A: string (node name), ...
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   198
/// R: lm message node object or nil
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   199
int llm_message_node_path (lua_State *L)
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   200
{
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   201
	LmMessageNode *node = luaL_checkLmMessageNode (L, 1);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   202
	int i = 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   203
	while (i++ < lua_gettop (L)) {
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   204
		node = lm_message_node_get_child (node, luaL_checkstring (L, i));
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   205
		if (node == NULL) {
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   206
			lua_pushnil (L);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   207
			return 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   208
		}
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   209
	}
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   210
	llm_message_node_bless (L, node);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   211
	// XXX lm_message_node_unref (child);
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   212
	return 1;
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   213
}
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   214
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
/// message_node:pointer
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   216
/// Returns pointer to underlying C structure.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   217
/// R: lightuserdata
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
static int llm_message_node_pointer (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
	llm_message_node_t *object = luaL_checklm_message_node (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
	lua_pushlightuserdata (L, object->message_node);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
static int llm_message_node_gc (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
	llm_message_node_t *object = luaL_checklm_message_node (L, 1);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   228
	D ("Message node %X gc called", (int) object);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
	lm_message_node_unref (object->message_node);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
	return 0;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
static const luaL_Reg llm_message_node_reg_f[] = {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
	{ "bless", llm_message_node_bless_lua },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   235
	{ NULL,    NULL                       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   236
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
static const luaL_Reg llm_message_node_reg_m[] = {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
	{ "name",       llm_message_node_name       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   240
	{ "next",       llm_message_node_next       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   241
	{ "prev",       llm_message_node_prev       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   242
	{ "children",   llm_message_node_children   },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
	{ "parent",     llm_message_node_parent     },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
	{ "value",      llm_message_node_value      },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   245
	{ "child",      llm_message_node_child      },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   246
	{ "find_child", llm_message_node_find_child },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   247
	{ "attribute",  llm_message_node_attribute  },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   248
	{ "raw",        llm_message_node_raw        },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   249
	{ "xml",        llm_message_node_xml        },
8
951f92c66821 Path method
Myhailo Danylenko <isbear@ukrpost.net>
parents: 7
diff changeset
   250
	{ "path",       llm_message_node_path       },
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   251
	{ "pointer",    llm_message_node_pointer    },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   252
	{ "__gc",       llm_message_node_gc         },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   253
	{ NULL,         NULL                        },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
int luaopen_lm_message_node (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
	luaL_newmetatable (L, "loudmouth.message_node");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   259
	lua_pushstring (L, "__index");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
	lua_pushvalue (L, -2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
	lua_settable (L, -3);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
	luaL_register (L, NULL, llm_message_node_reg_m);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
	lua_pop (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
	luaL_register (L, "lm.message_node", llm_message_node_reg_f);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   265
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   266
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   267