Beispiel #1
0
char* cf_parse_cmd_line(int argc, char *argv[]) {
	int c;
	CONF_ITEM *cf;


	option_index = optind = 0;
#ifdef WII
	return NULL;
#endif
	while ((c = getopt_long(argc, argv, shortopt, longopt, &option_index)) != EOF) {
		//if (c != 0) {
//			printf("c=%d\n",c);
			cf = cf_get_item_by_val(c&0xFFF);
			if (cf) {
				cf->flags |= CF_SETBYCMD;
//				printf("flags %s set on cmd line\n", cf->name);
				switch (cf->type) {

					case CFT_INT:
						CF_VAL(cf) = atoi(optarg);
						break;
					case CFT_BOOLEAN:
					if (c & 0x1000)
						CF_BOOL(cf) = 0;
					else
						CF_BOOL(cf) = 1;
						break;
					case CFT_STRING:
						strcpy(CF_STR(cf), optarg);
						//printf("conf %s %s\n",CF_STR(cf),optarg);
						break;
					case CFT_ARRAY:
						read_array(CF_ARRAY(cf), optarg, CF_ARRAY_SIZE(cf));
						break;
					case CFT_ACTION_ARG:
						strcpy(CF_STR(cf), optarg);
						if (cf->action) {
							exit(cf->action(cf));
						}
						break;
					case CFT_ACTION:
						if (cf->action) {
							exit(cf->action(cf));
						}
						break;
					case CFT_STR_ARRAY:
						/* TODO */
						break;
				}
			//}
		}
	}
	cf_cache_conf();
	if (optind >= argc)
		return NULL;

	return strdup(argv[optind]);
}
Beispiel #2
0
void calculate_hotkey_bitmasks()
{
    int *p;
    int i, j, mask;
    const char *p1_key_list[] = { "p1hotkey0", "p1hotkey1", "p1hotkey2", "p1hotkey3" };
    const char *p2_key_list[] = { "p2hotkey0", "p2hotkey1", "p2hotkey2", "p2hotkey3" };


    for ( i = 0; i < 4; i++ ) {
	p=CF_ARRAY(cf_get_item_by_name(p1_key_list[i]));
	for ( mask = 0, j = 0; j < 4; j++ ) mask |= p[j];
	conf.p1_hotkey[i] = mask;
    }

    for ( i = 0; i < 4; i++ ) {
	p=CF_ARRAY(cf_get_item_by_name(p2_key_list[i]));
	for ( mask = 0, j = 0; j < 4; j++ ) mask |= p[j];
	conf.p2_hotkey[i] = mask;
    }

}
Beispiel #3
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;
}
Beispiel #4
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;
}
Beispiel #5
0
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;
}
Beispiel #6
0
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;
}