/* * Function: GetValue * Usage: Nkmax = (int)GetValue("datafile","Nkmax"); * -------------------------------------------- * Returns the value of the specified variable defined in datafile. * */ double GetValue(char *filename, char *str, int *status) { int ispace; char c, istr[BUFFERLENGTH], ostr[BUFFERLENGTH]; FILE *ifile = MyFOpen(filename,"r","GetValue"); *status = 0; while(1) { mygetline(ifile,istr,""); if(strlen(istr)==0) break; getchunk(istr,ostr); if(!strcmp(ostr,str)) { for(ispace=strlen(ostr);ispace<strlen(istr);ispace++) if(!isspace(istr[ispace])) break; if(ispace==strlen(istr)-1) *status=0; else *status=1; getchunk(&(istr[ispace]),ostr); break; } } fclose(ifile); if(*status) return strtod(ostr,(char **)NULL); else return 0; }
boost::optional<boost::shared_ptr<Error> > FileRead(const std::wstring &path, std::vector<char> *data) { if(path.empty() || data == NULL) { return CREATE_ERROR(ERROR_CODE_INVALID_PARAMETER); } boost::shared_ptr<FILE> fp = MyFOpen(path, L"rb"); if(!fp) { return CREATE_ERROR(ERROR_CODE_FILE_NOT_FOUND); } ::fseek(fp.get(), 0, SEEK_END); const long size = ::ftell(fp.get()); if(size < 0) { return CREATE_ERROR(ERROR_CODE_INTERNAL_ERROR); } ::fseek(fp.get(), 0, SEEK_SET); data->resize(static_cast<unsigned int>(size)); const unsigned int read_size = ::fread(&data->front(), 1, data->size(), fp.get()); if(read_size != data->size()) { return CREATE_ERROR(ERROR_CODE_INTERNAL_ERROR); } return boost::none; }