On Thu, Mar 26, 2015 at 3:40 PM, Michael Niedermayer <michae...@gmx.at> wrote:
> On Thu, Mar 26, 2015 at 01:25:19AM +0100, Mariusz Szczepańczyk wrote: > > From: Lukasz Marek <lukasz.m.lu...@gmail.com> > > > > Convinient function to build paths. > > --- > > libavutil/avstring.c | 43 +++++++++++++++++++++++++++++++++++++++++++ > > libavutil/avstring.h | 10 ++++++++++ > > tests/ref/fate/avstring | 9 +++++++++ > > 3 files changed, 62 insertions(+) > > > > diff --git a/libavutil/avstring.c b/libavutil/avstring.c > > index 25c65b4..f105aa7 100644 > > --- a/libavutil/avstring.c > > +++ b/libavutil/avstring.c > > @@ -269,6 +269,35 @@ const char *av_dirname(char *path) > > return path; > > } > > > > +char *av_append_path_component(const char *path, const char *component) > > +{ > > + size_t p_len, c_len; > > + char *fullpath; > > + > > + if (!path) > > + return component ? av_strdup(component) : NULL; > > + if (!component) > > + return av_strdup(path); > > + > > + p_len = strlen(path); > > + c_len = strlen(component); > > > + fullpath = malloc(p_len + c_len + 2); > > av_malloc() > > fixed > > + if (fullpath) { > > + if (p_len) { > > > + strcpy(fullpath, path); > > av_strlcpy() is more robust/secure > > fixed > > > + if (c_len) { > > + if (fullpath[p_len - 1] != '/' && component[0] != '/') > > + fullpath[p_len++] = '/'; > > + else if (fullpath[p_len - 1] == '/' && component[0] == > '/') > > + p_len--; > > + } > > + } > > + strcpy(&fullpath[p_len], component); > > av_strlcpy() > fixed Mariusz
From a79c0aceef2d3c9f51973958910bed773462fdd8 Mon Sep 17 00:00:00 2001 From: Lukasz Marek <lukasz.m.lu...@gmail.com> Date: Sat, 5 Jul 2014 18:12:02 +0200 Subject: [PATCH 3/4] lavu/avstring: add av_append_path_component() funcion Convinient function to build paths. --- libavutil/avstring.c | 43 +++++++++++++++++++++++++++++++++++++++++++ libavutil/avstring.h | 10 ++++++++++ tests/ref/fate/avstring | 9 +++++++++ 3 files changed, 62 insertions(+) diff --git a/libavutil/avstring.c b/libavutil/avstring.c index 25c65b4..24bc23a 100644 --- a/libavutil/avstring.c +++ b/libavutil/avstring.c @@ -269,6 +269,35 @@ const char *av_dirname(char *path) return path; } +char *av_append_path_component(const char *path, const char *component) +{ + size_t p_len, c_len; + char *fullpath; + + if (!path) + return component ? av_strdup(component) : NULL; + if (!component) + return av_strdup(path); + + p_len = strlen(path); + c_len = strlen(component); + 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; + } + return fullpath; +} + int av_escape(char **dst, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags) { @@ -427,6 +456,7 @@ int av_match_list(const char *name, const char *list, char separator) int main(void) { int i; + char *fullpath; static const char * const strings[] = { "''", "", @@ -467,6 +497,19 @@ int main(void) av_free(q); } + printf("Testing av_append_path_component()\n"); + #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \ + fullpath = av_append_path_component((path), (component)); \ + printf("%s = %s\n", fullpath, expected); \ + av_free(fullpath); + TEST_APPEND_PATH_COMPONENT(NULL, NULL, "(null)") + TEST_APPEND_PATH_COMPONENT("path", NULL, "path"); + TEST_APPEND_PATH_COMPONENT(NULL, "comp", "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/", "/comp", "path/comp"); + TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2"); return 0; } diff --git a/libavutil/avstring.h b/libavutil/avstring.h index ffb7aa6..466edaf 100644 --- a/libavutil/avstring.h +++ b/libavutil/avstring.h @@ -276,6 +276,16 @@ const char *av_dirname(char *path); */ int av_match_name(const char *name, const char *names); +/** + * Append path component to the existing path. + * Path separator '/' is placed between when needed. + * Resulting string have to be freed with av_free(). + * @param path base path + * @param component component to be appended + * @return new path or NULL on error. + */ +char *av_append_path_component(const char *path, const char *component); + enum AVEscapeMode { AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode. AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping. diff --git a/tests/ref/fate/avstring b/tests/ref/fate/avstring index bc231e8..1ca9be5 100644 --- a/tests/ref/fate/avstring +++ b/tests/ref/fate/avstring @@ -25,3 +25,12 @@ Testing av_get_token() |'foo : \ \ ' : blahblah| -> |foo : \ \ | + |: blahblah| |'\fo\o:': blahblah| -> |\fo\o:| + |: blahblah| |\'fo\o\:': foo ' :blahblah| -> |'foo:: foo | + |:blahblah| +Testing av_append_path_component() +(null) = (null) +path = path +comp = comp +path/comp = path/comp +path/comp = path/comp +path/comp = path/comp +path/comp = path/comp +path/path2/comp/comp2 = path/path2/comp/comp2 -- 2.3.3
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel