void InText::readString(std::string& value, PhysicalInStream& stream) { value = ""; skipWhitespace(stream); bool containsSpaces = theChar == '"'; if(containsSpaces && !isEof(stream)) nextChar(stream); while(!isEof(stream) && (containsSpaces || !isWhitespace()) && (!containsSpaces || theChar != '"')) { if(theChar == '\\') { nextChar(stream); if(theChar == 'n') theChar = '\n'; else if(theChar == 'r') theChar = '\r'; else if(theChar == 't') theChar = '\t'; } value += theChar; if(!isEof(stream)) nextChar(stream); } if(containsSpaces && !isEof(stream)) nextChar(stream); skipWhitespace(stream); }
void InConfig::skipLine(PhysicalInStream& stream) { while(!isEof(stream) && theChar != '\n') nextChar(stream); if(!isEof(stream)) nextChar(stream); }
void TextSplitter::processLine() { if (isEof()) return; _currLine = _lines[_lineIndex++]; // Cut off comments char *comment_start = strchr(_currLine, '#'); if (comment_start) *comment_start = '\0'; // Cut off trailing whitespace (including '\r') char *strend = strchr(_currLine, '\0'); while (strend > _currLine && isspace(strend[-1])) strend--; *strend = '\0'; // Skip blank lines if (*_currLine == '\0') nextLine(); // Convert to lower case if (!isEof()) for (char *s = _currLine; *s != '\0'; s++) *s = tolower(*s); }
Token *Lexer::scanCharacter() { startToken(); consume(); // left single quote if(isEof()) throw lexerException("unexpected EOF found in a character literal"); if(_currentChar != '\'') { if(_currentChar == '\\') consumeEscapeSequence(); else consume(); if(isEof()) throw lexerException("unexpected EOF found in a character literal"); if(_currentChar != '\'') throw lexerException("a character literal can't have more than one character"); } consume(); // right single quote return finishToken(Token::Character); }
void InConfig::skipComment(PhysicalInStream& stream) { // skip /* nextChar(stream); nextChar(stream); while(!isEof(stream) && (theChar != '*' || theNextChar != '/')) nextChar(stream); // skip */ if(!isEof(stream)) nextChar(stream); if(!isEof(stream)) nextChar(stream); }
void InText::readInt(int& d, PhysicalInStream& stream) { skipWhitespace(stream); int sign = 1; if(!isEof(stream) && theChar == '-') { sign = -1; nextChar(stream); } else if(!isEof(stream) && theChar == '+') nextChar(stream); unsigned u; readUInt(u, stream); d = sign * static_cast<int>(u); }
void InConfig::nextChar(PhysicalInStream& stream) { InText::nextChar(stream); if(readSection && theChar == '[') while(!isEof(stream)) InText::nextChar(stream); }
int CpuProfileInputStream::readLine(gtString& str) { wchar_t buf = L'\0'; str.makeEmpty(); if (!m_fileStream.isOpened()) { return -1; } while (!isEof()) { buf = fgetwc(m_fileStream.getHandler()); if (buf == (wchar_t) WEOF) { break; } if (buf != L'\n') { str += buf; } else { str += L'\0'; break; } } return str.length(); }
void Lexer::consumeEscapeSequenceNumber() { char type; QString allowedChars; short maxSize; if(_currentChar == 'x' || _currentChar == 'X') { type = 'x'; allowedChars = "0123456789abcdef"; maxSize = 2; consume(); } else if(_currentChar == 'u' || _currentChar == 'U') { type = 'u'; allowedChars = "0123456789abcdef"; maxSize = 4; consume(); } else { type = 'o'; allowedChars = "01234567"; maxSize = 3; } QString number = ""; while(number.size() < maxSize) { if(isEof()) throw lexerException("unexpected EOF found in an escape sequence number"); if(!allowedChars.contains(_currentChar, Qt::CaseInsensitive)) break; number.append(_currentChar); consume(); } if(number.isEmpty()) throw lexerException("invalid escape sequence number"); bool ok; ushort code = type == 'o' ? number.toUShort(&ok, 8) : number.toUShort(&ok, 16); if(!ok) throw lexerException("invalid number in escape sequence"); if(type != 'u' && code > 0xFF) throw lexerException("invalid number in escape sequence"); }
Token *Lexer::nextToken() { while(true) { switch(_currentChar.toAscii()) { case '\'': return scanCharacter(); case '"': return scanText(); case '(': return scan(Token::LeftParenthesis); case ')': return scan(Token::RightParenthesis); case '[': return scan(Token::LeftBracket); case ']': return scanRightBracket(); case '{': return scan(Token::LeftBrace); case '}': return scan(Token::RightBrace); case ';': return scan(Token::Semicolon); default: if(isEof()) return scan(Token::Eof); else if(isLineComment()) consumeLineComment(); else if(isBlockComment()) consumeBlockComment(); else if(isNewline()) return scanNewline(); else if(isSpace()) consumeSpaces(); else if(isName()) return scanName(); else if(isBackquotedName()) return scanBackquotedName(); else if(isNumber()) return scanNumber(); else if(isOperator()) return scanOperator(); else throw lexerException(QString("invalid character: '%1'").arg(_currentChar)); } } }
void InConfig::skipWhitespace(PhysicalInStream& stream) { while(!isEof(stream) && isWhitespace()) { while(!isEof(stream) && InText::isWhitespace()) nextChar(stream); if(!isEof(stream)) { if(theChar == '/' && theNextChar == '/') skipLine(stream); else if(theChar == '/' && theNextChar == '*') skipComment(stream); else if(theChar == '#') skipLine(stream); } } }
// Returns whether or not to keep reading the file. // Stop reading (false) if eof or there is a problem reading the file. bool FastQFile::keepReadingFile() { if(isEof() || myFileProblem) { return(false); } return(true); }
unsigned char InputCompressedFileStreamBuffer::popByte() { char byte = 0; if(!isEof()) byte = data->buffer[data->readPosition++]; return byte; }
void Lexer::consumeEscapeSequence() { consume(); // anti-slash if(isEof()) throw lexerException("unexpected EOF found in an escape sequence"); if(QString("trn\"'{\\").contains(_currentChar)) consume(); else if(QString("01234567xu").contains(_currentChar, Qt::CaseInsensitive)) consumeEscapeSequenceNumber(); else throw lexerException(QString("unknown escape sequence: '\\%1'").arg(_currentChar)); }
void CpuProfileInputStream::markPos(const gtString& mark) { if (!mark.isEmpty() && m_fileStream.isOpened() && !isEof()) { fpos_t pos; getCurrentPosition(&pos); m_sectionMap.insert(SectionStreamPosMap::value_type(mark, pos)); } }
Token *Lexer::scanText() { startToken(); consume(); // left double quote while(_currentChar != '"') { if(isEof()) throw lexerException("unexpected EOF found in a text literal"); if(_currentChar == '\\') consumeEscapeSequence(); else consume(); }; consume(); // right double quote return finishToken(Token::Text); }
void Lexer::consumeBlockComment() { consume(); // Slash consume(); // * while(!(_currentChar == '*' && _nextChar == '/')) { if(isEof()) throw lexerException("unexpected EOF found before the end of a comment"); consume(); } consume(); // * consume(); // Slash }
Token *Lexer::scanBackquotedName() { consume(); // opening backquote startToken(); while(_currentChar != '`') { if(isEof()) throw lexerException("unexpected EOF found in a text literal"); consume(); }; Token *token = finishToken(Token::Name); consume(); // closing backquote return token; }
void InConfig::create(const std::string& sectionName, PhysicalInStream& stream) { if(stream.exists() && sectionName != "") { std::string fileEntry; std::string section = std::string("[") + sectionName + "]"; while(!isEof(stream)) { readString(fileEntry, stream); if(fileEntry == section) { if(theChar == '[') // handle empty section while(!isEof(stream)) InText::nextChar(stream); break; } } readSection = true; } }
void InText::readUInt(unsigned int& d, PhysicalInStream& stream) { buf = ""; skipWhitespace(stream); while(!isEof(stream) && isdigit(theChar)) { buf += theChar; nextChar(stream); } d = static_cast<unsigned>(strtoul(buf.c_str(), nullptr, 0)); skipWhitespace(stream); }
bool getEnd() const { if( isEof() ) { return true; } switch( *this->current ) { case ' ': case '\t': case '\r': case '\n': case '}': case ']': case ',': return true; } return false; }
void InText::readBool(bool& value, PhysicalInStream& stream) { skipWhitespace(stream); if(!isEof(stream)) { if(theChar == '0' || theChar == '1') { value = theChar != '0'; nextChar(stream); } else { value = theChar != 'f'; static const char* falseString = "false"; static const char* trueString = "true"; const char* p = value ? trueString : falseString; while(!isEof(stream) && *p && theChar == *p) { ++p; nextChar(stream); } } } }
Buffer File::readLine(bool keepEol, size_t maxLen) { if (!m_fd) { throw IOException("readLine", EINVAL); } uint32_t bytesRead = 0; Buffer buf; for (;;) { const size_t lineChunk = 2048; char * tmpBuf = buf.lockBuffer(bytesRead + lineChunk); tmpBuf += bytesRead; char *r = fgets(tmpBuf, lineChunk, m_fd); if (r == NULL && isEof() == false) { buf.unlockBuffer(bytesRead); throw IOException("fgets", errno); } if (isEof()) { break; } size_t len = strlen(r); bytesRead += len; if (tmpBuf[len-1] != '\n') { buf.unlockBuffer(bytesRead); if (maxLen && len > maxLen) throw BufferOverflowException(); } else { if (!keepEol) { tmpBuf[len-1] = '\0'; bytesRead--; } break; } } buf.unlockBuffer(bytesRead); return buf; }
bool InText::expectString(const std::string& str, PhysicalInStream& stream) { const char* p = str.c_str(); if(str.length()) { while(*p) { if(isEof(stream) || theChar != *p) return false; ++p; nextChar(stream); } } return true; }
int V3PreProcImp::getRawToken() { // Get a token from the file, whatever it may be. while (1) { next_tok: if (m_lineAdd) { m_lineAdd--; m_rawAtBol = true; yyourtext("\n",1); if (debug()>=5) debugToken(VP_WHITE, "LNA"); return (VP_WHITE); } if (m_lineCmt!="") { // We have some `line directive or other processed data to return to the user. static string rtncmt; // Keep the c string till next call rtncmt = m_lineCmt; if (m_lineCmtNl) { if (!m_rawAtBol) rtncmt = "\n"+rtncmt; m_lineCmtNl = false; } yyourtext(rtncmt.c_str(), rtncmt.length()); m_lineCmt = ""; if (yyourleng()) m_rawAtBol = (yyourtext()[yyourleng()-1]=='\n'); if (m_states.top()==ps_DEFVALUE) { V3PreLex::s_currentLexp->appendDefValue(yyourtext(),yyourleng()); goto next_tok; } else { if (debug()>=5) debugToken(VP_TEXT, "LCM"); return (VP_TEXT); } } if (isEof()) return (VP_EOF); // Snarf next token from the file int tok = m_lexp->lex(); if (debug()>=5) debugToken(tok, "RAW"); // A EOF on an include, so we can print `line and detect mis-matched "s if (tok==VP_EOF) { goto next_tok; // find the EOF, after adding needed lines } if (yyourleng()) m_rawAtBol = (yyourtext()[yyourleng()-1]=='\n'); return tok; } }
eIoAction EventTrigger::handleIoEvent() { if (isError()) { throw std::system_error(errno, std::system_category(), "EventTrigger::handleIoEvent: error."); } if (isEof()) { throw std::system_error(errno, std::system_category(), "EventTrigger::handleIoEvent: eof."); } acknowledge(); return eIoAction::Read; }
void InText::readDouble(double& d, PhysicalInStream& stream) { buf = ""; skipWhitespace(stream); if(!isEof(stream) && (theChar == '-' || theChar == '+')) { buf += theChar; nextChar(stream); } while(!isEof(stream) && isdigit(theChar)) { buf += theChar; nextChar(stream); } if(!isEof(stream) && theChar == '.') { buf += theChar; nextChar(stream); } while(!isEof(stream) && isdigit(theChar)) { buf += theChar; nextChar(stream); } if(!isEof(stream) && (theChar == 'e' || theChar == 'E')) { buf += theChar; nextChar(stream); } if(!isEof(stream) && (theChar == '-' || theChar == '+')) { buf += theChar; nextChar(stream); } while(!isEof(stream) && isdigit(theChar)) { buf += theChar; nextChar(stream); } d = atof(buf.c_str()); skipWhitespace(stream); }
bool PipeIODevice::onRun() { if( this->isReading() ) { if( _ravail || isEof() || _impl.runRead( *loop() ) ) { inputReady().send(*this); return true; } } if( this->isWriting() ) { if( _wavail || _impl.runWrite( *loop() ) ) { outputReady().send(*this); return true; } } return false; }
void InText::readAngle(Angle& d, PhysicalInStream& stream) { static const std::string degString = "deg"; static const std::string radString = "rad"; static const std::string piRadString = "pi"; float value = d; readFloat(value, stream); bool isDeg = false; bool isPiRad = false; if(!isEof(stream)) { if(theChar == 'd') isDeg = expectString(degString, stream); else if(theChar == 'p') isPiRad = expectString(piRadString, stream); else if(theChar == 'r') expectString(radString, stream); } d = isDeg ? Angle::fromDegrees(value) : isPiRad ? Angle(value * pi) : Angle(value); }
string V3PreProcImp::getline() { // Get a single line from the parse stream. Buffer unreturned text until the newline. if (isEof()) return ""; const char* rtnp; bool gotEof = false; while (NULL==(rtnp=strchr(m_lineChars.c_str(),'\n')) && !gotEof) { string buf; int tok = getFinalToken(buf/*ref*/); if (debug()>=5) { string bufcln = V3PreLex::cleanDbgStrg(buf); fprintf (stderr,"%d: GETFETC: %-10s: %s\n", m_lexp->m_tokFilelinep->lineno(), tokenName(tok), bufcln.c_str()); } if (tok==VP_EOF) { // Add a final newline, if the user forgot the final \n. if (m_lineChars != "" && m_lineChars[m_lineChars.length()-1] != '\n') { m_lineChars.append("\n"); } gotEof = true; } else { m_lineChars.append(buf); } } // Make new string with data up to the newline. int len = rtnp-m_lineChars.c_str()+1; string theLine(m_lineChars, 0, len); m_lineChars = m_lineChars.erase(0,len); // Remove returned characters if (debug()>=4) { string lncln = V3PreLex::cleanDbgStrg(theLine); fprintf (stderr,"%d: GETLINE: %s\n", m_lexp->m_tokFilelinep->lineno(), lncln.c_str()); } return theLine; }