You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
intparse_get_line(char *lineout, int max_line_len, constchar *start, int max_size, constchar *cur)
2140
+
// Reads one line of text from the input, returning the number of input chars read. Also sets the line ending type if found;
2141
+
// and if there is a mismatch, displays a warning.
2142
+
intparse_get_line(char *lineout, int max_line_len, constchar *textin, int input_len, int line_num, LineEndingType &file_line_ending_type, bool &warned_for_this_file)
2141
2143
{
2142
-
char * t = lineout;
2143
-
int i, num_chars_read=0;
2144
-
char c;
2144
+
auto found_line_ending = LineEndingType::UNKNOWN;
2145
+
char prev_c = '\0';
2146
+
int num_chars_written = 0;
2145
2147
2146
-
for ( i = 0; i < max_line_len-1; i++ ) {
2147
-
do {
2148
-
if ( (cur - start) >= max_size ) {
2149
-
*lineout = 0;
2150
-
if ( lineout > t ) {
2151
-
return num_chars_read;
2152
-
} else {
2153
-
return0;
2154
-
}
2148
+
for (int num_chars_read = 1; num_chars_read <= input_len; ++num_chars_read)
2149
+
{
2150
+
char c = *textin++;
2151
+
2152
+
if (c == '\0' || c == EOF) // hard stop
2153
+
{
2154
+
input_len = num_chars_read;
2155
+
break;
2156
+
}
2157
+
elseif (c == EOLN)
2158
+
{
2159
+
if (prev_c == CARRIAGE_RETURN)
2160
+
found_line_ending = LineEndingType::CRLF;
2161
+
else
2162
+
found_line_ending = LineEndingType::LF;
2163
+
}
2164
+
elseif (c == CARRIAGE_RETURN)
2165
+
{
2166
+
if (*textin != EOLN)
2167
+
found_line_ending = LineEndingType::CR;
2168
+
}
2169
+
else
2170
+
{
2171
+
if (num_chars_written == max_line_len)
2172
+
{
2173
+
num_chars_read--; // back out the character we just read, since we can't write it
2174
+
2175
+
// terminate the string and return
2176
+
*lineout = '\0';
2177
+
return num_chars_read;
2155
2178
}
2156
-
c = *cur++;
2157
-
num_chars_read++;
2158
-
} while ( c == 13 );
2159
2179
2160
-
*lineout++ = c;
2161
-
if ( c=='\n' ) break;
2180
+
*lineout++ = c;
2181
+
num_chars_written++;
2182
+
}
2183
+
2184
+
if (found_line_ending != LineEndingType::UNKNOWN)
2185
+
{
2186
+
if (file_line_ending_type == LineEndingType::UNKNOWN)
// we can't use error_display() here because we're in the middle of reading the file
2191
+
Warning(LOCATION, "In %s, an inconsistent line ending was detected on line %d. Please check the file for line ending errors.", Current_filename_sub, line_num);
2192
+
warned_for_this_file = true;
2193
+
}
2194
+
2195
+
// ugh, if we're at the max length, we can't write the newline, so back out the newline we read
2196
+
if (num_chars_written == max_line_len)
2197
+
{
2198
+
if (found_line_ending == LineEndingType::CRLF)
2199
+
num_chars_read -= 2;
2200
+
else
2201
+
num_chars_read--;
2202
+
}
2203
+
else
2204
+
{
2205
+
*lineout++ = EOLN; // normalize line endings to single-character EOLN
2206
+
num_chars_written++;
2207
+
}
2208
+
2209
+
// terminate the string and return
2210
+
*lineout = '\0';
2211
+
return num_chars_read;
2212
+
}
2213
+
2214
+
prev_c = c;
2162
2215
}
2163
2216
2164
-
*lineout++ = 0;
2165
-
return num_chars_read;
2217
+
// we read the entire input without reaching a newline
0 commit comments