#include "CORE_general.h" #include "CORE_fileio.h" #include "CORE_xml.h" #include "CORE_network.h" #include "CORE_power.h" #include "CORE_turbo_keys.h" #include #include #include #include #define CONFIG_PATH "/DSPad.xml" /* Process xml atoms */ static int config_process(void* elem, char* tag, char* content){ env_t* env = (env_t*)elem; if(tag != NULL && content != NULL){ if(strcmp(content, "")){ if(!strcmp(tag, "ds2key_compat")){ env->ds2key_compat = atoi(content); return 1; } /* process atom: */ if(!strcmp(tag, "network")){ return xml_read(&(env->network), network_process_xml, content); } else if(!strcmp(tag, "power")){ return xml_read(&(env->power), power_process_xml, content); } else if(!strcmp(tag, "turbo_keys")){ return xml_read(&(env->turbo), turbo_keys_process_xml, content); } return xml_read(elem, config_process, content); } } /* Should not happend... (compiler warning) */ return 0; } /* Read config file returns 1 OK 0 error */ int config_read(void){ int ok = 0; char* text = NULL; env_t tmp; memcpy(&tmp, &Env, sizeof(env_t)); if(!Env.fat_enabled) return 1; text = xml_load(CONFIG_PATH); if(text){ ok = xml_read((void*)(&tmp), config_process, text); free(text); if(ok){ memcpy(&Env, &tmp, sizeof(env_t)); #ifdef DEBUG PA_Print(USCR, "Reading \"%s\" SUCCESS\n", CONFIG_PATH); #endif } else{ #ifdef DEBUG PA_Print(USCR, "Reading \"%s\" FAILED\n", CONFIG_PATH); #endif } } return ok; } /* Write config file returns 1 OK 0 error */ int config_write(void){ if(!Env.fat_enabled) return 1; FILE* out = fopen(CONFIG_PATH, "w"); fprintf(out, "\n"); fprintf(out, "\n"); // TODO: ds2key parser, same as network or power.. fprintf(out, " %d\n", Env.ds2key_compat); network_write_xml(&(Env.network), out); power_write_xml(&(Env.power), out); turbo_keys_write_xml(&(Env.turbo), out); fprintf(out, ""); #ifdef DEBUG PA_Print(USCR, "Writing \"%s\" SUCCESS\n", CONFIG_PATH); #endif return !fclose(out); } /* Alloc a buffer matching the sizeof a given file returns the pointer or NULL */ unsigned char* file_load(char* path){ int n = 0; int i = 0; unsigned char* m; FILE* f = fopen(path,"r"); if(f == NULL){ #ifdef DEBUG PA_Print(USCR, "Loading \"%s\" FAILED\n", path); #endif return NULL; } while(fgetc(f) != EOF) {n++;} m = (unsigned char*)malloc(n*sizeof(unsigned char)); if (m == NULL){ #ifdef DEBUG PA_Print(USCR, "Loading \"%s\" FAILED\n", path); #endif return NULL; } rewind(f); for(i=0;i<(n-1);i++){ *(m+i)=(unsigned char)fgetc(f); } fclose(f); #ifdef DEBUG PA_Print(USCR, "Loading \"%s\" SUCCESS\n", path); #endif return m; }