Revision: 48453
          http://brlcad.svn.sourceforge.net/brlcad/?rev=48453&view=rev
Author:   brlcad
Date:     2012-01-11 23:51:39 +0000 (Wed, 11 Jan 2012)
Log Message:
-----------
refactor bu_list_path() and bu_count_path() into a new bu_dir_list() function 
that performs both.  returns the count now and optionally takes a pattern 
(instead of a suffix) and conditionally handles all of the memory allocation 
for the caller automatically.  fixed a memory leak and off-by-one bug.  
documented the new function and one caller in nirt accordingly.  initiated in 
response to RESOURCE_LEAK reported by coverity (cov cid 478).

Modified Paths:
--------------
    brlcad/trunk/NEWS
    brlcad/trunk/TODO
    brlcad/trunk/doc/deprecation.txt
    brlcad/trunk/include/bu.h
    brlcad/trunk/src/conv/intaval/Makefile.am
    brlcad/trunk/src/conv/intaval/tgf-g.cpp
    brlcad/trunk/src/libbu/Makefile.am
    brlcad/trunk/src/libbu/dirent.c
    brlcad/trunk/src/nirt/nirt.c

Modified: brlcad/trunk/NEWS
===================================================================
--- brlcad/trunk/NEWS   2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/NEWS   2012-01-11 23:51:39 UTC (rev 48453)
@@ -13,6 +13,7 @@
 --- 2011-12-XX  Release 7.21.X                                     ---
 ----------------------------------------------------------------------
 
+* fixed nirt memory leak and scriptfile off-by-one bug - Sean Morrison
 * d2-c now handles the imaginary part correctly - Erik Greenwald
 * fixed rt* crash when output file cannot be created - Erik Greenwald
 * fixed mged bot editor inoperative bug (3392650) - Tom Browder

Modified: brlcad/trunk/TODO
===================================================================
--- brlcad/trunk/TODO   2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/TODO   2012-01-11 23:51:39 UTC (rev 48453)
@@ -18,6 +18,8 @@
 THESE TASKS SHOULD HAPPEN BEFORE THE NEXT RELEASE
 -------------------------------------------------
 
+* test nirt template file loading
+
 * g-nff is crashing, fix it; g-dot outputs garbage
 
 * get rtweight to use more robust density file parsing

Modified: brlcad/trunk/doc/deprecation.txt
===================================================================
--- brlcad/trunk/doc/deprecation.txt    2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/doc/deprecation.txt    2012-01-11 23:51:39 UTC (rev 48453)
@@ -837,3 +837,6 @@
         renamed hook functions to be consistent with other libbu API [7.21]
 s/bu_file_exists\(([^)]*)\)/bu_file_exists(\1, NULL)/g
        new file descriptor parameter added [7.21]
+s/bu_list_path\(([^,]*),([^,]*),[[:space:]]([^)]*)\)/bu_dir_list(\1,\2, 
&(\3))/g
+s/bu_count_path(.*)\);/bu_dir_list\1, NULL);/g
+       made bu_dir_list() return the number of entries [7.21

Modified: brlcad/trunk/include/bu.h
===================================================================
--- brlcad/trunk/include/bu.h   2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/include/bu.h   2012-01-11 23:51:39 UTC (rev 48453)
@@ -2984,7 +2984,7 @@
 
 /**
  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
- * Compares a filename or pathname to a pattern.
+ * Compares a string filename or pathname to a pattern.
  *
  * Returns 0 if a match is found or BU_FNMATCH_NOMATCH otherwise.
  *
@@ -2999,16 +2999,17 @@
  */
 
 /**
- * Count number of files in directory whose type matches substr
+ * Returns the number of directory entries for a given path matching
+ * an optional glob pattern.  If the caller provides a pointer to an
+ * argv-style 'files' array, this function will allocate the array
+ * with dynamically allocated strings for any matching file(s).
+ *
+ * It is the caller's responsibility to free a non-NULL 'files' array
+ * with bu_free_argv().
  */
-BU_EXPORT extern int bu_count_path(char *path, char *substr);
+BU_EXPORT extern size_t bu_dir_list(const char *path, const char *pattern, 
char ***files);
 
-/**
- * Return array with filenames with suffix matching substr
- */
-BU_EXPORT extern void bu_list_path(char *path, char *substr, char **filearray);
 
-
 /** @file libbu/brlcad_path.c
  *
  * @brief

Modified: brlcad/trunk/src/conv/intaval/Makefile.am
===================================================================
--- brlcad/trunk/src/conv/intaval/Makefile.am   2012-01-11 23:36:03 UTC (rev 
48452)
+++ brlcad/trunk/src/conv/intaval/Makefile.am   2012-01-11 23:51:39 UTC (rev 
48453)
@@ -9,7 +9,7 @@
        tgf-g.cpp \
        write_brl.cpp
 
-tgf_g_LDADD = ${WDB} ${WDB_LIBS}
+tgf_g_LDADD = ${WDB} ${WDB_LIBS} ${BU} ${BU_LIBS}
 
 noinst_HEADERS = glob.h \
        read_dra.h \

Modified: brlcad/trunk/src/conv/intaval/tgf-g.cpp
===================================================================
--- brlcad/trunk/src/conv/intaval/tgf-g.cpp     2012-01-11 23:36:03 UTC (rev 
48452)
+++ brlcad/trunk/src/conv/intaval/tgf-g.cpp     2012-01-11 23:51:39 UTC (rev 
48453)
@@ -30,6 +30,8 @@
 #include <iostream>
 #include <fstream>
 
+#include "bu.h"
+
 #include "regtab.h"
 #include "read_dra.h"
 

Modified: brlcad/trunk/src/libbu/Makefile.am
===================================================================
--- brlcad/trunk/src/libbu/Makefile.am  2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/src/libbu/Makefile.am  2012-01-11 23:51:39 UTC (rev 48453)
@@ -27,6 +27,7 @@
        color.c \
        convert.c \
        crashreport.c \
+       ctype.c \
        dirent.c \
        dirname.c \
        dlfcn.c \

Modified: brlcad/trunk/src/libbu/dirent.c
===================================================================
--- brlcad/trunk/src/libbu/dirent.c     2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/src/libbu/dirent.c     2012-01-11 23:51:39 UTC (rev 48453)
@@ -29,44 +29,49 @@
 #include "uce-dirent.h"
 
 
-int
-bu_count_path(char *path, char *substr)
+size_t
+bu_dir_list(const char *path, const char *pattern, char ***files)
 {
-    int filecount = 0;
-    DIR *dir = opendir(path);
-    struct dirent *dp;
+    size_t i = 0;
+    size_t filecount = 0;
+    DIR *dir = NULL;
+    struct dirent *dp = NULL;
+
+    /* calculate file cound */
+    dir = opendir(path);
     while ((dp = readdir(dir)) != NULL) {
-       if (strlen(substr) == 0) {
+       if (!pattern
+           || (strlen(pattern) == 0)
+           || (bu_fnmatch(pattern, dp->d_name, 0) == 0))
+       {
            filecount++;
-       } else {
-           if (BU_STR_EQUAL(dp->d_name+(strlen(dp->d_name)-strlen(substr)), 
substr)) {
-               filecount++;
-           }
        }
     }
-    closedir(dir);
-    return filecount;
-}
+    (void)closedir(dir);
 
-void
-bu_list_path(char *path, char *substr, char **filearray)
-{
-    int filecount = -1;
-    DIR *dir = opendir(path);
-    struct dirent *dp;
+    /* bail now if there's no files array pointer to fill in */
+    if (!files) {
+       return filecount;
+    }
+
+    /* allocate enough space plus room for a null entry too */
+    *files = (char **)bu_calloc(filecount+1, sizeof(char *), "files alloc");
+
+    dir = opendir(path);
     while ((dp = readdir(dir)) != NULL) {
-       if (strlen(substr) == 0) {
-           filecount++;
-           filearray[filecount]=dp->d_name;
-       } else {
-           if (BU_STR_EQUAL(dp->d_name+(strlen(dp->d_name)-strlen(substr)), 
substr)) {
-               filecount++;
-               filearray[filecount]=dp->d_name;
-           }
+       if (!pattern
+           || (strlen(pattern) == 0)
+           || (bu_fnmatch(pattern, dp->d_name, 0) == 0))
+       {
+           (*files)[i++] = bu_strdup(dp->d_name);
        }
     }
+    (void)closedir(dir);
+
+    return filecount;
 }
 
+
 /*
  * Local Variables:
  * mode: C

Modified: brlcad/trunk/src/nirt/nirt.c
===================================================================
--- brlcad/trunk/src/nirt/nirt.c        2012-01-11 23:36:03 UTC (rev 48452)
+++ brlcad/trunk/src/nirt/nirt.c        2012-01-11 23:51:39 UTC (rev 48453)
@@ -123,26 +123,24 @@
 /**
  * List formats installed in global nirt data directory
  */
-void listformats(void)
+void
+listformats(void)
 {
-    int files, i;
-    char **filearray;
-    struct bu_vls nirtfilespath, nirtpathtofile, vlsfileline;
-    char suffix[5]=".nrt";
-    FILE *cfPtr;
+    size_t files, i;
+    char **filearray = NULL;
+    char suffix[6]="*.nrt";
+    FILE *cfPtr = NULL;
     int fnddesc;
 
-    bu_vls_init(&vlsfileline);
-    bu_vls_init(&nirtfilespath);
-    bu_vls_init(&nirtpathtofile);
+    struct bu_vls nirtfilespath = BU_VLS_INIT_ZERO;
+    struct bu_vls nirtpathtofile = BU_VLS_INIT_ZERO;
+    struct bu_vls vlsfileline = BU_VLS_INIT_ZERO;
+
+    /* get a nirt directory listing */
     bu_vls_printf(&nirtfilespath, "%s", bu_brlcad_data("nirt", 0));
+    files = bu_dir_list(bu_vls_addr(&nirtfilespath), suffix, &filearray);
 
-    files = bu_count_path(bu_vls_addr(&nirtfilespath), suffix);
-
-    filearray = (char **)bu_malloc(files*sizeof(char *), "filelist");
-
-    bu_list_path(bu_vls_addr(&nirtfilespath), suffix, filearray);
-
+    /* open every nirt file we find and extract the description */
     for (i = 0; i < files; i++) {
        bu_vls_trunc(&nirtpathtofile, 0);
        bu_vls_trunc(&vlsfileline, 0);
@@ -159,7 +157,8 @@
        fclose(cfPtr);
     }
 
-    bu_free(filearray, "filelist");
+    /* release resources */
+    bu_free_argv(files, filearray);
     bu_vls_free(&vlsfileline);
     bu_vls_free(&nirtfilespath);
     bu_vls_free(&nirtpathtofile);

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual 
desktops for less than the cost of PCs and save 60% on VDI infrastructure 
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to