void RunInputThread() { RString line; LineReader * linereader; LOG->Trace("Input thread started; getting line reader"); linereader = getLineReader(); if(linereader == NULL) { LOG->Warn("Could not open line reader for SextetStream input"); } else { LOG->Trace("Got line reader"); while(continueInputThread) { LOG->Trace("Reading line"); if(linereader->ReadLine(line)) { LOG->Trace("Got line: '%s'", line.c_str()); if(line.length() > 0) { uint8_t newStateBuffer[STATE_BUFFER_SIZE]; GetNewState(newStateBuffer, line); ReactToChanges(newStateBuffer); } } else { // Error or EOF condition. LOG->Trace("Reached end of SextetStream input"); continueInputThread = false; } } LOG->Info("SextetStream input stopped"); delete linereader; } }
void RuleTableLoaderCompact::LoadPhraseSection( LineReader &reader, const std::vector<Word> &vocab, std::vector<Phrase> &rhsPhrases, std::vector<size_t> &lhsIds) { // Read phrase count. reader.ReadLine(); const size_t phraseCount = std::atoi(reader.m_line.c_str()); // Reads lines, storing Phrase object for each RHS and vocab ID for each LHS. rhsPhrases.resize(phraseCount, Phrase(0)); lhsIds.resize(phraseCount); std::vector<size_t> tokenPositions; for (size_t i = 0; i < phraseCount; ++i) { reader.ReadLine(); tokenPositions.clear(); FindTokens(tokenPositions, reader.m_line); const char *charLine = reader.m_line.c_str(); lhsIds[i] = std::atoi(charLine+tokenPositions[0]); for (size_t j = 1; j < tokenPositions.size(); ++j) { rhsPhrases[i].AddWord(vocab[std::atoi(charLine+tokenPositions[j])]); } } }
const BVParametersTable::SetOfBVParam& BVParametersTable::getStandardSetOfBVParam() const { using namespace diffpy::runtimepath; using diffpy::validators::ensureFileOK; static boost::scoped_ptr<SetOfBVParam> the_set; if (!the_set) { the_set.reset(new SetOfBVParam); string bvparmfile = datapath("bvparm2011sel.cif"); ifstream fp(bvparmfile.c_str()); ensureFileOK(bvparmfile, fp); // read the header up to _valence_param_B and then up to an empty line. LineReader lnrd; lnrd.commentmark = '#'; while (fp >> lnrd) { if (lnrd.wcount() && lnrd.words[0] == "_valence_param_B") break; } // skip to an empty line while (fp >> lnrd && !lnrd.isblank()) { } // load data lines skipping the empty or commented entries while (fp >> lnrd) { if (lnrd.isignored()) continue; BVParam bp; bp.setFromCifLine(lnrd.line); assert(!the_set->count(bp)); the_set->insert(bp); } }
std::string ODMatrix::getNextNonCommentLine(LineReader& lr) { while (lr.good() && lr.hasMore()) { const std::string line = lr.readLine(); if (line[0] != '*') { return StringUtils::prune(line); } } throw ProcessError("End of file while reading " + lr.getFileName() + "."); }
int main() { LineReader lr; while (true) { n = lr.next_u32(); if (n == 0) break; lr.skip_line(); g.init(n); for (int i = 0; i < n; ++i) { int u = lr.next_u32(); lr.next_char(); while (lr.has_next()) { int v = lr.next_u32(); g.add(u, (Edge) { v }); g.add(v, (Edge) { u }); } lr.skip_line(); } lr.skip_line(); printf("%d\n", solve()); } return 0; }
LineReader * getLineReader() { LineReader * linereader = getUnvalidatedLineReader(); if(linereader != NULL) { if(!linereader->IsValid()) { delete linereader; linereader = NULL; } } return linereader; }
int main(char ** argv, int argc) { signal(SIGINT, &sighandler); signal(SIGTERM, &sighandler); signal(SIGABRT, &sighandler); LineReader * reader = new LineReader(cin); Output * out = new Output(); reader->apply(out); return 0; }
void ODMatrix::readO(LineReader& lr, double scale, std::string vehType, bool matrixHasVehType) { PROGRESS_BEGIN_MESSAGE("Reading matrix '" + lr.getFileName() + "' stored as OR"); // parse first defs std::string line; if (matrixHasVehType) { line = getNextNonCommentLine(lr); int type = TplConvert::_2int(StringUtils::prune(line).c_str()); if (vehType == "") { vehType = toString(type); } } // parse time std::pair<SUMOTime, SUMOTime> times = readTime(lr); SUMOTime begin = times.first; SUMOTime end = times.second; // factor double factor = readFactor(lr, scale); // parse the cells while (lr.hasMore()) { line = getNextNonCommentLine(lr); if (line.length() == 0) { continue; } StringTokenizer st2(line, StringTokenizer::WHITECHARS); if (st2.size() == 0) { continue; } try { std::string sourceD = st2.next(); std::string destD = st2.next(); double vehNumber = TplConvert::_2double(st2.next().c_str()) * factor; if (vehNumber != 0) { add(vehNumber, begin, end, sourceD, destD, vehType); } } catch (OutOfBoundsException&) { throw ProcessError("Missing at least one information in line '" + line + "'."); } catch (NumberFormatException&) { throw ProcessError("Not numeric vehicle number in line '" + line + "'."); } } PROGRESS_DONE_MESSAGE(); }
void expect(LineReader& lr, const char* expected) { StringPiece line; size_t expectedLen = strlen(expected); EXPECT_EQ(expectedLen != 0 ? LineReader::kReading : LineReader::kEof, lr.readLine(line)); EXPECT_EQ(expectedLen, line.size()); EXPECT_EQ(std::string(expected, expectedLen), line.str()); }
StrVec read_list (const char* fname) { StrVec rv; LineReader rd (fname); char *l, *lc; while ((l = rd.nextLine ())) { // find first token and add to rv while (*l && isspace (*l)) l ++; if (!*l) continue; lc = l + 1; while (*lc && !isspace (*lc)) lc ++; *lc = 0; rv.push_back (l); } rd.close (); return rv; }
void RuleTableLoaderCompact::LoadVocabularySection( LineReader &reader, const std::vector<FactorType> &factorTypes, std::vector<Word> &vocabulary) { // Read symbol count. reader.ReadLine(); const size_t vocabSize = std::atoi(reader.m_line.c_str()); // Read symbol lines and create Word objects. vocabulary.resize(vocabSize); for (size_t i = 0; i < vocabSize; ++i) { reader.ReadLine(); const size_t len = reader.m_line.size(); bool isNonTerm = (reader.m_line[0] == '[' && reader.m_line[len-1] == ']'); if (isNonTerm) { reader.m_line = reader.m_line.substr(1, len-2); } vocabulary[i].CreateFromString(Input, factorTypes, reader.m_line, isNonTerm); } }
void UserInterface::exec_stream(FILE *fp) { LineReader reader; char *line; string s; while ((line = reader.next(fp)) != NULL) { if (ctx_->get_verbosity() >= 0) show_message(kQuoted, string("> ") + line); s += line; if (*(s.end() - 1) == '\\') { s.resize(s.size()-1); continue; } Status r = execute_line(s); if (r != kStatusOk) break; s.clear(); } if (line == NULL && !s.empty()) throw SyntaxError("unfinished line"); }
int main() { freopen("Yahtzee.in", "r", stdin); LineReader reader; while (true) { string s = reader.getLine(); if (s.empty() || s == "\n") { break; } vector<vector<int>> nums; for (int i = 1; i <= 13; ++i) { vector<int> v(5); sscanf(s.c_str(), "%d%d%d%d%d", &v[0], &v[1], &v[2], &v[3], &v[4]); if (i < 13) { s = reader.getLine(); } nums.push_back(v); } SolutionDP solution; solution.solve(nums); } }
bool TestFileUtils :: process () { std::string fname = fixture_.dirname (); fname += path_separator (); fname += "*.txt"; StrVec sv; expand_wildcards (fname.c_str (), sv); StrVec::iterator itr = sv.begin (); for (; itr != sv.end (); itr ++) { o_ << (*itr).c_str () << std::endl; } if (!sv.size ()) { o_ << "No files to test reader" << std::endl; return false; } LineReader lr (sv.front ().c_str ()); char* ln; int no = 0; int slen = 0; time_t st = time (NULL); while ((ln = lr.nextLine ())) { ++ no; slen += strlen (ln); //if (strlen (ln) > 100) o_ << ln; if (no % 100000 == 0) { int td = time (NULL) - st; double rd = double (slen) / (1024*1024); o_ << "\rline " << no << ", " << rd << " Mb, average speed " << rd / (td?td:1) << " Mb/s " << std::flush; } } time_t et = time (NULL); lr.close (); return true; }
void RuleTableLoaderCompact::LoadAlignmentSection( LineReader &reader, std::vector<const AlignmentInfo *> &alignmentSets, std::vector<Phrase> &sourcePhrases) { // Read alignment set count. reader.ReadLine(); const size_t alignmentSetCount = std::atoi(reader.m_line.c_str()); alignmentSets.resize(alignmentSetCount * 2); AlignmentInfo::CollType alignTerm, alignNonTerm; std::vector<std::string> tokens; std::vector<size_t> points; for (size_t i = 0; i < alignmentSetCount; ++i) { // Read alignment set, lookup in collection, and store pointer. alignTerm.clear(); alignNonTerm.clear(); tokens.clear(); reader.ReadLine(); Tokenize(tokens, reader.m_line); std::vector<std::string>::const_iterator p; for (p = tokens.begin(); p != tokens.end(); ++p) { points.clear(); Tokenize<size_t>(points, *p, "-"); std::pair<size_t, size_t> alignmentPair(points[0], points[1]); if (sourcePhrases[i].GetWord(alignmentPair.first).IsNonTerminal()) { alignNonTerm.insert(alignmentPair); } else { alignTerm.insert(alignmentPair); } } alignmentSets[i*2] = AlignmentInfoCollection::Instance().Add(alignNonTerm); alignmentSets[i*2 + 1] = AlignmentInfoCollection::Instance().Add(alignTerm); } }
// =========================================================================== // method definitions // =========================================================================== // --------------------------------------------------------------------------- // static methods // --------------------------------------------------------------------------- void NIImporter_DlrNavteq::loadNetwork(const OptionsCont& oc, NBNetBuilder& nb) { // check whether the option is set (properly) if (!oc.isSet("dlr-navteq-prefix")) { return; } time_t csTime; time(&csTime); // parse file(s) LineReader lr; // load nodes std::map<std::string, PositionVector> myGeoms; PROGRESS_BEGIN_MESSAGE("Loading nodes"); std::string file = oc.getString("dlr-navteq-prefix") + "_nodes_unsplitted.txt"; NodesHandler handler1(nb.getNodeCont(), file, myGeoms); if (!lr.setFile(file)) { throw ProcessError("The file '" + file + "' could not be opened."); } lr.readAll(handler1); PROGRESS_DONE_MESSAGE(); // load street names if given and wished std::map<std::string, std::string> streetNames; // nameID : name if (oc.getBool("output.street-names")) { file = oc.getString("dlr-navteq-prefix") + "_names.txt"; if (lr.setFile(file)) { PROGRESS_BEGIN_MESSAGE("Loading Street Names"); NamesHandler handler4(file, streetNames); lr.readAll(handler4); PROGRESS_DONE_MESSAGE(); } else { WRITE_WARNING("Output will not contain street names because the file '" + file + "' was not found"); } } // load edges PROGRESS_BEGIN_MESSAGE("Loading edges"); file = oc.getString("dlr-navteq-prefix") + "_links_unsplitted.txt"; // parse the file EdgesHandler handler2(nb.getNodeCont(), nb.getEdgeCont(), nb.getTypeCont(), file, myGeoms, streetNames); if (!lr.setFile(file)) { throw ProcessError("The file '" + file + "' could not be opened."); } lr.readAll(handler2); nb.getEdgeCont().recheckLaneSpread(); PROGRESS_DONE_MESSAGE(); // load traffic lights if given file = oc.getString("dlr-navteq-prefix") + "_traffic_signals.txt"; if (lr.setFile(file)) { PROGRESS_BEGIN_MESSAGE("Loading traffic lights"); TrafficlightsHandler handler3(nb.getNodeCont(), nb.getTLLogicCont(), nb.getEdgeCont(), file); lr.readAll(handler3); PROGRESS_DONE_MESSAGE(); } // load prohibited manoeuvres if given file = oc.getString("dlr-navteq-prefix") + "_prohibited_manoeuvres.txt"; if (lr.setFile(file)) { PROGRESS_BEGIN_MESSAGE("Loading prohibited manoeuvres"); ProhibitionHandler handler6(nb.getEdgeCont(), file, csTime); lr.readAll(handler6); PROGRESS_DONE_MESSAGE(); } // load connected lanes if given file = oc.getString("dlr-navteq-prefix") + "_connected_lanes.txt"; if (lr.setFile(file)) { PROGRESS_BEGIN_MESSAGE("Loading connected lanes"); ConnectedLanesHandler handler7(nb.getEdgeCont()); lr.readAll(handler7); PROGRESS_DONE_MESSAGE(); } // load time restrictions if given file = oc.getString("dlr-navteq-prefix") + "_links_timerestrictions.txt"; if (lr.setFile(file)) { PROGRESS_BEGIN_MESSAGE("Loading time restrictions"); if (!oc.isDefault("construction-date")) { csTime = readDate(oc.getString("construction-date")); } TimeRestrictionsHandler handler5(nb.getEdgeCont(), nb.getDistrictCont(), csTime); lr.readAll(handler5); handler5.printSummary(); PROGRESS_DONE_MESSAGE(); } }
TEST(LineReader, EmptyConstructor) { LineReader reader; EXPECT_FALSE(reader.GetNextLine()); }
bool RuleTableLoaderCompact::LoadRuleSection( LineReader &reader, const std::vector<Word> &vocab, const std::vector<Phrase> &sourcePhrases, const std::vector<Phrase> &targetPhrases, const std::vector<size_t> &targetLhsIds, const std::vector<const AlignmentInfo *> &alignmentSets, RuleTableTrie &ruleTable) { // Read rule count. reader.ReadLine(); const size_t ruleCount = std::atoi(reader.m_line.c_str()); // Read rules and add to table. const size_t numScoreComponents = ruleTable.GetNumScoreComponents(); std::vector<float> scoreVector(numScoreComponents); std::vector<size_t> tokenPositions; for (size_t i = 0; i < ruleCount; ++i) { reader.ReadLine(); tokenPositions.clear(); FindTokens(tokenPositions, reader.m_line); const char *charLine = reader.m_line.c_str(); // The first three tokens are IDs for the source phrase, target phrase, // and alignment set. const int sourcePhraseId = std::atoi(charLine+tokenPositions[0]); const int targetPhraseId = std::atoi(charLine+tokenPositions[1]); const int alignmentSetId = std::atoi(charLine+tokenPositions[2]); const Phrase &sourcePhrase = sourcePhrases[sourcePhraseId]; const Phrase &targetPhrasePhrase = targetPhrases[targetPhraseId]; const Word *targetLhs = new Word(vocab[targetLhsIds[targetPhraseId]]); Word sourceLHS("X"); // TODO not implemented for compact const AlignmentInfo *alignNonTerm = alignmentSets[alignmentSetId]; // Then there should be one score for each score component. for (size_t j = 0; j < numScoreComponents; ++j) { float score = std::atof(charLine+tokenPositions[3+j]); scoreVector[j] = FloorScore(TransformScore(score)); } if (reader.m_line[tokenPositions[3+numScoreComponents]] != ':') { std::stringstream msg; msg << "Size of scoreVector != number (" << scoreVector.size() << "!=" << numScoreComponents << ") of score components on line " << reader.m_lineNum; UserMessage::Add(msg.str()); return false; } // The remaining columns are currently ignored. // Create and score target phrase. TargetPhrase *targetPhrase = new TargetPhrase(targetPhrasePhrase); targetPhrase->SetAlignNonTerm(alignNonTerm); targetPhrase->SetTargetLHS(targetLhs); targetPhrase->SetSourcePhrase(sourcePhrase); targetPhrase->Evaluate(sourcePhrase, ruleTable.GetFeaturesToApply()); // Insert rule into table. TargetPhraseCollection &coll = GetOrCreateTargetPhraseCollection( ruleTable, sourcePhrase, *targetPhrase, &sourceLHS); coll.Add(targetPhrase); } return true; }
void ODMatrix::readV(LineReader& lr, double scale, std::string vehType, bool matrixHasVehType) { PROGRESS_BEGIN_MESSAGE("Reading matrix '" + lr.getFileName() + "' stored as VMR"); // parse first defs std::string line; if (matrixHasVehType) { line = getNextNonCommentLine(lr); if (vehType == "") { vehType = StringUtils::prune(line); } } // parse time std::pair<SUMOTime, SUMOTime> times = readTime(lr); SUMOTime begin = times.first; SUMOTime end = times.second; // factor double factor = readFactor(lr, scale); // districts line = getNextNonCommentLine(lr); const int numDistricts = TplConvert::_2int(StringUtils::prune(line).c_str()); // parse district names (normally ints) std::vector<std::string> names; while ((int)names.size() != numDistricts) { line = getNextNonCommentLine(lr); StringTokenizer st2(line, StringTokenizer::WHITECHARS); while (st2.hasNext()) { names.push_back(st2.next()); } } // parse the cells for (std::vector<std::string>::iterator si = names.begin(); si != names.end(); ++si) { std::vector<std::string>::iterator di = names.begin(); // do { line = getNextNonCommentLine(lr); if (line.length() == 0) { continue; } try { StringTokenizer st2(line, StringTokenizer::WHITECHARS); while (st2.hasNext()) { assert(di != names.end()); double vehNumber = TplConvert::_2double(st2.next().c_str()) * factor; if (vehNumber != 0) { add(vehNumber, begin, end, *si, *di, vehType); } if (di == names.end()) { throw ProcessError("More entries than districts found."); } ++di; } } catch (NumberFormatException&) { throw ProcessError("Not numeric vehicle number in line '" + line + "'."); } if (!lr.hasMore()) { break; } } while (di != names.end()); } PROGRESS_DONE_MESSAGE(); }