# New Ticket Created by  Mike Mattie 
# Please include the string:  [perl #41900]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=41900 >


This patch hoists the finalization of a path with a invisible cstr terminator 
into a function path_finalize
removing duplicated code, and making the Parrot_locate_runtime_str function 
self-documenting with named
procedures.

I have compile tested, and suite tested on linux-i386. Other platforms should 
not be affected AFAICT.

This series of re-factors serves three purposes:

1. remove code duplication, review code.

2. create path functions that can later be reviewed on a API level , and moved 
into their own file
   for use across the project.

3. simplify Parrot_locate_runtime_str so my later feature enhancement of 
Parrot_locate_runtime_str
   will balance out complexity wise instead of increasing the complexity.

I will introduce the enhancement of Parrot_locate_runtime_str at the end of 
this cleanup series.

Patch is against rev 17626, my last change to src/library.c
--- HEAD/src/library.c	2007-03-18 18:20:55.000000000 -0700
+++ parrot-0.4.9.test/src/library.c	2007-03-18 21:27:52.000000000 -0700
@@ -175,6 +178,28 @@
 }
 #endif
 
+static STRING*
+path_finalize(Interp *interp, STRING *path ) 
+{
+
+    /* TODO create a string API that just does that
+     *      a lot of ICU lib functions also need 0-terminated strings
+     *      the goal is just to have for sure an invisible 0 at end
+     */
+
+    STRING *nul = string_from_const_cstring(interp, "\0", 1);
+
+    path = string_append(interp, path, nul);
+    path->bufused--;
+    path->strlen--;
+    
+#ifdef WIN32
+    cnv_to_win32_filesep( path );
+#endif
+
+    return path;
+}
+
 /*
 
 =item C<char* Parrot_locate_runtime_file(Interp *, const char *file_name,
@@ -203,7 +228,7 @@
 Parrot_locate_runtime_file_str(Interp *interp, STRING *file,
         enum_runtime_ft type)
 {
-    STRING *prefix, *path, *full_name, *slash, *nul;
+    STRING *prefix, *path, *full_name, *slash;
     INTVAL i, n;
     PMC *paths;
 
@@ -224,7 +249,6 @@
     slash = CONST_STRING(interp, "/");
 #endif
 
-    nul = string_from_const_cstring(interp, "\0", 1);
     Parrot_get_runtime_prefix(interp, &prefix);
     n = VTABLE_elements(interp, paths);
     for (i = 0; i < n; ++i) {
@@ -243,34 +267,18 @@
             full_name = string_append(interp, full_name, slash);
 
         full_name = string_append(interp, full_name, file);
-        /* TODO create a string API that just does that
-         *      a lot of ICU lib functions also need 0-terminated strings
-         *      the goal is just to have for sure an invisible 0 at end
-         */
-        full_name = string_append(interp, full_name, nul);
-        full_name->bufused--;
-        full_name->strlen--;
-
-#ifdef WIN32
-	cnv_to_win32_filesep( full_name );
-#endif
 
+	full_name = path_finalize(interp, full_name );
         if (Parrot_stat_info_intval(interp, full_name, STAT_EXISTS)) {
             return full_name;
         }
     }
-    /* finally try as is */
-    full_name = string_append(interp, file, nul);
-    full_name->bufused--;
-    full_name->strlen--;
-
-#ifdef WIN32
-    cnv_to_win32_filesep( full_name );
-#endif
 
+    full_name = path_finalize( interp, file );
     if (Parrot_stat_info_intval(interp, full_name, STAT_EXISTS)) {
         return full_name;
     }
+
     return NULL;
 }
 

Attachment: signature.asc
Description: PGP signature

Reply via email to