corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmke...@apache.org
Subject [3/3] incubator-corinthia git commit: DFAddDirContents: Don't use DFArray or DFError
Date Sat, 20 Dec 2014 06:29:59 GMT
DFAddDirContents: Don't use DFArray or DFError

Avoid using DFArray in DFAddDirContents, as a step towards removing the
dependency of the platform module upon core. We instead use the
DFDirEntryList type, which is simply a linked list of string values
which is manipulated manually, without requiring any convenience
functions defined in core.

This function also no longer takes a DFError object as a paramater,
though others in DFPlatform.h still do.


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/614eabac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/614eabac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/614eabac

Branch: refs/heads/stable
Commit: 614eabac2e4bb8e82a3c16bfa6ece2eb45434989
Parents: 248faa5
Author: Peter Kelly <peter@uxproductivity.com>
Authored: Sat Dec 20 13:26:02 2014 +0700
Committer: Peter Kelly <peter@uxproductivity.com>
Committed: Sat Dec 20 13:26:02 2014 +0700

----------------------------------------------------------------------
 DocFormats/core/src/lib/DFFilesystem.c   | 34 +++++++++++++++++++---
 DocFormats/platform/headers/DFPlatform.h |  9 +++++-
 DocFormats/platform/src/Unix.c           | 23 +++++++++++----
 DocFormats/platform/src/Win32.c          | 42 +++++++++++++++++++++++----
 consumers/dfutil/src/main.c              |  2 ++
 5 files changed, 95 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/614eabac/DocFormats/core/src/lib/DFFilesystem.c
----------------------------------------------------------------------
diff --git a/DocFormats/core/src/lib/DFFilesystem.c b/DocFormats/core/src/lib/DFFilesystem.c
index 24afc0c..b5da8ec 100644
--- a/DocFormats/core/src/lib/DFFilesystem.c
+++ b/DocFormats/core/src/lib/DFFilesystem.c
@@ -160,15 +160,41 @@ int DFDeleteFile(const char *path, DFError **error)
     return ok;
 }
 
-const char **DFContentsOfDirectory(const char *path, int recursive, DFError **error)
+static void freeDirEntryList(DFDirEntryList *list)
+{
+    DFDirEntryList *next;
+    for (DFDirEntryList *l = list; l != NULL; l = next) {
+        next = l->next;
+        free(l->name);
+        free(l);
+    }
+}
+
+static DFArray *arrayFromDirEntryList(DFDirEntryList *list)
 {
-    const char **entries = NULL;
     DFArray *array = DFArrayNew((DFCopyFunction)strdup,(DFFreeFunction)free);
+    for (DFDirEntryList *l = list; l != NULL; l = l->next)
+        DFArrayAppend(array,l->name);
+    return array;
+}
 
-    if (DFAddDirContents(path,"",recursive,array,error))
-        entries = DFStringArrayFlatten(array);
+const char **DFContentsOfDirectory(const char *path, int recursive, DFError **error)
+{
+    DFDirEntryList *list = NULL;
+    DFDirEntryList **listptr = &list;
+    char *errmsg = NULL;
+
+    if (!DFAddDirContents(path,"",recursive,&listptr,&errmsg)) {
+        DFErrorFormat(error,"%s",errmsg);
+        free(errmsg);
+        freeDirEntryList(list);
+        return NULL;
+    }
 
+    DFArray *array = arrayFromDirEntryList(list);
+    const char **entries = DFStringArrayFlatten(array);
     DFArrayRelease(array);
+    freeDirEntryList(list);
     return entries;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/614eabac/DocFormats/platform/headers/DFPlatform.h
----------------------------------------------------------------------
diff --git a/DocFormats/platform/headers/DFPlatform.h b/DocFormats/platform/headers/DFPlatform.h
index 0719c33..8f858cc 100755
--- a/DocFormats/platform/headers/DFPlatform.h
+++ b/DocFormats/platform/headers/DFPlatform.h
@@ -19,8 +19,15 @@
 #include "DFArray.h"
 #include <stddef.h>
 
+typedef struct DFDirEntryList DFDirEntryList;
+
+struct DFDirEntryList {
+    char *name;
+    DFDirEntryList *next;
+};
+
 int DFMkdirIfAbsent(const char *path, DFError **error);
-int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFArray *array,
DFError **error);
+int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFDirEntryList
***list, char **errmsg);
 int DFGetImageDimensions(const void *data, size_t len, const char *ext,
                          unsigned int *width, unsigned int *height, DFError **error);
 

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/614eabac/DocFormats/platform/src/Unix.c
----------------------------------------------------------------------
diff --git a/DocFormats/platform/src/Unix.c b/DocFormats/platform/src/Unix.c
index e4ad42f..27c7019 100644
--- a/DocFormats/platform/src/Unix.c
+++ b/DocFormats/platform/src/Unix.c
@@ -19,6 +19,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 #include <sys/stat.h>
 
 // This file contains functions that are applicable to all Unix-based platforms, including
Linux, iOS, and OS X
@@ -53,11 +54,17 @@ int DFMkdirIfAbsent(const char *path, DFError **error)
     return 1;
 }
 
-int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFArray *array,
DFError **error)
+int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFDirEntryList
***list, char **errmsg)
 {
+    DFDirEntryList **listptr = *list;
+
     DIR *dir = opendir(absPath);
     if (dir == NULL) {
-        DFErrorFormat(error,"%s: %s",relPath,strerror(errno));
+        if (errmsg != NULL) {
+            char temp[1024];
+            snprintf(temp,1024,"%s: %s",relPath,strerror(errno));
+            *errmsg = strdup(temp);
+        }
         return 0;
     }
 
@@ -71,18 +78,24 @@ int DFAddDirContents(const char *absPath, const char *relPath, int recursive,
DF
         char *absSubPath = DFAppendPathComponent(absPath,result->d_name);
         char *relSubPath = DFAppendPathComponent(relPath,result->d_name);
 
+        char *entryName;
         if (relSubPath[0] == '/')
-            DFArrayAppend(array,&relSubPath[1]);
+            entryName = &relSubPath[1];
         else
-            DFArrayAppend(array,relSubPath);
+            entryName = relSubPath;
+
+        *listptr = (DFDirEntryList *)calloc(1,sizeof(DFDirEntryList));
+        (*listptr)->name = strdup(entryName);
+        listptr = &(*listptr)->next;
 
         if (recursive && DFIsDirectory(absSubPath))
-            ok = DFAddDirContents(absSubPath,relSubPath,recursive,array,error);
+            ok = DFAddDirContents(absSubPath,relSubPath,recursive,&listptr,errmsg);
 
         free(absSubPath);
         free(relSubPath);
     }
     closedir(dir);
+    *list = listptr;
     return ok;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/614eabac/DocFormats/platform/src/Win32.c
----------------------------------------------------------------------
diff --git a/DocFormats/platform/src/Win32.c b/DocFormats/platform/src/Win32.c
index 6e680ff..9aeda88 100755
--- a/DocFormats/platform/src/Win32.c
+++ b/DocFormats/platform/src/Win32.c
@@ -47,6 +47,31 @@ void DFErrorSetWin32(DFError **error, DWORD code)
     LocalFree(lpMsgBuf);
 }
 
+void DFErrorMsgSetWin32(char **errmsg, DWORD code)
+{
+    if (errmsg == NULL)
+        return;
+
+    char *lpMsgBuf;
+    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                  FORMAT_MESSAGE_FROM_SYSTEM |
+                  FORMAT_MESSAGE_IGNORE_INSERTS,
+                  NULL,
+                  code,
+                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                  (LPTSTR)&lpMsgBuf,
+                  0, NULL);
+    size_t len = strlen(lpMsgBuf);
+    while ((len > 0) &&
+           ((lpMsgBuf[len - 1] == '\n') ||
+            (lpMsgBuf[len - 1] == '\r') ||
+            (lpMsgBuf[len - 1] == '.')))
+        len--;
+    lpMsgBuf[len] = '\0';
+    *errmsg = strdup(lpMsgBuf);
+    LocalFree(lpMsgBuf);
+}
+
 HANDLE onceMutex = NULL;
 
 static int testAndSet(int *var,int value,HANDLE lock)
@@ -81,14 +106,15 @@ int DFMkdirIfAbsent(const char *path,DFError **error)
     return 1;
 }
 
-int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFArray *array,
DFError **error)
+int DFAddDirContents(const char *absPath, const char *relPath, int recursive, DFDirEntryList
***list, char **errmsg)
 {
+    DFDirEntryList **listptr = *list;
     WIN32_FIND_DATA ffd;
     HANDLE hFind = INVALID_HANDLE_VALUE;
     char *pattern = DFFormatString("%s/*",absPath);
     hFind = FindFirstFile(pattern,&ffd);
     if (hFind == INVALID_HANDLE_VALUE) {
-        DFErrorSetWin32(error,GetLastError());
+        DFErrorMsgSetWin32(errmsg,GetLastError());
         free(pattern);
         return 0;
     }
@@ -101,13 +127,18 @@ int DFAddDirContents(const char *absPath, const char *relPath, int recursive,
DF
         char *absSubPath = DFAppendPathComponent(absPath,ffd.cFileName);
         char *relSubPath = DFAppendPathComponent(relPath,ffd.cFileName);
 
+        char *entryName;
         if (relSubPath[0] == '/')
-            DFArrayAppend(array,&relSubPath[1]);
+            entryName = &relSubPath[1];
         else
-            DFArrayAppend(array,relSubPath);
+            entryName = relSubPath;
+
+        *listptr = (DFDirEntryList *)calloc(1,sizeof(DFDirEntryList));
+        (*listptr)->name = strdup(entryName);
+        listptr = &(*listptr)->next;
 
         if (recursive && (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
-            ok = DFAddDirContents(absSubPath,relSubPath,recursive,array,error);
+            ok = DFAddDirContents(absSubPath,relSubPath,recursive,&listptr,errmsg);
 
         free(absSubPath);
         free(relSubPath);
@@ -115,6 +146,7 @@ int DFAddDirContents(const char *absPath, const char *relPath, int recursive,
DF
 
     FindClose(hFind);
     free(pattern);
+    *list = listptr;
     return ok;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/614eabac/consumers/dfutil/src/main.c
----------------------------------------------------------------------
diff --git a/consumers/dfutil/src/main.c b/consumers/dfutil/src/main.c
index 5348cc0..21b95a2 100644
--- a/consumers/dfutil/src/main.c
+++ b/consumers/dfutil/src/main.c
@@ -21,9 +21,11 @@
 #include "StringTests.h"
 #include "DFZipFile.h"
 #include "DFCommon.h"
+#include "DFFilesystem.h"
 #include <DocFormats/DocFormats.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 
 static int runCommand(int argc, const char **argv, DFError **dferr)
 {


Mime
View raw message