[FFmpeg-devel] [PATCH 4/6] lavu/avstring: add av_append_path_component() funcion
wm4
nfxjfg at googlemail.com
Sat Jul 5 18:52:00 CEST 2014
On Sat, 5 Jul 2014 18:12:02 +0200
Lukasz Marek <lukasz.m.luki2 at gmail.com> wrote:
> TODO: bump minor, update doc/APIchanges
>
> Convinient function to build paths.
>
> Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
> ---
> libavutil/avstring.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> libavutil/avstring.h | 10 ++++++++++
> 2 files changed, 53 insertions(+)
>
> diff --git a/libavutil/avstring.c b/libavutil/avstring.c
> index e75cdc6..d7060ab 100644
> --- a/libavutil/avstring.c
> +++ b/libavutil/avstring.c
> @@ -268,6 +268,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);
> + if (fullpath) {
> + if (p_len) {
> + strcpy(fullpath, path);
> + if (c_len) {
> + if (fullpath[p_len - 1] != '/' && component[0] != '/')
> + fullpath[p_len++] = '/';
> + else if (fullpath[p_len - 1] == '/' && component[0] == '/')
What about MS Windows paths?
> + p_len--;
> + }
> + }
> + strcpy(&fullpath[p_len], component);
> + fullpath[p_len + c_len] = 0;
Maybe use snprintf() instead of the utterly broken strcpy function?
(Didn't really look at the rest of the logic.)
> + }
> + return fullpath;
> +}
> +
> int av_escape(char **dst, const char *src, const char *special_chars,
> enum AVEscapeMode mode, int flags)
> {
> @@ -376,6 +405,7 @@ end:
> int main(void)
> {
> int i;
> + char *fullpath;
> static const char * const strings[] = {
> "''",
> "",
> @@ -416,6 +446,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 de2f71d..0a91dbf 100644
> --- a/libavutil/avstring.h
> +++ b/libavutil/avstring.h
> @@ -268,6 +268,16 @@ const char *av_basename(const char *path);
> */
> const char *av_dirname(char *path);
>
> +/**
> + * 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);
IMO the name is awkward. Maybe av_path_join()?
Did you have a look at Python's os.path.join()? IMO it has pretty good
and well documented semantics, which you could steal.
> +
> enum AVEscapeMode {
> AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.
> AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
More information about the ffmpeg-devel
mailing list