void ReadWholeTag (istream& i, KKStr& tagStr ) { tagStr = ""; while (!i.eof ()) { char nextCh = i.get (); if (nextCh != 0) tagStr.Append (nextCh); if (nextCh == '>') break; } return; } /* ReadWholeTag */
KKStr KKStrParser::GetRestOfLine () { if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPos]) != NULL)) nextPos++; } if (nextPos >= len) return KKStr::EmptyStr (); KKStr result (1 + len - nextPos); char lastChar = 0; while (nextPos < len) { lastChar = str[nextPos]; if (lastChar == '\n') break; else if (lastChar == '\r') break; result.Append (lastChar); nextPos++; } if (nextPos < len) { if (lastChar == '\n') { if (str[nextPos] == '\r') nextPos++; } else if (lastChar == '\r') { if (str[nextPos] == '\n') nextPos ++; } } if (trimWhiteSpace) result.TrimRight (whiteSpace); return result; } /* GetRestOfLine */
void ScannerFile::ReadHeaderOneLine (FILE* f, bool& endOfText, KKStr& line ) { char ch; line = ""; endOfText = false; kkint32 bytesReturned = fread (&ch, 1, 1, f); while ((ch != 0) && (ch != '\n') && (bytesReturned > 0)) { line.Append (ch); bytesReturned = fread (&ch, 1, 1, f); } if ((ch == 0) || (bytesReturned == 0)) endOfText = true; } /* ReadHeaderOneLine */
KKStr KKStrParser::SubStrPart (kkuint32 firstChar, kkuint32 lastChar ) const { if (lastChar < firstChar) return KKStr::EmptyStr (); kkuint32 subStrLen = (1 + lastChar - firstChar); KKStr result (subStrLen + 1); if (lastChar >= len) lastChar = len - 1; kkuint32 idx = 0; for (idx = firstChar; idx <= lastChar; idx++) result.Append (str[idx]); return result; }
KKStr ParameterJob::ToString () { KKStr statusStr (""); statusStr.Append (curStatus); statusStr << "\t" << processNum << "\t" << jobId << "\t" << cParm << "\t" << gammaParm << "\t" << aParm << "\t" << accuracy << "\t" << trainTime << "\t" << classTime << "\t" << numSVs; return statusStr; } /* ToString */
void FeatureFileIO::GetLine (istream& _in, KKStr& _line, bool& _eof ) { _line = ""; if (_in.eof ()) { _eof = true; return; } kkint32 ch = _in.peek (); while ((ch != '\n') && (ch != '\r') && (!_in.eof ())) { ch = _in.get (); _line.Append ((char)ch); ch = _in.peek (); } if (!_in.eof ()) { _in.get (); // Skip over end of line character. if (ch == '\n') { ch = _in.peek (); if (ch == '\r') _in.get (); // line is terminated by LineFeed + CarrageReturn; we need to skip over both. } else if (ch == '\r') { ch = _in.peek (); if (ch == '\n') _in.get (); // line is terminated by CarrageReturn + LineFeed; we need to skip over both. } } _eof = false; return; } /* GetLine */
KKStr KKStrParser::GetRestOfStr () { if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPos]) != NULL)) nextPos++; } if (nextPos >= len) return KKStr::EmptyStr (); KKStr result (1 + len - nextPos); while (nextPos < len) { result.Append (str[nextPos]); nextPos++; } if (trimWhiteSpace) result.TrimRight (whiteSpace); return result; } /* GetRestOfStr */
void FeatureFileIO::GetToken (istream& _in, const char* _delimiters, KKStr& _token, bool& _eof, bool& _eol ) { _token = ""; _eof = false; _eol = false; if (_in.eof ()) { _eof = true; _eol = true; return; } kkint32 ch; // Skip past any leading white space. ch = _in.peek (); while ((ch == ' ') && (!_in.eof ())) { _in.get (); ch = _in.peek (); } if (_in.eof ()) { _eof = true; _eol = true; return; } if (ch == '\n') { _eol = true; _in.get (); if (_in.peek () == '\r') _in.get (); return; } if (ch == '\r') { _eol = true; _in.get (); if (_in.peek () == '\n') _in.get (); return; } while ((!_in.eof ()) && (ch != '\n') && (ch != '\r') && (strchr (_delimiters, ch) == NULL)) { _in.get (); _token.Append ((char)ch); ch = _in.peek (); } if (strchr (_delimiters, ch) != NULL) { // the next character was a delimiter; in this case we want to remove from stream. _in.get (); } return; } /* GetToken */
/** *@brief Returns what the next token that 'GetNextToken' will without updating the position in the string buffer. *@details Allows you to see what the token would be without updating the KKStrParser instance. *@param[in] delStr List of delimiting characters. *@returns Next Token to be returned by 'GetNextToken'. */ KKStr KKStrParser::PeekNextToken (const char* delStr) const { kkuint32 nextPosP = nextPos; if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPosP]) != NULL)) nextPosP++; } if (nextPosP >= len) return KKStr::EmptyStr (); kkuint32 startPos = nextPosP; kkuint32 endPos = startPos; char ch = str[endPos]; if ((ch == '\'') || (ch == '"')) { KKStr token (30); // Token is a string char quoteChar = ch; // We have a quoted String need to skip to end of quote. ++endPos; while (endPos < len) { ch = str[endPos]; if (ch == quoteChar) { ++endPos; break; } if ((ch == '\\') && (endPos < (len - 1))) { ++endPos; char ec = str[endPos]; switch (ec) { case '"': ch = '"'; break; case '\\': ch = '\\'; break; case 'r': ch = '\r'; break; case 'n': ch = '\n'; break; case 't': ch = '\t'; break; } } token.Append (ch); ++endPos; } return token; } else { // scan until end of string or next delimiter while (endPos < len) { ch = str[endPos]; if (strchr (delStr, ch) != NULL) { break; } ++endPos; } endPos--; // Move endPos back to last character in token. if (trimWhiteSpace) { while ((endPos >= startPos) && (strchr (whiteSpace, str[endPos]) != NULL)) endPos--; } return KKStr (str, startPos, endPos); } } /* PeekNextToken */
KKStr KKStrParser::GetNextToken (const char* delStr) { lastDelimiter = 0; if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPos]) != NULL)) nextPos++; } if (nextPos >= len) return KKStr::EmptyStr (); kkuint32 startPos = nextPos; kkuint32 endPos = startPos; char ch = str[endPos]; if ((ch == '\'') || (ch == '"')) { KKStr token (30); // Token is a string char quoteChar = ch; // We have a quoted String need to skip to end of quote. ++endPos; // Skipped past initial quote character. while (endPos < len) { ch = str[endPos]; if (ch == quoteChar) break; if ((ch == '\\') && (endPos < (len - 1))) { ++endPos; char ec = str[endPos]; switch (ec) { case '\\': ch = '\\'; break; case '"': ch = '"'; break; case 'r': ch = '\r'; break; case 'n': ch = '\n'; break; case 't': ch = '\t'; break; } } token.Append (ch); ++endPos; } if (endPos >= len) { nextPos = len; } else { // Now that we are at the end of the quoted String we need set the next character pointer to just past the following delimiter character. nextPos = endPos + 1; if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPos]) != NULL)) nextPos++; } if ((nextPos < len) && (strchr (delStr, str[nextPos]) != NULL)) { lastDelimiter = str[nextPos]; ++nextPos; } } return token; } else { // scan until end of string or next delimiter kkuint32 delimeterIdx = 0; bool delimeterFound = false; while (endPos < len) { ch = str[endPos]; if (strchr (delStr, ch) != NULL) { lastDelimiter = ch; delimeterFound = true; delimeterIdx = endPos; break; } ++endPos; } endPos--; // Move endPos back to last character in token. if (trimWhiteSpace) { while ((endPos >= startPos) && (strchr (whiteSpace, str[endPos]) != NULL)) endPos--; } if (delimeterFound) nextPos = delimeterIdx + 1; else if (endPos >= len) nextPos = len; else nextPos = endPos + 1; if (trimWhiteSpace) { while ((nextPos < len) && (strchr (whiteSpace, str[nextPos]) != NULL)) ++nextPos; } return KKStr (str, startPos, endPos); } } /* GetNextToken */