inline void readPNMHeader(std::ifstream& file, unsigned int& bpc, unsigned int& max_value, unsigned int& width, unsigned int& height) { char m[2]; //1. "magic number" stripComment(file); file >> m[0];//P file >> m[1];//1..6 if (m[0]!= 'P' || m[1] != magic) throw PNMImageExcpetion("Wrong magic number"); //3-5. Width and height stripComment(file); file >> width; stripComment(file); file >> height; //7 maximum gray value stripComment(file); file >> max_value; if (max_value > 65535) throw PNMImageExcpetion("Wrong max value"); bpc = (max_value < 256) ? 1 : 2; //8 skip whitespace stripComment(file); }
/** * Loads a policy file. * * @return True on success. */ bool AlphaPolicy::loadFromFile(const char* path) { int action; double alpha; std::vector<double> alphas; std::string line; bool alpha_line = false; // True for alpha line, false for action line. (Simple state machine.) // Open file. std::ifstream policy_file; policy_file.open(path); if(!policy_file.good()) { ROS_ERROR_STREAM("Unable to open file " << path); return false; } // Check Policy has what it needs to read the file. ROS_ASSERT_MSG(state_space_, "Requires StateSpace to be initialized!"); // Read file. while(!policy_file.eof()) { std::getline(policy_file, line); if(policy_file.fail()) continue; line = stripComment(line); if(isBlank(line)) continue; // Skip line std::stringstream line_ss(line); // Action line if(!alpha_line) { // Read element from line. std::string action_str; line_ss >> action_str; if(line_ss.fail()) { ROS_ERROR_STREAM("Invalid action line: '" << line << "'"); return false; } // Convert to action. // Kludgy. @todo Change actions from ints to FeatureValues. shared_ptr<FeatureValue> action_obj = action_space_->readFeatureValue(action_str); action = boost::dynamic_pointer_cast<SimpleFeatureValue>(action_obj)->asInt(); if(!action_space_->isValid(SimpleFeatureValue(action))) { ROS_ERROR_STREAM("Invalid action: '" << action_str << "'"); return false; } actions_.push_back(action); alpha_line = true; } // Alpha line else if(alpha_line)
bool config::loadFile(string path) { std::ifstream file(path.c_str(), std::ios::in); if (!file.is_open()) { return false; } string line, key, value; std::vector<string> parts; while (!file.eof()) { std::getline(file, line); stripComment(line); if (line.trim().empty()) { continue; } parts = line.explode("="); key = parts.front().trim(); value = parts.back().trim(); values[key] = value; } file.close(); return true; }
// --------------------------------------------------------------------------- // readPair: read content of entry // --------------------------------------------------------------------------- Int32 UdrCfgParser::readPair( FILE *is, char *buf, Int32 bufSize , NAString &errorText ) { char lineBuf[BUFFMAX+2]; char *p, *cur; Int32 len = -1; NABoolean quote = FALSE; while( fgets( lineBuf, sizeof(lineBuf), is ) ) { len = -1; remEOL(lineBuf); if( isTitleLine( lineBuf ) ) // section is ended break; p=lineBuf; if (quote == FALSE) stripComment( p ); rtrim(p); if ((len = (Int32)strlen(p)) == 0) continue; if( bufSize-1 < len ) { errorText += "*** ERROR: UdrCfgParser():fgets read line longer than BUFFMAX of "; errorText += LongToNAString((Lng32) BUFFMAX); errorText += " in config file "; errorText += cfgFileName; errorText += ".\n"; return -1; } cur = buf; *cur = '\0'; strncpy( cur, p, len ); cur[len] = 0; break; } if (ferror(is)) { errorText += "*** ERROR: UdrCfgParser(): fgets failed on config file "; errorText += cfgFileName; errorText += " due to an I/O error: "; errorText += strerror(errno); errorText += ".\n"; } return len; }
bool LineReader::getNext() { if (! _lines.empty()) { _current_line = _lines.back(); _lines.pop_back(); } else if (_is->eof()) return(false); else { while (getline(*_is, _current_line)) { ++_lineno; stripComment(_current_line); stripLeadingWhitespace(_current_line); if (! skipLine(_current_line) ) break; } } checkState(); return( _is->good() ); }
/* ------------------------------------------------------------------------------ */ static int readEntry( FILE *is, const char *entry, char *buf, int bufSize, int strip ) { char lineBuf[256]; char *p, *cur; int len; bool quote = false; cur = buf; *cur = '\0'; len = -1; while( fgets( lineBuf, 256, is ) ) { remEOL(lineBuf); if( isTitleLine( lineBuf ) ) /* section is ended */ break; p = textPos( lineBuf, entry ); /* not equal this entry */ if( p == 0 ) continue; if( strip ) quote = stripQuotationChar( p ); if (quote == false) stripComment( p ); rtrim(p); len = (int)strlen(p); if( bufSize-1 < len ) len = bufSize-1; strncpy( cur, p, len ); cur[len] = 0; break; } return len; }
/** * Open and read an initialization file, putting the information * therein into a newly-allocated object of type Ini. * * @param[in] ifname Name of input file * * @returns Newly-allocate Ini object containing info from input file. */ Ini *Ini_new(const char *ifname) { FILE *ifp = fopen(ifname, "r"); int inPopHist = 0; if(ifp == NULL) return NULL; Ini *ini = malloc(sizeof(Ini)); checkmem(ini, __FILE__, __LINE__); memset(ini, 0, sizeof(Ini)); ini->a = NULL; ini->epochList = NULL; Tokenizer *tkz = Tokenizer_new(100); char buff[1000]; int lineno = 0, ntokens; while(fgets(buff, sizeof(buff), ifp) != NULL) { ++lineno; if(!strchr(buff, '\n') && !feof(ifp)) eprintf("ERR@%s:%d: Buffer overflow. buff=\"%s\"\n", __FILE__, __LINE__, buff); /* skip blank lines and comments */ stripComment(buff); if(strempty(buff)) continue; if(inPopHist) { Tokenizer_split(tkz, buff, " \t"); /* tokenize */ ntokens = Tokenizer_strip(tkz, " \t\n"); /* strip extraneous */ if(ntokens != 2) break; double t = strtod(Tokenizer_token(tkz, 0), NULL); double twoN = strtod(Tokenizer_token(tkz, 1), NULL); ini->epochList = EpochLink_new(ini->epochList, t, twoN); if(!isfinite(t)) break; } else if(strchr(buff, '=')) { Tokenizer_split(tkz, buff, "="); /* tokenize */ ntokens = Tokenizer_strip(tkz, " \t\n"); /* strip extraneous */ if(ntokens != 2) eprintf("ERR@:%s:%d:" "Broken assignment @ line %u" " of initialization file", __FILE__, __LINE__, lineno); ini->a = Assignment_new(ini->a, Tokenizer_token(tkz, 0), Tokenizer_token(tkz, 1)); } else { Tokenizer_split(tkz, buff, " \t"); /* tokenize */ ntokens = Tokenizer_strip(tkz, " \t\n"); /* strip * extraneous */ if(ntokens == 0) continue; if(ntokens != 1) eprintf("ERR@:%s:%d:" "Broken command @ line %u" " of initialization file." " inPopHist=%d; ntokens=%d\n", __FILE__, __LINE__, lineno, inPopHist, ntokens); if(!strcmp("PopHist", Tokenizer_token(tkz, 0))) inPopHist = 1; else ini->a = Assignment_new(ini->a, Tokenizer_token(tkz, 0), "1"); } } Tokenizer_free(tkz); fclose(ifp); return ini; }
/// Main int main(int argc, char **argv) { FILE *fh; int RC=10; char readbuf[BUFSIZ]; argc--; argv++; if(argc>0) { setup_filename = *argv; argc--; argv++; } if(argc>0) { configin_filename = *argv; argc--; argv++; } if(argc>0) { configout_filename = *argv; argc--; argv++; } printf("Files: Setup=%s, Config in=%s, Config out=%s\n", setup_filename,configin_filename,configout_filename); if(fh=fopen(setup_filename,"r")) { int skip=0; lineNumber=1; while(fgets(readbuf,BUFSIZ,fh)!=NULL) { stripNL(readbuf); switch (classifyLine(readbuf)) { case L_SKIP: // skip this line break; case L_MODULE: if(!skip) { stripComment(readbuf); addStrToList(&moduleList, &moduleTail, readbuf); } break; case L_DEFINE: if(!skip) { addStrToList(&defineList, &defineTail, readbuf); } break; case L_OPTION: if(strcmp(readbuf,"*static*")==NULL) skip=0; if(strcmp(readbuf,"*doconfig*")==NULL) skip=0; if(strcmp(readbuf,"*shared*")==NULL) skip=1; if(strcmp(readbuf,"*noconfig*")==NULL) skip=1; break; } lineNumber++; } fclose(fh); if(processDefines() && processModules()) RC=0; } else { printf("Cannot read `%s'\n",setup_filename); } return RC; }