| 91 | |
| 92 | |
| 93 | wxString CTextFile::GetNextLine(EReadTextFile flags, const wxMBConv& conv, bool* result) |
| 94 | { |
| 95 | wxCHECK_MSG(m_file.IsOpened(), "", "Trying to read from closed file."); |
| 96 | wxCHECK_MSG(!m_file.Eof(), "", "Trying to read past EOF"); |
| 97 | wxCHECK_MSG((m_mode == read), "", "Trying to read from non-readable file."); |
| 98 | |
| 99 | bool is_filtered = false; |
| 100 | |
| 101 | wxString line; |
| 102 | char buffer[TXTBUF_SIZE]; |
| 103 | |
| 104 | // Loop until EOF (fgets will then return NULL) or a newline is read. |
| 105 | while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) { |
| 106 | // Filters must be first applied here to avoid unnecessary CPU usage. |
| 107 | |
| 108 | if (line.IsEmpty()) { |
| 109 | if (buffer[0] == '\0') { |
| 110 | // Empty line. |
| 111 | break; |
| 112 | } else if (flags & txtIgnoreComments) { |
| 113 | int i = 0; |
| 114 | char t = buffer[i]; |
| 115 | while (t) { |
| 116 | if ((t == ' ') || (t == '\t')) { |
| 117 | ++i; |
| 118 | t = buffer[i]; |
| 119 | } else { |
| 120 | is_filtered = (buffer[i] == '#'); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!is_filtered) { |
| 128 | // NB: The majority of the time spent by this function is |
| 129 | // spent converting the multibyte string to wide-char. |
| 130 | line += conv.cMB2WC(buffer); |
| 131 | |
| 132 | // Remove any newlines, carriage returns, etc. |
| 133 | if (line.Length() && (line.Last() == '\n')) { |
| 134 | if ((line.Length() > 1)) { |
| 135 | if (line[line.Length() - 2] == '\r') { |
| 136 | // Carriage return + newline |
| 137 | line.RemoveLast(2); |
| 138 | } else { |
| 139 | // Only a newline. |
| 140 | line.RemoveLast(1); |
| 141 | } |
| 142 | } else { |
| 143 | // Empty line |
| 144 | line.Clear(); |
| 145 | } |
| 146 | |
| 147 | // We've read an entire line. |
| 148 | break; |
| 149 | } |
| 150 | } else { |