lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r467734 - in /lucene/lucy/trunk/charmonizer/src/Charmonizer/Core: ModHandler.charm Util.charm Util.harm
Date Wed, 25 Oct 2006 19:01:01 GMT
Author: marvin
Date: Wed Oct 25 12:01:00 2006
New Revision: 467734

URL: http://svn.apache.org/viewvc?view=rev&rev=467734
Log:
Add some string concatenation convenience routines to Util.  Use them to clean
up ModHandler.  Add a missing call to free() in ModHandler.

Modified:
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/ModHandler.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.harm

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/ModHandler.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/ModHandler.charm?view=diff&rev=467734&r1=467733&r2=467734
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/ModHandler.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/ModHandler.charm Wed Oct 25 12:01:00
2006
@@ -49,14 +49,11 @@
     size_t len;
 
     /* set the name of the application which we "try" to execute */
-    len = strlen(TRY_APP_BASENAME) + strlen(os->exe_ext) + 1;
-    try_app_path = (char*)malloc(len);
-    sprintf(try_app_path, "%s%s", TRY_APP_BASENAME, os->exe_ext);
+    (void)join_strings(&try_app_path, 0, TRY_APP_BASENAME, os->exe_ext, NULL);
 
     /* set the invocation string for the "try" application */
-    len += strlen(os->local_command_start);
-    try_app_command = (char*)malloc(len);
-    sprintf(try_app_command, "%s%s", os->local_command_start, try_app_path);
+    (void)join_strings(&try_app_command, 0, os->local_command_start,
+        try_app_path, NULL);
 
     /* write files needed by this module and others */
     build_charm_run();
@@ -120,13 +117,11 @@
 static void
 build_charm_run()
 {
-    size_t code_len = strlen(charm_run_code_a) + strlen(charm_run_code_b) +
-        strlen(os->devnull) + 20;
-    char *code = (char*)malloc(code_len);
+    char *code = NULL;
     chaz_bool_t compile_succeeded = false;
 
-    sprintf(code, "%s \"%s\" %s", charm_run_code_a, os->devnull,
-        charm_run_code_b);
+    (void)join_strings(&code, 0, charm_run_code_a, " \"", os->devnull, "\" ",
+        charm_run_code_b, NULL);
 
     compile_succeeded = compiler->compile_exe(compiler, "_charm_run.c", 
         "_charm_run", code, strlen(code));
@@ -134,6 +129,7 @@
         die("failed to compile _charm_run helper utility");
 
     remove("_charm_run.c");
+    free(code);
     charm_run_available = true;
 }
 

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.charm?view=diff&rev=467734&r1=467733&r2=467734
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.charm Wed Oct 25 12:01:00 2006
@@ -140,7 +140,66 @@
             die("buffer allocation failed");
     }
 
-    return new_len;
+    return new_len > old_len ? new_len : old_len;
+}
+
+size_t
+chaz_Util_append_strings(char **buf, size_t buf_len, ...)
+{
+    va_list args;
+    size_t retval;
+
+    va_start(args, buf_len);
+    retval = vappend_strings(buf, buf_len, args);
+    va_end(args);
+
+    return retval;
+}
+
+size_t
+chaz_Util_vappend_strings(char **buf, size_t buf_len, va_list args)
+{
+    char *str;
+    size_t new_len = *buf == NULL ? 0 : strlen(*buf);
+    size_t retval;
+    va_list save_args = args;
+
+    /* calculate buffer requirements and grow if needed */
+    while ( (str = va_arg(args, char*)) != NULL ) {
+        new_len += strlen(str);
+    }
+    new_len += 1; /* terminating null */
+    retval = grow_buf(buf, buf_len, new_len);
+
+    args = save_args;
+
+    while ( (str = va_arg(args, char*)) != NULL ) {
+        strcat(*buf, str);
+    }
+
+    return retval;
+}
+
+size_t
+chaz_Util_join_strings(char **buf, size_t buf_len, ...)
+{
+    va_list args;
+    size_t retval;
+
+    va_start(args, buf_len);
+    retval = vjoin_strings(buf, buf_len, args);
+    va_end(args);
+
+    return retval;
+}
+
+size_t
+chaz_Util_vjoin_strings(char **buf, size_t buf_len, va_list args)
+{
+    if (*buf != NULL)
+        (*buf)[0] = '\0';
+
+    return vappend_strings(buf, buf_len, args);
 }
 
 /**

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.harm?view=diff&rev=467734&r1=467733&r2=467734
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.harm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Util.harm Wed Oct 25 12:01:00 2006
@@ -56,6 +56,29 @@
 size_t
 chaz_Util_grow_buf(char **buf_ptr, size_t old_len, size_t new_len);
 
+/* Concatenate each string in a null-terminated list onto the end of [buf].
+ * Grow [buf] first if necessary and return its size.
+ */
+size_t
+chaz_Util_append_strings(char **buf, size_t buf_len, ...);
+
+/* Varargs version of append_str
+ */
+size_t
+chaz_Util_vappend_strings(char **buf, size_t buf_len, va_list args);
+
+/* Replace the contents of [buf] (if any) with a string formed by
+ * concatenating all the char* arguments in a null-terminated list.
+ * Grow [buf] first if necessary and return its size.
+ */
+size_t
+chaz_Util_join_strings(char **buf, size_t buf_len, ...);
+
+/* Varargs version of join_strings.
+ */
+size_t
+chaz_Util_vjoin_strings(char **buf, size_t buf_len, va_list args);
+
 #ifdef CHAZ_USE_SHORT_NAMES
 # define verbosity              chaz_Util_verbosity 
 # define write_file             chaz_Util_write_file 
@@ -66,6 +89,10 @@
 # define remove_and_verify      chaz_Util_remove_and_verify 
 # define can_open_file          chaz_Util_can_open_file 
 # define grow_buf               chaz_Util_grow_buf
+# define append_strings         chaz_Util_append_strings
+# define vappend_strings        chaz_Util_vappend_strings
+# define join_strings           chaz_Util_join_strings
+# define vjoin_strings          chaz_Util_vjoin_strings
 #endif
 
 #endif /* H_CHAZ_UTIL */



Mime
View raw message