gs_translate.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:5k
- /* -------------------------------------------------------------------- */
- /* SMS Client, send messages to mobile phones and pagers */
- /* */
- /* gs_translate.c */
- /* */
- /* Copyright (C) 1997,1998 Angelo Masci */
- /* */
- /* This library is free software; you can redistribute it and/or */
- /* modify it under the terms of the GNU Library General Public */
- /* License as published by the Free Software Foundation; either */
- /* version 2 of the License, or (at your option) any later version. */
- /* */
- /* This library is distributed in the hope that it will be useful, */
- /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
- /* Library General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU Library General Public */
- /* License along with this library; if not, write to the Free */
- /* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
- /* */
- /* You can contact the author at this e-mail address: */
- /* */
- /* angelo@styx.demon.co.uk */
- /* */
- /* -------------------------------------------------------------------- */
- /* $Id$
- -------------------------------------------------------------------- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "common.h"
- #include "logfile.h"
- #include "gs_token.h"
- #include "gs_list.h"
- #include "gs_translate.h"
- #include "gs_parser.h"
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static char *get_word(char *string, char **eptr);
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- TOKEN_HEAP *gs_parse_file(char *file)
- {
- TOKEN_HEAP
- *token_heap;
- TOKEN_ID
- token_id;
- init_builtin_heap();
- token_heap = (TOKEN_HEAP *)malloc(sizeof(TOKEN_HEAP));
- if (token_heap == NULL)
- { return NULL;
- }
- token_heap->list = NULL;
- if (gs_open(file))
- { return NULL;
- }
- if (parse_dictionary(token_heap))
- {
- token_id = get_next_token(token_heap);
- if (token_id != eof_tok)
- {
- gs_close();
- return NULL;
- }
- }
- else
- { gs_close();
- return NULL;
- }
-
- if (gs_close())
- { return NULL;
- }
- return token_heap;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static char *get_word(char *string, char **eptr)
- {
- static char word[1024],
- *word_ptr,
- *ptr;
- if (*string == ' ')
- { return NULL;
- }
- word_ptr = word;
- ptr = string;
- while (*ptr != ' ')
- {
- if (*ptr == '.')
- { break;
- }
- *word_ptr++ = *ptr++;
- }
- *word_ptr = ' ';
- if (*ptr != ' ')
- { ptr++;
- }
- *eptr = ptr;
- return word;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- char *get_strvalue(TOKEN_HEAP *heap, char *variable)
- {
- char *ptr,
- *str,
- *word;
- TOKEN *token;
- lprintf(LOG_VERYVERBOSE, "Searching for variable '%s'n", variable);
- str = NULL;
- word = get_word(variable, &ptr);
- while(word != NULL)
- {
- token = find_item(heap->list, word, T_IDENTIFIER);
- if (token == NULL)
- { return NULL;
- }
- if (get_token_type(heap, token) == T_IDENTIFIER)
- {
- if (get_token_indirect_type(heap, token) == TP_STRING)
- {
- str = get_token_indirect_string(heap, token);
- }
- else
- if ((get_token_indirect_type(heap, token) == TP_DICTIONARY) ||
- (get_token_indirect_type(heap, token) == TP_LIST))
- {
- heap = get_token_indirect_dictionary(heap, token);
- }
- }
- else
- { return NULL;
- }
- word = get_word(ptr, &ptr);
- }
- return str;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- TOKEN_HEAP *get_varlist(TOKEN_HEAP *heap, char *variable)
- {
- char *ptr,
- *word;
- TOKEN *token;
- word = get_word(variable, &ptr);
- while(word != NULL)
- {
- token = find_item(heap->list, word, T_IDENTIFIER);
- if (token == NULL)
- { return NULL;
- }
- if (get_token_type(heap, token) == T_IDENTIFIER)
- {
- if ((get_token_indirect_type(heap, token) == TP_DICTIONARY) ||
- (get_token_indirect_type(heap, token) == TP_LIST))
- {
- heap = get_token_indirect_dictionary(heap, token);
- }
- else
- { return NULL;
- }
- }
- else
- { return NULL;
- }
- word = get_word(ptr, &ptr);
- }
- return heap;
- }