2016-09-06 17:44:27 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-06 17:44:27 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-06 17:44:27 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "filehandling.h"
|
|
|
|
|
|
|
|
uint count_lines (FILE *fd)
|
|
|
|
{
|
|
|
|
uint cnt = 0;
|
|
|
|
|
2016-09-08 10:17:56 +00:00
|
|
|
char *buf = (char *) mymalloc (HCBUFSIZ_LARGE + 1);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
char prev = '\n';
|
|
|
|
|
|
|
|
while (!feof (fd))
|
|
|
|
{
|
2016-09-08 10:17:56 +00:00
|
|
|
size_t nread = fread (buf, sizeof (char), HCBUFSIZ_LARGE, fd);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
if (nread < 1) continue;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < nread; i++)
|
|
|
|
{
|
|
|
|
if (prev == '\n') cnt++;
|
|
|
|
|
|
|
|
prev = buf[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myfree (buf);
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgetl (FILE *fp, char *line_buf)
|
|
|
|
{
|
|
|
|
int line_len = 0;
|
|
|
|
|
|
|
|
while (!feof (fp))
|
|
|
|
{
|
|
|
|
const int c = fgetc (fp);
|
|
|
|
|
|
|
|
if (c == EOF) break;
|
|
|
|
|
|
|
|
line_buf[line_len] = (char) c;
|
|
|
|
|
|
|
|
line_len++;
|
|
|
|
|
2016-09-08 10:17:56 +00:00
|
|
|
if (line_len == HCBUFSIZ_LARGE) line_len--;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_len == 0) return 0;
|
|
|
|
|
|
|
|
if (line_buf[line_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
line_len--;
|
|
|
|
|
|
|
|
line_buf[line_len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_len == 0) return 0;
|
|
|
|
|
|
|
|
if (line_buf[line_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
line_len--;
|
|
|
|
|
|
|
|
line_buf[line_len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (line_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int in_superchop (char *buf)
|
|
|
|
{
|
|
|
|
int len = strlen (buf);
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[len - 1] == '\r')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|