summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/raptor-1-to-2-map.tsv4
-rw-r--r--docs/raptor2-sections.txt5
-rw-r--r--src/raptor2.h.in9
-rw-r--r--src/raptor_sax2.c25
-rw-r--r--src/raptor_uri.c112
5 files changed, 77 insertions, 78 deletions
diff --git a/docs/raptor-1-to-2-map.tsv b/docs/raptor-1-to-2-map.tsv
index 7807404d..9d28b539 100644
--- a/docs/raptor-1-to-2-map.tsv
+++ b/docs/raptor-1-to-2-map.tsv
@@ -406,9 +406,9 @@
2.0.5 - - - 2.0.6 int raptor_sequence_swap (raptor_sequence* seq, int i, int j) -
2.0.5 - - - 2.0.6 int raptor_sequence_reverse (raptor_sequence* seq, int start_index, int length) -
2.0.5 - - - 2.0.6 int raptor_sequence_next_permutation (raptor_sequence *seq, raptor_data_compare_handler compare) -
-2.0.6 - - - 2.0.7 char* raptor_uri_uri_string_to_filename (const unsigned char* uri_string, size_t uri_string_len, int* exists_p) -
2.0.6 - - - 2.0.7 char* raptor_uri_uri_string_to_counted_filename_fragment (const unsigned char *uri_string, size_t* len_p, unsigned char **fragment_p, size_t* fragment_len_p) -
-2.0.6 - - - 2.0.7 char* raptor_uri_uri_string_to_counted_filename (const unsigned char* uri_string, size_t uri_string_len, size_t* path_len_p, int* exists_p) -
+2.0.6 - - - 2.0.7 int raptor_uri_file_exists (raptor_uri* uri) -
+2.0.6 - - - 2.0.7 raptor_uri* raptor_new_uri_from_uri_or_file_string (raptor_world* world, raptor_uri* base_uri, const unsigned char* uri_or_file_string) -
2.0.6 - - - 2.0.7 void raptor_sax2_set_uri_filter (raptor_sax2* sax2, raptor_uri_filter_func filter, void *user_data) -
#
# Types
diff --git a/docs/raptor2-sections.txt b/docs/raptor2-sections.txt
index b447961c..b56a231d 100644
--- a/docs/raptor2-sections.txt
+++ b/docs/raptor2-sections.txt
@@ -221,6 +221,7 @@ raptor_uri
raptor_new_uri
raptor_new_uri_from_counted_string
raptor_new_uri_from_uri_local_name
+raptor_new_uri_from_uri_or_file_string
raptor_new_uri_relative_to_base
raptor_new_uri_from_id
raptor_new_uri_for_rdf_concept
@@ -238,14 +239,14 @@ raptor_uri_to_counted_string
raptor_uri_to_string
raptor_uri_resolve_uri_reference
raptor_uri_filename_to_uri_string
-raptor_uri_uri_string_to_counted_filename
-raptor_uri_uri_string_to_counted_filename_fragment
raptor_uri_uri_string_to_filename
raptor_uri_uri_string_to_filename_fragment
+raptor_uri_uri_string_to_counted_filename_fragment
raptor_uri_uri_string_is_file_uri
raptor_uri_print
raptor_uri_get_world
raptor_uri_write
+raptor_uri_file_exists
</SECTION>
<SECTION>
diff --git a/src/raptor2.h.in b/src/raptor2.h.in
index 8467ff29..9e11a401 100644
--- a/src/raptor2.h.in
+++ b/src/raptor2.h.in
@@ -1252,6 +1252,8 @@ raptor_uri* raptor_new_uri_relative_to_base(raptor_world* world, raptor_uri *bas
RAPTOR_API
raptor_uri* raptor_new_uri_from_id(raptor_world* world, raptor_uri *base_uri, const unsigned char *id);
RAPTOR_API
+raptor_uri* raptor_new_uri_from_uri_or_file_string(raptor_world* world, raptor_uri* base_uri, const unsigned char* uri_or_file_string);
+RAPTOR_API
raptor_uri* raptor_new_uri_for_rdf_concept(raptor_world* world, const unsigned char *name);
RAPTOR_API
raptor_uri* raptor_new_uri_for_xmlbase(raptor_uri* old_uri);
@@ -1284,10 +1286,7 @@ unsigned char* raptor_uri_to_string(raptor_uri *uri);
RAPTOR_API
raptor_world* raptor_uri_get_world(raptor_uri *uri);
RAPTOR_API
-char* raptor_uri_uri_string_to_counted_filename_fragment(const unsigned char *uri_string, size_t* len_p, unsigned char **fragment_p, size_t* fragment_len_p);
-RAPTOR_API
-char* raptor_uri_uri_string_to_counted_filename(const unsigned char* uri_string, size_t uri_string_len, size_t* path_len_p, int* exists_p);
-
+int raptor_uri_file_exists(raptor_uri* uri);
/* XML utility functions */
RAPTOR_API
@@ -1327,6 +1326,8 @@ RAPTOR_API
int raptor_uri_uri_string_is_file_uri(const unsigned char* uri_string);
RAPTOR_API
int raptor_stringbuffer_append_uri_escaped_counted_string(raptor_stringbuffer* sb, const char* string, size_t length, int space_is_plus);
+RAPTOR_API
+char* raptor_uri_uri_string_to_counted_filename_fragment(const unsigned char *uri_string, size_t* len_p, unsigned char **fragment_p, size_t* fragment_len_p);
/**
diff --git a/src/raptor_sax2.c b/src/raptor_sax2.c
index 00b06862..3081d001 100644
--- a/src/raptor_sax2.c
+++ b/src/raptor_sax2.c
@@ -1091,30 +1091,17 @@ raptor_sax2_check_load_uri_string(raptor_sax2* sax2,
{
raptor_uri* abs_uri;
const unsigned char* abs_uri_string;
- char* path;
int abs_uri_is_file;
int load_uri = 0;
- abs_uri = raptor_new_uri_relative_to_base(sax2->world, sax2->base_uri,
- uri_string);
- abs_uri_string = raptor_uri_as_string(abs_uri);
- path = raptor_uri_uri_string_to_counted_filename(abs_uri_string, 0,
- NULL, &abs_uri_is_file);
- if(path) {
- unsigned char* new_uri_string;
-
- raptor_free_uri(abs_uri); abs_uri = NULL;
-
- /* new_uri_string is a string like "file://" + path */
- new_uri_string = raptor_uri_filename_to_uri_string(path);
- RAPTOR_FREE(char*, path); path = NULL;
+ abs_uri = raptor_new_uri_from_uri_or_file_string(sax2->world, sax2->base_uri,
+ uri_string);
+ if(!abs_uri)
+ return 1;
- abs_uri = raptor_new_uri(sax2->world, new_uri_string);
- RAPTOR_FREE(char*, new_uri_string);
+ abs_uri_string = raptor_uri_as_string(abs_uri);
- abs_uri_string = raptor_uri_as_string(abs_uri);
- }
-
+ abs_uri_is_file = raptor_uri_uri_string_is_file_uri(abs_uri_string);
if(abs_uri_is_file)
load_uri = !RAPTOR_OPTIONS_GET_NUMERIC(sax2, RAPTOR_OPTION_NO_FILE);
else
diff --git a/src/raptor_uri.c b/src/raptor_uri.c
index 6c2963d1..e702228d 100644
--- a/src/raptor_uri.c
+++ b/src/raptor_uri.c
@@ -394,6 +394,48 @@ raptor_new_uri_for_rdf_concept(raptor_world* world, const unsigned char *name)
/**
+ * raptor_new_uri_from_uri_or_file_string:
+ * @world: raptor_world object
+ * @base_uri: existing base URI
+ * @uri_or_file_string: URI string or filename
+ *
+ * Constructor - create a raptor URI from a string that is a relative or absolute URI or a filename
+ *
+ * If the @uri_or_file_string is a filename (file: or a file that exists),
+ * the resulting URI will be file://PATH
+ *
+ * Return value: a new #raptor_uri object or NULL on failure
+ **/
+raptor_uri*
+raptor_new_uri_from_uri_or_file_string(raptor_world* world,
+ raptor_uri* base_uri,
+ const unsigned char* uri_or_file_string)
+{
+ raptor_uri* new_uri;
+ const unsigned char* new_uri_string;
+ char* path;
+
+ new_uri = raptor_new_uri_relative_to_base(world, base_uri,
+ uri_or_file_string);
+ new_uri_string = raptor_uri_as_string(new_uri);
+ path = raptor_uri_uri_string_to_counted_filename_fragment(new_uri_string,
+ NULL, NULL, NULL);
+ if(path) {
+ raptor_free_uri(new_uri); new_uri = NULL;
+
+ /* new_uri_string is a string like "file://" + path */
+ new_uri_string = raptor_uri_filename_to_uri_string(path);
+ RAPTOR_FREE(char*, path); path = NULL;
+
+ new_uri = raptor_new_uri(world, new_uri_string);
+ RAPTOR_FREE(char*, new_uri_string);
+ }
+
+ return new_uri;
+}
+
+
+/**
* raptor_free_uri:
* @uri: URI to destroy
*
@@ -924,6 +966,7 @@ raptor_uri_uri_string_to_filename(const unsigned char *uri_string)
}
+
/**
* raptor_uri_uri_string_is_file_uri:
* @uri_string: The URI string to check
@@ -935,22 +978,10 @@ raptor_uri_uri_string_to_filename(const unsigned char *uri_string)
int
raptor_uri_uri_string_is_file_uri(const unsigned char* uri_string)
{
- int is_file;
- char* path;
-
if(!uri_string || !*uri_string)
return 1;
- is_file = raptor_strncasecmp((const char*)uri_string, "file:", 5) == 0;
- if(is_file)
- return is_file;
-
- path = raptor_uri_uri_string_to_counted_filename(uri_string, 0,
- NULL, &is_file);
- if(path)
- RAPTOR_FREE(char*, path);
-
- return is_file;
+ return raptor_strncasecmp((const char*)uri_string, "file:", 5) == 0;
}
@@ -1546,61 +1577,40 @@ raptor_uri_get_world(raptor_uri *uri)
/**
- * raptor_uri_uri_string_to_counted_filename:
- * @uri_string: uri string
- * @uri_string_len: length of @uri_string or 0 to count it here
- * @path_p: address of pointer to store filename length (or NULL)
- * @exists_p: address of pointer to store file exists status (or NULL), returning < 0 on error, 0 if not a file, > 0 if is a file
+ * raptor_uri_file_exists:
+ * @uri: URI string
*
- * Turn a file-or-URI into a filename and check the file exists
+ * Check if a file: URI is a file that exists
*
- * Return value: newly allocated filename (if file exists) or NULL if not a file URI or file does not exist
+ * Return value: > 0 if file exists, 0 if does not exist, < 0 if not a file URI or error
**/
-char*
-raptor_uri_uri_string_to_counted_filename(const unsigned char* uri_string,
- size_t uri_string_len,
- size_t* path_len_p,
- int* exists_p)
+int
+raptor_uri_file_exists(raptor_uri* uri)
{
- char *path = NULL;
+ const unsigned char* uri_string;
+ const unsigned char *path = NULL;
int exists = -1;
#ifdef HAVE_STAT
struct stat stat_buffer;
#endif
- if(!uri_string)
- return NULL;
-
- if(!uri_string_len)
- uri_string_len = strlen(RAPTOR_GOOD_CAST(const char*, uri_string));
-
- path = raptor_uri_uri_string_to_counted_filename_fragment(uri_string,
- path_len_p,
- NULL, NULL);
- if(!path) {
- path = RAPTOR_MALLOC(char*, uri_string_len + 1);
- if(!path)
- return NULL;
+ if(!uri)
+ return -1;
- memcpy(path, uri_string, uri_string_len + 1);
- }
+ uri_string = raptor_uri_as_string(uri);
+ if(!raptor_uri_uri_string_is_file_uri(uri_string))
+ return -1;
+
+ path = uri_string + 6;
#ifdef HAVE_STAT
- if(!stat(path, &stat_buffer))
+ if(!stat((const char*)path, &stat_buffer))
exists = S_ISREG(stat_buffer.st_mode);
#else
exists = (access(path, R_OK) < 0) ? -1 : 1
#endif
- if(exists > 0 && path) {
- RAPTOR_FREE(char*, path);
- path = NULL;
- }
-
- if(exists_p)
- *exists_p = exists;
-
- return path;
+ return exists;
}