diff mbox series

[FFmpeg-devel,v1,2/3] avutil/avstring: add support for '\\' seperator for the path append

Message ID 20200113134451.9511-2-lance.lmwang@gmail.com
State New
Headers show
Series [FFmpeg-devel,v1,1/3] avutil/avstring: Fix warning: ISO C90 forbids mixed declarations and code
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Limin Wang Jan. 13, 2020, 1:44 p.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

I don't have a windows develoment system yet, so I had to test with modified
avutil/tests/avstring.c like below,
(change / to \\) and change HAVE_DOS_PATHS to 1 in config.h:
-    TEST_APPEND_PATH_COMPONENT("path", "/", "path");
-    TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp");
-    TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp");
-    TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
-    TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
-    TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
+    TEST_APPEND_PATH_COMPONENT("path", "\\", "path");
+    TEST_APPEND_PATH_COMPONENT("path", "comp", "path\\comp");
+    TEST_APPEND_PATH_COMPONENT("path\\", "comp", "path\\comp");
+    TEST_APPEND_PATH_COMPONENT("path", "\\comp", "path\\comp");
+    TEST_APPEND_PATH_COMPONENT("path\\", "\\comp", "path\\comp");
+    TEST_APPEND_PATH_COMPONENT("path\\path2\\", "\\comp\\comp2", "path\\path2\\comp\\comp2");

Then do test with fate-avstring for valid checking:

make fate-avstring SAMPLES=../fate-suite
master:
-path/comp = path/comp
-path/comp = path/comp
-path/comp = path/comp
-path/comp = path/comp
-path/path2/comp/comp2 = path/path2/comp/comp2
+path/comp = path\comp
+path\/comp = path\comp
+path/\comp = path\comp
+path\/\comp = path\comp
+path\path2\/\comp\comp2 = path\path2\comp\comp2

Applied the patch:
-path/comp = path/comp
-path/comp = path/comp
-path/comp = path/comp
-path/comp = path/comp
-path/path2/comp/comp2 = path/path2/comp/comp2
+path\comp = path\comp
+path\comp = path\comp
+path\comp = path\comp
+path\comp = path\comp
+path\path2\comp\comp2 = path\path2\comp\comp2

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavutil/avstring.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/avstring.c b/libavutil/avstring.c
index f4b8ed2..24bcc7d 100644
--- a/libavutil/avstring.c
+++ b/libavutil/avstring.c
@@ -299,14 +299,20 @@  const char *av_dirname(char *path)
     return path;
 }
 
+#if HAVE_DOS_PATHS
+#define SEPARATOR '\\'
+#else
+#define SEPARATOR '/'
+#endif
+
 char *av_append_path_component(const char *path, const char *component)
 {
     size_t p_len, c_len;
     char *fullpath;
 
-    if (!path)
+    if (!path || strlen(path) == 0 )
         return av_strdup(component);
-    if (!component)
+    if (!component || strlen(component) == 0)
         return av_strdup(path);
 
     p_len = strlen(path);
@@ -315,18 +321,16 @@  char *av_append_path_component(const char *path, const char *component)
         return NULL;
     fullpath = av_malloc(p_len + c_len + 2);
     if (fullpath) {
-        if (p_len) {
-            av_strlcpy(fullpath, path, p_len + 1);
-            if (c_len) {
-                if (fullpath[p_len - 1] != '/' && component[0] != '/')
-                    fullpath[p_len++] = '/';
-                else if (fullpath[p_len - 1] == '/' && component[0] == '/')
-                    p_len--;
-            }
-        }
-        av_strlcpy(&fullpath[p_len], component, c_len + 1);
-        fullpath[p_len + c_len] = 0;
+        const char *component1 = component;
+
+        av_strlcpy(fullpath, path, p_len + 1);
+        if (fullpath[p_len - 1] != SEPARATOR)
+            fullpath[p_len++] = SEPARATOR;
+        if (*component1 == SEPARATOR)
+            component1++;
+        av_strlcpy(fullpath + p_len, component1, strlen(component1) + 1);
     }
+
     return fullpath;
 }