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"
|
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
u64 count_lines (FILE *fd)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2016-09-30 14:43:59 +00:00
|
|
|
u64 cnt = 0;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2016-11-20 21:54:52 +00:00
|
|
|
char *buf = (char *) hcmalloc (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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
hcfree (buf);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
while (line_len)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2017-01-05 20:58:24 +00:00
|
|
|
if (line_buf[line_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
line_len--;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
if (line_buf[line_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
line_len--;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
break;
|
2016-09-06 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
line_buf[line_len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
return (line_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int in_superchop (char *buf)
|
|
|
|
{
|
2016-11-17 05:17:28 +00:00
|
|
|
size_t len = strlen (buf);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[len - 1] == '\r')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|