lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [07/15] git commit: refs/heads/cfish-string-prep1 - Use Str_To_Utf8 to get NULL-terminated C strings
Date Sat, 14 Sep 2013 19:29:39 GMT
Use Str_To_Utf8 to get NULL-terminated C strings


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/235519b2
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/235519b2
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/235519b2

Branch: refs/heads/cfish-string-prep1
Commit: 235519b29ffd9cdc62e4282e53f434fa3641f470
Parents: 41a5ef7
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 14 18:28:53 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sat Sep 14 18:28:53 2013 +0200

----------------------------------------------------------------------
 c/sample/getting_started.c                      |   4 +-
 c/src/Lucy/Analysis/RegexTokenizer.c            |   7 +-
 clownfish/runtime/c/src/Clownfish/Err.c         |   9 +-
 .../core/Clownfish/TestHarness/TestFormatter.c  |   5 +-
 clownfish/runtime/perl/xs/XSBind.c              |   2 +
 core/Lucy/Store/FSDirHandle.c                   |  12 ++-
 core/Lucy/Store/FSFileHandle.c                  |  12 ++-
 core/Lucy/Store/FSFolder.c                      | 100 ++++++++++++-------
 8 files changed, 101 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/c/sample/getting_started.c
----------------------------------------------------------------------
diff --git a/c/sample/getting_started.c b/c/sample/getting_started.c
index 2816a30..f7bbcfe 100644
--- a/c/sample/getting_started.c
+++ b/c/sample/getting_started.c
@@ -175,9 +175,11 @@ S_search(IndexSearcher *searcher, const char *query) {
     // Loop over search results.
     while (NULL != (hit = Hits_Next(hits))) {
         String *value_str = (String*)HitDoc_Extract(hit, field_str);
+        char *value = Str_To_Utf8(value_str);
 
-        printf("Result %d: %s\n", i, Str_Get_Ptr8(value_str));
+        printf("Result %d: %s\n", i, value);
 
+        DECREF(value);
         DECREF(value_str);
         DECREF(hit);
         i++;

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/c/src/Lucy/Analysis/RegexTokenizer.c
----------------------------------------------------------------------
diff --git a/c/src/Lucy/Analysis/RegexTokenizer.c b/c/src/Lucy/Analysis/RegexTokenizer.c
index bb3f96b..62085cb 100644
--- a/c/src/Lucy/Analysis/RegexTokenizer.c
+++ b/c/src/Lucy/Analysis/RegexTokenizer.c
@@ -48,10 +48,12 @@ RegexTokenizer_init(RegexTokenizer *self, const String *pattern) {
     Analyzer_init((Analyzer*)self);
     RegexTokenizerIVARS *const ivars = RegexTokenizer_IVARS(self);
 
+    char *pattern_buf = NULL;
     const char *pattern_ptr;
     if (pattern) {
         ivars->pattern = Str_Clone(pattern);
-        pattern_ptr = (char*)Str_Get_Ptr8(ivars->pattern);
+        pattern_buf = Str_To_Utf8(ivars->pattern);
+        pattern_ptr = pattern_buf;
     }
     else {
         pattern_ptr = "\\w+(?:['\\x{2019}]\\w+)*";
@@ -71,6 +73,9 @@ RegexTokenizer_init(RegexTokenizer *self, const String *pattern) {
     const char *err_ptr;
     int err_offset;
     pcre *re = pcre_compile(pattern_ptr, options, &err_ptr, &err_offset, NULL);
+    if (pattern_buf) {
+        FREEMEM(pattern_buf);
+    }
     if (!re) {
         THROW(ERR, "%s", err_ptr);
     }

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/clownfish/runtime/c/src/Clownfish/Err.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/c/src/Clownfish/Err.c b/clownfish/runtime/c/src/Clownfish/Err.c
index ae13494..b360d5a 100644
--- a/clownfish/runtime/c/src/Clownfish/Err.c
+++ b/clownfish/runtime/c/src/Clownfish/Err.c
@@ -26,6 +26,7 @@
 
 #include "Clownfish/Err.h"
 #include "Clownfish/String.h"
+#include "Clownfish/Util/Memory.h"
 #include "Clownfish/VTable.h"
 
 /* TODO: Thread safety */
@@ -58,7 +59,9 @@ Err_do_throw(Err *error) {
     }
     else {
         String *message = Err_Get_Mess(error);
-        fprintf(stderr, "%s", Str_Get_Ptr8(message));
+        char *utf8 = Str_To_Utf8(message);
+        fprintf(stderr, "%s", utf8);
+        FREEMEM(utf8);
         exit(EXIT_FAILURE);
     }
 }
@@ -78,7 +81,9 @@ Err_throw_mess(VTable *vtable, String *message) {
 
 void
 Err_warn_mess(String *message) {
-    fprintf(stderr, "%s", Str_Get_Ptr8(message));
+    char *utf8 = Str_To_Utf8(message);
+    fprintf(stderr, "%s", utf8);
+    FREEMEM(utf8);
     DECREF(message);
 }
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/clownfish/runtime/core/Clownfish/TestHarness/TestFormatter.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/TestHarness/TestFormatter.c b/clownfish/runtime/core/Clownfish/TestHarness/TestFormatter.c
index 6088c89..c5016be 100644
--- a/clownfish/runtime/core/Clownfish/TestHarness/TestFormatter.c
+++ b/clownfish/runtime/core/Clownfish/TestHarness/TestFormatter.c
@@ -28,6 +28,7 @@
 #include "Clownfish/Err.h"
 #include "Clownfish/TestHarness/TestBatch.h"
 #include "Clownfish/TestHarness/TestSuiteRunner.h"
+#include "Clownfish/Util/Memory.h"
 #include "Clownfish/VTable.h"
 
 TestFormatter*
@@ -79,7 +80,9 @@ TestFormatterCF_Batch_Prologue_IMP(TestFormatterCF *self, TestBatch *batch,
     UNUSED_VAR(self);
     UNUSED_VAR(num_planned);
     String *class_name = TestBatch_Get_Class_Name(batch);
-    printf("Running %s...\n", Str_Get_Ptr8(class_name));
+    char *utf8 = Str_To_Utf8(class_name);
+    printf("Running %s...\n", utf8);
+    FREEMEM(utf8);
 }
 
 void

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/clownfish/runtime/perl/xs/XSBind.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/perl/xs/XSBind.c b/clownfish/runtime/perl/xs/XSBind.c
index 379bc7d..0f21e50 100644
--- a/clownfish/runtime/perl/xs/XSBind.c
+++ b/clownfish/runtime/perl/xs/XSBind.c
@@ -85,6 +85,8 @@ cfish_Obj*
 XSBind_maybe_sv_to_cfish_obj(SV *sv, cfish_VTable *vtable, void *allocation) {
     cfish_Obj *retval = NULL;
     if (XSBind_sv_defined(sv)) {
+        // Assume that the class name is always NULL-terminated. Somewhat
+        // dangerous but should be safe.
         if (sv_isobject(sv)
             && sv_derived_from(sv, (char*)CFISH_Str_Get_Ptr8(CFISH_VTable_Get_Name(vtable)))
            ) {

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/core/Lucy/Store/FSDirHandle.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSDirHandle.c b/core/Lucy/Store/FSDirHandle.c
index 631f2f6..6fcf793 100644
--- a/core/Lucy/Store/FSDirHandle.c
+++ b/core/Lucy/Store/FSDirHandle.c
@@ -208,13 +208,13 @@ FSDH_Next_IMP(FSDirHandle *self) {
 
 FSDirHandle*
 FSDH_do_open(FSDirHandle *self, const String *dir) {
-    char *dir_path_ptr = (char*)Str_Get_Ptr8(dir);
-
     DH_init((DirHandle*)self, dir);
     FSDirHandleIVARS *const ivars = FSDH_IVARS(self);
     ivars->sys_dir_entry = NULL;
 
+    char *dir_path_ptr = Str_To_Utf8(dir);
     ivars->sys_dirhandle = opendir(dir_path_ptr);
+    FREEMEM(dir_path_ptr);
     if (!ivars->sys_dirhandle) {
         Err_set_error(Err_new(Str_newf("Failed to opendir '%o'", dir)));
         DECREF(self);
@@ -272,9 +272,11 @@ FSDH_Entry_Is_Dir_IMP(FSDirHandle *self) {
     struct stat stat_buf;
     String *fullpath = Str_newf("%o%s%o", ivars->dir, CHY_DIR_SEP,
                                 ivars->entry);
-    if (stat((char*)Str_Get_Ptr8(fullpath), &stat_buf) != -1) {
+    char *fullpath_ptr = Str_To_Utf8(fullpath);
+    if (stat(fullpath_ptr, &stat_buf) != -1) {
         if (stat_buf.st_mode & S_IFDIR) { retval = true; }
     }
+    FREEMEM(fullpath_ptr);
     DECREF(fullpath);
     return retval;
 }
@@ -293,9 +295,11 @@ FSDH_Entry_Is_Symlink_IMP(FSDirHandle *self) {
         struct stat stat_buf;
         String *fullpath = Str_newf("%o%s%o", ivars->dir, CHY_DIR_SEP,
                                     ivars->entry);
-        if (stat((char*)Str_Get_Ptr8(fullpath), &stat_buf) != -1) {
+        char *fullpath_ptr = Str_To_Utf8(fullpath);
+        if (stat(fullpath_ptr, &stat_buf) != -1) {
             if (stat_buf.st_mode & S_IFLNK) { retval = true; }
         }
+        FREEMEM(fullpath_ptr);
         DECREF(fullpath);
         return retval;
     }

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/core/Lucy/Store/FSFileHandle.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFileHandle.c b/core/Lucy/Store/FSFileHandle.c
index 020eb9f..1f8b831 100644
--- a/core/Lucy/Store/FSFileHandle.c
+++ b/core/Lucy/Store/FSFileHandle.c
@@ -100,7 +100,9 @@ FSFH_do_open(FSFileHandle *self, const String *path, uint32_t flags) {
 
     // Attempt to open file.
     if (flags & FH_WRITE_ONLY) {
-        ivars->fd = open((char*)Str_Get_Ptr8(path), SI_posix_flags(flags), 0666);
+        char *path_ptr = Str_To_Utf8(path);
+        ivars->fd = open(path_ptr, SI_posix_flags(flags), 0666);
+        FREEMEM(path_ptr);
         if (ivars->fd == -1) {
             ivars->fd = 0;
             Err_set_error(Err_new(Str_newf("Attempt to open '%o' failed: %s",
@@ -330,8 +332,9 @@ SI_init_read_only(FSFileHandle *self, FSFileHandleIVARS *ivars) {
     UNUSED_VAR(self);
 
     // Open.
-    ivars->fd = open((char*)Str_Get_Ptr8(ivars->path),
-                    SI_posix_flags(ivars->flags), 0666);
+    char *path_ptr = Str_To_Utf8(ivars->path);
+    ivars->fd = open(path_ptr, SI_posix_flags(ivars->flags), 0666);
+    FREEMEM(path_ptr);
     if (ivars->fd == -1) {
         ivars->fd = 0;
         Err_set_error(Err_new(Str_newf("Can't open '%o': %s", ivars->path,
@@ -438,7 +441,7 @@ FSFH_Read_IMP(FSFileHandle *self, char *dest, int64_t offset, size_t len)
{
 
 static CFISH_INLINE bool
 SI_init_read_only(FSFileHandle *self, FSFileHandleIVARS *ivars) {
-    char *filepath = (char*)Str_Get_Ptr8(ivars->path);
+    char *filepath = Str_To_Utf8(ivars->path);
     SYSTEM_INFO sys_info;
 
     // Get system page size.
@@ -455,6 +458,7 @@ SI_init_read_only(FSFileHandle *self, FSFileHandleIVARS *ivars) {
                             FILE_ATTRIBUTE_READONLY | FILE_FLAG_OVERLAPPED,
                             NULL
                         );
+    FREEMEM(filepath);
     if (ivars->win_fhandle == INVALID_HANDLE_VALUE) {
         char *win_error = Err_win_error();
         Err_set_error(Err_new(Str_newf("CreateFile for %o failed: %s",

http://git-wip-us.apache.org/repos/asf/lucy/blob/235519b2/core/Lucy/Store/FSFolder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFolder.c b/core/Lucy/Store/FSFolder.c
index 70522f3..eb1c54f 100644
--- a/core/Lucy/Store/FSFolder.c
+++ b/core/Lucy/Store/FSFolder.c
@@ -50,6 +50,10 @@
 static String*
 S_fullpath(FSFolder *self, const String *path);
 
+// Return a String containing a platform-specific absolute filepath.
+static char*
+S_fullpath_ptr(FSFolder *self, const String *path);
+
 // Return true if the supplied path is a directory.
 static bool
 S_dir_ok(const String *path);
@@ -72,7 +76,7 @@ S_absolutify(const String *path);
 
 // Create a hard link.
 static bool
-S_hard_link(String *from_path, String *to_path);
+S_hard_link(char *from_path, char *to_path);
 
 FSFolder*
 FSFolder_new(const String *path) {
@@ -142,12 +146,12 @@ FSFolder_Local_Exists_IMP(FSFolder *self, const String *name) {
     }
     else {
         struct stat stat_buf;
-        String *fullpath = S_fullpath(self, name);
+        char *fullpath_ptr = S_fullpath_ptr(self, name);
         bool retval = false;
-        if (stat((char*)Str_Get_Ptr8(fullpath), &stat_buf) != -1) {
+        if (stat(fullpath_ptr, &stat_buf) != -1) {
             retval = true;
         }
-        DECREF(fullpath);
+        FREEMEM(fullpath_ptr);
         return retval;
     }
 }
@@ -171,27 +175,26 @@ FSFolder_Local_Is_Directory_IMP(FSFolder *self, const String *name)
{
 
 bool
 FSFolder_Rename_IMP(FSFolder *self, const String* from, const String *to) {
-    String *from_path = S_fullpath(self, from);
-    String *to_path   = S_fullpath(self, to);
-    bool    retval    = !rename((char*)Str_Get_Ptr8(from_path),
-                                 (char*)Str_Get_Ptr8(to_path));
+    char *from_path = S_fullpath_ptr(self, from);
+    char *to_path   = S_fullpath_ptr(self, to);
+    bool  retval    = !rename(from_path, to_path);
     if (!retval) {
-        Err_set_error(Err_new(Str_newf("rename from '%o' to '%o' failed: %s",
+        Err_set_error(Err_new(Str_newf("rename from '%s' to '%s' failed: %s",
                                        from_path, to_path, strerror(errno))));
     }
-    DECREF(from_path);
-    DECREF(to_path);
+    FREEMEM(from_path);
+    FREEMEM(to_path);
     return retval;
 }
 
 bool
 FSFolder_Hard_Link_IMP(FSFolder *self, const String *from,
                        const String *to) {
-    String *from_path = S_fullpath(self, from);
-    String *to_path   = S_fullpath(self, to);
-    bool    retval    = S_hard_link(from_path, to_path);
-    DECREF(from_path);
-    DECREF(to_path);
+    char *from_path_ptr = S_fullpath_ptr(self, from);
+    char *to_path_ptr   = S_fullpath_ptr(self, to);
+    bool  retval        = S_hard_link(from_path_ptr, to_path_ptr);
+    FREEMEM(from_path_ptr);
+    FREEMEM(to_path_ptr);
     return retval;
 }
 
@@ -199,15 +202,14 @@ bool
 FSFolder_Local_Delete_IMP(FSFolder *self, const String *name) {
     FSFolderIVARS *const ivars = FSFolder_IVARS(self);
 
-    String *fullpath = S_fullpath(self, name);
-    char   *path_ptr = (char*)Str_Get_Ptr8(fullpath);
+    char *path_ptr = S_fullpath_ptr(self, name);
 #ifdef CHY_REMOVE_ZAPS_DIRS
     bool result = !remove(path_ptr);
 #else
     bool result = !rmdir(path_ptr) || !remove(path_ptr);
 #endif
     DECREF(Hash_Delete(ivars->entries, (Obj*)name));
-    DECREF(fullpath);
+    FREEMEM(path_ptr);
     return result;
 }
 
@@ -281,23 +283,53 @@ S_fullpath(FSFolder *self, const String *path) {
     return retval;
 }
 
+static char*
+S_fullpath_ptr(FSFolder *self, const String *path) {
+    FSFolderIVARS *const ivars = FSFolder_IVARS(self);
+    size_t folder_size = Str_Get_Size(ivars->path);
+    size_t path_size   = Str_Get_Size(path);
+    size_t full_size   = folder_size + 1 + path_size;
+    const char *folder_ptr = (char*)Str_Get_Ptr8(ivars->path);
+    const char *path_ptr   = (char*)Str_Get_Ptr8(path);
+
+    char *buf = (char*)MALLOCATE(full_size + 1);
+    memcpy(buf, folder_ptr, folder_size);
+    buf[folder_size] = DIR_SEP[0];
+    memcpy(buf + folder_size + 1, path_ptr, path_size);
+    buf[full_size] = '\0';
+
+    if (DIR_SEP[0] != '/') {
+        for (size_t i = 0; i < full_size; ++i) {
+            if (buf[i] == '/') { buf[i] = DIR_SEP[0]; }
+        }
+    }
+
+    return buf;
+}
+
 static bool
 S_dir_ok(const String *path) {
+    bool retval = false;
+    char *path_ptr = Str_To_Utf8(path);
     struct stat stat_buf;
-    if (stat((char*)Str_Get_Ptr8(path), &stat_buf) != -1) {
-        if (stat_buf.st_mode & S_IFDIR) { return true; }
+    if (stat(path_ptr, &stat_buf) != -1) {
+        if (stat_buf.st_mode & S_IFDIR) { retval = true; }
     }
-    return false;
+    FREEMEM(path_ptr);
+    return retval;
 }
 
 static bool
 S_create_dir(const String *path) {
-    if (-1 == chy_makedir((char*)Str_Get_Ptr8(path), 0777)) {
+    bool retval = true;
+    char *path_ptr = Str_To_Utf8(path);
+    if (-1 == chy_makedir(path_ptr, 0777)) {
         Err_set_error(Err_new(Str_newf("Couldn't create directory '%o': %s",
                                        path, strerror(errno))));
-        return false;
+        retval = false;
     }
-    return true;
+    FREEMEM(path_ptr);
+    return retval;
 }
 
 static bool
@@ -346,17 +378,14 @@ S_absolutify(const String *path) {
 }
 
 static bool
-S_hard_link(String *from_path, String *to_path) {
-    char *from8 = (char*)Str_Get_Ptr8(from_path);
-    char *to8   = (char*)Str_Get_Ptr8(to_path);
-
+S_hard_link(char *from8, char *to8) {
     if (CreateHardLink(to8, from8, NULL)) {
         return true;
     }
     else {
         char *win_error = Err_win_error();
-        Err_set_error(Err_new(Str_newf("CreateHardLink for new file '%o' from '%o' failed:
%s",
-                                       to_path, from_path, win_error)));
+        Err_set_error(Err_new(Str_newf("CreateHardLink for new file '%s' from '%s' failed:
%s",
+                                       to8, from8, win_error)));
         FREEMEM(win_error);
         return false;
     }
@@ -382,13 +411,10 @@ S_absolutify(const String *path) {
 }
 
 static bool
-S_hard_link(String *from_path, String *to_path) {
-    char *from8 = (char*)Str_Get_Ptr8(from_path);
-    char *to8   = (char*)Str_Get_Ptr8(to_path);
-
+S_hard_link(char *from8, char *to8) {
     if (-1 == link(from8, to8)) {
-        Err_set_error(Err_new(Str_newf("hard link for new file '%o' from '%o' failed: %s",
-                                       to_path, from_path, strerror(errno))));
+        Err_set_error(Err_new(Str_newf("hard link for new file '%s' from '%s' failed: %s",
+                                       to8, from8, strerror(errno))));
         return false;
     }
     else {


Mime
View raw message