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"
|
2017-07-06 08:35:25 +00:00
|
|
|
#include "shared.h"
|
2016-09-06 17:44:27 +00:00
|
|
|
#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))
|
|
|
|
{
|
2017-07-06 08:35:25 +00:00
|
|
|
size_t nread = hc_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;
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
size_t fgetl (FILE *fp, char *line_buf)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2018-02-08 18:13:29 +00:00
|
|
|
size_t line_len = 0;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-02-14 10:14:32 +00:00
|
|
|
size_t superchop_with_length (char *buf, const size_t len)
|
|
|
|
{
|
|
|
|
size_t new_len = len;
|
|
|
|
|
|
|
|
while (new_len)
|
|
|
|
{
|
|
|
|
if (buf[new_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
new_len--;
|
|
|
|
|
|
|
|
buf[new_len] = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[new_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
new_len--;
|
|
|
|
|
|
|
|
buf[new_len] = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_len;
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
size_t in_superchop (char *buf)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
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--;
|
|
|
|
|
2017-02-14 10:14:32 +00:00
|
|
|
buf[len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[len - 1] == '\r')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
2017-02-14 10:14:32 +00:00
|
|
|
buf[len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|