/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. * History:
  26. * ================================================================
  27. * 2010-07-31 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "coder_share.h"
  31. #define STR_LENGTH 127
  32. typedef struct _PrivInfo
  33. {
  34. GString* req_decls;
  35. gboolean has_interface;
  36. char interface[STR_LENGTH+1];
  37. char interface_lower[STR_LENGTH+1];
  38. char interface_upper[STR_LENGTH+1];
  39. }PrivInfo;
  40. static gboolean coder_share_end_interface(Coder* thiz)
  41. {
  42. FILE* fp = NULL;
  43. char filename[260] = {0};
  44. PrivInfo* priv = (PrivInfo*)thiz->priv;
  45. if(!priv->has_interface)
  46. {
  47. return TRUE;
  48. }
  49. mkdir(priv->interface, 0777);
  50. g_string_append_printf(priv->req_decls, " %s_REQ_NR\n", priv->interface_upper);
  51. g_string_append_printf(priv->req_decls, "}%s_REQ;\n", priv->interface_upper);
  52. snprintf(filename, sizeof(filename) - 1, "%s/%s_share.h", priv->interface, priv->interface_lower);
  53. fp = fopen(filename, "w+");
  54. if(fp != NULL)
  55. {
  56. coder_write_header(fp);
  57. fprintf(fp, "#ifndef %s_SHARE_H\n", priv->interface_upper);
  58. fprintf(fp, "#define %s_SHARE_H\n\n", priv->interface_upper);
  59. fprintf(fp, "#include \"fbus_typedef.h\"\n\n");
  60. fprintf(fp, "FTK_BEGIN_DECLS\n\n");
  61. fprintf(fp, "%s\n", priv->req_decls->str);
  62. fprintf(fp, "#define FBUS_SERVICE_%s \"fbus.service.%s\"\n\n", priv->interface_upper, priv->interface_lower);
  63. fprintf(fp, "FTK_END_DECLS\n\n");
  64. fprintf(fp, "#endif/*%s_SHARE_H*/\n", priv->interface_upper);
  65. fclose(fp);
  66. }
  67. g_string_free(priv->req_decls, 1);
  68. priv->has_interface = FALSE;
  69. return TRUE;
  70. }
  71. static gboolean coder_share_on_interface(Coder* thiz, const char* name, const char* parent)
  72. {
  73. PrivInfo* priv = (PrivInfo*)thiz->priv;
  74. coder_share_end_interface(thiz);
  75. priv->has_interface = 1;
  76. strncpy(priv->interface, name, STR_LENGTH);
  77. coder_name_to_lower(name, priv->interface_lower);
  78. strcpy(priv->interface_upper, priv->interface_lower);
  79. coder_to_upper(priv->interface_upper);
  80. priv->req_decls = g_string_sized_new(4096);
  81. g_string_append_printf(priv->req_decls, "typedef enum _%s_REQ\n{", priv->interface_upper);
  82. return TRUE;
  83. }
  84. typedef struct _TypeInfo
  85. {
  86. int attr;
  87. gboolean is_binary;
  88. char name[STR_LENGTH+1];
  89. }TypeInfo;
  90. static gboolean coder_share_on_function(Coder* thiz, struct _IDL_OP_DCL f)
  91. {
  92. char req_coder_name[STR_LENGTH+1];
  93. char lower_func_name[STR_LENGTH+1] = {0};
  94. PrivInfo* priv = (PrivInfo*)thiz->priv;
  95. const char* func_name = IDL_IDENT(f.ident).str;
  96. coder_name_to_lower(func_name, lower_func_name);
  97. snprintf(req_coder_name, sizeof(req_coder_name)-1, "%s_%s", priv->interface_lower, lower_func_name);
  98. coder_to_upper(req_coder_name);
  99. g_string_append_printf(priv->req_decls, " %s,\n", req_coder_name);
  100. return TRUE;
  101. }
  102. static gboolean coder_share_on_const(Coder* thiz, struct _IDL_CONST_DCL c)
  103. {
  104. return TRUE;
  105. }
  106. static gboolean coder_share_on_struct(Coder* thiz, struct _IDL_TYPE_STRUCT s)
  107. {
  108. return TRUE;
  109. }
  110. static gboolean coder_share_on_enum(Coder* thiz, struct _IDL_TYPE_ENUM e)
  111. {
  112. return TRUE;
  113. }
  114. static gboolean coder_share_on_union(Coder* thiz, struct _IDL_TYPE_UNION u)
  115. {
  116. return TRUE;
  117. }
  118. static void coder_share_destroy(Coder* thiz)
  119. {
  120. if(thiz != NULL)
  121. {
  122. coder_share_end_interface(thiz);
  123. g_free(thiz);
  124. }
  125. return;
  126. }
  127. Coder* coder_share_create(const char* name)
  128. {
  129. Coder* thiz = g_malloc0(sizeof(Coder) + sizeof(PrivInfo));
  130. if(thiz != NULL)
  131. {
  132. thiz->on_interface = coder_share_on_interface;
  133. thiz->on_function = coder_share_on_function;
  134. thiz->on_const = coder_share_on_const;
  135. thiz->on_enum = coder_share_on_enum;
  136. thiz->on_struct = coder_share_on_struct;
  137. thiz->on_union = coder_share_on_union;
  138. thiz->destroy = coder_share_destroy;
  139. }
  140. return thiz;
  141. }