Selaa lähdekoodia

WIP more adapting to ISO C11.

scossu 1 viikko sitten
vanhempi
commit
f2ad09a2a7
7 muutettua tiedostoa jossa 41 lisäystä ja 28 poistoa
  1. 2 1
      README.md
  2. 1 1
      src/codec/codec_ttl.c
  3. 28 19
      src/codec/lexer_nt.re
  4. 6 3
      src/namespace.c
  5. 1 1
      src/store.c
  6. 2 2
      src/term.c
  7. 1 1
      test/test_codec_nt.c

+ 2 - 1
README.md

@@ -62,7 +62,8 @@ of features as a standalone library:
 - ✓ Serialization and de-serialization to/from N-Triples and N-Quads
 - ✓ Serialization and de-serialization to/from Turtle and TriG
 - ✓ Compile-time configuration of max graph size (efficiency vs. capacity)
-- ⚒ Python bindings
+- ⚒ Lua bindings
+- ⛌ Python bindings (moved to separate project and temporarily on hold)
 - ⚒ Basic command line utilities
 - ⚒ Store interface for custom back end implementations
 

+ 1 - 1
src/codec/codec_ttl.c

@@ -134,7 +134,7 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
             buf_len = strlen (tmp) + 3; // Room for "" and terminator
 
             if (term->lang[0] != '\0') {
-                metadata = strndup (term->lang, sizeof (LSUP_LangTag));
+                metadata = str_ndup (term->lang, sizeof (LSUP_LangTag));
                 buf_len += strlen (metadata) + 1; // Room for @
             }
 

+ 28 - 19
src/codec/lexer_nt.re

@@ -10,15 +10,16 @@
 
 
 typedef struct {
-    FILE *          fh;                 ///< Input file handle.
+    FILE           *fh;                 ///< Input file handle.
+    const char     *sh;                 ///< Input string. Exclusive with fh.
     YYCTYPE         buf[CHUNK_SIZE],    ///< Start of buffer.
-            *       lim,                ///< Position after the last available
+                   *lim,                ///< Position after the last available
                                         ///<   input character (YYLIMIT).
-            *       cur,                ///< Next input character to be read
+                   *cur,                ///< Next input character to be read
                                         ///<   (YYCURSOR)
-            *       mar,                ///< Most recent match (YYMARKER)
-            *       tok,                ///< Start of current token.
-            *       bol;                ///< Address of the beginning of the
+                   *mar,                ///< Most recent match (YYMARKER)
+                   *tok,                ///< Start of current token.
+                   *bol;                ///< Address of the beginning of the
                                         ///<   current line (for debugging).
     unsigned        line;               ///< Current line no. (for debugging).
     unsigned        ct;                 ///< Number of parsed triples.
@@ -37,12 +38,14 @@ static int fill(ParseIterator *it)
         return 2;
     }
     LOG_DEBUG("Shifting bytes: %lu", shift);
-    memmove(it->buf, it->tok, it->lim - it->tok);
+    memmove (it->buf, it->tok, it->lim - it->tok);
     it->lim -= shift;
     it->cur -= shift;
     it->mar -= shift;
     it->tok -= shift;
-    it->lim += fread(it->lim, 1, shift, it->fh);
+    if (it->fh) it->lim += fread (it->lim, 1, shift, it->fh);
+    // With a string handle, assume the whole input fits in CHUNK_SIZE.
+    else it->lim = memcpy (it->lim, it->sh, sizeof(it->buf));
     /*!stags:re2c format = "if (it->@@) it->@@ -= shift; "; */
     it->lim[0] = 0;
     it->eof |= it->lim < it->buf + CHUNK_SIZE - 1;
@@ -50,9 +53,19 @@ static int fill(ParseIterator *it)
 }
 
 
-static void parse_init(ParseIterator *it, FILE *fh)
+/** @brief Initialize parser.
+ *
+ * @param[in] it iterator handle to be initialized.
+ *
+ * @param[in] fh Open file handle to read from. This is exclusive with sh. If
+ *  both fh and sh are provided, fh has precedence.
+ *
+ * @param[in] sh String to read from. This is exclusive with fh.
+ */
+static void parse_init(ParseIterator *it, FILE *fh, const char *sh)
 {
     it->fh = fh;
+    it->sh = sh;
     it->cur = it->mar = it->tok = it->lim = it->buf + CHUNK_SIZE - 1;
     it->line = 1;
     it->bol = it->buf;
@@ -64,9 +77,9 @@ static void parse_init(ParseIterator *it, FILE *fh)
 
 
 // Parser interface. Required here to silence linters.
-//void *NTParseAlloc(void *);
-//void NTParse(void *);
-//void NTParseFree(void *);
+void *NTParseAlloc();
+void NTParse();
+void NTParseFree();
 #ifdef DEBUG
 void NTParseTrace();
 #endif
@@ -234,15 +247,11 @@ loop:
 LSUP_rc
 LSUP_nt_parse_term (const char *rep, const LSUP_NSMap *map, LSUP_Term **term)
 {
-    FILE *fh = fmemopen ((void *)rep, strlen (rep), "r");
-
     ParseIterator it;
-    parse_init (&it, fh);
+    parse_init (&it, NULL, rep);
 
     int ttype = lex (&it, term);
 
-    fclose (fh);
-
     switch (ttype) {
         case T_IRIREF:
         case T_LITERAL:
@@ -260,7 +269,7 @@ LSUP_nt_parse_doc (FILE *fh, LSUP_Graph **gr_p, size_t *ct, char **err_p)
     *gr_p = NULL;
 
     ParseIterator parse_it;
-    parse_init (&parse_it, fh);
+    parse_init (&parse_it, fh, NULL);
 
 #ifdef DEBUG
     NTParseTrace (stdout, "NT Parser > ");
@@ -285,7 +294,7 @@ LSUP_nt_parse_doc (FILE *fh, LSUP_Graph **gr_p, size_t *ct, char **err_p)
         int ttype = lex (&parse_it, &term);
 
         if (ttype == -1) {
-            char token[16] = {};
+            char token[16] = {'\0'};
             strncpy (token, (const char *)parse_it.tok, 15);
 
             char *err_start = "Parse error near token `";

+ 6 - 3
src/namespace.c

@@ -73,7 +73,8 @@ LSUP_nsmap_free (NSMap *map)
 LSUP_rc
 LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
 {
-    NSEntry entry_s = {};
+    NSEntry entry_s;
+    memset (&entry_s, 0, sizeof (entry_s));
 
     if (strlen(pfx) >= PFX_LEN)
         log_warn(
@@ -104,7 +105,8 @@ LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
 LSUP_rc
 LSUP_nsmap_remove (NSMap *map, const char *pfx)
 {
-    NSEntry entry_s = {};
+    NSEntry entry_s;
+    memset (&entry_s, 0, sizeof (entry_s));
     strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
     NSEntry *entry = hashmap_delete (map, &entry_s);
 
@@ -119,7 +121,8 @@ LSUP_nsmap_remove (NSMap *map, const char *pfx)
 const char *
 LSUP_nsmap_get_ns (const NSMap *map, const char *pfx)
 {
-    NSEntry entry_s = {};
+    NSEntry entry_s;
+    memset (&entry_s, 0, sizeof (entry_s));
     strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
     NSEntry *entry = hashmap_get ((NSMap *)map, &entry_s);
 

+ 1 - 1
src/store.c

@@ -68,4 +68,4 @@ LSUP_store_commit (LSUP_Store *store, void *txn)
 
 void
 LSUP_store_abort (LSUP_Store *store, void *txn)
-{ return store->sif->txn_abort_fn (txn); }
+{ store->sif->txn_abort_fn (txn); }

+ 2 - 2
src/term.c

@@ -800,7 +800,7 @@ term_init (
             }
 
             // Capture interesting IRI parts.
-            MatchCoord matches[7] = {};  // Initialize all to 0.
+            MatchCoord matches[7] = {0};  // Initialize all to 0.
             if (UNLIKELY (parse_iri (fquri, matches) != LSUP_OK)) {
                 log_error ("Error matching URI pattern.");
 
@@ -940,7 +940,7 @@ static LSUP_rc
 parse_iri (char *iri_str, MatchCoord coord[]) {
     char *cur = iri_str;
     size_t iri_len = strlen (iri_str);
-    MatchCoord tmp = {};  // Temporary storage for capture groups
+    MatchCoord tmp = {0};  // Temporary storage for capture groups
 
     // Redundant if only called by term_init.
     // memset (coord, 0, sizeof(*coord));

+ 1 - 1
test/test_codec_nt.c

@@ -256,7 +256,7 @@ int
 test_decode_nt_bad_graph()
 {
     log_info ("testing illegal NT document.");
-    FILE *input = fmemopen ((void *)bad_nt_doc, strlen (start_nt_doc), "r");
+    FILE *input = fmemopen ((void *)bad_nt_doc, strlen (bad_nt_doc), "r");
 
     LSUP_Graph *gr;
     size_t ct;