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]); }
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; }
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; }