int serialParseFlowControl (SerialFlowControl *flow, const char *string) { if (isAbbreviation(string, "none")) { *flow = SERIAL_FLOW_NONE; } else if (isAbbreviation(string, "hardware")) { *flow = SERIAL_FLOW_HARDWARE; } else { logMessage(LOG_WARNING, "invalid serial flow control: %s", string); return 0; } return 1; }
int serialParseParity (SerialParity *parity, const char *string) { if (isAbbreviation(string, "none")) { *parity = SERIAL_PARITY_NONE; } else if (isAbbreviation(string, "odd")) { *parity = SERIAL_PARITY_ODD; } else if (isAbbreviation(string, "even")) { *parity = SERIAL_PARITY_EVEN; } else if (isAbbreviation(string, "space")) { *parity = SERIAL_PARITY_SPACE; } else if (isAbbreviation(string, "mark")) { *parity = SERIAL_PARITY_MARK; } else { logMessage(LOG_WARNING, "invalid serial parity: %s", string); return 0; } return 1; }
int OptionsParser::check(const char* arg1, const char* arg2, bool& ok) { // the first argument should be an option // (only the second may be a free string) if (!checkParameter(arg1)) { ok = false; return 1; } OptionsCont& oc = OptionsCont::getOptions(); // process not abbreviated switches if (!isAbbreviation(arg1)) { std::string tmp(arg1 + 2); size_t idx1 = tmp.find('='); // check whether a parameter was submitted if (idx1 != std::string::npos) { ok &= oc.set(tmp.substr(0, idx1), tmp.substr(idx1 + 1)); } else { if (arg2 == 0 || (oc.isBool(convert(arg1 + 2)) && arg2[0] == '-')) { ok &= oc.set(convert(arg1 + 2), "true"); } else { ok &= oc.set(convert(arg1 + 2), convert(arg2)); return 2; } } return 1; } // go through the abbreviated switches for (int i = 1; arg1[i] != 0; i++) { // set boolean switches if (oc.isBool(convert(arg1[i]))) { if (arg2 == 0 || arg2[0] == '-' || arg1[i + 1] != 0) { ok &= oc.set(convert(arg1[i]), "true"); } else { ok &= oc.set(convert(arg1[i]), convert(arg2)); return 2; } // set non-boolean switches } else { // check whether the parameter comes directly after the switch // and process if so if (arg2 == 0 || arg1[i + 1] != 0) { ok &= processNonBooleanSingleSwitch(oc, arg1 + i); return 1; // process parameter following after a space } else { ok &= oc.set(convert(arg1[i]), convert(arg2)); // option name and attribute were in two arguments return 2; } } } // all switches within the current argument were boolean switches return 1; }
void TextSplitter::getSentences(std::ostream &out, size_t &count) { std::string buffer; std::string lastWord; // Last read word size_t wordCount = 0; // Count words in each sentence char c; count = 0; while ((c = stream_.get()) != EOF) { // If we reached sentence delimiter (.?!) we should: // 1. Skip other delimiters (situations like '??!') // 2. Check if last word was abbreviation (then do not split by dot) // 3. Check for closing quote (do not move closing quote to next sentence) if (isSentenceDelimiter(c)) { // Skip abbreviations if (c == '.' && isAbbreviation(lastWord)) { lastWord.clear(); ++wordCount; buffer.push_back(c); continue; } if (!lastWord.empty()) { ++wordCount; } while (isSentenceDelimiter(c)) { buffer.push_back(c); c = stream_.get(); } while (c == ' ') { c = stream_.get(); } if (c == '"') { buffer.push_back(c); c = stream_.get(); } if (wordCount >= MIN_SENTENCE_LEN) { out << removeStartingSpaces(buffer) << std::endl; wordCount = 0; ++count; } buffer.clear(); // clear sentence buffer lastWord.clear(); // clear word buffer } // If we reached '\n' we will merge lines by space if (isCRLF(c)) { while (isCRLF(c)) { c = stream_.get(); } if (!buffer.empty()) { buffer.push_back(' '); } if (!lastWord.empty()) { ++wordCount; lastWord.clear(); } } // Clear last word if we found word delimiter if (isWordDelimiter(c)) { if (!lastWord.empty()) { ++wordCount; lastWord.clear(); } } if (c != EOF) { buffer.push_back(c); } if (!isWordDelimiter(c)) { lastWord.push_back(c); } } }