Revision: 6601
Author: ek.kato
Date: Tue Jul 27 21:29:41 2010
Log: * src/load.c
  - (PATH_SEPARATOR)
  - (scm_set_lib_path)
  - (find_path)
    - Support multiple paths separated with ":" for
      scm_set_lib_path().

http://code.google.com/p/uim/source/detail?r=6601

Modified:
 /sigscheme-trunk/src/load.c

=======================================
--- /sigscheme-trunk/src/load.c Wed Feb 13 07:03:48 2008
+++ /sigscheme-trunk/src/load.c Tue Jul 27 21:29:41 2010
@@ -55,6 +55,7 @@
 =======================================*/
 /* FIXME: only supports UNIX flavors */
 #define ABSOLUTE_PATHP(path) ((path)[0] == '/')
+#define PATH_SEPARATOR ':'

 #if SCM_USE_SRFI22
/* SRFI-22: The <script prelude> line may not be longer than 64 characters. */
@@ -121,10 +122,22 @@
 SCM_EXPORT void
 scm_set_lib_path(const char *path)
 {
+    const char *begin, *end;
     DECLARE_INTERNAL_FUNCTION("scm_set_lib_path");

-    if (!ABSOLUTE_PATHP(path))
-        ERR("library path must be absolute but got: ~S", path);
+    begin = path;
+    while (begin[0] != '\0') {
+        while (begin[0] == PATH_SEPARATOR)
+            begin++;
+
+        end = begin;
+        while (end[0] != '\0' && end[0] != PATH_SEPARATOR)
+            end++;
+
+        if (!ABSOLUTE_PATHP(begin))
+            ERR("library path must be absolute but got: ~S", path);
+        begin = end;
+    }

     free(l_scm_lib_path);
     l_scm_lib_path = (path) ? scm_strdup(path) : NULL;
@@ -228,6 +241,7 @@
 find_path(const char *filename)
 {
     char *path;
+    const char *begin, *end;
     size_t lib_path_len, filename_len, path_len;

     SCM_ASSERT(filename);
@@ -237,16 +251,34 @@
         return scm_strdup(filename);

     /* try under l_scm_lib_path */
+    /* l_scm_lib_path is separated with PATH_SEPARATOR */
     if (l_scm_lib_path) {
-        lib_path_len = (l_scm_lib_path) ? strlen(l_scm_lib_path) : 0;
-        filename_len = strlen(filename);
- path_len = lib_path_len + sizeof((char)'/') + filename_len + sizeof("");
-
-        path = scm_malloc(path_len);
-        sprintf(path, "%s/%s", l_scm_lib_path, filename);
-        if (file_existsp(path))
-            return path;
-        free(path);
+        begin = l_scm_lib_path;
+        while (begin[0] != '\0') {
+            while (begin[0] == PATH_SEPARATOR)
+                begin++;
+
+            end = begin;
+            while (end[0] != '\0' && end[0] != PATH_SEPARATOR)
+                end++;
+
+            if (end > begin)
+                lib_path_len = end - begin;
+            else
+                lib_path_len = 0;
+            filename_len = strlen(filename);
+ path_len = lib_path_len + sizeof((char)'/') + filename_len + sizeof("");
+
+            path = scm_malloc(path_len);
+            strncpy(path, begin, lib_path_len);
+            path[lib_path_len] = '\0';
+            strcat(path, "/");
+            strcat(path, filename);
+            if (file_existsp(path))
+                return path;
+            free(path);
+            begin = end;
+        }
     }

     return NULL;

Reply via email to