PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/fbusgen/coder_share.c

http://ftk.googlecode.com/
C | 171 lines | 112 code | 30 blank | 29 comment | 7 complexity | b3fb4d35cdb798d2c1427c141e5f5ba6 MD5 | raw file
  1/*
  2 * File:    coder_share.h
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   generate code shared by service and client.
  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_share.h"
 33
 34#define STR_LENGTH 127
 35
 36typedef struct _PrivInfo
 37{
 38	GString*  req_decls;
 39	gboolean  has_interface;
 40    char interface[STR_LENGTH+1];
 41    char interface_lower[STR_LENGTH+1];
 42    char interface_upper[STR_LENGTH+1];
 43}PrivInfo;
 44
 45static gboolean coder_share_end_interface(Coder* thiz)
 46{
 47	FILE* fp = NULL;
 48	char filename[260] = {0};
 49	PrivInfo* priv = (PrivInfo*)thiz->priv;
 50	
 51	if(!priv->has_interface)
 52	{
 53		return TRUE;
 54	}
 55
 56	mkdir(priv->interface, 0777);
 57	g_string_append_printf(priv->req_decls, "	%s_REQ_NR\n", priv->interface_upper);
 58	g_string_append_printf(priv->req_decls, "}%s_REQ;\n", priv->interface_upper);
 59
 60	snprintf(filename, sizeof(filename) - 1, "%s/%s_share.h", priv->interface, priv->interface_lower);
 61	fp = fopen(filename, "w+");
 62	if(fp != NULL)
 63	{
 64		coder_write_header(fp);
 65		fprintf(fp, "#ifndef %s_SHARE_H\n", priv->interface_upper);
 66		fprintf(fp, "#define %s_SHARE_H\n\n", priv->interface_upper);
 67		fprintf(fp, "#include \"fbus_typedef.h\"\n\n");
 68		fprintf(fp, "FTK_BEGIN_DECLS\n\n");
 69		fprintf(fp, "%s\n", priv->req_decls->str);
 70		fprintf(fp, "#define FBUS_SERVICE_%s \"fbus.service.%s\"\n\n", priv->interface_upper, priv->interface_lower);
 71		fprintf(fp, "FTK_END_DECLS\n\n");
 72		fprintf(fp, "#endif/*%s_SHARE_H*/\n", priv->interface_upper);
 73		fclose(fp);
 74	}
 75	g_string_free(priv->req_decls, 1);
 76
 77	priv->has_interface = FALSE;
 78
 79	return TRUE;
 80}
 81
 82static gboolean coder_share_on_interface(Coder* thiz, const char* name, const char* parent)
 83{
 84	PrivInfo* priv = (PrivInfo*)thiz->priv;
 85
 86	coder_share_end_interface(thiz);
 87	
 88	priv->has_interface = 1;
 89	strncpy(priv->interface, name, STR_LENGTH);
 90	coder_name_to_lower(name, priv->interface_lower);
 91	strcpy(priv->interface_upper, priv->interface_lower);
 92	coder_to_upper(priv->interface_upper);
 93
 94	priv->req_decls = g_string_sized_new(4096);
 95	g_string_append_printf(priv->req_decls, "typedef enum _%s_REQ\n{", priv->interface_upper);
 96
 97	return TRUE;
 98}
 99
100typedef struct _TypeInfo
101{
102	int attr;
103	gboolean is_binary;
104	char name[STR_LENGTH+1];
105}TypeInfo;
106
107static gboolean coder_share_on_function(Coder* thiz, struct _IDL_OP_DCL f)
108{
109	char req_coder_name[STR_LENGTH+1];
110	char lower_func_name[STR_LENGTH+1] = {0};
111	PrivInfo* priv = (PrivInfo*)thiz->priv;
112	const char* func_name = IDL_IDENT(f.ident).str;
113
114	coder_name_to_lower(func_name, lower_func_name);
115	snprintf(req_coder_name, sizeof(req_coder_name)-1, "%s_%s", priv->interface_lower, lower_func_name);
116	coder_to_upper(req_coder_name);
117	g_string_append_printf(priv->req_decls, "	%s,\n", req_coder_name);
118
119
120	return TRUE;
121}
122
123static gboolean coder_share_on_const(Coder* thiz, struct _IDL_CONST_DCL c)
124{
125	return TRUE;
126}
127
128static gboolean coder_share_on_struct(Coder* thiz, struct _IDL_TYPE_STRUCT s)
129{
130	return TRUE;
131}
132
133static gboolean coder_share_on_enum(Coder* thiz, struct _IDL_TYPE_ENUM e)
134{
135	return TRUE;
136}
137
138static gboolean coder_share_on_union(Coder* thiz, struct _IDL_TYPE_UNION u)
139{
140	return TRUE;
141}
142
143static void coder_share_destroy(Coder* thiz)
144{
145	if(thiz != NULL)
146	{
147		coder_share_end_interface(thiz);
148
149		g_free(thiz);
150	}
151
152	return;
153}
154
155Coder* coder_share_create(const char* name)
156{
157	Coder* thiz = g_malloc0(sizeof(Coder) + sizeof(PrivInfo));
158
159	if(thiz != NULL)
160	{
161		thiz->on_interface = coder_share_on_interface;
162		thiz->on_function  = coder_share_on_function;
163		thiz->on_const     = coder_share_on_const;
164		thiz->on_enum      = coder_share_on_enum;
165		thiz->on_struct    = coder_share_on_struct;
166		thiz->on_union     = coder_share_on_union;
167		thiz->destroy      = coder_share_destroy;
168	}
169
170	return thiz;
171}