[Piglit] [PATCH 5/9] util: Add piglit_join_paths()

Jose Fonseca jfonseca at vmware.com
Wed Jul 11 14:42:09 PDT 2012



----- Original Message -----
> This is a variable argument function that joins together paths with
> the
> system path separator (Unix='/', Windows='\\').
> 
> Signed-off-by: Chad Versace <chad.versace at linux.intel.com>
> ---
>  tests/util/piglit-util.c | 51
>  ++++++++++++++++++++++++++++++++++++++++++++++++
>  tests/util/piglit-util.h | 13 ++++++++++++
>  2 files changed, 64 insertions(+)
> 
> diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
> index f01ff5e..07b5b31 100644
> --- a/tests/util/piglit-util.c
> +++ b/tests/util/piglit-util.c
> @@ -347,3 +347,54 @@ piglit_source_dir(void)
>      }
>  
>      return s;
> +}
> +
> +#ifdef _WIN32
> +#  define PIGLIT_PATH_SEP '\\'
> +#else
> +#  define PIGLIT_PATH_SEP '/'
> +#endif
> +
> +size_t
> +piglit_join_paths(char buf[], size_t buf_size, int n, ...)
> +{
> +	char *dest = buf;
> +	size_t size_written = 0;
> +
> +	int i;
> +	va_list va;
> +
> +	if (buf_size  == 0 || n < 1)
> +		return 0;
> +
> +	va_start(va, n);
> +
> +	i = 0;
> +	while (true) {
> +		const char *p = va_arg(va, const char*);
> +
> +		while (*p != 0) {
> +			if (size_written == buf_size - 1)
> +				break;

You should probably return immediately here, otherwise you might set buf[buf_size -1 ] = PIGLIT_PATH_SEP and then write buf[buf_size] = '\0', i.e., overflow.

But provided bug_size is big enough, this should never happen in practice anyway. Otherwise looks good.

Jose

> +
> +			*dest = *p;
> +			++dest;
> +			++p;
> +			++size_written;
> +		}
> +
> +		++i;
> +		if (i == n)
> +			break;
> +
> +		*dest = PIGLIT_PATH_SEP;
> +		++dest;
> +		++size_written;
> +	}
> +
> +	*dest = '\0';
> +	++size_written;
> +
> +	va_end(va);
> +	return size_written;
> +}
> diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
> index b5e6d3a..269a590 100644
> --- a/tests/util/piglit-util.h
> +++ b/tests/util/piglit-util.h
> @@ -135,6 +135,19 @@ char *piglit_load_text_file(const char
> *file_name, unsigned *size);
>  const char*
>  piglit_source_dir(void);
>  
> +/**
> + * \brief Join paths together with the system path separator.
> + *
> + * On Unix, the path separator is '/'. On Windows, '\\'.
> + *
> + * The variable argument consists of \a n null-terminated strings.
>  The
> + * resultant path is written to \a buf.  No more than \a buf_size
> bytes are
> + * written to \a buf. The last byte written is always the null
> character.
> + * Returned is the number of bytes written, including the
> terminating null.
> + */
> +size_t
> +piglit_join_paths(char buf[], size_t buf_size, int n, ...);
> +
>  #ifdef __cplusplus
>  } /* end extern "C" */
>  #endif
> --
> 1.7.11.1
> 
> _______________________________________________
> Piglit mailing list
> Piglit at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/piglit
>  


More information about the Piglit mailing list