#include "readwrite.h"
#include <stddef.h> /* size_t */
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
#include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
- * fputc(), fwrite(), fclose(), fopen()
+ * fputc(), fwrite(), fclose(), fopen(), clearerr()
*/
#include <string.h> /* strlen() */
#include <unistd.h> /* for access(), unlink() */
extern int try_fgetc(FILE * file, char * f)
{
+ clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
int test = fgetc(file);
exit_trouble(EOF == test && ferror(file), f, "fgetc()");
return test;
-extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
+extern uint32_t textfile_width(FILE * file)
{
- char * f_name = "textfile_sizes()";
+ char * f_name = "textfile_width()";
int c = 0;
uint32_t c_count = 0;
- uint32_t n_lines = 0;
uint32_t linemax = 0;
while (1)
{
break;
}
c_count++;
+ exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
if ('\n' == c)
{
if (c_count > linemax)
linemax = c_count;
}
c_count = 0;
- if (n_lines_p)
- {
- n_lines++;
- }
}
}
if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
linemax = c_count;
}
exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
- if (n_lines_p)
- {
- * n_lines_p = n_lines;
- }
return linemax;
}