From 97258f0e85834d71b17e3c1997a5c7dc136e0b98 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 16 Mar 2016 16:21:20 -0400 Subject: Use char* for strings in public API The constant casting just makes user code a mess, for no benefit. --- src/base64.c | 20 ++++++---------- src/byte_sink.h | 19 ++++++++-------- src/byte_source.c | 8 +++---- src/byte_source.h | 10 ++++---- src/env.c | 21 ++++++++--------- src/n3.c | 12 ++++------ src/node.c | 59 +++++++++++++++++++++++------------------------ src/reader.c | 45 ++++++++++++++++++------------------ src/reader.h | 10 ++++---- src/serdi.c | 54 +++++++++++++++++++++---------------------- src/stack.h | 21 ++++++++--------- src/string.c | 32 +++++++++++++------------- src/string_utils.h | 4 ++-- src/uri.c | 60 +++++++++++++++++++++++------------------------- src/uri_utils.h | 17 +++++++------- src/writer.c | 67 +++++++++++++++++++++++++----------------------------- 16 files changed, 217 insertions(+), 242 deletions(-) (limited to 'src') diff --git a/src/base64.c b/src/base64.c index 8569577b..0d79fd88 100644 --- a/src/base64.c +++ b/src/base64.c @@ -73,7 +73,6 @@ serd_base64_encode(uint8_t* const str, const bool wrap_lines) { bool has_newline = false; - for (size_t i = 0, j = 0; i < size; i += 3, j += 4) { uint8_t in[4] = {0, 0, 0, 0}; size_t n_in = MIN(3, size - i); @@ -107,28 +106,23 @@ decode_chunk(const uint8_t in[4], uint8_t out[3]) } void* -serd_base64_decode(const uint8_t* const str, - const size_t len, - size_t* const size) +serd_base64_decode(const char* const str, const size_t len, size_t* const size) { - void* buf = malloc((len * 3) / 4 + 2); + const uint8_t* const ustr = (const uint8_t*)str; - *size = 0; + void* buf = malloc((len * 3) / 4 + 2); + *size = 0; for (size_t i = 0, j = 0; i < len; j += 3) { uint8_t in[] = "===="; size_t n_in = 0; for (; i < len && n_in < 4; ++n_in) { - for (; i < len && !is_base64(str[i]); ++i) { - // Skip junk - } - - in[n_in] = str[i++]; + for (; i < len && !is_base64(ustr[i]); ++i) { + } // Skip junk + in[n_in] = ustr[i++]; } - if (n_in > 1) { *size += decode_chunk(in, (uint8_t*)buf + j); } } - return buf; } diff --git a/src/byte_sink.h b/src/byte_sink.h index dc55ba1e..d5222843 100644 --- a/src/byte_sink.h +++ b/src/byte_sink.h @@ -23,13 +23,12 @@ #include "serd/serd.h" #include -#include #include typedef struct SerdByteSinkImpl { SerdSink sink; void* stream; - uint8_t* buf; + char* buf; size_t size; size_t block_size; } SerdByteSink; @@ -37,12 +36,13 @@ typedef struct SerdByteSinkImpl { static inline SerdByteSink serd_byte_sink_new(SerdSink sink, void* stream, size_t block_size) { - SerdByteSink bsink = {sink, stream, NULL, 0, block_size}; - - if (block_size > 1) { - bsink.buf = (uint8_t*)serd_allocate_buffer(block_size); - } - + SerdByteSink bsink; + bsink.sink = sink; + bsink.stream = stream; + bsink.size = 0; + bsink.block_size = block_size; + bsink.buf = + ((block_size > 1) ? (char*)serd_allocate_buffer(block_size) : NULL); return bsink; } @@ -82,7 +82,7 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) // Write as much as possible into the remaining buffer space memcpy(bsink->buf + bsink->size, buf, n); bsink->size += n; - buf = (const uint8_t*)buf + n; + buf = (const char*)buf + n; len -= n; // Flush page if buffer is full @@ -91,7 +91,6 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) bsink->size = 0; } } - return orig_len; } diff --git a/src/byte_source.c b/src/byte_source.c index 4790a4b2..dac5d049 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -51,7 +51,7 @@ serd_byte_source_open_source(SerdByteSource* const source, const SerdSource read_func, const SerdStreamErrorFunc error_func, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { const Cursor cur = {name, 1, 1}; @@ -91,13 +91,13 @@ serd_byte_source_prepare(SerdByteSource* const source) SerdStatus serd_byte_source_open_string(SerdByteSource* const source, - const uint8_t* const utf8) + const char* const utf8) { - const Cursor cur = {(const uint8_t*)"(string)", 1, 1}; + const Cursor cur = {"(string)", 1, 1}; memset(source, '\0', sizeof(*source)); source->cur = cur; - source->read_buf = utf8; + source->read_buf = (const uint8_t*)utf8; return SERD_SUCCESS; } diff --git a/src/byte_source.h b/src/byte_source.h index 48794a37..7a42feaa 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -26,9 +26,9 @@ #include typedef struct { - const uint8_t* filename; - unsigned line; - unsigned col; + const char* filename; + unsigned line; + unsigned col; } Cursor; typedef struct { @@ -51,14 +51,14 @@ SerdStatus serd_byte_source_open_file(SerdByteSource* source, FILE* file, bool bulk); SerdStatus -serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8); +serd_byte_source_open_string(SerdByteSource* source, const char* utf8); SerdStatus serd_byte_source_open_source(SerdByteSource* source, SerdSource read_func, SerdStreamErrorFunc error_func, void* stream, - const uint8_t* name, + const char* name, size_t page_size); SerdStatus diff --git a/src/env.c b/src/env.c index a9ae6af2..9df2c41e 100644 --- a/src/env.c +++ b/src/env.c @@ -17,7 +17,6 @@ #include "serd/serd.h" #include -#include #include #include #include @@ -101,7 +100,7 @@ serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const uri) SERD_PURE_FUNC static SerdPrefix* serd_env_find(const SerdEnv* const env, - const uint8_t* const name, + const char* const name, const size_t name_len) { for (size_t i = 0; i < env->n_prefixes; ++i) { @@ -163,9 +162,9 @@ serd_env_set_prefix(SerdEnv* const env, } SerdStatus -serd_env_set_prefix_from_strings(SerdEnv* const env, - const uint8_t* const name, - const uint8_t* const uri) +serd_env_set_prefix_from_strings(SerdEnv* const env, + const char* const name, + const char* const uri) { const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); @@ -186,9 +185,7 @@ serd_env_qualify(const SerdEnv* const env, for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_uri = &env->prefixes[i].uri; if (uri->n_bytes >= prefix_uri->n_bytes) { - if (!strncmp((const char*)uri->buf, - (const char*)prefix_uri->buf, - prefix_uri->n_bytes)) { + if (!strncmp(uri->buf, prefix_uri->buf, prefix_uri->n_bytes)) { *prefix = env->prefixes[i].name; suffix->buf = uri->buf + prefix_uri->n_bytes; suffix->len = uri->n_bytes - prefix_uri->n_bytes; @@ -209,8 +206,8 @@ serd_env_expand(const SerdEnv* const env, return SERD_ERR_BAD_CURIE; } - const uint8_t* const colon = - (const uint8_t*)memchr(curie->buf, ':', curie->n_bytes + 1); + const char* const colon = + (const char*)memchr(curie->buf, ':', curie->n_bytes + 1); if (curie->type != SERD_CURIE || !colon) { return SERD_ERR_BAD_ARG; } @@ -249,9 +246,9 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) return SERD_NODE_NULL; } const size_t len = prefix.len + suffix.len; - uint8_t* buf = (uint8_t*)malloc(len + 1); + char* buf = (char*)malloc(len + 1); SerdNode ret = {buf, len, 0, SERD_URI}; - snprintf((char*)buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf); + snprintf(buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf); return ret; } case SERD_BLANK: diff --git a/src/n3.c b/src/n3.c index 101768bc..23180204 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1010,11 +1010,10 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, eat_byte_safe(reader, '_'); eat_byte_check(reader, ':'); - const Ref ref = *dest = - push_node(reader, - SERD_BLANK, - reader->bprefix ? (char*)reader->bprefix : "", - reader->bprefix_len); + const Ref ref = *dest = push_node(reader, + SERD_BLANK, + reader->bprefix ? reader->bprefix : "", + reader->bprefix_len); int c = peek_byte(reader); // First: (PN_CHARS | '_' | [0-9]) if (is_digit(c) || c == '_') { @@ -1053,7 +1052,6 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, } } } - return SERD_SUCCESS; } @@ -1579,7 +1577,7 @@ tokcmp(SerdReader* const reader, return -1; } - return serd_strncasecmp((const char*)node->buf, tok, n); + return serd_strncasecmp(node->buf, tok, n); } SerdStatus diff --git a/src/node.c b/src/node.c index c8fba903..229f7902 100644 --- a/src/node.c +++ b/src/node.c @@ -60,14 +60,14 @@ serd_uri_string_length(const SerdURI* const uri) static size_t string_sink(const void* const buf, const size_t len, void* const stream) { - uint8_t** ptr = (uint8_t**)stream; + char** ptr = (char**)stream; memcpy(*ptr, buf, len); *ptr += len; return len; } SerdNode -serd_node_from_string(const SerdType type, const uint8_t* const str) +serd_node_from_string(SerdType type, const char* const str) { if (!str) { return SERD_NODE_NULL; @@ -80,9 +80,9 @@ serd_node_from_string(const SerdType type, const uint8_t* const str) } SerdNode -serd_node_from_substring(const SerdType type, - const uint8_t* const str, - const size_t len) +serd_node_from_substring(const SerdType type, + const char* const str, + const size_t len) { if (!str) { return SERD_NODE_NULL; @@ -102,7 +102,7 @@ serd_node_copy(const SerdNode* const node) } SerdNode copy = *node; - uint8_t* buf = (uint8_t*)malloc(copy.n_bytes + 1); + char* buf = (char*)malloc(copy.n_bytes + 1); memcpy(buf, node->buf, copy.n_bytes + 1); copy.buf = buf; return copy; @@ -113,8 +113,7 @@ serd_node_equals(const SerdNode* const a, const SerdNode* const b) { return (a == b) || (a->type == b->type && a->n_bytes == b->n_bytes && - ((a->buf == b->buf) || - !memcmp((const char*)a->buf, (const char*)b->buf, a->n_bytes + 1))); + ((a->buf == b->buf) || !memcmp(a->buf, b->buf, a->n_bytes + 1))); } SerdNode @@ -128,7 +127,7 @@ serd_node_new_uri_from_node(const SerdNode* const uri_node, } SerdNode -serd_node_new_uri_from_string(const uint8_t* const str, +serd_node_new_uri_from_string(const char* const str, const SerdURI* const base, SerdURI* const out) { @@ -143,7 +142,7 @@ serd_node_new_uri_from_string(const uint8_t* const str, } static bool -is_uri_path_char(const uint8_t c) +is_uri_path_char(const char c) { if (is_alpha(c) || is_digit(c)) { return true; @@ -179,28 +178,28 @@ is_uri_path_char(const uint8_t c) } SerdNode -serd_node_new_file_uri(const uint8_t* const path, - const uint8_t* const hostname, - SerdURI* const out) +serd_node_new_file_uri(const char* const path, + const char* const hostname, + SerdURI* const out) { - const size_t path_len = strlen((const char*)path); - const size_t hostname_len = hostname ? strlen((const char*)hostname) : 0; + const size_t path_len = strlen(path); + const size_t hostname_len = hostname ? strlen(hostname) : 0; const bool is_windows = is_windows_path(path); size_t uri_len = 0; - uint8_t* uri = NULL; + char* uri = NULL; if (path[0] == '/' || is_windows) { uri_len = strlen("file://") + hostname_len + is_windows; - uri = (uint8_t*)calloc(uri_len + 1, 1); + uri = (char*)calloc(uri_len + 1, 1); memcpy(uri, "file://", 7); if (hostname) { - memcpy(uri + 7, hostname, hostname_len); + memcpy(uri + 7, hostname, hostname_len + 1); } if (is_windows) { - ((char*)uri)[7 + hostname_len] = '/'; + uri[7 + hostname_len] = '/'; } } @@ -213,7 +212,7 @@ serd_node_new_file_uri(const uint8_t* const path, } else if (is_uri_path_char(path[i])) { serd_buffer_sink(path + i, 1, &buffer); } else { - char escape_str[4] = {'%', 0, 0, 0}; + char escape_str[10] = {'%', 0, 0, 0, 0, 0, 0, 0, 0, 0}; snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path[i]); serd_buffer_sink(escape_str, 3, &buffer); } @@ -221,11 +220,11 @@ serd_node_new_file_uri(const uint8_t* const path, serd_buffer_sink_finish(&buffer); if (out) { - serd_uri_parse((const uint8_t*)buffer.buf, out); + serd_uri_parse((const char*)buffer.buf, out); } return serd_node_from_substring( - SERD_URI, (const uint8_t*)buffer.buf, buffer.len); + SERD_URI, (const char*)buffer.buf, buffer.len); } SerdNode @@ -239,9 +238,9 @@ serd_node_new_uri(const SerdURI* const uri, } const size_t len = serd_uri_string_length(&abs_uri); - uint8_t* buf = (uint8_t*)malloc(len + 1); + char* buf = (char*)malloc(len + 1); SerdNode node = {buf, len, 0, SERD_URI}; - uint8_t* ptr = buf; + char* ptr = buf; const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr); buf[actual_len] = '\0'; @@ -262,9 +261,9 @@ serd_node_new_relative_uri(const SerdURI* const uri, { const size_t uri_len = serd_uri_string_length(uri); const size_t base_len = serd_uri_string_length(base); - uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); + char* buf = (char*)malloc(uri_len + base_len + 1); SerdNode node = {buf, 0, 0, SERD_URI}; - uint8_t* ptr = buf; + char* ptr = buf; const size_t actual_len = serd_uri_serialise_relative(uri, base, root, string_sink, &ptr); @@ -295,7 +294,7 @@ serd_node_new_decimal(const double d, const unsigned frac_digits) const double abs_d = fabs(d); const unsigned int_digits = serd_digits(abs_d); char* buf = (char*)calloc(int_digits + frac_digits + 3, 1); - SerdNode node = {(const uint8_t*)buf, 0, 0, SERD_LITERAL}; + SerdNode node = {buf, 0, 0, SERD_LITERAL}; const double int_part = floor(abs_d); // Point s to decimal point location @@ -346,7 +345,7 @@ serd_node_new_integer(const int64_t i) uint64_t abs_i = (i < 0) ? -i : i; const unsigned digits = serd_digits((double)abs_i); char* buf = (char*)calloc(digits + 2, 1); - SerdNode node = {(const uint8_t*)buf, 0, 0, SERD_LITERAL}; + SerdNode node = {(const char*)buf, 0, 0, SERD_LITERAL}; // Point s to the end char* s = buf + digits - 1; @@ -372,7 +371,7 @@ serd_node_new_blob(const void* const buf, { const size_t len = serd_base64_get_length(size, wrap_lines); uint8_t* str = (uint8_t*)calloc(len + 2, 1); - SerdNode node = {str, len, 0, SERD_LITERAL}; + SerdNode node = {(const char*)str, len, 0, SERD_LITERAL}; if (serd_base64_encode(str, buf, size, wrap_lines)) { node.flags |= SERD_HAS_NEWLINE; @@ -384,7 +383,7 @@ void serd_node_free(SerdNode* const node) { if (node && node->buf) { - free((uint8_t*)node->buf); + free((char*)node->buf); node->buf = NULL; } } diff --git a/src/reader.c b/src/reader.c index 8d4ac604..92281149 100644 --- a/src/reader.c +++ b/src/reader.c @@ -99,15 +99,15 @@ push_node_padded(SerdReader* const reader, node->type = type; node->buf = NULL; - uint8_t* buf = (uint8_t*)(node + 1); + char* buf = (char*)(node + 1); memcpy(buf, str, n_bytes + 1); #ifdef SERD_STACK_CHECK reader->allocs = (Ref*)realloc(reader->allocs, sizeof(reader->allocs) * (++reader->n_allocs)); - reader->allocs[reader->n_allocs - 1] = ((uint8_t*)mem - reader->stack.buf); + reader->allocs[reader->n_allocs - 1] = ((char*)mem - reader->stack.buf); #endif - return (Ref)((uint8_t*)node - reader->stack.buf); + return (Ref)((char*)node - reader->stack.buf); } Ref @@ -124,7 +124,7 @@ deref(SerdReader* const reader, const Ref ref) { if (ref) { SerdNode* node = (SerdNode*)(reader->stack.buf + ref); - node->buf = (uint8_t*)node + sizeof(SerdNode); + node->buf = (char*)node + sizeof(SerdNode); return node; } return NULL; @@ -140,8 +140,8 @@ pop_node(SerdReader* const reader, const Ref ref) --reader->n_allocs; #endif SerdNode* const node = deref(reader, ref); - uint8_t* const top = reader->stack.buf + reader->stack.size; - serd_stack_pop_aligned(&reader->stack, (size_t)(top - (uint8_t*)node)); + char* const top = reader->stack.buf + reader->stack.size; + serd_stack_pop_aligned(&reader->stack, (size_t)(top - (char*)node)); } return 0; } @@ -260,17 +260,16 @@ serd_reader_get_handle(const SerdReader* const reader) } void -serd_reader_add_blank_prefix(SerdReader* const reader, - const uint8_t* const prefix) +serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix) { free(reader->bprefix); reader->bprefix_len = 0; reader->bprefix = NULL; - const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + const size_t prefix_len = prefix ? strlen(prefix) : 0; if (prefix_len) { reader->bprefix_len = prefix_len; - reader->bprefix = (uint8_t*)malloc(reader->bprefix_len + 1); + reader->bprefix = (char*)malloc(reader->bprefix_len + 1); memcpy(reader->bprefix, prefix, reader->bprefix_len + 1); } } @@ -284,14 +283,14 @@ serd_reader_set_default_graph(SerdReader* const reader, } SerdStatus -serd_reader_read_file(SerdReader* const reader, const uint8_t* const uri) +serd_reader_read_file(SerdReader* const reader, const char* const uri) { - uint8_t* const path = serd_file_uri_parse(uri, NULL); + char* const path = serd_file_uri_parse(uri, NULL); if (!path) { return SERD_ERR_BAD_ARG; } - FILE* fd = serd_fopen((const char*)path, "rb"); + FILE* fd = serd_fopen(path, "rb"); if (!fd) { serd_free(path); return SERD_ERR_UNKNOWN; @@ -321,10 +320,10 @@ skip_bom(SerdReader* const me) } SerdStatus -serd_reader_start_stream(SerdReader* const reader, - FILE* const file, - const uint8_t* const name, - const bool bulk) +serd_reader_start_stream(SerdReader* const reader, + FILE* const file, + const char* const name, + const bool bulk) { return serd_reader_start_source_stream(reader, bulk ? (SerdSource)fread @@ -340,7 +339,7 @@ serd_reader_start_source_stream(SerdReader* const reader, const SerdSource read_func, const SerdStreamErrorFunc error_func, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { return serd_byte_source_open_source( @@ -386,9 +385,9 @@ serd_reader_end_stream(SerdReader* const reader) } SerdStatus -serd_reader_read_file_handle(SerdReader* const reader, - FILE* const file, - const uint8_t* const name) +serd_reader_read_file_handle(SerdReader* const reader, + FILE* const file, + const char* const name) { return serd_reader_read_source(reader, (SerdSource)fread, @@ -403,7 +402,7 @@ serd_reader_read_source(SerdReader* const reader, const SerdSource source, const SerdStreamErrorFunc error, void* const stream, - const uint8_t* const name, + const char* const name, const size_t page_size) { SerdStatus st = serd_reader_start_source_stream( @@ -423,7 +422,7 @@ serd_reader_read_source(SerdReader* const reader, } SerdStatus -serd_reader_read_string(SerdReader* const reader, const uint8_t* const utf8) +serd_reader_read_string(SerdReader* const reader, const char* const utf8) { serd_byte_source_open_string(&reader->source, utf8); diff --git a/src/reader.h b/src/reader.h index fb638a0b..dec01f3b 100644 --- a/src/reader.h +++ b/src/reader.h @@ -73,7 +73,7 @@ struct SerdReaderImpl { SerdSyntax syntax; unsigned next_id; uint8_t* buf; - uint8_t* bprefix; + char* bprefix; size_t bprefix_len; bool strict; ///< True iff strict parsing bool seen_genid; @@ -172,11 +172,13 @@ push_byte(SerdReader* reader, Ref ref, const int c) assert(c != EOF); SERD_STACK_ASSERT_TOP(reader, ref); - uint8_t* const s = (uint8_t*)serd_stack_push(&reader->stack, 1); + char* const s = (char*)serd_stack_push(&reader->stack, 1); SerdNode* const node = (SerdNode*)(reader->stack.buf + ref); + + *(uint8_t*)(s - 1) = (uint8_t)c; + *s = '\0'; + ++node->n_bytes; - *(s - 1) = (uint8_t)c; - *s = '\0'; return SERD_SUCCESS; } diff --git a/src/serdi.c b/src/serdi.c index c594a30a..de965005 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -33,7 +33,6 @@ #include #include -#include #include #include #include @@ -186,24 +185,24 @@ main(int argc, char** argv) return print_usage(prog, true); } - FILE* in_fd = NULL; - SerdSyntax input_syntax = (SerdSyntax)0; - SerdSyntax output_syntax = (SerdSyntax)0; - bool from_file = true; - bool ascii = false; - bool bulk_read = true; - bool bulk_write = false; - bool full_uris = false; - bool lax = false; - bool quiet = false; - const uint8_t* in_name = NULL; - const uint8_t* add_prefix = NULL; - const uint8_t* chop_prefix = NULL; - const uint8_t* root_uri = NULL; - int a = 1; + FILE* in_fd = NULL; + SerdSyntax input_syntax = (SerdSyntax)0; + SerdSyntax output_syntax = (SerdSyntax)0; + bool from_file = true; + bool ascii = false; + bool bulk_read = true; + bool bulk_write = false; + bool full_uris = false; + bool lax = false; + bool quiet = false; + const char* in_name = NULL; + const char* add_prefix = NULL; + const char* chop_prefix = NULL; + const char* root_uri = NULL; + int a = 1; for (; a < argc && from_file && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { - in_name = (const uint8_t*)"(stdin)"; + in_name = (const char*)"(stdin)"; in_fd = stdin; break; } @@ -228,7 +227,7 @@ main(int argc, char** argv) } else if (opt == 'v') { return print_version(); } else if (opt == 's') { - in_name = (const uint8_t*)"(string)"; + in_name = "(string)"; from_file = false; break; } else if (opt == 'c') { @@ -236,7 +235,7 @@ main(int argc, char** argv) return missing_arg(prog, 'c'); } - chop_prefix = (const uint8_t*)argv[a]; + chop_prefix = argv[a]; break; } else if (opt == 'i') { if (argv[a][o + 1] || ++a == argc) { @@ -261,14 +260,14 @@ main(int argc, char** argv) return missing_arg(prog, 'p'); } - add_prefix = (const uint8_t*)argv[a]; + add_prefix = argv[a]; break; } else if (opt == 'r') { if (argv[a][o + 1] || ++a == argc) { return missing_arg(prog, 'r'); } - root_uri = (const uint8_t*)argv[a]; + root_uri = argv[a]; break; } else { SERDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1); @@ -287,22 +286,22 @@ main(int argc, char** argv) _setmode(_fileno(stdout), _O_BINARY); #endif - uint8_t* input_path = NULL; - const uint8_t* input = (const uint8_t*)argv[a++]; + char* input_path = NULL; + const char* input = (const char*)argv[a++]; if (from_file) { in_name = in_name ? in_name : input; if (!in_fd) { - if (!strncmp((const char*)input, "file:", 5)) { + if (!strncmp(input, "file:", 5)) { input_path = serd_file_uri_parse(input, NULL); input = input_path; } - if (!input || !(in_fd = serd_fopen((const char*)input, "rb"))) { + if (!input || !(in_fd = serd_fopen(input, "rb"))) { return 1; } } } - if (!input_syntax && !(input_syntax = guess_syntax((const char*)in_name))) { + if (!input_syntax && !(input_syntax = guess_syntax(in_name))) { input_syntax = SERD_TRIG; } @@ -319,8 +318,7 @@ main(int argc, char** argv) SerdURI base_uri = SERD_URI_NULL; SerdNode base = SERD_NODE_NULL; if (a < argc) { // Base URI given on command line - base = - serd_node_new_uri_from_string((const uint8_t*)argv[a], NULL, &base_uri); + base = serd_node_new_uri_from_string((const char*)argv[a], NULL, &base_uri); } else if (from_file && in_fd != stdin) { // Use input file URI base = serd_node_new_file_uri(input, NULL, &base_uri); } diff --git a/src/stack.h b/src/stack.h index ac30658c..b49d645a 100644 --- a/src/stack.h +++ b/src/stack.h @@ -28,9 +28,9 @@ /** A dynamic stack in memory. */ typedef struct { - uint8_t* buf; ///< Stack memory - size_t buf_size; ///< Allocated size of buf (>= size) - size_t size; ///< Conceptual size of stack in buf + char* buf; ///< Stack memory + size_t buf_size; ///< Allocated size of buf (>= size) + size_t size; ///< Conceptual size of stack in buf } SerdStack; /** An offset to start the stack at. Note 0 is reserved for NULL. */ @@ -40,7 +40,7 @@ static inline SerdStack serd_stack_new(size_t size) { SerdStack stack; - stack.buf = (uint8_t*)calloc(size, 1); + stack.buf = (char*)calloc(size, 1); stack.buf_size = size; stack.size = SERD_STACK_BOTTOM; return stack; @@ -67,12 +67,10 @@ serd_stack_push(SerdStack* stack, size_t n_bytes) const size_t new_size = stack->size + n_bytes; if (stack->buf_size < new_size) { stack->buf_size += (stack->buf_size >> 1); // *= 1.5 - stack->buf = (uint8_t*)realloc(stack->buf, stack->buf_size); + stack->buf = (char*)realloc(stack->buf, stack->buf_size); } - - uint8_t* const ret = (stack->buf + stack->size); - - stack->size = new_size; + char* const ret = (stack->buf + stack->size); + stack->size = new_size; return ret; } @@ -96,8 +94,7 @@ serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) } // Set top of stack to pad count so we can properly pop later - assert(pad < UINT8_MAX); - stack->buf[stack->size - 1] = (uint8_t)pad; + stack->buf[stack->size - 1] = (char)pad; // Push requested space at aligned location return serd_stack_push(stack, n_bytes); @@ -110,7 +107,7 @@ serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes) serd_stack_pop(stack, n_bytes); // Get amount of padding from top of stack - const uint8_t pad = stack->buf[stack->size - 1]; + const uint8_t pad = (uint8_t)stack->buf[stack->size - 1]; // Pop padding and pad count serd_stack_pop(stack, pad + 1u); diff --git a/src/string.c b/src/string.c index c9f1f612..f3c0e50a 100644 --- a/src/string.c +++ b/src/string.c @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -30,34 +29,35 @@ serd_free(void* const ptr) free(ptr); } -const uint8_t* +const char* serd_strerror(const SerdStatus status) { switch (status) { case SERD_SUCCESS: - return (const uint8_t*)"Success"; + return "Success"; case SERD_FAILURE: - return (const uint8_t*)"Non-fatal failure"; + return "Non-fatal failure"; case SERD_ERR_UNKNOWN: - return (const uint8_t*)"Unknown error"; + break; case SERD_ERR_BAD_SYNTAX: - return (const uint8_t*)"Invalid syntax"; + return "Invalid syntax"; case SERD_ERR_BAD_ARG: - return (const uint8_t*)"Invalid argument"; + return "Invalid argument"; case SERD_ERR_NOT_FOUND: - return (const uint8_t*)"Not found"; + return "Not found"; case SERD_ERR_ID_CLASH: - return (const uint8_t*)"Blank node ID clash"; + return "Blank node ID clash"; case SERD_ERR_BAD_CURIE: - return (const uint8_t*)"Invalid CURIE"; + return "Invalid CURIE"; case SERD_ERR_INTERNAL: - return (const uint8_t*)"Internal error"; + return "Internal error"; } - return (const uint8_t*)"Unknown error"; // never reached + + return "Unknown error"; } static void -serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) +serd_update_flags(const char c, SerdNodeFlags* const flags) { switch (c) { case '\r': @@ -73,7 +73,7 @@ serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) } size_t -serd_substrlen(const uint8_t* const str, +serd_substrlen(const char* const str, const size_t len, SerdNodeFlags* const flags) { @@ -89,7 +89,7 @@ serd_substrlen(const uint8_t* const str, } size_t -serd_strlen(const uint8_t* const str, SerdNodeFlags* const flags) +serd_strlen(const char* const str, SerdNodeFlags* const flags) { if (flags) { size_t i = 0; @@ -100,7 +100,7 @@ serd_strlen(const uint8_t* const str, SerdNodeFlags* const flags) return i; } - return strlen((const char*)str); + return strlen(str); } static double diff --git a/src/string_utils.h b/src/string_utils.h index b9d8ebff..0e9eee43 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -90,14 +90,14 @@ is_base64(const int c) } static inline bool -is_windows_path(const uint8_t* path) +is_windows_path(const char* path) { return is_alpha(path[0]) && (path[1] == ':' || path[1] == '|') && (path[2] == '/' || path[2] == '\\'); } size_t -serd_substrlen(const uint8_t* str, size_t len, SerdNodeFlags* flags); +serd_substrlen(const char* str, size_t len, SerdNodeFlags* flags); static inline char serd_to_upper(const char c) diff --git a/src/uri.c b/src/uri.c index 41e406d9..a7c7a212 100644 --- a/src/uri.c +++ b/src/uri.c @@ -25,25 +25,25 @@ #include #include -uint8_t* -serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) +char* +serd_file_uri_parse(const char* const uri, char** const hostname) { - const uint8_t* path = uri; + const char* path = uri; if (hostname) { *hostname = NULL; } - - if (!strncmp((const char*)uri, "file://", 7)) { - const uint8_t* auth = uri + 7; + if (!strncmp(uri, "file://", 7)) { + const char* auth = uri + 7; if (*auth == '/') { // No hostname path = auth; } else { // Has hostname - if (!(path = (const uint8_t*)strchr((const char*)auth, '/'))) { + if (!(path = strchr(auth, '/'))) { return NULL; } if (hostname) { - *hostname = (uint8_t*)calloc((size_t)(path - auth + 1), 1); - memcpy(*hostname, auth, (size_t)(path - auth)); + const size_t len = (size_t)(path - auth); + *hostname = (char*)calloc(len + 1, 1); + memcpy(*hostname, auth, len); } } } @@ -53,14 +53,14 @@ serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) } SerdBuffer buffer = {NULL, 0}; - for (const uint8_t* s = path; *s; ++s) { + for (const char* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { serd_buffer_sink("%", 1, &buffer); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { - const uint8_t code[3] = {*(s + 1), *(s + 2), 0}; - const uint8_t c = (uint8_t)strtoul((const char*)code, NULL, 16); + const char code[3] = {*(s + 1), *(s + 2), 0}; + const char c = (char)strtoul(code, NULL, 16); serd_buffer_sink(&c, 1, &buffer); s += 2; } else { @@ -74,14 +74,14 @@ serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) } bool -serd_uri_string_has_scheme(const uint8_t* utf8) +serd_uri_string_has_scheme(const char* utf8) { // RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) if (!utf8 || !is_alpha(utf8[0])) { return false; // Invalid scheme initial character, URI is relative } - for (uint8_t c = 0; (c = *++utf8) != '\0';) { + for (char c = 0; (c = *++utf8) != '\0';) { if (!is_uri_scheme_char(c)) { return false; } @@ -95,11 +95,11 @@ serd_uri_string_has_scheme(const uint8_t* utf8) } SerdStatus -serd_uri_parse(const uint8_t* const utf8, SerdURI* const out) +serd_uri_parse(const char* const utf8, SerdURI* const out) { *out = SERD_URI_NULL; - const uint8_t* ptr = utf8; + const char* ptr = utf8; /* See http://tools.ietf.org/html/rfc3986#section-3 URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] @@ -107,7 +107,7 @@ serd_uri_parse(const uint8_t* const utf8, SerdURI* const out) /* S3.1: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */ if (is_alpha(*ptr)) { - for (uint8_t c = *++ptr; true; c = *++ptr) { + for (char c = *++ptr; true; c = *++ptr) { switch (c) { case '\0': case '/': @@ -139,7 +139,7 @@ maybe_authority: if (*ptr == '/' && *(ptr + 1) == '/') { ptr += 2; out->authority.buf = ptr; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { switch (c) { case '/': goto path; @@ -169,7 +169,7 @@ path: } out->path.buf = ptr; out->path.len = 0; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { switch (c) { case '?': goto query; @@ -187,7 +187,7 @@ path: query: if (*ptr == '?') { out->query.buf = ++ptr; - for (uint8_t c = 0; (c = *ptr) != '\0'; ++ptr) { + for (char c = 0; (c = *ptr) != '\0'; ++ptr) { if (c == '#') { goto fragment; } @@ -217,13 +217,11 @@ end: @param up Set to the number of up-references (e.g. "../") trimmed @return A pointer to the new start of `path` */ -static const uint8_t* -remove_dot_segments(const uint8_t* const path, - const size_t len, - size_t* const up) +static const char* +remove_dot_segments(const char* const path, const size_t len, size_t* const up) { - const uint8_t* begin = path; - const uint8_t* const end = path + len; + const char* begin = path; + const char* const end = path + len; *up = 0; while (begin < end) { @@ -253,7 +251,6 @@ remove_dot_segments(const uint8_t* const path, return begin; } break; - case '/': switch (begin[1]) { case '.': @@ -273,7 +270,6 @@ remove_dot_segments(const uint8_t* const path, } } return begin; - default: return begin; // Finished chopping dot components } @@ -286,13 +282,13 @@ remove_dot_segments(const uint8_t* const path, static void merge(SerdChunk* const base, SerdChunk* const path) { - size_t up = 0; - const uint8_t* begin = remove_dot_segments(path->buf, path->len, &up); - const uint8_t* end = path->buf + path->len; + size_t up = 0; + const char* begin = remove_dot_segments(path->buf, path->len, &up); + const char* end = path->buf + path->len; if (base->len) { // Find the up'th last slash - const uint8_t* base_last = (base->buf + base->len - 1); + const char* base_last = (base->buf + base->len - 1); ++up; do { if (*base_last == '/') { diff --git a/src/uri_utils.h b/src/uri_utils.h index 2dd4924f..2544eea2 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -22,14 +22,12 @@ #include "string_utils.h" #include -#include #include static inline bool chunk_equals(const SerdChunk* a, const SerdChunk* b) { - return a->len == b->len && - !strncmp((const char*)a->buf, (const char*)b->buf, a->len); + return a->len == b->len && !strncmp(a->buf, b->buf, a->len); } static inline size_t @@ -38,11 +36,14 @@ uri_path_len(const SerdURI* uri) return uri->path_base.len + uri->path.len; } -static inline uint8_t +static inline char uri_path_at(const SerdURI* uri, size_t i) { - return (i < uri->path_base.len) ? uri->path_base.buf[i] - : uri->path.buf[i - uri->path_base.len]; + if (i < uri->path_base.len) { + return uri->path_base.buf[i]; + } + + return uri->path.buf[i - uri->path_base.len]; } /** @@ -63,8 +64,8 @@ uri_rooted_index(const SerdURI* uri, const SerdURI* root) const size_t root_len = uri_path_len(root); size_t last_root_slash = 0; for (size_t i = 0; i < path_len && i < root_len; ++i) { - const uint8_t u = uri_path_at(uri, i); - const uint8_t r = uri_path_at(root, i); + const char u = uri_path_at(uri, i); + const char r = uri_path_at(root, i); differ = differ || u != r; if (r == '/') { diff --git a/src/writer.c b/src/writer.c index 4b4d480b..84c704ee 100644 --- a/src/writer.c +++ b/src/writer.c @@ -106,7 +106,7 @@ struct SerdWriterImpl { SerdNode list_subj; unsigned list_depth; unsigned indent; - uint8_t* bprefix; + char* bprefix; size_t bprefix_len; Sep last_sep; bool empty; @@ -145,7 +145,7 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) va_list args; va_start(args, fmt); - const SerdError e = {st, (const uint8_t*)"", 0, 0, fmt, &args}; + const SerdError e = {st, "", 0, 0, fmt, &args}; serd_error(writer->error_sink, writer->error_handle, &e); va_end(args); } @@ -162,7 +162,7 @@ static void copy_node(SerdNode* dst, const SerdNode* src) { if (src) { - dst->buf = (uint8_t*)realloc((char*)dst->buf, src->n_bytes + 1); + dst->buf = (char*)realloc((char*)dst->buf, src->n_bytes + 1); dst->n_bytes = src->n_bytes; dst->flags = src->flags; dst->type = src->type; @@ -211,7 +211,7 @@ write_character(SerdWriter* writer, const uint8_t* utf8, size_t* size) } static bool -uri_must_escape(const uint8_t c) +uri_must_escape(const int c) { switch (c) { case ' ': @@ -231,7 +231,7 @@ uri_must_escape(const uint8_t c) } static size_t -write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +write_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) { size_t len = 0; for (size_t i = 0; i < n_bytes;) { @@ -250,23 +250,22 @@ write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) // Write UTF-8 character size_t size = 0; - len += write_character(writer, utf8 + i, &size); + len += write_character(writer, (const uint8_t*)utf8 + i, &size); i += size; if (size == 0) { // Corrupt input, write percent-encoded bytes and scan to next start char escape[4] = {0, 0, 0, 0}; for (; i < n_bytes && (utf8[i] & 0x80); ++i) { - snprintf(escape, sizeof(escape), "%%%02X", utf8[i]); + snprintf(escape, sizeof(escape), "%%%02X", (uint8_t)utf8[i]); len += sink(escape, 3, writer); } } } - return len; } static bool -lname_must_escape(const uint8_t c) +lname_must_escape(const char c) { /* This arbitrary list of characters, most of which have nothing to do with Turtle, must be handled as special cases here because the RDF and SPARQL @@ -303,7 +302,7 @@ lname_must_escape(const uint8_t c) } static size_t -write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +write_lname(SerdWriter* writer, const char* utf8, size_t n_bytes) { size_t len = 0; for (size_t i = 0; i < n_bytes; ++i) { @@ -329,10 +328,10 @@ write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) } static size_t -write_text(SerdWriter* writer, - TextContext ctx, - const uint8_t* utf8, - size_t n_bytes) +write_text(SerdWriter* writer, + TextContext ctx, + const char* utf8, + size_t n_bytes) { size_t len = 0; size_t n_consecutive_quotes = 0; @@ -355,7 +354,7 @@ write_text(SerdWriter* writer, break; // Reached end } - const uint8_t in = utf8[i++]; + const char in = utf8[i++]; if (ctx == WRITE_LONG_STRING) { n_consecutive_quotes = (in == '\"') ? (n_consecutive_quotes + 1) : 0; @@ -419,7 +418,8 @@ write_text(SerdWriter* writer, // Write UTF-8 character size_t size = 0; - len += write_character(writer, utf8 + i - 1, &size); + len += write_character(writer, (const uint8_t*)utf8 + i - 1, &size); + if (size == 0) { // Corrupt input, write replacement character and scan to the next start len += sink(replacement_char, sizeof(replacement_char), writer); @@ -429,14 +429,13 @@ write_text(SerdWriter* writer, i += size - 1; } } - return len; } static size_t uri_sink(const void* buf, size_t len, void* stream) { - return write_uri((SerdWriter*)stream, (const uint8_t*)buf, len); + return write_uri((SerdWriter*)stream, (const char*)buf, len); } static void @@ -509,7 +508,7 @@ write_literal(SerdWriter* writer, SerdStatementFlags flags) { if (supports_abbrev(writer) && datatype && datatype->buf) { - const char* type_uri = (const char*)datatype->buf; + const char* type_uri = datatype->buf; if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && (!strcmp(type_uri + sizeof(NS_XSD) - 1, "boolean") || !strcmp(type_uri + sizeof(NS_XSD) - 1, "integer"))) { @@ -519,8 +518,7 @@ write_literal(SerdWriter* writer, if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") && - strchr((const char*)node->buf, '.') && - node->buf[node->n_bytes - 1] != '.') { + strchr(node->buf, '.') && node->buf[node->n_bytes - 1] != '.') { /* xsd:decimal literals without trailing digits, e.g. "5.", can not be written bare in Turtle. We could add a 0 which is prettier, but changes the text and breaks round tripping. @@ -552,7 +550,7 @@ write_literal(SerdWriter* writer, // Return true iff `buf` is a valid prefixed name prefix or suffix static bool -is_name(const uint8_t* buf, const size_t len) +is_name(const char* buf, const size_t len) { // TODO: This is more strict than it should be for (size_t i = 0; i < len; ++i) { @@ -574,12 +572,11 @@ write_uri_node(SerdWriter* const writer, const bool has_scheme = serd_uri_string_has_scheme(node->buf); if (supports_abbrev(writer)) { - if (field == FIELD_PREDICATE && - !strcmp((const char*)node->buf, NS_RDF "type")) { + if (field == FIELD_PREDICATE && !strcmp(node->buf, NS_RDF "type")) { return sink("a", 1, writer) == 1; } - if (!strcmp((const char*)node->buf, NS_RDF "nil")) { + if (!strcmp(node->buf, NS_RDF "nil")) { return sink("()", 2, writer) == 2; } @@ -623,7 +620,6 @@ write_uri_node(SerdWriter* const writer, } else { write_uri(writer, node->buf, node->n_bytes); } - write_sep(writer, SEP_URI_END); return true; } @@ -688,9 +684,8 @@ write_blank(SerdWriter* const writer, } sink("_:", 2, writer); - if (writer->bprefix && !strncmp((const char*)node->buf, - (const char*)writer->bprefix, - writer->bprefix_len)) { + if (writer->bprefix && + !strncmp(node->buf, writer->bprefix, writer->bprefix_len)) { sink(node->buf + writer->bprefix_len, node->n_bytes - writer->bprefix_len, writer); @@ -753,13 +748,13 @@ write_list_obj(SerdWriter* writer, const SerdNode* datatype, const SerdNode* lang) { - if (!strcmp((const char*)object->buf, NS_RDF "nil")) { + if (!strcmp(object->buf, NS_RDF "nil")) { --writer->indent; write_sep(writer, SEP_LIST_END); return true; } - if (!strcmp((const char*)predicate->buf, NS_RDF "first")) { + if (!strcmp(predicate->buf, NS_RDF "first")) { write_sep(writer, SEP_LIST_SEP); write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); } @@ -981,16 +976,16 @@ serd_writer_set_error_sink(SerdWriter* writer, } void -serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) +serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) { free(writer->bprefix); writer->bprefix_len = 0; writer->bprefix = NULL; - const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + const size_t prefix_len = prefix ? strlen(prefix) : 0; if (prefix_len) { writer->bprefix_len = prefix_len; - writer->bprefix = (uint8_t*)malloc(writer->bprefix_len + 1); + writer->bprefix = (char*)malloc(writer->bprefix_len + 1); memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); } } @@ -1095,9 +1090,9 @@ serd_buffer_sink(const void* const buf, const size_t len, void* const stream) return len; } -uint8_t* +char* serd_buffer_sink_finish(SerdBuffer* const stream) { serd_buffer_sink("", 1, stream); - return (uint8_t*)stream->buf; + return (char*)stream->buf; } -- cgit v1.2.1