示例#1
0
bool cf_open_file(char *filename) 
{
	/* if filename==NULL, we use the default one: $HOME/.gngeo/gngeorc */
	FILE *f;
	int i = 0;
	char *buf=NULL;
	char *name=NULL, *ptr;
	CONF_ITEM *cf;

	f = fopen(filename, "rb");
	if (! f)
	{
		printf("ERROR: Unable to open %s\n",filename);
		return false;
	}

	buf=calloc(8192,sizeof(char));
	while (!feof(f)) {
		i = 0;
		my_fgets(buf, 8190, f);
		if (discard_line(buf))
			continue;
	
		ptr=get_token(buf, " =", &name);

		cf = cf_get_item_by_name(name);
		if (cf && !(cf->flags & CF_SETBYCMD) && (!cf->modified)) {
			switch (cf->type) {
				case CFT_INT:
					CF_VAL(cf) = atoi(ptr);
					break;
				case CFT_BOOLEAN:
					CF_BOOL(cf) = (strcasecmp(ptr, "true") == 0 ? true : false);
					break;
				case CFT_STRING:
					printf("ST: %s\n",ptr);
					CF_STR(cf)=rstrcpy(CF_STR(cf), ptr, 8190);
					break;
				case CFT_ARRAY:
					read_array(CF_ARRAY(cf), ptr, CF_ARRAY_SIZE(cf));
					break;
				case CFT_ACTION:
				case CFT_ACTION_ARG:
					/* action are not available in the conf file */
					break;
				case CFT_STR_ARRAY:
					CF_STR_ARRAY(cf) = read_str_array(ptr, &CF_STR_ARRAY_SIZE(cf));
					break;
			}
		} else {
			/*printf("Unknow option %s\n",name);*/
			/* unknow option...*/
		}
	}

	if (name) free(name);
	if (buf) free(buf);

	cf_cache_conf();
	return true;
}
示例#2
0
bool cf_save_file(char *filename, int flags) {
	char *conf_file = NULL;
	char *conf_file_dst;
	FILE *f;
	FILE *f_dst;
	int i = 0, j, a;
	char buf[512];
	char *name=NULL, *ptr;
	CONF_ITEM *cf;

	if (! sstrlen(filename)) conf_file=cf_default_path(conf_file, "gngeorc", "");
	else conf_file=rstrcpy(conf_file, filename, 1024);

	conf_file_dst = alloca(strlen(conf_file) + 4);
	sprintf(conf_file_dst, "%s.t", conf_file);

	if ((f_dst = fopen(conf_file_dst, "w")) == 0) {
		//printf("Unable to open %s\n",conf_file);
		if (conf_file) free(conf_file);
		return false;
	}

	if ((f = fopen(conf_file, "rb"))) {

		//printf("Loading current .cf\n");

		while (!feof(f)) {
			i = 0;
			my_fgets(buf, 510, f);
			if (discard_line(buf)) {
				fprintf(f_dst, "%s\n", buf);
				continue;
			}

			//this is an odd approach, seeks to replace existing config
			//items in the order they exist in config file?
			ptr=get_token(buf, " ", &name);

			cf = cf_get_item_by_name(name);
			if (cf) {
				if (cf->modified) {
					cf->modified = 0;
					switch (cf->type) {
						case CFT_INT:
							fprintf(f_dst, "%s %d\n", cf->name, CF_VAL(cf));
							break;
						case CFT_BOOLEAN:
							if (CF_BOOL(cf))
								fprintf(f_dst, "%s true\n", cf->name);
							else
								fprintf(f_dst, "%s false\n", cf->name);
							break;
						case CFT_STRING:
							fprintf(f_dst, "%s %s\n", cf->name, CF_STR(cf));
							break;
						case CFT_ARRAY:
							fprintf(f_dst, "%s ", cf->name);
							for (a = 0; a < CF_ARRAY_SIZE(cf) - 1; a++)
								fprintf(f_dst, "%d,", CF_ARRAY(cf)[a]);
							fprintf(f_dst, "%d\n", CF_ARRAY(cf)[a]);
							break;
						case CFT_ACTION:
						case CFT_ACTION_ARG:
							break;
						case CFT_STR_ARRAY:
							printf("TODO: Save CFT_STR_ARRAY\n");
							break;
					}
				} else
					fprintf(f_dst, "%s\n", buf);
			}
		}
		fclose(f);

	}
	/* Now save options that were not in the previous file */
	for (i = 0; i < 128; i++) {
		for (j = 0; j < cf_hash[i].nb_item; j++) {
			cf = cf_hash[i].conf[j];
			//printf("Option %s %d\n",cf->name,cf->modified);
			if (cf->modified!=0) {
				cf->modified=0;
				switch (cf->type) {
					case CFT_INT:
						fprintf(f_dst, "%s %d\n", cf->name, CF_VAL(cf));
						break;
					case CFT_BOOLEAN:
						if (CF_BOOL(cf))
							fprintf(f_dst, "%s true\n", cf->name);
						else
							fprintf(f_dst, "%s false\n", cf->name);
						break;
					case CFT_STRING:
						fprintf(f_dst, "%s %s\n", cf->name, CF_STR(cf));
						break;
					case CFT_ARRAY:
						fprintf(f_dst, "%s ", cf->name);
						for (a = 0; a < CF_ARRAY_SIZE(cf) - 1; a++)
							fprintf(f_dst, "%d,", CF_ARRAY(cf)[a]);
						fprintf(f_dst, "%d\n", CF_ARRAY(cf)[a]);
						break;
					case CFT_ACTION:
					case CFT_ACTION_ARG:
						/* action are not available in the conf file */
						break;
					case CFT_STR_ARRAY:
						printf("TODO: Save CFT_STR_ARRAY\n");
						break;
				}
			}
		}
	}
	fclose(f_dst);

	remove(conf_file);
	rename(conf_file_dst, conf_file);

	if (name) free(name);
	if (conf_file) free(conf_file);

	return true;
}
示例#3
0
文件: ini.c 项目: rxi/ini
/* Splits data in place into strings containing section-headers, keys and
 * values using one or more '\0' as a delimiter. Unescapes quoted values */
static void split_data(ini_t *ini) {
  char *value_start, *line_start;
  char *p = ini->data;

  while (p < ini->end) {
    switch (*p) {
      case '\r':
      case '\n':
      case '\t':
      case ' ':
        *p = '\0';
        /* Fall through */

      case '\0':
        p++;
        break;

      case '[':
        p += strcspn(p, "]\n");
        *p = '\0';
        break;

      case ';':
        p = discard_line(ini, p);
        break;

      default:
        line_start = p;
        p += strcspn(p, "=\n");

        /* Is line missing a '='? */
        if (*p != '=') {
          p = discard_line(ini, line_start);
          break;
        }
        trim_back(ini, p - 1);

        /* Replace '=' and whitespace after it with '\0' */
        do {
          *p++ = '\0';
        } while (*p == ' ' || *p == '\r' || *p == '\t');

        /* Is a value after '=' missing? */
        if (*p == '\n' || *p == '\0') {
          p = discard_line(ini, line_start);
          break;
        }

        if (*p == '"') {
          /* Handle quoted string value */
          value_start = p;
          p = unescape_quoted_value(ini, p);

          /* Was the string empty? */
          if (p == value_start) {
            p = discard_line(ini, line_start);
            break;
          }

          /* Discard the rest of the line after the string value */
          p = discard_line(ini, p);

        } else {
          /* Handle normal value */
          p += strcspn(p, "\n");
          trim_back(ini, p - 1);
        }
        break;
    }
  }
}
示例#4
0
bool Lexer::lex_symbol(FILE * fp) {
    Token token;
    token.filename = current_filename;
	token.line_number = current_line;
	token.column_number = current_column;

    char cur = next_char(fp);
    switch (cur) {
    /* ARITHMATIC */
    case '+': {
        if (peek_char(fp) == '+') {
            token.type = TOKEN_INCREMENT;
            next_char(fp);
        }
        else if (peek_char(fp) == '=') {
            token.type = TOKEN_ADD_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '+';
        }
    }
    break;
    case '-': {
        if (peek_char(fp) == '-') {
            token.type = TOKEN_DECREMENT;
            next_char(fp);
        }
        else if (peek_char(fp) == '=') {
            token.type == TOKEN_SUB_EQUALS;
            next_char(fp);
        }
        else if (peek_char(fp) == '>') {
            token.type = TOKEN_POINTER_MEMBER_DEREF;
            next_char(fp);
        }
        else {
            token.type = '-';
        }
    }
    break;
    case '/': {
        if (peek_char(fp) == '/') {
            // Comment.
            // @TODO: CLEANUP: this should really be handled elsewhere.
            discard_line(fp);
            return true;
        }
        else if (peek_char(fp) == '=') {
            token.type = TOKEN_DIV_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '/';
        }
    }
    break;
    case '*': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_MUL_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '*';
        }
    }
    break;
    case '%': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_MOD_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '%';
        }
    }
    break;


    /* BRACKETS */
    case '(': 
    case ')':
    case '{':
    case '}':
    case '[':
    case ']': {
        token.type = cur;
    }
    break;
    

    /* COMPARISON */
    case '>': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_GREATER_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '>';
        }
    }
    break;
    case '<': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_LESS_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '<';
        }
    }
    break;
    case '=': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '=';
        }
    }
    break;
    case '!': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_NOT_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '!';
        }
    }
    break;

    
    /* BOOLEAN / BITWISE */
    case '|': {
        if (peek_char(fp) == '|') {
            token.type = TOKEN_LOGICAL_OR;
            next_char(fp);
        }
        else if (peek_char(fp) == '=') {
            token.type = TOKEN_BITWISE_OR_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '|';
        }
    }
    break;
    case '&': {
        if (peek_char(fp) == '&') {
            token.type = TOKEN_LOGICAL_AND;
            next_char(fp);
        }
        else if (peek_char(fp) == '=') {
            token.type = TOKEN_BITWISE_AND_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '&';
        }
    }
    break;
    case '~': {
        if (peek_char(fp) == '=') {
            token.type = TOKEN_NOT_EQUALS;
            next_char(fp);
        }
        else {
            token.type = '~';
        }
    }
    break;

    
    /* MISC */
    case '.': {
        token.type = '.';
    }
    break;
	case ';': {
		token.type = ';';
	}
	break;
    default:
        REPORT_TOKEN_ERROR("Lexer error: Unrecognized token.", token);
        break;
    }

    // Sanity check: type must be assigned.
    assert(token.type > 0);
	set_token_symbol_text(&token);
	tokens.push_back(token);
}
示例#5
0
文件: conf.c 项目: yoyofr/iNEOGEO
bool cf_open_file(char *filename) {
	/* if filename==NULL, we use the default one: $HOME/Documents/gngeorc */
	char *conf_file = filename;
	FILE *f;
	int i = 0;
	char buf[512];
	char name[32];
	char val[255];
	CONF_ITEM *cf;

	if (!conf_file) {
#ifdef EMBEDDED_FS
		int len = strlen("gngeorc") + strlen(ROOTPATH"conf/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, ROOTPATH"conf/gngeorc");
#elif __AMIGA__
		int len = strlen("gngeorc") + strlen("/PROGDIR/data/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, "/PROGDIR/data/gngeorc");
#else
		int len = strlen("gngeorc") + strlen(getenv("HOME")) + strlen("/Documents/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, "%s/Documents/gngeorc", getenv("HOME"));
#endif
	}
	if ((f = fopen(conf_file, "rb")) == 0) {
		//printf("Unable to open %s\n",conf_file);
		return false;
	}

	while (!feof(f)) {
		i = 0;
		my_fgets(buf, 510, f);
		if (discard_line(buf))
			continue;
	
		/* TODO: Verify this on Win32 */
		sscanf(buf, "%s %s", name, val);

		//sscanf(buf, "%s ", name);
		//strncpy(val,buf+strlen(name)+1,254);

//		printf("%s|%s|\n",name,val);
		cf = cf_get_item_by_name(name);
		if (cf && !(cf->flags & CF_SETBYCMD) && (!cf->modified)) {
//			printf("Option %s\n",cf->name);
			switch (cf->type) {
				case CFT_INT:
					CF_VAL(cf) = atoi(val);
//                    printf("got val: %d\n",CF_VAL(cf));
					break;
				case CFT_BOOLEAN:
					CF_BOOL(cf) = (strcasecmp(val, "true") == 0 ? true : false);
					break;
				case CFT_STRING:
					strncpy(CF_STR(cf), val, 254);
					break;
				case CFT_ARRAY:
					read_array(CF_ARRAY(cf), val, CF_ARRAY_SIZE(cf));
					break;
				case CFT_ACTION:
				case CFT_ACTION_ARG:
					/* action are not available in the conf file */
					break;
				case CFT_STR_ARRAY:
					CF_STR_ARRAY(cf) = read_str_array(val, &CF_STR_ARRAY_SIZE(cf));
					break;
			}
		} else {
			/*printf("Unknow option %s\n",name);*/
			/* unknow option...*/
		}
	}

	cf_cache_conf();
	return true;
}
示例#6
0
文件: conf.c 项目: yoyofr/iNEOGEO
bool cf_save_file(char *filename, int flags) {
	char *conf_file = filename;
	char *conf_file_dst;
	FILE *f;
	FILE *f_dst;
	int i = 0, j, a;
	char buf[512];
	char name[32];
	char val[255];
	CONF_ITEM *cf;

	if (!conf_file) {
#ifdef EMBEDDED_FS
		int len = strlen("gngeorc") + strlen(ROOTPATH"conf/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, ROOTPATH"conf/gngeorc");
#elif __AMIGA__
		int len = strlen("gngeorc") + strlen("/PROGDIR/data/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, "/PROGDIR/data/gngeorc");
#else /* POSIX */
		int len = strlen("gngeorc") + strlen(getenv("HOME")) + strlen("/Documents/") + 1;
		conf_file = (char *) alloca(len * sizeof (char));
		sprintf(conf_file, "%s/Documents/gngeorc", getenv("HOME"));
#endif
	}
	conf_file_dst = alloca(strlen(conf_file) + 4);
	sprintf(conf_file_dst, "%s.t", conf_file);

	if ((f_dst = fopen(conf_file_dst, "w")) == 0) {
		//printf("Unable to open %s\n",conf_file);
		return false;
	}

	if ((f = fopen(conf_file, "rb"))) {

		//printf("Loading current .cf\n");

		while (!feof(f)) {
			i = 0;
			my_fgets(buf, 510, f);
			if (discard_line(buf)) {
				fprintf(f_dst, "%s\n", buf);
				continue;
			}

			//sscanf(buf, "%s %s", name, val);
			sscanf(buf, "%s ", name);
			strncpy(val, buf + strlen(name) + 1, 254);

			cf = cf_get_item_by_name(name);
			if (cf) {
				if (cf->modified) {
					cf->modified = 0;
					switch (cf->type) {
						case CFT_INT:
							fprintf(f_dst, "%s %d\n", cf->name, CF_VAL(cf));
							break;
						case CFT_BOOLEAN:
							if (CF_BOOL(cf))
								fprintf(f_dst, "%s true\n", cf->name);
							else
								fprintf(f_dst, "%s false\n", cf->name);
							break;
						case CFT_STRING:
							fprintf(f_dst, "%s %s\n", cf->name, CF_STR(cf));
							break;
						case CFT_ARRAY:
							fprintf(f_dst, "%s ", cf->name);
							for (a = 0; a < CF_ARRAY_SIZE(cf) - 1; a++)
								fprintf(f_dst, "%d,", CF_ARRAY(cf)[a]);
							fprintf(f_dst, "%d\n", CF_ARRAY(cf)[a]);
							break;
						case CFT_ACTION:
						case CFT_ACTION_ARG:
							break;
						case CFT_STR_ARRAY:
							printf("TODO: Save CFT_STR_ARRAY\n");
							break;
					}
				} else
					fprintf(f_dst, "%s\n", buf);
			}
		}
		fclose(f);

	}
	/* Now save options that were not in the previous file */
	for (i = 0; i < 128; i++) {
		for (j = 0; j < cf_hash[i].nb_item; j++) {
			cf = cf_hash[i].conf[j];
			//printf("Option %s %d\n",cf->name,cf->modified);
			if (cf->modified!=0) {
				cf->modified=0;
				switch (cf->type) {
					case CFT_INT:
						fprintf(f_dst, "%s %d\n", cf->name, CF_VAL(cf));
						break;
					case CFT_BOOLEAN:
						if (CF_BOOL(cf))
							fprintf(f_dst, "%s true\n", cf->name);
						else
							fprintf(f_dst, "%s false\n", cf->name);
						break;
					case CFT_STRING:
						fprintf(f_dst, "%s %s\n", cf->name, CF_STR(cf));
						break;
					case CFT_ARRAY:
						fprintf(f_dst, "%s ", cf->name);
						for (a = 0; a < CF_ARRAY_SIZE(cf) - 1; a++)
							fprintf(f_dst, "%d,", CF_ARRAY(cf)[a]);
						fprintf(f_dst, "%d\n", CF_ARRAY(cf)[a]);
						break;
					case CFT_ACTION:
					case CFT_ACTION_ARG:
						/* action are not available in the conf file */
						break;
					case CFT_STR_ARRAY:
						printf("TODO: Save CFT_STR_ARRAY\n");
						break;
				}
			}
		}
	}
	fclose(f_dst);

	remove(conf_file);
	rename(conf_file_dst, conf_file);

	return true;
}