/tools/fbusgen/main.c

http://ftk.googlecode.com/ · C · 154 lines · 107 code · 18 blank · 29 comment · 33 complexity · fefb37b67b536422557ed3e07985419d 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. * History:
  26. * ================================================================
  27. * 2010-07-31 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "coder_intf.h"
  31. #include "coder_client.h"
  32. #include "coder_xxxx.h"
  33. #include "coder_service.h"
  34. #include "coder_share.h"
  35. static gboolean coder_generator(IDL_tree_func_data *tfd, Coder* coder)
  36. {
  37. IDL_tree p = tfd->tree;
  38. if (IDL_NODE_TYPE (p) == IDLN_INTERFACE)
  39. {
  40. const char* parent = "";
  41. struct _IDL_INTERFACE intf = IDL_INTERFACE(p);
  42. const char* name = IDL_IDENT(intf.ident).str;
  43. if(intf.inheritance_spec != NULL)
  44. {
  45. struct _IDL_LIST list = IDL_LIST(intf.inheritance_spec);
  46. parent = IDL_IDENT(list.data).str;
  47. }
  48. return coder_on_interface(coder, name, parent);
  49. }
  50. else if(IDL_NODE_TYPE (p) == IDLN_OP_DCL)
  51. {
  52. return coder_on_function(coder, IDL_OP_DCL(p));
  53. }
  54. else if(IDL_NODE_TYPE (p) == IDLN_CONST_DCL)
  55. {
  56. return coder_on_const(coder, IDL_CONST_DCL(p));
  57. }
  58. else if(IDL_NODE_TYPE(p) == IDLN_TYPE_STRUCT)
  59. {
  60. return coder_on_struct(coder, IDL_TYPE_STRUCT(p));
  61. }
  62. else if(IDL_NODE_TYPE(p) == IDLN_TYPE_ENUM)
  63. {
  64. return coder_on_enum(coder, IDL_TYPE_ENUM(p));
  65. }
  66. else if(IDL_NODE_TYPE(p) == IDLN_TYPE_UNION)
  67. {
  68. return coder_on_union(coder, IDL_TYPE_UNION(p));
  69. }
  70. return TRUE;
  71. }
  72. struct _CoderNameAndCreator
  73. {
  74. char* name;
  75. CoderCreate create;
  76. }g_coders [] =
  77. {
  78. {"intf", coder_intf_create},
  79. {"client", coder_client_create},
  80. {"service", coder_service_create},
  81. {"share", coder_share_create},
  82. {NULL, NULL}
  83. };
  84. static Coder* coder_create(const char* name)
  85. {
  86. int i = 0;
  87. g_return_val_if_fail(name != NULL, NULL);
  88. for(i = 0; g_coders[i].name != NULL; i++)
  89. {
  90. if(strcmp(name, g_coders[i].name) == 0)
  91. {
  92. printf("Found coder %s.\n", name);
  93. return g_coders[i].create(name);
  94. }
  95. }
  96. printf("Cannt found coder %s, use xxxx\n", name);
  97. return coder_xxxx_create(name);
  98. }
  99. int main (int argc, char *argv[])
  100. {
  101. int rv = 0;
  102. IDL_ns ns = {0};
  103. IDL_tree tree = {0};
  104. Coder* coder = NULL;
  105. const char* type = NULL;
  106. const char* filename = NULL;
  107. unsigned long parse_flags = 0;
  108. IDL_check_cast_enable (TRUE);
  109. if (argc < 3 || (type = strstr(argv[1], "-type=")) == NULL)
  110. {
  111. fprintf (stderr, "usage: %s -type=intf|client|service|xxx idl \n", argv[0]);
  112. exit (1);
  113. }
  114. type += 6;
  115. filename = argv[2];
  116. rv = IDL_parse_filename (filename, NULL, NULL, &tree, &ns, parse_flags, IDL_WARNING1);
  117. if (rv == IDL_ERROR)
  118. {
  119. g_message ("IDL_ERROR: %s\n", filename);
  120. exit (1);
  121. }
  122. else if (rv < 0)
  123. {
  124. perror (filename);
  125. exit (1);
  126. }
  127. coder = coder_create(type);
  128. if(coder != NULL)
  129. {
  130. IDL_tree_walk_in_order (tree, (IDL_tree_func)coder_generator, coder);
  131. coder_destroy(coder);
  132. }
  133. IDL_ns_free (ns);
  134. IDL_tree_free (tree);
  135. return 0;
  136. }