tapas

A small program used for compiling refer output into the APA reference format.
git clone git://git.noxz.tech/tapas
Log | Files | Refs | README | LICENSE

commit cd78b8f851c7f97d3a168f3cf82542da188aea64
parent 60db686da3513fb1079b327afb37082f40542abf
Author: Chris Noxz <chris@noxz.tech>
Date:   Sun, 10 Nov 2019 17:53:28 +0100

Declutter main, and fix error in readstr & readattr

Diffstat:
Mtapas.c | 106++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 64 insertions(+), 42 deletions(-)

diff --git a/tapas.c b/tapas.c @@ -80,6 +80,19 @@ typedef struct { char url[BUF_SIZE]; } Referece; +static void trim(char*); +static int readstr(const char*, const char*, char*, int); +static int readattr(const char*, const char*, char*, char*, int); +static void setattr(Referece*, char, char*); +static void printref(Referece*, int, int); +static void wordsym(Referece*); +static void findreplace(char*, const char*, const char*); +static void wordsym(Referece*); +static void format_article(Referece*, int); +static void format_book(Referece*, int); +static void format_article_in_book(Referece*, int); +static int loadstr(char*); + void trim(char *source) { @@ -109,11 +122,10 @@ readstr(const char *src, const char *pre, char *str, int length) int c = 0; const char *p; - memset(str, 0x0, length); - if (strncmp(src, pre, strlen(pre)) != 0) return 0; + memset(str, 0, length); p = (src + strlen(pre)); while (*p != '\n' && c < length) @@ -128,11 +140,10 @@ readattr(const char *src, const char *fmt, char *t, char *val, int length) int c = 0; const char *p; - memset(val, 0x0, length); - if (sscanf(src, fmt, t) == 0) return 0; + memset(val, 0, length); p = (src + strlen(fmt)); while (*p != '\n' && c < length) @@ -142,6 +153,50 @@ readattr(const char *src, const char *fmt, char *t, char *val, int length) } void +setattr(Referece *ref, char type, char *val) +{ + switch (type) { + case 'A': strcpy(ref->author, val); break; + case 'T': strcpy(ref->title, val); break; + case 'B': strcpy(ref->book_title, val); break; + case 'R': strcpy(ref->report_number, val); break; + case 'J': strcpy(ref->journal_name, val); break; + case 'E': strcpy(ref->editor, val); break; + case 'e': strcpy(ref->edition, val); break; + case 'V': strcpy(ref->volume, val); break; + case 'N': strcpy(ref->journal_number, val); break; + case 'S': strcpy(ref->series, val); break; + case 'C': strcpy(ref->city, val); break; + case 'I': strcpy(ref->publisher, val); break; + case 'D': strcpy(ref->publication_date, val); break; + case 'P': strcpy(ref->page_number, val); break; + case 'G': strcpy(ref->gov_number, val); break; + case 'O': strcpy(ref->other, val); break; + case 'K': strcpy(ref->keywords, val); break; + case 'd': strcpy(ref->original_pub_date, val); break; + case 'a': strcpy(ref->additions, val); break; + case 't': strcpy(ref->reprint_title, val); break; + case 'l': strcpy(ref->translator, val); break; + case 'r': strcpy(ref->translator_editor, val); break; + case 's': strcpy(ref->site_name, val); break; + case 'c': strcpy(ref->site_content, val); break; + case 'o': strcpy(ref->organization, val); break; + case 'u': strcpy(ref->url, val); break; + } +} + +void +printref(Referece *ref, int type, int macroset) +{ + wordsym(ref); + switch (type) { + case 1: format_article(ref, macroset); break; + case 2: format_book(ref, macroset); break; + case 3: format_article_in_book(ref, macroset); break; + } +} + +void findreplace(char *source, const char *find, const char *replace) { char *dest = malloc(strlen(source) - strlen(find) + strlen(replace) + 1); @@ -275,8 +330,8 @@ main(int arc, char *argv[]) int inbib = 0; int macroset = ms_ms; int reftype; - char at; - char av[BUF_SIZE]; + char atype; + char val[BUF_SIZE]; size_t size; char *line; Referece *ref = NULL; @@ -311,43 +366,10 @@ main(int arc, char *argv[]) if (inbib) { if (!ref && strcmp(line, REF_START) == 0) { ref = (Referece*)calloc(1, sizeof(Referece)); - } else if (ref && readattr(line, REF_ATTR, &at, av, BUF_SIZE)) { - switch (at) { - case 'A': strcpy(ref->author, av); break; - case 'T': strcpy(ref->title, av); break; - case 'B': strcpy(ref->book_title, av); break; - case 'R': strcpy(ref->report_number, av); break; - case 'J': strcpy(ref->journal_name, av); break; - case 'E': strcpy(ref->editor, av); break; - case 'e': strcpy(ref->edition, av); break; - case 'V': strcpy(ref->volume, av); break; - case 'N': strcpy(ref->journal_number, av); break; - case 'S': strcpy(ref->series, av); break; - case 'C': strcpy(ref->city, av); break; - case 'I': strcpy(ref->publisher, av); break; - case 'D': strcpy(ref->publication_date, av); break; - case 'P': strcpy(ref->page_number, av); break; - case 'G': strcpy(ref->gov_number, av); break; - case 'O': strcpy(ref->other, av); break; - case 'K': strcpy(ref->keywords, av); break; - case 'd': strcpy(ref->original_pub_date, av); break; - case 'a': strcpy(ref->additions, av); break; - case 't': strcpy(ref->reprint_title, av); break; - case 'l': strcpy(ref->translator, av); break; - case 'r': strcpy(ref->translator_editor, av); break; - case 's': strcpy(ref->site_name, av); break; - case 'c': strcpy(ref->site_content, av); break; - case 'o': strcpy(ref->organization, av); break; - case 'u': strcpy(ref->url, av); break; - default: continue; - } + } else if (ref && readattr(line, REF_ATTR, &atype, val, BUF_SIZE)) { + setattr(ref, atype, val); } else if (ref && sscanf(line, REF_END, &reftype) == 1) { - wordsym(ref); - switch (reftype) { - case 1: format_article(ref, macroset); break; - case 2: format_book(ref, macroset); break; - case 3: format_article_in_book(ref, macroset); break; - } + printref(ref, reftype, macroset); free(ref); ref = NULL; }