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

lance.lmwang at gmail.com lance.lmwang at gmail.com
Mon Jan 13 15:44:50 EET 2020


From: Limin Wang <lance.lmwang at 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 at gmail.com>
---
 libavutil/avstring.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

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;
 }
 
-- 
2.9.5



More information about the ffmpeg-devel mailing list