Просмотр исходного кода

Fix header refs for codecs; some free safeguards.

scossu 1 неделя назад
Родитель
Сommit
940d4aa136

+ 1 - 1
include/codec/codec_nt.h

@@ -1,7 +1,7 @@
 #ifndef _LSUP_CODEC_NT_H
 #define _LSUP_CODEC_NT_H
 
-#include "codec/parser_nt.h"
+#include "lsup/codec/parser_nt.h"
 
 /** @brief N-Triples codec.
  */

+ 1 - 1
include/codec/codec_ttl.h

@@ -1,7 +1,7 @@
 #ifndef _LSUP_CODEC_TTL_H
 #define _LSUP_CODEC_TTL_H
 
-#include "codec/parser_ttl.h"
+#include "lsup/codec/parser_ttl.h"
 
 /** @brief Turtle codec.
  *

+ 1 - 1
include/codec/parser_nt.h

@@ -1,7 +1,7 @@
 #ifndef _LSUP_NT_PARSER_H
 #define _LSUP_NT_PARSER_H
 
-#include "codec.h"
+#include "lsup/codec.h"
 
 
 /** @brief Parse a single term.

+ 1 - 1
include/codec/parser_ttl.h

@@ -1,7 +1,7 @@
 #ifndef _LSUP_TTL_PARSER_H
 #define _LSUP_TTL_PARSER_H
 
-#include "codec.h"
+#include "lsup/codec.h"
 
 
 /** @brief Parse a N-Triples document from a file handle.

+ 76 - 73
include/core.h

@@ -63,7 +63,7 @@
 
 /* * * RETURN CODES * * */
 
-/**@defgroup rc Return codes
+/**@defgroup rc return codes
  * 0 is success, positive integers (>88800) are warnings, and negative integers
  * (>-88900) are errors.
  *
@@ -228,23 +228,6 @@ typedef LSUP_Key LSUP_QuadKey[4];
 typedef char uuid_str_t[UUIDSTR_SIZE];
 
 
-/** @brief Make recursive directories.
- *
- * Modified from
- * https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
- */
-LSUP_rc
-mkdir_p (const char *path, mode_t mode);
-
-
-/** @brief Remove a directory recursively, as in Unix "rm -r".
- *
- * @param[in] path Path of directory to remove.
- */
-LSUP_rc
-rm_r (const char *path);
-
-
 /** @brief Return an error message for a return code.
  */
 const char *
@@ -263,61 +246,6 @@ LSUP_strerror (LSUP_rc rc);
  */
 #define LSUP_MAX_ERROR      LSUP_ENV_ERR
 
-/** @brief Encode a code point using UTF-8.
- *
- * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
- *
- * @author Ondřej Hruška <ondra@ondrovo.com>
- *
- * @copyright MIT
- *
- * @param out - output buffer (min 5 characters), will be 0-terminated
- * @param utf - code point 0-0x10FFFF
- * @return number of bytes on success, 0 on failure (also produces U+FFFD,
- *  which uses 3 bytes)
- */
-inline int utf8_encode (const uint32_t utf, unsigned char *out)
-{
-  if (utf <= 0x7F) {
-    // Plain ASCII
-    out[0] = (char) utf;
-    out[1] = 0;
-    return 1;
-  }
-  else if (utf <= 0x07FF) {
-    // 2-byte unicode
-    out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
-    out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
-    out[2] = 0;
-    return 2;
-  }
-  else if (utf <= 0xFFFF) {
-    // 3-byte unicode
-    out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
-    out[1] = (char) (((utf >>  6) & 0x3F) | 0x80);
-    out[2] = (char) (((utf >>  0) & 0x3F) | 0x80);
-    out[3] = 0;
-    return 3;
-  }
-  else if (utf <= 0x10FFFF) {
-    // 4-byte unicode
-    out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
-    out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
-    out[2] = (char) (((utf >>  6) & 0x3F) | 0x80);
-    out[3] = (char) (((utf >>  0) & 0x3F) | 0x80);
-    out[4] = 0;
-    return 4;
-  }
-  else {
-    // error - use replacement character
-    out[0] = (char) 0xEF;
-    out[1] = (char) 0xBF;
-    out[2] = (char) 0xBD;
-    out[3] = 0;
-    return 0;
-  }
-}
-
 
 /*
  * Only compile debug code in debug mode. Parsing variables for debug and
@@ -403,6 +331,81 @@ inline int utf8_encode (const uint32_t utf, unsigned char *out)
 #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL)              \
 #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL)              \
 */
+
+
+/** @brief Make recursive directories.
+ *
+ * Modified from
+ * https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
+ */
+LSUP_rc
+mkdir_p (const char *path, mode_t mode);
+
+
+/** @brief Remove a directory recursively, as in Unix "rm -r".
+ *
+ * @param[in] path Path of directory to remove.
+ */
+LSUP_rc
+rm_r (const char *path);
+
+
+/** @brief Encode a code point using UTF-8.
+ *
+ * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
+ *
+ * @author Ondřej Hruška <ondra@ondrovo.com>
+ *
+ * @copyright MIT
+ *
+ * @param out - output buffer (min 5 characters), will be 0-terminated
+ * @param utf - code point 0-0x10FFFF
+ * @return number of bytes on success, 0 on failure (also produces U+FFFD,
+ *  which uses 3 bytes)
+ */
+inline int utf8_encode (const uint32_t utf, unsigned char *out)
+{
+  if (utf <= 0x7F) {
+    // Plain ASCII
+    out[0] = (char) utf;
+    out[1] = 0;
+    return 1;
+  }
+  else if (utf <= 0x07FF) {
+    // 2-byte unicode
+    out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
+    out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
+    out[2] = 0;
+    return 2;
+  }
+  else if (utf <= 0xFFFF) {
+    // 3-byte unicode
+    out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
+    out[1] = (char) (((utf >>  6) & 0x3F) | 0x80);
+    out[2] = (char) (((utf >>  0) & 0x3F) | 0x80);
+    out[3] = 0;
+    return 3;
+  }
+  else if (utf <= 0x10FFFF) {
+    // 4-byte unicode
+    out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
+    out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
+    out[2] = (char) (((utf >>  6) & 0x3F) | 0x80);
+    out[3] = (char) (((utf >>  0) & 0x3F) | 0x80);
+    out[4] = 0;
+    return 4;
+  }
+  else {
+    // error - use replacement character
+    out[0] = (char) 0xEF;
+    out[1] = (char) 0xBF;
+    out[2] = (char) 0xBD;
+    out[3] = 0;
+    return 0;
+  }
+}
+
+
 /// @} END defgroup private
 
 #endif  /* _LSUP_CORE_H */

+ 12 - 3
include/environment.h

@@ -15,13 +15,20 @@
 #include "term.h"
 
 
+/**@defgroup environment Environment module
+ * @ingroup public
+ * @{
+ */
 /*
  * External variables.
  */
 
-extern LSUP_NSMap *LSUP_default_nsm; /// Default namespace prefix map.
-extern LSUP_Term *LSUP_default_ctx; /// Default context.
-extern LSUP_Buffer *LSUP_default_ctx_buf; /// Serialized default context.
+/// Default namespace prefix map.
+extern LSUP_NSMap *LSUP_default_nsm;
+/// Default context.
+extern LSUP_Term *LSUP_default_ctx;
+/// Serialized default context.
+extern LSUP_Buffer *LSUP_default_ctx_buf;
 
 
 /** @brief Initialize the default environment.
@@ -57,4 +64,6 @@ LSUP_env_put_id (const uint32_t key, const char *data);
 const char *
 LSUP_env_get_id (const uint32_t key);
 
+/// @} END defgroup environment
+
 #endif /* _LSUP_ENVIRONMENT_H */

+ 5 - 0
src/graph.c

@@ -618,6 +618,8 @@ LSUP_graph_iter_graph (LSUP_GraphIterator *it)
 void
 LSUP_graph_iter_free (LSUP_GraphIterator *it)
 {
+    if (UNLIKELY (!it)) return;
+
     it->graph->store->sif->lu_free_fn (it->data);
 
     /*
@@ -709,6 +711,8 @@ LSUP_graph_connections (
 
     // Gather all linking terms in a set first.
     LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
+    if (!it) return NULL;
+
     LSUP_TermSet *lts = LSUP_term_set_new();
     while (graph_iter_next_buffer (it) != LSUP_END) {
         LSUP_Term
@@ -722,6 +726,7 @@ LSUP_graph_connections (
     LSUP_graph_iter_free(it);
 
     LSUP_LinkMap *ret = LSUP_link_map_new (t, type);
+    if (!ret) return NULL;
     size_t i = 0;
     LSUP_Term *lt;
     while (LSUP_term_set_next (lts, &i, &lt) != LSUP_END) {

+ 2 - 0
src/store.c

@@ -43,6 +43,8 @@ LSUP_store_new (
 void
 LSUP_store_free (LSUP_Store *store)
 {
+    if (!UNLIKELY (!store)) return;
+
     store->sif->free_fn (store->data);
     if (store->id) free (store->id);
     free (store);

+ 3 - 3
src/store_mdb.c

@@ -737,9 +737,9 @@ mdbstore_lookup (
 
     uint8_t idx0, idx1;
 
-    // Start RO transaction if not in a write txn already.
     if (th) it->txn = th;
-    else {
+    else if (!it->txn) {
+        // Start RO transaction if not in a write txn already.
         it->rc = mdb_txn_begin (it->store->env, NULL, MDB_RDONLY, &it->txn);
         if (it->rc != MDB_SUCCESS) {
             log_error ("Database error in lookup: %s", LSUP_strerror (it->rc));
@@ -863,7 +863,7 @@ mdbiter_next_key (MDBIterator *it)
                 LOG_TRACE("Triple found for context.");
 
             } else if (db_rc == MDB_NOTFOUND) {
-                LOG_TRACE("No triples found for context.");
+                LOG_TRACE("No triples found for context: %x", it->luc);
                 if (it->rc == MDB_NOTFOUND) rc = LSUP_END;
                 else it->iter_op_fn (it);