gs_translate.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:5k
源码类别:

手机短信编程

开发平台:

Unix_Linux

  1. /* -------------------------------------------------------------------- */
  2. /* SMS Client, send messages to mobile phones and pagers */
  3. /* */
  4. /* gs_translate.c */
  5. /* */
  6. /*  Copyright (C) 1997,1998 Angelo Masci */
  7. /* */
  8. /*  This library is free software; you can redistribute it and/or */
  9. /*  modify it under the terms of the GNU Library General Public */
  10. /*  License as published by the Free Software Foundation; either */
  11. /*  version 2 of the License, or (at your option) any later version. */
  12. /* */
  13. /*  This library is distributed in the hope that it will be useful, */
  14. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of */
  15. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
  16. /*  Library General Public License for more details. */
  17. /* */
  18. /*  You should have received a copy of the GNU Library General Public */
  19. /*  License along with this library; if not, write to the Free */
  20. /*  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  21. /* */
  22. /*  You can contact the author at this e-mail address: */
  23. /* */
  24. /*  angelo@styx.demon.co.uk */
  25. /* */
  26. /* -------------------------------------------------------------------- */
  27. /* $Id$
  28.    -------------------------------------------------------------------- */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "common.h"
  32. #include "logfile.h"
  33. #include "gs_token.h"
  34. #include "gs_list.h"
  35. #include "gs_translate.h"
  36. #include "gs_parser.h"
  37. /* -------------------------------------------------------------------- */
  38. /* -------------------------------------------------------------------- */
  39. static char *get_word(char *string, char **eptr);
  40. /* -------------------------------------------------------------------- */
  41. /* -------------------------------------------------------------------- */
  42. TOKEN_HEAP *gs_parse_file(char *file)
  43. {
  44. TOKEN_HEAP 
  45. *token_heap;
  46. TOKEN_ID
  47. token_id;
  48. init_builtin_heap();
  49. token_heap = (TOKEN_HEAP *)malloc(sizeof(TOKEN_HEAP));
  50. if (token_heap == NULL)
  51. { return NULL;
  52. }
  53. token_heap->list = NULL;
  54. if (gs_open(file))
  55. { return NULL;
  56. }
  57. if (parse_dictionary(token_heap))
  58. {
  59. token_id = get_next_token(token_heap);
  60. if (token_id != eof_tok)
  61. {
  62. gs_close();
  63. return NULL;
  64. }
  65. }
  66. else
  67. { gs_close();
  68. return NULL;
  69. }
  70. if (gs_close())
  71. { return NULL;
  72. }
  73. return token_heap;
  74. }
  75. /* -------------------------------------------------------------------- */
  76. /* -------------------------------------------------------------------- */
  77. static char *get_word(char *string, char **eptr)
  78. {
  79. static char  word[1024],
  80. *word_ptr,
  81. *ptr;
  82. if (*string == '')
  83. { return NULL;
  84. }
  85. word_ptr = word;
  86. ptr = string;
  87. while (*ptr != '')
  88. {
  89. if (*ptr == '.')
  90. { break;
  91. }
  92. *word_ptr++ = *ptr++;
  93. }
  94. *word_ptr = '';
  95. if (*ptr != '')
  96. { ptr++;
  97. }
  98. *eptr = ptr;
  99. return word;
  100. }
  101. /* -------------------------------------------------------------------- */
  102. /* -------------------------------------------------------------------- */
  103. char *get_strvalue(TOKEN_HEAP *heap, char *variable)
  104. {
  105. char  *ptr,
  106. *str,
  107. *word;
  108. TOKEN *token;
  109. lprintf(LOG_VERYVERBOSE, "Searching for variable '%s'n", variable);
  110. str = NULL;
  111. word = get_word(variable, &ptr);
  112. while(word != NULL)
  113. {
  114. token = find_item(heap->list, word, T_IDENTIFIER);
  115. if (token == NULL)
  116. { return NULL;
  117. }
  118. if (get_token_type(heap, token) == T_IDENTIFIER)
  119. {
  120. if (get_token_indirect_type(heap, token) == TP_STRING)
  121. {
  122. str = get_token_indirect_string(heap, token);
  123. }
  124. else
  125. if ((get_token_indirect_type(heap, token) == TP_DICTIONARY) ||
  126.     (get_token_indirect_type(heap, token) == TP_LIST))
  127. {
  128. heap = get_token_indirect_dictionary(heap, token);
  129. }
  130. }
  131. else
  132. { return NULL;
  133. }
  134. word = get_word(ptr, &ptr);
  135. }
  136. return str;
  137. }
  138. /* -------------------------------------------------------------------- */
  139. /* -------------------------------------------------------------------- */
  140. TOKEN_HEAP *get_varlist(TOKEN_HEAP *heap, char *variable)
  141. {
  142. char  *ptr,
  143. *word;
  144. TOKEN *token;
  145. word = get_word(variable, &ptr);
  146. while(word != NULL)
  147. {
  148. token = find_item(heap->list, word, T_IDENTIFIER);
  149. if (token == NULL)
  150. { return NULL;
  151. }
  152. if (get_token_type(heap, token) == T_IDENTIFIER)
  153. {
  154. if ((get_token_indirect_type(heap, token) == TP_DICTIONARY) ||
  155.     (get_token_indirect_type(heap, token) == TP_LIST))
  156. {
  157. heap = get_token_indirect_dictionary(heap, token);
  158. }
  159. else
  160. { return NULL;
  161. }
  162. }
  163. else
  164. { return NULL;
  165. }
  166. word = get_word(ptr, &ptr);
  167. }
  168. return heap;
  169. }