sms_resource.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:6k
- /* -------------------------------------------------------------------- */
- /* SMS Client, send messages to mobile phones and pagers */
- /* */
- /* sms_resource.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 <string.h>
- #include <stdlib.h>
- #include "sms_error.h"
- #include "logfile.h"
- #include "token.h"
- #include "sms_resource.h"
- #include "common.h"
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void set_defaults(char *filename, RESOURCE *resource_list);
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void set_defaults(char *filename, RESOURCE *resource_list)
- {
- RESOURCE
- *resource;
- resource = resource_list;
- while (resource->type != RESOURCE_NULL)
- {
- if (!resource->found)
- {
- if (resource->type == RESOURCE_STRING)
- {
- if (resource->default_warning)
- { lprintf(LOG_WARNING, "Parsing '%s' Could not find %s, using default '%s'n",
- filename,
- (resource->name != NULL)?(resource->name):("NULL"),
- (resource->default_str_value != NULL)?(resource->default_str_value):("NULL"));
- }
- resource->str_value = resource->default_str_value;
- }
- else if (resource->type == RESOURCE_NUMERIC)
- {
- if (resource->default_warning)
- { lprintf(LOG_WARNING, "Parsing '%s' Could not find %s, using default %ldn", filename,
- (resource->name != NULL)?(resource->name):("NULL"),
- resource->default_int_value);
- }
- resource->int_value = resource->default_int_value;
- }
- }
- if (resource->type == RESOURCE_STRING)
- {
- if (resource->var != NULL)
- { *((char **)(resource->var)) = resource->str_value;
- }
- lprintf(LOG_VERBOSE, "%s %s = %sn",
- filename,
- (resource->name != NULL)?(resource->name):("NULL"),
- (resource->str_value != NULL)?(resource->str_value):("NULL"));
- }
- else if (resource->type == RESOURCE_NUMERIC)
- {
- if (resource->var != NULL)
- { *((long *)(resource->var)) = resource->int_value;
- }
- lprintf(LOG_VERBOSE, "%s %s = %ldn",
- filename,
- (resource->name != NULL)?(resource->name):("NULL"),
- resource->int_value);
- }
- resource++;
- }
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- int read_resource_file(char *filename, RESOURCE *resource_list, int warnings)
- {
- RESOURCE
- *resource;
- char line[MAXLINELEN],
- token[MAXLINELEN],
- value_token[MAXLINELEN],
- *src,
- *ptr;
-
- int type,
- line_count;
- FILE *fp;
- lprintf(LOG_VERBOSE, "Parsing Resource File '%s'n", filename);
- fp = fopen(filename, "r");
- if (fp == NULL)
- { lprintf(LOG_WARNING, "Could not open file '%s'n", filename);
- set_defaults(filename, resource_list);
- return RESOURCE_FILE_ERROR;
- }
- line_count = 0;
- while (get_line(line, MAXLINELEN, &line_count, fp) != NULL)
- {
- src = line;
- type = get_token(token, MAXLINELEN, src, &src);
- if (type != STRING_TOKEN)
- {
- if (type == COMMENT_TOKEN)
- { continue;
- }
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' Identifier expected at line %dn", filename, line_count);
- continue;
- }
- resource = resource_list;
- while (resource->type != RESOURCE_NULL)
- {
- if (strcmp(resource->name, token) == 0)
- { break;
- }
- resource++;
- }
- if (resource->type == RESOURCE_NULL)
- { if (warnings)
- { lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' Unexpected identifier %s at line %dn", filename, token, line_count);
- }
- continue;
- }
- type = get_token(token, MAXLINELEN, src, &src);
- if (type != ASSIGNMENT_TOKEN)
- {
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' Assignment expected at line %dn", filename, line_count);
- continue;
- }
- type = get_token(value_token, MAXLINELEN, src, &src);
- if ((type != STRING_TOKEN) &&
- (type != QUOTED_STRING_TOKEN) &&
- (type != SINGLE_QUOTED_STRING_TOKEN) &&
- (type != NULL_TOKEN) &&
- (type != COMMENT_TOKEN))
- {
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' String expected at line %dn", filename, line_count);
- continue;
- }
- if (type == COMMENT_TOKEN)
- { sms_strcpy(token, "");
- }
- type = get_token(token, MAXLINELEN, src, &src);
- if ((type != NULL_TOKEN) &&
- (type != COMMENT_TOKEN))
- {
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' EOL or comment expected at line %dn", filename, line_count);
- continue;
- }
- if (resource->type == RESOURCE_STRING)
- {
- resource->str_value = malloc(sms_strlen(value_token) +1);
- if (resource->str_value == NULL)
- { lprintf(LOG_ERROR, "Allocating memoryn");
- exit(EMALLOC);
- }
- sms_strcpy(resource->str_value, value_token);
- resource->found = TRUE;
- }
- else if (resource->type == RESOURCE_NUMERIC)
- {
- if (value_token[0] == ' ')
- {
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' Numeric Value expected at line %d assuming 0n", filename, line_count);
- resource->int_value = 0;
- }
- else
- { resource->int_value = strtol(value_token, &ptr, 10);
- if (ptr != &value_token[sms_strlen(value_token)])
- {
- lprintf(LOG_WARNING, "Syntax Error: Parsing '%s' Numeric Value expected at line %d assuming 0n", filename, line_count);
- resource->int_value = 0;
- }
- }
- resource->found = TRUE;
- }
- }
- fclose(fp);
- set_defaults(filename, resource_list);
- return RESOURCE_FILE_OK;
- }