File: lang-entity.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (209 lines) | stat: -rw-r--r-- 5,126 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* I had a code-dream last night that lead me to believe i should * add the
 * entity lang renderers and language bindings.  I know * its funny but i
 * have code-dreams all the time.  Funny thing * is when they feel right the 
 * code i create is always good.  MW */

#include <glib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "entity.h"

/* Rational: * What am i thinking?  I'm thinking that we will be able to
 * create * some helper function that people can call when they want
 * something * simple done.  So mostly entity:<func> with be helper
 * functions. */

static GHashTable *entity_func_ht;

typedef EBuf *(*EntityFunc) (ENode * calling_node,
			     gchar * function, GSList * args);

static EBuf *
lang_entity_delete (ENode * calling_node, gchar * function, GSList * args)
{
    LangArg *arg_node;
    ENode *node;
    arg_node = (LangArg *) args->data;

    if (LANG_NODE == arg_node->type) {
	node = (ENode *) arg_node->data;
	enode_destroy (node);
    }

    return NULL;
}


static EBuf *
lang_entity_hide (ENode * calling_node, gchar * function, GSList * args)
{
    LangArg *arg_node;
    ENode *node;
    arg_node = (LangArg *) args->data;

    if (LANG_NODE == arg_node->type) {
	node = (ENode *) arg_node->data;
	enode_attrib_str (node, "visible", "false");
    }

    return NULL;
}

static EBuf *
lang_entity_hide_window (ENode * calling_node, gchar * function, GSList * args)
{
    LangArg *arg_node;
    ENode *node;
    arg_node = (LangArg *) args->data;

    if (LANG_NODE == arg_node->type) {
	node = (ENode *) arg_node->data;
	node = enode_parent (node, "window");

	if (!node)
	    return NULL;

	enode_attrib_str (node, "visible", "false");
    }

    return NULL;
}

static EBuf *
lang_entity_exit (ENode * calling_node, gchar * function, GSList * args)
{
    LangArg *arg_node;
    ENode *node;
    ENode *objnode;
    arg_node = (LangArg *) args->data;

    if (LANG_NODE == arg_node->type) {
	node = (ENode *) arg_node->data;

	if (ebuf_equal_str (node->element, "object"))
	    objnode = node;
	else
	    objnode = enode_parent (node, "object");

	if (!objnode)
	    return NULL;

	enode_destroy (objnode);
    }
    return NULL;
}

static EBuf *
lang_entity_quit (ENode * calling_node, gchar * function, GSList * args)
{
    entity_mainloop_exit ();

    /* Another brick in the -Wall */
    return NULL;
}

static EBuf *
lang_entity_version (ENode * calling_node, gchar * function, GSList * args)
{
    static EBuf *ver = NULL;

    if (!ver)
	ver = ebuf_new_with_str (VERSION);

    return (ver);
}

static EBuf *
lang_entity_languages (ENode * calling_node, gchar * function, GSList * args)
{
    static EBuf *langs = NULL;

    if (!langs) {
	GSList *tmp;
	GSList *lst = entity_supported_languages ();

	langs = ebuf_new ();

	tmp = lst;
	while (tmp) {
	    gchar *language = tmp->data;
	    ebuf_append_str (langs, language);

	    tmp = tmp->next;
	    if (tmp)
		ebuf_append_str (langs, ", ");
	}
	g_slist_free (lst);
    }

    return (langs);
}

static EBuf *
lang_entity_show_args (ENode * calling_node, gchar * function, GSList * args)
{
    LangArg *arg;
    GSList *tmp;
    ENode *node;
    EBuf *path;
    gint arg_int;
    gchar *arg_string;

    for (tmp = args; tmp; tmp = tmp->next) {
	arg = (LangArg *) tmp->data;
	if (LANG_NODE == arg->type) {
	    node = (ENode *) arg->data;
	    path = enode_path (node);
	    fprintf (stderr, "entity:%s node = %s\n", function, path->str);
	    ebuf_free (path);
	} else if (LANG_INT == arg->type) {
	    arg_int = arg->intdata;
	    fprintf (stderr, "entity:%s int = %i\n", function, arg_int);
	} else if (LANG_STRING == arg->type) {
	    arg_string = arg->data;
	    fprintf (stderr, "entity:%s string = %s\n", function, arg_string);
	} else if (LANG_BINSTRING == arg->type) {
	    fprintf (stderr, "entity:%s <binary data(not shown)>\n", function);
	}
    }

    return NULL;
}


static EBuf *
entity_function_execute (ENode * calling_node, gchar * function, GSList * args)
{
    EntityFunc func;

    func = g_hash_table_lookup (entity_func_ht, function);

    if (!func) {
	g_warning ("Undefined 'entity' function '%s'", function);
	return (NULL);
    }

    /* Just pass on the function... */
    return func (calling_node, function, args);
}

void
entity_lang_init (void)
{
    language_register ("entity", entity_function_execute);
    language_register ("e", entity_function_execute);

    entity_func_ht = g_hash_table_new (g_str_hash, g_str_equal);

    /* Setup the function string -> code ht. */
    g_hash_table_insert (entity_func_ht, "delete", lang_entity_delete);
    g_hash_table_insert (entity_func_ht, "exit", lang_entity_exit);
    g_hash_table_insert (entity_func_ht, "quit", lang_entity_quit);
    g_hash_table_insert (entity_func_ht, "hide", lang_entity_hide);
    g_hash_table_insert (entity_func_ht, "hide_window",
			 lang_entity_hide_window);
    g_hash_table_insert (entity_func_ht, "show_args", lang_entity_show_args);
    g_hash_table_insert (entity_func_ht, "version", lang_entity_version);
    g_hash_table_insert (entity_func_ht, "languages", lang_entity_languages);
}