r15403 - gnucash/trunk - Use g_dir_{open,read_name,close}.

Andreas Köhler andi5 at cvs.gnucash.org
Sat Jan 20 13:37:39 EST 2007


Author: andi5
Date: 2007-01-20 13:37:35 -0500 (Sat, 20 Jan 2007)
New Revision: 15403
Trac: http://svn.gnucash.org/trac/changeset/15403

Modified:
   gnucash/trunk/lib/libqof/qof/guid.c
   gnucash/trunk/src/backend/file/gnc-backend-file.c
   gnucash/trunk/src/backend/file/io-example-account.c
   gnucash/trunk/src/backend/file/test/test-load-example-account.c
   gnucash/trunk/src/backend/file/test/test-load-xml2.c
   gnucash/trunk/src/backend/file/test/test-save-in-lang.c
   gnucash/trunk/src/gnc-module/gnc-module.c
Log:
Use g_dir_{open,read_name,close}.

Replace opendir, readdir and closedir by their GLib wrappers so that
files on Windows will be retrieved with the wide character api.


Modified: gnucash/trunk/lib/libqof/qof/guid.c
===================================================================
--- gnucash/trunk/lib/libqof/qof/guid.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/lib/libqof/qof/guid.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -235,16 +235,16 @@
 init_from_dir(const char *dirname, unsigned int max_files)
 {
   char filename[1024];
-  struct dirent *de;
+  const gchar *de;
   struct stat stats;
   size_t total;
   int result;
-  DIR *dir;
+  GDir *dir;
 
   if (max_files <= 0)
     return 0;
 
-  dir = opendir (dirname);
+  dir = g_dir_open(dirname, 0, NULL);
   if (dir == NULL)
     return 0;
 
@@ -252,15 +252,15 @@
 
   do
   {
-    de = readdir(dir);
+    de = g_dir_read_name(dir);
     if (de == NULL)
       break;
 
-    md5_process_bytes(de->d_name, strlen(de->d_name), &guid_context);
-    total += strlen(de->d_name);
+    md5_process_bytes(de, strlen(de), &guid_context);
+    total += strlen(de);
 
     result = snprintf(filename, sizeof(filename),
-                      "%s/%s", dirname, de->d_name);
+                      "%s/%s", dirname, de);
     if ((result < 0) || (result >= (int)sizeof(filename)))
       continue;
 
@@ -273,7 +273,7 @@
     max_files--;
   } while (max_files > 0);
 
-  closedir(dir);
+  g_dir_close(dir);
 
   return total;
 }

Modified: gnucash/trunk/src/backend/file/gnc-backend-file.c
===================================================================
--- gnucash/trunk/src/backend/file/gnc-backend-file.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/backend/file/gnc-backend-file.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -642,23 +642,18 @@
 /* ================================================================= */
 
 static int
-gnc_file_be_select_files (const struct dirent *d)
+gnc_file_be_select_files (const gchar *d)
 {
-    int len = strlen(d->d_name) - 4;
-
-    if (len <= 0)
-        return(0);
-  
-    return((strcmp(d->d_name + len, ".LNK") == 0) ||
-           (strcmp(d->d_name + len, ".xac") == 0) ||
-           (strcmp(d->d_name + len, ".log") == 0));
+    return (g_str_has_suffix(d, ".LNK") ||
+            g_str_has_suffix(d, ".xac") ||
+            g_str_has_suffix(d, ".log"));
 }
 
 static void
 gnc_file_be_remove_old_files(FileBackend *be)
 {
-    struct dirent *dent;
-    DIR *dir;
+    const gchar *dent;
+    GDir *dir;
     struct stat lockstatbuf, statbuf;
     int pathlen;
     time_t now;
@@ -684,20 +679,20 @@
      * directory and then one pass over the 'matching' files. --
      * warlord at MIT.EDU 2002-05-06
      */
-    
-    dir = opendir (be->dirname);
+
+    dir = g_dir_open (be->dirname, 0, NULL);
     if (!dir)
         return;
 
     now = time(NULL);
-    while((dent = readdir(dir)) != NULL) {
+    while((dent = g_dir_read_name(dir)) != NULL) {
         char *name;
         int len;
 
         if (gnc_file_be_select_files (dent) == 0)
              continue;
 
-        name = g_build_filename(be->dirname, dent->d_name, (char*)NULL);
+        name = g_build_filename(be->dirname, dent, (gchar*)NULL);
         len = strlen(name) - 4;
 
         /* Is this file associated with the current data file */
@@ -743,7 +738,7 @@
         }
         g_free(name);
     }
-    closedir (dir);
+    g_dir_close (dir);
 }
 
 static void

Modified: gnucash/trunk/src/backend/file/io-example-account.c
===================================================================
--- gnucash/trunk/src/backend/file/io-example-account.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/backend/file/io-example-account.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -423,24 +423,14 @@
     g_slist_free(list);
 }
 
-static gboolean
-is_directory(const gchar *filename)
-{
-    struct stat fileinfo;
-
-    stat(filename, &fileinfo);
-
-    return S_ISDIR(fileinfo.st_mode);
-}
-    
 GSList*
 gnc_load_example_account_list(QofBook *book, const char *dirname)
 {
     GSList *ret;
-    DIR *dir;
-    struct dirent *direntry;
+    GDir *dir;
+    const gchar *direntry;
 
-    dir = opendir(dirname);
+    dir = g_dir_open(dirname, 0, NULL);
 
     if(dir == NULL)
     {
@@ -449,13 +439,14 @@
     
     ret = NULL;
 
-    for(direntry = readdir(dir); direntry != NULL; direntry = readdir(dir))
+    for(direntry = g_dir_read_name(dir); direntry != NULL;
+        direntry = g_dir_read_name(dir))
     {
         gchar *filename;
         GncExampleAccount *gea;
-        filename = g_strdup_printf("%s/%s", dirname, direntry->d_name);
+        filename = g_build_filename(dirname, direntry, (gchar*) NULL);
 
-        if(!is_directory(filename))
+        if(!g_file_test(filename, G_FILE_TEST_IS_DIR))
         {
             gea = gnc_read_example_account(book, filename);
 
@@ -463,14 +454,16 @@
             {
                 g_free(filename);
                 gnc_free_example_account_list(ret);
+                g_dir_close(dir);
                 return NULL;
             }
-            
+
             ret = g_slist_append(ret, gea);
         }
-        
+
         g_free(filename);
     }
+    g_dir_close(dir);
 
     return ret;
 }

Modified: gnucash/trunk/src/backend/file/test/test-load-example-account.c
===================================================================
--- gnucash/trunk/src/backend/file/test/test-load-example-account.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/backend/file/test/test-load-example-account.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -68,7 +68,7 @@
 {
     const char *location = getenv("GNC_ACCOUNT_PATH");
     GSList *list = NULL;
-    DIR *ea_dir;
+    GDir *ea_dir;
     QofBook *book;
 
     if (!location)
@@ -81,37 +81,28 @@
 
     book = qof_book_new ();
 
-    if((ea_dir = opendir(location)) == NULL)
+    if((ea_dir = g_dir_open(location, 0, NULL)) == NULL)
     {
         failure("unable to open ea directory");
     }
     else
     {
-        struct dirent *entry;
+        const gchar *entry;
 
-        while((entry = readdir(ea_dir)) != NULL)
+        while((entry = g_dir_read_name(ea_dir)) != NULL)
         {
-            struct stat file_info;
-            if(strstr(entry->d_name, da_ending) != NULL)
+            if(g_str_has_suffix(entry, da_ending))
             {
-                char *to_open = g_strdup_printf("%s/%s", location,
-                                                entry->d_name);
-                if(stat(to_open, &file_info) != 0)
+                gchar *to_open = g_build_filename(location, entry, (gchar*)NULL);
+                if (!g_file_test(to_open, G_FILE_TEST_IS_DIR))
                 {
-                    failure("unable to stat file");
+                    test_load_file(book, to_open);
                 }
-                else
-                {
-                    if(!S_ISDIR(file_info.st_mode))
-                    {
-                        test_load_file(book, to_open);
-                    }
-                }
                 g_free(to_open);
             }
         }
     }
-    closedir(ea_dir);
+    g_dir_close(ea_dir);
     
     {
         list = gnc_load_example_account_list(book, location);

Modified: gnucash/trunk/src/backend/file/test/test-load-xml2.c
===================================================================
--- gnucash/trunk/src/backend/file/test/test-load-xml2.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/backend/file/test/test-load-xml2.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -107,7 +107,7 @@
 main (int argc, char ** argv)
 {
     const char *location = getenv("GNC_TEST_FILES");
-    DIR *xml2_dir;
+    GDir *xml2_dir;
 
     g_type_init();
     qof_init();
@@ -122,38 +122,29 @@
 
     xaccLogDisable();
     
-    if((xml2_dir = opendir(location)) == NULL)
+    if((xml2_dir = g_dir_open(location, 0, NULL)) == NULL)
     {
         failure("unable to open xml2 directory");
     }
     else
     {
-        struct dirent *entry;
+        const gchar *entry;
 
-        while((entry = readdir(xml2_dir)) != NULL)
+        while((entry = g_dir_read_name(xml2_dir)) != NULL)
         {
-            if(strstr(entry->d_name, ".gml2") != NULL)
+            if(g_str_has_suffix(entry, ".gml2"))
             {
-                struct stat file_info;
-                char *to_open = g_strdup_printf("%s/%s", location,
-                                                entry->d_name);
-                if(stat(to_open, &file_info) != 0)
+                gchar *to_open = g_build_filename(location, entry, (gchar*)NULL);
+                if(!g_file_test(to_open, G_FILE_TEST_IS_DIR))
                 {
-                    failure("unable to stat file");
+                    test_load_file(to_open);
                 }
-                else
-                {
-                    if(!S_ISDIR(file_info.st_mode))
-                    {
-                        test_load_file(to_open);
-                    }
-                }
                 g_free(to_open);
             }
         }
     }
 
-    closedir(xml2_dir);
+    g_dir_close(xml2_dir);
 
     print_test_results();
     qof_close();

Modified: gnucash/trunk/src/backend/file/test/test-save-in-lang.c
===================================================================
--- gnucash/trunk/src/backend/file/test/test-save-in-lang.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/backend/file/test/test-save-in-lang.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -134,26 +134,26 @@
 int
 main(int argc, char **argv)
 {
-    DIR *adir;
+    GDir *adir;
 
     gnc_engine_init(argc, argv);
     xaccLogDisable();
     
-    if((adir = opendir(test_dir)) == NULL)
+    if((adir = g_dir_open(test_dir, 0, NULL)) == NULL)
     {
-        failure_args("opendir", __FILE__, __LINE__,
+        failure_args("g_dir_open", __FILE__, __LINE__,
                      "couldn't open dir %s", test_dir);
     }
     else
     {
-        struct dirent *next_file;
+        const gchar *next_file;
 
-        while((next_file = readdir(adir)) != NULL)
+        while((next_file = g_dir_read_name(adir)) != NULL)
         {
             struct stat file_info;
             char* filename;
 
-            filename = g_strdup_printf("%s/%s", test_dir, next_file->d_name);
+            filename = g_strdup_printf("%s/%s", test_dir, next_file);
             
             if(stat(filename, &file_info) != 0)
             {
@@ -187,6 +187,7 @@
 
             g_free(filename);
         }
+        g_dir_close(adir);
     }
     
     print_test_results();

Modified: gnucash/trunk/src/gnc-module/gnc-module.c
===================================================================
--- gnucash/trunk/src/gnc-module/gnc-module.c	2007-01-20 15:27:21 UTC (rev 15402)
+++ gnucash/trunk/src/gnc-module/gnc-module.c	2007-01-20 18:37:35 UTC (rev 15403)
@@ -198,27 +198,27 @@
   /* look in each search directory */
   for(current = search_dirs; current; current = current->next) 
   {
-      DIR *d = opendir(current->data);
-      struct dirent * dent = NULL;
+      GDir *d = g_dir_open(current->data, 0,NULL);
+      const gchar *dent = NULL;
       char * fullpath = NULL;
       GNCModuleInfo * info;
 
       if (!d) continue;
 
-      while ((dent = readdir(d)) != NULL)
+      while ((dent = g_dir_read_name(d)) != NULL)
       {
         /* is the file a loadable module? */
 
         /* Gotcha: On MacOS, G_MODULE_SUFFIX is defined as "so", but if we do
          * not build clean libtool modules with "-module", we get dynamic
          * libraries ending on .dylib */
-        if (g_str_has_suffix(dent->d_name, "." G_MODULE_SUFFIX) ||
-            g_str_has_suffix(dent->d_name, ".dylib"))
+        if (g_str_has_suffix(dent, "." G_MODULE_SUFFIX) ||
+            g_str_has_suffix(dent, ".dylib"))
         {
           /* get the full path name, then dlopen the library and see
            * if it has the appropriate symbols to be a gnc_module */
-          fullpath = g_build_filename((const gchar *)(current->data), 
-				      dent->d_name, (char*)NULL);
+          fullpath = g_build_filename((const gchar *)(current->data),
+                                      dent, (char*)NULL);
           info     = gnc_module_get_info(fullpath);
           
           if(info) 
@@ -228,7 +228,7 @@
           g_free(fullpath);
         }
       }
-      closedir(d);
+      g_dir_close(d);
 
   }
   /* free the search dir strings */



More information about the gnucash-changes mailing list