//REWRITE: this is inefficient. We better read and parse file once int getConfigValue(char* basedir, char* lookupKey, char* outValue, int buf_size) { char config[MAX_PATH] = {0}; char buffer[MAX_PATH*2]; char *value; FILE *fp; char resolved_file[MAX_PATH]; if(boinc_resolve_filename(CONFIG_FILENAME, resolved_file+1, MAX_PATH-1)){ printf("Unable to resolve file (%s) via boinc_resolve_filename!\n", CONFIG_FILE); return FALSE; } resolved_file[0] = '/'; #ifdef DEBUG printf("Resolved %s to %s\n", CONFIG_FILENAME, resolved_file); #endif if (!getFileInPackage(basedir, resolved_file, config, MAX_PATH)) { printf("Configuration file (%s) is not found!\n", config); return FALSE; } #ifdef DEBUG printf("FullFilePath: %s\n", config); #endif //scan file for the key fp = fopen(config, "r"); if (fp == NULL) { return FALSE; } while (fgets(buffer, MAX_PATH*2, fp)) { value = strchr(buffer, '='); if (value != NULL) { //end key on the '=', value will point to the value string *value = 0; value++; if (!strcmp(buffer, lookupKey)) { //found it fclose(fp); strip_endofline(value); strncpy(outValue, value, buf_size); return TRUE; } } } fclose(fp); return FALSE; }
//REWRITE: this is inefficient. We better read and parse file once bool getConfigValue(TCHAR* basedir, TCHAR* lookupKey, TCHAR* outValue, int buf_size) { TCHAR config[LAUNCHER_MAXPATH] = {0}; TCHAR buffer[LAUNCHER_MAXPATH*2]; TCHAR *value; FILE *fp; *outValue = 0; if (!getFileInPackage(basedir, CONFIG_FILE, config, LAUNCHER_MAXPATH)) { showError(config, _T("Configuration file is not found!")); return false; } //scan file for the key errno_t err = _tfopen_s(&fp, config, _T("r")); if (err) { return false; } while (_fgetts(buffer, LAUNCHER_MAXPATH*2, fp)) { value = _tcschr(buffer, '='); if (value != NULL) { //end key on the '=', value will point to the value string *value = 0; value++; if (!_tcscmp(buffer, lookupKey)) { //found it fclose(fp); strip_endofline(value); _tcscpy_s(outValue, buf_size, value); return true; } } } fclose(fp); return false; }
//REWRITE: this is inefficient. We better read and parse file once int getConfigValue(char* basedir, char* lookupKey, char* outValue, int buf_size) { char config[MAX_PATH] = {0}; char buffer[MAX_PATH*2]; char *value; FILE *fp; if (!getFileInPackage(basedir, CONFIG_FILE, config, MAX_PATH)) { printf("Configuration file (%s) is not found!\n", config); return FALSE; } //scan file for the key fp = fopen(config, "r"); if (fp == NULL) { return FALSE; } while (fgets(buffer, MAX_PATH*2, fp)) { value = strchr(buffer, '='); if (value != NULL) { //end key on the '=', value will point to the value string *value = 0; value++; if (!strcmp(buffer, lookupKey)) { //found it fclose(fp); strip_endofline(value); strncpy(outValue, value, buf_size); return TRUE; } } } fclose(fp); return FALSE; }