PageRenderTime 78ms CodeModel.GetById 52ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/fbusgen/coder_xxxx.c

http://ftk.googlecode.com/
C | 309 lines | 239 code | 41 blank | 29 comment | 33 complexity | 82cb27eb65cc06859c37838db8fab024 MD5 | raw file
  1/*
  2 * File:    coder_xxxx.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   coder to generate frameworks of interface implementation.
  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 <sys/stat.h>
 33#include <sys/types.h>
 34#include "coder_intf.h"
 35
 36#define STR_LENGTH 127
 37
 38typedef struct _PrivInfo
 39{
 40	char*    name;
 41	char*    lower_name;
 42	GString* func_create;
 43	GString* func_empty;
 44	gboolean  has_interface;
 45    char interface[STR_LENGTH+1];
 46    char interface_lower[STR_LENGTH+1];
 47    char interface_upper[STR_LENGTH+1];
 48}PrivInfo;
 49
 50static gboolean coder_xxxx_end_interface(Coder* thiz)
 51{
 52	FILE* fp = NULL;
 53	char h_filename[260] = {0};
 54	char c_filename[260] = {0};
 55	char create_func[STR_LENGTH+1] = {0};
 56	PrivInfo* priv = (PrivInfo*)thiz->priv;
 57
 58	if(!priv->has_interface)
 59	{
 60		return TRUE;
 61	}
 62	
 63	mkdir(priv->interface, 0777);
 64	snprintf(create_func, STR_LENGTH, "%s_%s_create(void)", priv->interface_lower, priv->lower_name);
 65	
 66	snprintf(h_filename, sizeof(h_filename) - 1, "%s/%s_%s.h", priv->interface, priv->interface_lower, priv->lower_name);
 67	fp = fopen(h_filename, "w+");
 68	if(fp != NULL)
 69	{
 70		coder_write_header(fp);
 71		fprintf(fp, "#ifndef %s_IMPL_H\n", priv->interface_upper);
 72		fprintf(fp, "#define %s_IMPL_H\n\n", priv->interface_upper);
 73		fprintf(fp, "#include \"fbus_typedef.h\"\n\n");
 74		fprintf(fp, "#include \"%s.h\"\n\n", priv->interface_lower);
 75		fprintf(fp, "FTK_BEGIN_DECLS\n\n");
 76		fprintf(fp, "%s* %s;\n\n", priv->interface, create_func);
 77		fprintf(fp, "FTK_END_DECLS\n\n");
 78		fprintf(fp, "#endif/*%s_IMPL_H*/\n", priv->interface_upper);
 79		fclose(fp);
 80	}
 81	
 82	snprintf(h_filename, sizeof(h_filename) - 1, "%s_%s.h", priv->interface_lower, priv->lower_name);
 83	snprintf(c_filename, sizeof(c_filename) - 1, "%s/%s_%s.c", priv->interface, priv->interface_lower, priv->name);
 84	fp = fopen(c_filename, "w+");
 85	if(fp != NULL)
 86	{
 87		coder_write_header(fp);
 88		fprintf(fp, "#include \"%s\"\n\n", h_filename);
 89		fprintf(fp, "typedef struct _PrivInfo\n");
 90		fprintf(fp, "{\n");
 91		fprintf(fp, "\tint dummy;\n");
 92		fprintf(fp, "}PrivInfo;\n\n");
 93		fprintf(fp, "%s\n", priv->func_empty->str);
 94		fprintf(fp, "static void %s_%s_destroy(%s* thiz)\n", priv->interface_lower, priv->lower_name, priv->interface);
 95		fprintf(fp, "{\n");
 96		fprintf(fp, "	if(thiz != NULL)\n");
 97		fprintf(fp, "	{\n");
 98		fprintf(fp, "		FTK_FREE(thiz);\n");
 99		fprintf(fp, "	}\n\n");
100		fprintf(fp, "	return;\n");
101		fprintf(fp, "}\n\n");
102
103		fprintf(fp, "%s* %s\n", priv->interface, create_func);
104		fprintf(fp, "{\n");
105		fprintf(fp, "	%s* thiz = FTK_ZALLOC(sizeof(%s) + sizeof(PrivInfo));\n\n", priv->interface, priv->interface);
106		fprintf(fp, "	if(thiz != NULL)\n");
107		fprintf(fp, "	{\n");
108		fprintf(fp, "%s", priv->func_create->str);
109		fprintf(fp, "		thiz->destroy = %s_%s_destroy;\n", priv->interface_lower, priv->lower_name);
110		fprintf(fp, "	}\n\n");
111		fprintf(fp, "	return thiz;\n");
112		fprintf(fp, "}\n");
113		fclose(fp);
114	}
115
116	g_string_free(priv->func_empty, 1);
117	g_string_free(priv->func_create, 1);
118
119	priv->has_interface = FALSE;
120
121	return TRUE;
122}
123
124static gboolean coder_xxxx_on_interface(Coder* thiz, const char* name, const char* parent)
125{
126	PrivInfo* priv = (PrivInfo*)thiz->priv;
127
128	coder_xxxx_end_interface(thiz);
129	
130	priv->has_interface = TRUE;
131	strncpy(priv->interface, name, STR_LENGTH);
132	coder_name_to_lower(name, priv->interface_lower);
133	strcpy(priv->interface_upper, priv->interface_lower);
134	coder_to_upper(priv->interface_upper);
135
136	priv->func_empty = g_string_sized_new(4096);
137	priv->func_create = g_string_sized_new(4096);
138
139	return TRUE;
140}
141
142typedef struct _TypeInfo
143{
144	int attr;
145	gboolean is_binary;
146	char name[STR_LENGTH+1];
147	char org_name[STR_LENGTH+1];
148}TypeInfo;
149
150static gboolean coder_get_type_info(IDL_tree tree, int attr, TypeInfo* type)
151{
152	const char* name = "";
153	type->attr = attr;
154	if(attr == IDL_PARAM_INOUT)
155	{
156		assert(!"Not supported.");
157	}
158
159	memset(type, 0x00, sizeof(TypeInfo));
160	if(IDL_NODE_IS_TYPE(tree))
161	{
162		switch(IDL_NODE_TYPE(tree))
163		{
164			case IDLN_TYPE_INTEGER:
165			{
166				name = "int";
167				break;
168			}
169			case IDLN_TYPE_STRING:
170			{
171				name = "String";
172				break;
173			}
174			default:
175			{
176				assert(!"Not supported");
177				break;
178			}
179
180		}
181	}
182	else if(IDL_NODE_TYPE(tree) == IDLN_IDENT)
183	{
184		name = IDL_IDENT(tree).str;
185	}
186	
187	strcat(type->org_name, name);
188	if(strcmp(name, "int") == 0)
189	{
190		strcat(type->name, "int");
191		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
192	}
193	else if(strcmp(name, "String") == 0)
194	{
195		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
196		strcat(type->name, "char*");
197		strcat(type->name, attr == IDL_PARAM_OUT ? "*" : "");
198	}
199	else if(strcmp(name, "FBusBinary") == 0)
200	{
201		type->is_binary = TRUE;
202	}
203	else
204	{
205		strcat(type->name, attr == IDL_PARAM_OUT ? "" : "const ");
206		strcat(type->name, name);
207		strcat(type->name, "*");
208	}
209
210	return TRUE;
211}
212
213static gboolean coder_xxxx_on_function(Coder* thiz, struct _IDL_OP_DCL f)
214{
215	int i = 0;
216	TypeInfo  type = {0};
217	struct _IDL_LIST iter = {0};
218	const char* param_name = NULL;
219	char lower_func_name[STR_LENGTH+1] = {0};
220	PrivInfo* priv = (PrivInfo*)thiz->priv;
221	const char* func_name = IDL_IDENT(f.ident).str;
222	GString* params_list = g_string_sized_new(1024);
223	coder_name_to_lower(func_name, lower_func_name);
224	coder_get_type_info(f.op_type_spec, 0, &type);
225
226	if(f.parameter_dcls != NULL)
227	{
228		for (i = 0, iter = IDL_LIST(f.parameter_dcls); iter.data != NULL; iter = IDL_LIST(iter.next))
229		{
230			int attr = IDL_PARAM_DCL(iter.data).attr;
231			param_name = IDL_IDENT(IDL_PARAM_DCL(iter.data).simple_declarator).str;
232			coder_get_type_info(IDL_PARAM_DCL(iter.data).param_type_spec, attr, &type);
233			g_string_append_printf(params_list, ", %s %s", type.name, param_name);
234
235			if(iter.next == NULL)
236			{
237				break;
238			}
239		}
240	}
241	g_string_append_printf(priv->func_empty, "static Ret %s_%s_%s(%s* thiz%s)\n{\n", 
242		priv->interface_lower, priv->lower_name, lower_func_name, priv->interface,  params_list->str);
243	g_string_append_printf(priv->func_empty, "	return RET_OK;\n}\n\n");
244	
245	g_string_append_printf(priv->func_create, "		thiz->%s = %s_%s_%s;\n",
246		lower_func_name, priv->interface_lower, priv->lower_name, lower_func_name);
247
248	g_string_free(params_list, 1);
249
250	return TRUE;
251}
252
253static gboolean coder_xxxx_on_const(Coder* thiz, struct _IDL_CONST_DCL c)
254{
255	return TRUE;
256}
257
258static gboolean coder_xxxx_on_struct(Coder* thiz, struct _IDL_TYPE_STRUCT s)
259{
260	return TRUE;
261}
262
263static gboolean coder_xxxx_on_enum(Coder* thiz, struct _IDL_TYPE_ENUM e)
264{
265	return TRUE;
266}
267
268static gboolean coder_xxxx_on_union(Coder* thiz, struct _IDL_TYPE_UNION u)
269{
270	return TRUE;
271}
272
273static void coder_xxxx_destroy(Coder* thiz)
274{
275	if(thiz != NULL)
276	{
277		PrivInfo* priv = (PrivInfo*)thiz->priv;
278		
279		coder_xxxx_end_interface(thiz);
280		g_free(priv->name);
281		g_free(priv->lower_name);
282		g_free(thiz);
283	}
284
285	return;
286}
287
288Coder* coder_xxxx_create(const char* name)
289{
290	Coder* thiz = g_malloc0(sizeof(Coder) + sizeof(PrivInfo));
291
292	if(thiz != NULL)
293	{
294		PrivInfo* priv = (PrivInfo*)thiz->priv;
295		
296		priv->name = g_strdup(name);
297		priv->lower_name = g_strdup(name);
298		coder_to_lower(priv->lower_name);
299		thiz->on_interface = coder_xxxx_on_interface;
300		thiz->on_function  = coder_xxxx_on_function;
301		thiz->on_const     = coder_xxxx_on_const;
302		thiz->on_enum      = coder_xxxx_on_enum;
303		thiz->on_struct    = coder_xxxx_on_struct;
304		thiz->on_union     = coder_xxxx_on_union;
305		thiz->destroy      = coder_xxxx_destroy;
306	}
307
308	return thiz;
309}