PageRenderTime 197ms CodeModel.GetById 180ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/fbusgen/coder_intf.c

http://ftk.googlecode.com/
C | 289 lines | 219 code | 41 blank | 29 comment | 31 complexity | 4fd395992d15d0c332c19b95ad1671ed MD5 | raw file
  1/*
  2 * File:    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-07-31 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "coder_intf.h"
 33
 34#define STR_LENGTH 127
 35
 36typedef struct _PrivInfo
 37{
 38	GString* func_types;
 39	GString* func_decls;
 40	GString* func_inlines;
 41	gboolean  has_interface;
 42    char interface[STR_LENGTH+1];
 43    char interface_lower[STR_LENGTH+1];
 44    char interface_upper[STR_LENGTH+1];
 45}PrivInfo;
 46
 47static gboolean coder_intf_end_interface(Coder* thiz)
 48{
 49	FILE* fp = NULL;
 50	char filename[260] = {0};
 51	PrivInfo* priv = (PrivInfo*)thiz->priv;
 52	if(!priv->has_interface)
 53	{
 54		return TRUE;
 55	}
 56
 57	mkdir(priv->interface, 0777);
 58	g_string_append_printf(priv->func_types, "typedef void (*%sDestroy)(%s* thiz);\n", priv->interface, priv->interface);
 59	g_string_append_printf(priv->func_decls, "\t%sDestroy destroy;\n\n", priv->interface);
 60	g_string_append_printf(priv->func_decls, "\tchar priv[1];\n");
 61	g_string_append_printf(priv->func_decls, "};\n");
 62
 63	g_string_append_printf(priv->func_inlines, "static inline void %s_destroy(%s* thiz)\n{\n",
 64		priv->interface_lower, priv->interface);
 65	g_string_append_printf(priv->func_inlines, "\tif(thiz != NULL && thiz->destroy != NULL)\n");
 66	g_string_append_printf(priv->func_inlines, "\t{\n");
 67	g_string_append_printf(priv->func_inlines, "\t\tthiz->destroy(thiz);\n");
 68	g_string_append_printf(priv->func_inlines, "\t}\n\n");
 69	g_string_append_printf(priv->func_inlines, "\treturn;\n");
 70	g_string_append_printf(priv->func_inlines, "}\n");
 71	snprintf(filename, sizeof(filename) - 1, "%s/%s.h", priv->interface, priv->interface_lower);
 72	fp = fopen(filename, "w+");
 73	if(fp != NULL)
 74	{
 75		coder_write_header(fp);
 76		fprintf(fp, "#ifndef %s_H\n", priv->interface_upper);
 77		fprintf(fp, "#define %s_H\n\n", priv->interface_upper);
 78		fprintf(fp, "#include \"fbus_typedef.h\"\n\n");
 79		fprintf(fp, "FTK_BEGIN_DECLS\n\n");
 80		fprintf(fp, "%s\n", priv->func_types->str);
 81		fprintf(fp, "%s\n", priv->func_decls->str);
 82		fprintf(fp, "%s\n", priv->func_inlines->str);
 83		fprintf(fp, "FTK_END_DECLS\n\n");
 84		fprintf(fp, "#endif/*%s_H*/\n", priv->interface_upper);
 85		fclose(fp);
 86	}
 87	g_string_free(priv->func_types, 1);
 88	g_string_free(priv->func_decls, 1);
 89	g_string_free(priv->func_inlines, 1);
 90
 91	priv->has_interface = FALSE;
 92
 93	return TRUE;
 94}
 95
 96static gboolean coder_intf_on_interface(Coder* thiz, const char* name, const char* parent)
 97{
 98	PrivInfo* priv = (PrivInfo*)thiz->priv;
 99
100	coder_intf_end_interface(thiz);
101	
102	priv->has_interface = TRUE;
103	strncpy(priv->interface, name, STR_LENGTH);
104	coder_name_to_lower(name, priv->interface_lower);
105	strcpy(priv->interface_upper, priv->interface_lower);
106	coder_to_upper(priv->interface_upper);
107
108	priv->func_types = g_string_sized_new(4096);
109	priv->func_decls = g_string_sized_new(4096);
110	priv->func_inlines = g_string_sized_new(4096);
111
112	g_string_append_printf(priv->func_types, "struct _%s;\n", name);
113	g_string_append_printf(priv->func_types, "typedef struct _%s %s;\n\n", name, name);
114	g_string_append_printf(priv->func_decls, "struct _%s\n{\n", name);
115
116
117	return TRUE;
118}
119
120typedef struct _TypeInfo
121{
122	int attr;
123	gboolean is_binary;
124	char name[STR_LENGTH+1];
125	char org_name[STR_LENGTH+1];
126}TypeInfo;
127
128static gboolean coder_get_type_info(IDL_tree tree, int attr, TypeInfo* type)
129{
130	const char* name = "";
131	type->attr = attr;
132	if(attr == IDL_PARAM_INOUT)
133	{
134		assert(!"Not supported.");
135	}
136
137	memset(type, 0x00, sizeof(TypeInfo));
138	if(IDL_NODE_IS_TYPE(tree))
139	{
140		switch(IDL_NODE_TYPE(tree))
141		{
142			case IDLN_TYPE_INTEGER:
143			{
144				name = "int";
145				break;
146			}
147			case IDLN_TYPE_STRING:
148			{
149				name = "String";
150				break;
151			}
152			default:
153			{
154				assert(!"Not supported");
155				break;
156			}
157
158		}
159	}
160	else if(IDL_NODE_TYPE(tree) == IDLN_IDENT)
161	{
162		name = IDL_IDENT(tree).str;
163	}
164	
165	strcat(type->org_name, name);
166	if(strcmp(name, "int") == 0)
167	{
168		strcat(type->name, "int");
169		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
170	}
171	else if(strcmp(name, "String") == 0)
172	{
173		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
174		strcat(type->name, "char*");
175		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
176	}
177	else if(strcmp(name, "FBusBinary") == 0)
178	{
179		type->is_binary = TRUE;
180	}
181	else
182	{
183		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
184		strcat(type->name, name);
185		strcat(type->name, "*");
186	}
187
188	return TRUE;
189}
190
191static gboolean coder_intf_on_function(Coder* thiz, struct _IDL_OP_DCL f)
192{
193	int i = 0;
194	TypeInfo  type = {0};
195	const char* param_name = NULL;
196	struct _IDL_LIST iter = {0};
197	char lower_func_name[STR_LENGTH+1] = {0};
198	PrivInfo* priv = (PrivInfo*)thiz->priv;
199	const char* func_name = IDL_IDENT(f.ident).str;
200	GString* params_list = g_string_sized_new(1024);
201	GString* params_call = g_string_sized_new(1023);
202
203	coder_name_to_lower(func_name, lower_func_name);
204	coder_get_type_info(f.op_type_spec, 0, &type);
205	
206	g_string_append_printf(priv->func_types, "typedef Ret (*%s%s)(%s* thiz", priv->interface, func_name, priv->interface);
207	g_string_append_printf(priv->func_decls, "	%s%s %s;\n", priv->interface, func_name, lower_func_name);
208
209	if(f.parameter_dcls != NULL)
210	{
211		for (i = 0, iter = IDL_LIST(f.parameter_dcls); iter.data != NULL; iter = IDL_LIST(iter.next))
212		{
213			int attr = IDL_PARAM_DCL(iter.data).attr;
214			param_name = IDL_IDENT(IDL_PARAM_DCL(iter.data).simple_declarator).str;
215			coder_get_type_info(IDL_PARAM_DCL(iter.data).param_type_spec, attr, &type);
216
217			g_string_append_printf(params_call, ", %s", param_name);
218			g_string_append_printf(params_list, ", %s %s", type.name, param_name);
219
220			if(iter.next == NULL)
221			{
222				break;
223			}
224		}
225	}
226	g_string_append_printf(priv->func_inlines, "static inline Ret %s_%s(%s* thiz%s)\n{\n", 
227		priv->interface_lower, lower_func_name, priv->interface,  params_list->str);
228	g_string_append_printf(priv->func_inlines, "	return_val_if_fail(thiz != NULL && thiz->%s != NULL, RET_FAIL);\n\n",
229		lower_func_name);
230	g_string_append_printf(priv->func_inlines, "	return thiz->%s(thiz%s);\n", lower_func_name, params_call->str);
231	g_string_append_printf(priv->func_inlines, "}\n\n");
232
233	g_string_append_printf(priv->func_types, "%s);\n", params_list->str);
234
235	g_string_free(params_list, 1);
236	g_string_free(params_call, 1);
237
238	return TRUE;
239}
240
241static gboolean coder_intf_on_const(Coder* thiz, struct _IDL_CONST_DCL c)
242{
243	return TRUE;
244}
245
246static gboolean coder_intf_on_struct(Coder* thiz, struct _IDL_TYPE_STRUCT s)
247{
248	return TRUE;
249}
250
251static gboolean coder_intf_on_enum(Coder* thiz, struct _IDL_TYPE_ENUM e)
252{
253	return TRUE;
254}
255
256static gboolean coder_intf_on_union(Coder* thiz, struct _IDL_TYPE_UNION u)
257{
258	return TRUE;
259}
260
261static void coder_intf_destroy(Coder* thiz)
262{
263	if(thiz != NULL)
264	{
265		coder_intf_end_interface(thiz);
266
267		g_free(thiz);
268	}
269
270	return;
271}
272
273Coder* coder_intf_create(const char* name)
274{
275	Coder* thiz = g_malloc0(sizeof(Coder) + sizeof(PrivInfo));
276
277	if(thiz != NULL)
278	{
279		thiz->on_interface = coder_intf_on_interface;
280		thiz->on_function  = coder_intf_on_function;
281		thiz->on_const     = coder_intf_on_const;
282		thiz->on_enum      = coder_intf_on_enum;
283		thiz->on_struct    = coder_intf_on_struct;
284		thiz->on_union     = coder_intf_on_union;
285		thiz->destroy      = coder_intf_destroy;
286	}
287
288	return thiz;
289}