`
jinghuainfo
  • 浏览: 1525663 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

手机探索者开发实录—代码产生器(下)

 
阅读更多

手机探索者开发实录代码产生器(下)

转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>


代码产生器的代码:


gen_framework.sh

  1. #!/bin/bash
  2. if["$2"=""]
  3. then
  4. echo$0"intf|implsome.def"
  5. exit0
  6. fi
  7. ACTION=$1
  8. INPUT=$2
  9. CLASS=${INPUT/.def/}
  10. CLASS_NAME=`echo$CLASS|sed"s/_//g"`
  11. CLASS_UPPER=`echo$CLASS|tra-zA-Z`
  12. CLASS_LOWER=`echo$CLASS|trA-Za-z`
  13. functiongen_h_begin()
  14. {
  15. echo"#ifndef$CLASS_UPPER"$1"_H"
  16. echo"#define$CLASS_UPPER"$1"_H"
  17. echo""
  18. echo"ME_DECLS_BEGIN"
  19. echo""
  20. return;
  21. }
  22. functiongen_h_end()
  23. {
  24. echo"ME_DECLS_END"
  25. echo""
  26. echo"#endif/*$CLASS_UPPER"$1"_H*/"
  27. echo""
  28. return;
  29. }
  30. functiongen_intf()
  31. {
  32. echo"#include/"mobile_explorer_types.h/""
  33. gen_h_begin
  34. echo"struct_$CLASS_NAME;"
  35. echo"typedefstruct_$CLASS_NAME$CLASS_NAME;"
  36. echo""
  37. awk-F:'{
  38. Class=$3;gsub(/_/,"",Class);
  39. Func=$4;gsub(/_/,"",Func);
  40. print"typedef"$2"(*"ClassFunc"Func)("$5");";
  41. }'$INPUT
  42. echo"";
  43. echo"struct_$CLASS_NAME"
  44. echo"{"
  45. awk-F:'{
  46. Class=$3;gsub(/_/,"",Class);
  47. Func=$4;gsub(/_/,"",Func);
  48. print""ClassFunc"Func"tolower($4)";";
  49. }'$INPUT
  50. echo""
  51. echo"charpriv[0];"
  52. echo"};"
  53. echo""
  54. awk-F:'{
  55. Class=$3;
  56. Func=$4;
  57. print"staticinline"$2""tolower(Class)"_"tolower(Func)"("$5")";
  58. print"{";
  59. if($2!="void")
  60. {
  61. print"me_return_val_if_fail(thiz!=NULL&&thiz->"tolower(Func)"!=NULL,ME_RET_PARAMS);";
  62. }
  63. else
  64. {
  65. print"me_return_if_fail(thiz!=NULL&&thiz->"tolower(Func)"!=NULL);";
  66. }
  67. print"";
  68. if($2!="void")
  69. {
  70. printf"return";
  71. }
  72. else
  73. {
  74. printf"";
  75. }
  76. printf"thiz->"tolower(Func)"(";
  77. system("./param_gen_call.sh"$5);
  78. print");"
  79. if($2!="void")
  80. {
  81. }
  82. else
  83. {
  84. print"";
  85. print"return;";
  86. }
  87. print"}/n"
  88. }'$INPUT
  89. gen_h_end
  90. return;
  91. }
  92. IMPL=$ACTION
  93. IMPL_UPPER=`echo$IMPL|tra-zA-Z`
  94. IMPL_LOWER=`echo$IMPL|trA-Za-z`
  95. functiongen_impl_h
  96. {
  97. echo"#include<$CLASS_LOWER.h>"
  98. gen_h_begin"_"$IMPL_UPPER
  99. echo"$CLASS_NAME*"$CLASS_LOWER"_"$IMPL_LOWER"_create();"
  100. gen_h_end"_"$IMPL_UPPER
  101. return;
  102. }
  103. functiongen_impl_c
  104. {
  105. echo"#include<"$CLASS_LOWER"_"$IMPL_LOWER".h>"
  106. echo""
  107. echo"typedefstruct_PrivInfo"
  108. echo"{"
  109. echo"unsignedintdummy;"
  110. echo"}PrivInfo;"
  111. echo""
  112. awk-vimpl=$IMPL-F:'{
  113. Class=$3;
  114. Func=$4;
  115. print"static"$2""tolower(Class)"_"tolower(impl)"_"tolower(Func)"("$5")";
  116. print"{";
  117. if($2!="void")
  118. {
  119. print"me_return_val_if_fail(thiz!=NULL,ME_RET_PARAMS);";
  120. }
  121. else
  122. {
  123. print"me_return_if_fail(thiz!=NULL);";
  124. }
  125. print""
  126. print"PrivInfo*priv=(PrivInfo*)thiz->priv;"
  127. print""
  128. if($2!="void")
  129. {
  130. print"return("$2")0;";
  131. }
  132. else
  133. {
  134. print"return;";
  135. }
  136. print"}/n"
  137. }'$INPUT
  138. echo""
  139. echo"$CLASS_NAME*"$CLASS_LOWER"_"$IMPL_LOWER"_create()"
  140. echo"{"
  141. echo"$CLASS_NAME*thiz=($CLASS_NAME*)malloc(sizeof($CLASS_NAME)+sizeof(PrivInfo));"
  142. echo""
  143. echo"if(thiz!=NULL)"
  144. echo"{"
  145. awk-vimpl=$IMPL-F:'{
  146. Class=$3;
  147. Func=$4;
  148. print"thiz->"tolower(Func)"="tolower(Class)"_"tolower(impl)"_"tolower(Func)";";
  149. }'$INPUT
  150. echo"}"
  151. echo""
  152. echo"returnthiz;"
  153. echo"}"
  154. return;
  155. }
  156. if["$ACTION"="intf"]
  157. then
  158. echo"gen_intf>$CLASS_LOWER.h"
  159. gen_intf>$CLASS_LOWER.h
  160. else
  161. echo"gen_impl_h>$CLASS_LOWER"_"$IMPL_LOWER.h"
  162. gen_impl_h>$CLASS_LOWER"_"$IMPL_LOWER.h
  163. echo"gen_impl_c>$CLASS_LOWER"_"$IMPL_LOWER.c"
  164. gen_impl_c>$CLASS_LOWER"_"$IMPL_LOWER.c
  165. fi


param_gen_call.sh

  1. #!/bin/bash
  2. echo$*|awk-F,'
  3. {
  4. for(i=1;i<=NF;i++)
  5. {
  6. system("./param_gen_call_one.sh"$i);
  7. if(i!=NF)
  8. {
  9. printf(",");
  10. }
  11. }
  12. }'

param_gen_call_one.sh

  1. #!/bin/bash
  2. echo$*|awk'{printf$NF}'

~~end~~



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics