// Reads a line from m_file. Removes comments that are marked with // a hash ('#', aka the pound sign or indeed the octothorpe) character. // Returns 0 on EOF, 1 otherwise bool TextFileReader::ReadLine() { bool eof = false; m_tokenIndex = 0; // // Read some data from the file // If fgets returns NULL that means we've found the EOF if (fgets(m_line, m_maxLineLen + 1, m_file) == NULL) { m_line[0] = '\0'; eof = true; } // // Make sure we read a whole line bool found = false; while (!found && !eof) { if (strchr(m_line, '\n')) { found = true; } if (!found) { unsigned int oldLineLen = m_maxLineLen; DoubleMaxLineLen(); if (fgets(&m_line[oldLineLen], oldLineLen + 1, m_file) == NULL) { eof = true; } } } CleanLine(); m_lineNum++; return !eof; }
// Reads a line from a block of text data. Removes comments that are marked with // a hash ('#', aka the pound sign or indeed the octothorpe) character. // Returns 0 on EOF, 1 otherwise bool TextDataReader::ReadLine() { bool eof = false; m_tokenIndex = 0; // Find the next '\n' character unsigned int eolOffset = m_offset; for (eolOffset = m_offset; eolOffset < m_dataSize; ++eolOffset) { if (m_data[eolOffset] == '\n') { break; } } if (eolOffset == m_dataSize) { eolOffset--; eof = true; } // Make sure the line buffer is big enough to accomodate our painful length unsigned int lineLen = eolOffset - m_offset + 1; while (lineLen > m_maxLineLen) { DoubleMaxLineLen(); } // Copy from the data block into m_line memcpy(m_line, &m_data[m_offset], lineLen); m_line[lineLen] = '\0'; // Move m_offset on m_offset += lineLen; CleanLine(); m_lineNum++; return !eof; }