Skip to content

Commit

Permalink
skip the BOM before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jun 17, 2022
1 parent bb49980 commit 127873f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ additional documentation and more details are available in `man gplaces`. type `
## Statistic
Language|files|blank|comment|code
:-------|-------:|-------:|-------:|-------:
C|1|235|58|1072
C|1|236|58|1074
23 changes: 13 additions & 10 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ static int parse_url(Selector *from, Selector *sel, const char *url, const char
}


static void parse_plaintext_line(Selector *from, char *line, int *pre, int *index, Selector **sel, SelectorList *list) {
static void parse_plaintext_line(Selector *from, char *line, int start, int *pre, int *index, Selector **sel, SelectorList *list) {
(void)from;
(void)start;
(void)pre;
(void)index;

Expand All @@ -287,12 +288,14 @@ static void parse_plaintext_line(Selector *from, char *line, int *pre, int *inde
}


static void parse_gemtext_line(Selector *from, char *line, int *pre, int *index, Selector **sel, SelectorList *list) {
static void parse_gemtext_line(Selector *from, char *line, int start, int *pre, int *index, Selector **sel, SelectorList *list) {
char *url;
int level;

*sel = NULL;

if (start && strncmp(line, "\xef\xbb\xbf", 3) == 0) line += 3;

if (strncmp(line, "```", 3) == 0) {
*pre = !*pre;
return;
Expand Down Expand Up @@ -327,15 +330,15 @@ static void parse_gemtext_line(Selector *from, char *line, int *pre, int *index,
}


static SelectorList parse_file(Selector *from, FILE *fp, void (*parse_line)(Selector *, char *, int *, int *, Selector **, SelectorList *)) {
static SelectorList parse_file(Selector *from, FILE *fp, void (*parse_line)(Selector *, char *, int, int *, int *, Selector **, SelectorList *)) {
static char buffer[LINE_MAX];
char *line;
SelectorList list = SIMPLEQ_HEAD_INITIALIZER(list);
Selector *sel;
int pre = 0, index = 1;
int pre = 0, index = 1, start = 1;

for (sel = NULL; (line = fgets(buffer, sizeof(buffer), fp)) != NULL; sel = NULL) {
parse_line(from, line, &pre, &index, &sel, &list);
for (sel = NULL; (line = fgets(buffer, sizeof(buffer), fp)) != NULL; sel = NULL, start = 0) {
parse_line(from, line, start, &pre, &index, &sel, &list);
}

return list;
Expand Down Expand Up @@ -907,8 +910,8 @@ static SelectorList download_text(Selector *sel, int ask, int handle, int print)
end = &buffer[sizeof(buffer) - 1]; /* if the buffer is full and we haven't found a \n, terminate the line */
}
*end = '\0';
if (plain) parse_plaintext_line(sel, start, &pre, &index, &it, &list);
else parse_gemtext_line(sel, start, &pre, &index, &it, &list);
if (plain) parse_plaintext_line(sel, start, parsed == 0, &pre, &index, &it, &list);
else parse_gemtext_line(sel, start, parsed == 0, &pre, &index, &it, &list);
if (print && it) print_gemtext_line(stdout, it, NULL, width);
}
length -= parsed;
Expand All @@ -921,8 +924,8 @@ static SelectorList download_text(Selector *sel, int ask, int handle, int print)
if (prog > 0) fputc('\n', stderr);
if (!(ok = (received == 0 || (received < 0 && !ssl_error(sel, ssl, received))))) goto out;
if (length > 0) {
if (plain) parse_plaintext_line(sel, buffer, &pre, &index, &it, &list);
else parse_gemtext_line(sel, buffer, &pre, &index, &it, &list);
if (plain) parse_plaintext_line(sel, buffer, parsed == 0, &pre, &index, &it, &list);
else parse_gemtext_line(sel, buffer, parsed == 0, &pre, &index, &it, &list);
if (print && it) print_gemtext_line(stdout, it, NULL, width);
}

Expand Down

0 comments on commit 127873f

Please sign in to comment.