Changeset f1d036c in serd


Ignore:
Timestamp:
Sep 24, 2017, 10:02:59 AM (8 weeks ago)
Author:
David Robillard <d@…>
Branches:
master, serd1
Children:
f341716
Parents:
8d954ab
git-author:
David Robillard <d@…> (09/24/17 07:35:44)
git-committer:
David Robillard <d@…> (09/24/17 10:02:59)
Message:

Make serdi syntax options case-insensitive

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • NEWS

    r8d954ab rf1d036c  
    11serd (0.29.3) unstable;
    22
     3  * Make serdi syntax options case-insensitive
    34  * Add basic error tolerant parsing for line-based formats
    45  * Support strict parsing of prefixed names
     
    89  * Fix building with MSVC
    910
    10  -- David Robillard <d@drobilla.net>  Sat, 23 Sep 2017 20:33:19 +0200
     11 -- David Robillard <d@drobilla.net>  Sun, 24 Sep 2017 16:34:20 +0200
    1112
    1213serd (0.28.0) stable;
  • doc/serdi.1

    r8d954ab rf1d036c  
    3535.TP
    3636\fB\-i SYNTAX\fR
    37 Read input in SYNTAX (turtle/ntriples/trig/nquads).
     37Read input as SYNTAX.
     38Valid values (case-insensitive): turtle, ntriples, trig, nquads.
    3839
    3940.TP
     
    4344.TP
    4445\fB\-o SYNTAX\fR
    45 Write output in SYNTAX (turtle/ntriples/nquads).
     46Write output as SYNTAX.
     47Valid values (case-insensitive): turtle, ntriples, trig, nquads.
    4648
    4749.TP
  • src/reader.c

    r8d954ab rf1d036c  
    15941594        return -1;
    15951595    }
    1596     const char* s1 = (const char*)node->buf;
    1597     const char* s2 = tok;
    1598     for (; n > 0 && *s2; s1++, s2++, --n) {
    1599         if (toupper(*s1) != toupper(*s2)) {
    1600             return ((*(uint8_t*)s1 < *(uint8_t*)s2) ? -1 : +1);
    1601         }
    1602     }
    1603     return 0;
     1596    return serd_strncasecmp((const char*)node->buf, tok, n);
    16041597}
    16051598
  • src/serd_internal.h

    r8d954ab rf1d036c  
    2121
    2222#include <assert.h>
     23#include <ctype.h>
    2324#include <errno.h>
    2425#include <stdio.h>
     
    336337               SerdNodeFlags* flags);
    337338
     339static inline int
     340serd_strncasecmp(const char* s1, const char* s2, size_t n)
     341{
     342    for (; n > 0 && *s2; s1++, s2++, --n) {
     343        if (toupper(*s1) != toupper(*s2)) {
     344            return ((*(uint8_t*)s1 < *(uint8_t*)s2) ? -1 : +1);
     345        }
     346    }
     347    return 0;
     348}
     349
    338350static inline uint32_t
    339351utf8_num_bytes(const uint8_t c)
  • src/serdi.c

    r8d954ab rf1d036c  
    2424#define SERDI_ERROR(msg)       fprintf(stderr, "serdi: " msg);
    2525#define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__);
     26
     27typedef struct {
     28    SerdSyntax  syntax;
     29    const char* name;
     30    const char* extension;
     31} Syntax;
     32
     33static const Syntax syntaxes[] = {
     34    {SERD_TURTLE,   "turtle",   ".ttl"},
     35    {SERD_NTRIPLES, "ntriples", ".nt"},
     36    {SERD_NQUADS,   "nquads",   ".nq"},
     37    {SERD_TRIG,     "trig",     ".trig"},
     38    {(SerdSyntax)0, NULL, NULL}
     39};
     40
     41static SerdSyntax
     42get_syntax(const char* name)
     43{
     44    for (const Syntax* s = syntaxes; s->name; ++s) {
     45        if (!serd_strncasecmp(s->name, name, strlen(name))) {
     46            return s->syntax;
     47        }
     48    }
     49    SERDI_ERRORF("unknown syntax `%s'\n", name);
     50    return (SerdSyntax)0;
     51}
    2652
    2753static int
     
    5884    fprintf(os, "  -v           Display version information and exit.\n");
    5985    return error ? 1 : 0;
    60 }
    61 
    62 static bool
    63 set_syntax(SerdSyntax* syntax, const char* name)
    64 {
    65     if (!strcmp(name, "turtle")) {
    66         *syntax = SERD_TURTLE;
    67     } else if (!strcmp(name, "ntriples")) {
    68         *syntax = SERD_NTRIPLES;
    69     } else if (!strcmp(name, "nquads")) {
    70         *syntax = SERD_NQUADS;
    71     } else if (!strcmp(name, "trig")) {
    72         *syntax = SERD_TRIG;
    73     } else {
    74         SERDI_ERRORF("unknown syntax `%s'\n", name);
    75         return false;
    76     }
    77     return true;
    7886}
    7987
     
    139147            if (++a == argc) {
    140148                return missing_arg(argv[0], 'i');
    141             } else if (!set_syntax(&input_syntax, argv[a])) {
     149            } else if (!(input_syntax = get_syntax(argv[a]))) {
    142150                return print_usage(argv[0], true);
    143151            }
     
    145153            if (++a == argc) {
    146154                return missing_arg(argv[0], 'o');
    147             } else if (!set_syntax(&output_syntax, argv[a])) {
     155            } else if (!(output_syntax = get_syntax(argv[a]))) {
    148156                return print_usage(argv[0], true);
    149157            }
Note: See TracChangeset for help on using the changeset viewer.