/tools/fbusgen/coder_xxxx.c

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