Removed unnecessary variable from readlines, fixed bug where
EOF terminated line was skippedmaster
parent
a481bec3ba
commit
4efe1541d0
|
@ -1,9 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* array bounds */
|
||||
#define MAXLINES 100
|
||||
#define BUFF_SIZE (80 * 100)
|
||||
#define MAXLINES 100
|
||||
#define BUFF_SIZE (80 * MAXLINES)
|
||||
|
||||
int readlines(char *lines[], int maxlines, char *ln_buff, size_t lb_size);
|
||||
void writelines(char *lines[], int n);
|
||||
|
@ -16,16 +17,15 @@ int main()
|
|||
char buffer[BUFF_SIZE];
|
||||
|
||||
int line_count = readlines(lines, MAXLINES, buffer, BUFF_SIZE);
|
||||
if (line_count >= 0) {
|
||||
qsort_string(lines, 0, line_count-1);
|
||||
writelines(lines, line_count);
|
||||
} else if (line_count == -1) {
|
||||
puts("error: lines overflow");
|
||||
return 1;
|
||||
} else if (line_count == -2) {
|
||||
puts("error: buffer overflow");
|
||||
return 2;
|
||||
if (line_count < 0) {
|
||||
line_count = -line_count - 1;
|
||||
printf("warning: buffer overflow occured, read %d lines\n", line_count);
|
||||
} else if (line_count > MAXLINES) {
|
||||
line_count = MAXLINES;
|
||||
printf("warning: too many lines, read %d lines\n", line_count);
|
||||
}
|
||||
qsort_string(lines, 0, line_count-1);
|
||||
writelines(lines, line_count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -39,40 +39,40 @@ void writelines(char *lines[], int n)
|
|||
}
|
||||
|
||||
/* readlines: reads lines from input, writing them to ln_buff
|
||||
* and line pointers to lines. returns number of lines read,
|
||||
* -1 on lines overflow
|
||||
* -2 on buffer overflow */
|
||||
* and line pointers to lines.
|
||||
* returns number of lines read,
|
||||
* -lines_read-1 < 0 on buffer overflow
|
||||
* maxlines+1 on lines overflow */
|
||||
int readlines(char *lines[], int maxlines, char *buffer, size_t buff_size)
|
||||
{
|
||||
assert(maxlines > 0);
|
||||
char ** const fst_line = lines;
|
||||
char *ln_start = buffer;
|
||||
int c, not_empty = 0;
|
||||
|
||||
for (; (c = getchar()) != EOF && buff_size; --buff_size) {
|
||||
for (*lines = buffer; (c = getchar()) != EOF && buff_size; --buff_size) {
|
||||
not_empty |= !isspace(c);
|
||||
if (c == '\n') {
|
||||
if (!not_empty) { /* skip empty lines */
|
||||
buff_size += buffer - ln_start;
|
||||
buffer = ln_start;
|
||||
buff_size += buffer - *lines;
|
||||
buffer = *lines;
|
||||
continue;
|
||||
}
|
||||
not_empty = 0;
|
||||
*buffer++ = '\0';
|
||||
*lines++ = ln_start;
|
||||
if (!--maxlines)
|
||||
break;
|
||||
not_empty = 0;
|
||||
ln_start = buffer;
|
||||
*++lines = buffer;
|
||||
} else
|
||||
*buffer++ = c;
|
||||
}
|
||||
/* ignore trailing whitespace and emepty lines */
|
||||
/* ignore trailing whitespace and empty lines */
|
||||
while (c != EOF && isspace(c))
|
||||
c = getchar();
|
||||
if (!maxlines && c != EOF) /* lines overflow */
|
||||
return -1;
|
||||
if (!buff_size && c != EOF) /* buffer overflow */
|
||||
return -2;
|
||||
return lines - fst_line;
|
||||
if (c != EOF && !buff_size) /* buffer overflow, return -lines_read-1 */
|
||||
return fst_line - lines - 1;
|
||||
if (c != EOF && !maxlines) /* lines overflow, return maxlines+1 */
|
||||
return lines - fst_line + 2;
|
||||
return lines - fst_line + not_empty;
|
||||
}
|
||||
|
||||
/* ---------- string quicksort */
|
||||
|
|
Loading…
Reference in New Issue