存储va_list以便以后在C/C++中使用的最佳方法

res*_*fix 8 c c++ printf variadic-functions

我正在使用va_list来构造一个呈现的字符串.

void Text2D::SetText(const char *szText, ...)
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但现在用户可以在应用程序运行时更改语言.我需要重新生成所有文本字符串,并在初始化后重新缓存文本位图.我想存储va_list并在需要生成文本时使用它.

为了给你更多背景知识,这需要在我正在翻译的关键字符串中包含动态数据的情况下发生.

"Player Score:%d"
Run Code Online (Sandbox Code Playgroud)

这是我需要翻译的关键字符串.我希望保留va_list中提供的数字以供以后使用(在初始化文本的函数范围之外),以便在初始化后需要重新翻译.我希望保留一份va_list的副本,以便与vsnprintf一起使用.

我已经做了一些研究,并找到了一些方法.其中一些我质疑它是否是一种合适的方法(在稳定和便携方面).

Hea*_*utt 9

这个问题确实激起了我的兴趣.此外,我将在自己的工作中遇到类似的问题,所以这里设计的解决方案也可以帮助我.

简而言之,我编写了概念验证代码,用于缓存变量参数供以后使用 - 您可以在下面找到它.

我能够使以下代码在Windows和基于英特尔的Linux上正常工作.我在Linux上用gcc编译,在Windows上用MSVC编译.关于滥用gcc中的va_start()会有两次重复警告 - 您可以在makefile中禁用该警告.

我很想知道这段代码是否适用于Mac编译器.可能需要稍微调整才能编译它.

我意识到这段代码是:

  • 极端滥用由ANSI C标准定义的va_start().
  • 老派字节导向C.
  • 理论上它是不可移植的,它使用va_list变量作为指针.

我对malloc()和free()的使用是非常慎重的,因为va_list宏来自C标准而不是C++特性.我意识到你的问题标题提到了C++,但我试图生成一个完全C兼容的解决方案,而不是使用一些C++风格的注释.

毫无疑问,此代码在格式字符串处理中也存在一些错误或不可移植性.我提供这个作为概念证明,我在两个小时内一起攻击,而不是完成的代码样本准备专业用途.

免责声明说,我希望你发现结果和我一样愉快!这是一个很好的问题,可以解决这个问题.结果的恶劣和扭曲的性质给了我一个深深的肚子笑.;)

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#define VERBOSE 0

#ifdef WINDOWS
#define strdup _strdup
#endif

/*
 * struct cached_printf_args
 *
 * This is used as the pointer type of the dynamically allocated
 * memory which holds a copy of variable arguments.  The struct
 * begins with a const char * which recieves a copy of the printf()
 * format string.
 *
 * The purpose of ending a struct with a zero-length array is to
 * allow the array name to be a symbol to the data which follows
 * that struct.  In this case, additional memory will always be
 * allocted to actually contain the variable args, and cached_printf_args->args
 * will name the start address of that additional buffer space.
 *
 */
struct cached_printf_args
{
    const char * fmt;
    char  args[0];
};


/*
 * copy_va_args -- Accepts a printf() format string and va_list
 *                 arguments.
 *
 *                 Advances the va_list pointer in *p_arg_src in
 *                 accord with the specification in the format string.
 *
 *                 If arg_dest provided is not NULL, each argument
 *                 is copied from *p_arg_src to arg_dest according
 *                 to the format string.
 *
 */
int copy_va_args(const char * fmt, va_list * p_arg_src, va_list arg_dest)
{
    const char * pch = fmt;

    int processing_format = 0;

    while (*pch)
    {
        if (processing_format)
        {
            switch (*pch)
            {
            //case '!': Could be legal in some implementations such as FormatMessage()
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '.':
            case '-':

                // All the above characters are legal between the % and the type-specifier.
                // As the have no effect for caching the arguments, here they are simply
                // ignored.
                break;

            case 'l':
            case 'I':
            case 'h':
                printf("Size prefixes not supported yet.\n");
                exit(1);

            case 'c':
            case 'C':
                // the char was promoted to int when passed through '...'
            case 'x':
            case 'X':
            case 'd':
            case 'i':
            case 'o':
            case 'u':
                if (arg_dest)
                {
                     *((int *)arg_dest) = va_arg(*p_arg_src, int);
                     va_arg(arg_dest, int);
                }
                else
                    va_arg(*p_arg_src, int);
#if VERBOSE
                printf("va_arg(int), ap = %08X, &fmt = %08X\n", *p_arg_src, &fmt);
#endif
                processing_format = 0;
                break;

            case 's':
            case 'S':
            case 'n':
            case 'p':
                if (arg_dest)
                {
                    *((char **)arg_dest) = va_arg(*p_arg_src, char *);
                    va_arg(arg_dest, char *);
                }
                else
                    va_arg(*p_arg_src, char *);
#if VERBOSE
                printf("va_arg(char *), ap = %08X, &fmt = %08X\n", *p_arg_src, &fmt);
#endif
                processing_format = 0;
                break;

            case 'e':
            case 'E':
            case 'f':
            case 'F':
            case 'g':
            case 'G':
            case 'a':
            case 'A':
                if (arg_dest)
                {
                    *((double *)arg_dest) = va_arg(*p_arg_src, double);
                    va_arg(arg_dest, double);
                }
                else
                    va_arg(*p_arg_src, double);
#if VERBOSE
                printf("va_arg(double), ap = %08X, &fmt = %08X\n", *p_arg_src, &fmt);
#endif
                processing_format = 0;
                break;
            }
        }
        else if ('%' == *pch)
        {
            if (*(pch+1) == '%')
                pch ++;
            else
                processing_format = 1;
        }
        pch ++;
    }

    return 0;
}

/*
 * printf_later -- Accepts a printf() format string and variable
 *                 arguments.
 *
 *                 Returns NULL or a pointer to a struct which can
 *                 later be used with va_XXX() macros to retrieve
 *                 the cached arguments.
 *
 *                 Caller must free() the returned struct as well as
 *                 the fmt member within it.
 *
 */
struct cached_printf_args * printf_later(const char *fmt, ...)
{
    struct cached_printf_args * cache;
    va_list ap;
    va_list ap_dest;
    char * buf_begin, *buf_end;
    int buf_len;

    va_start(ap, fmt);
#if VERBOSE 
    printf("va_start, ap = %08X, &fmt = %08X\n", ap, &fmt);
#endif

    buf_begin = (char *)ap;

    // Make the 'copy' call with NULL destination.  This advances
    // the source point and allows us to calculate the required
    // cache buffer size.
    copy_va_args(fmt, &ap, NULL);

    buf_end = (char *)ap;

    va_end(ap);

    // Calculate the bytes required just for the arguments:
    buf_len = buf_end - buf_begin;

    if (buf_len)
    {
        // Add in the "header" bytes which will be used to fake
        // up the last non-variable argument.  A pointer to a
        // copy of the format string is needed anyway because
        // unpacking the arguments later requires that we remember
        // what type they are.
        buf_len += sizeof(struct cached_printf_args);

        cache = malloc(buf_len);
        if (cache)
        {
            memset(cache, 0, buf_len);
            va_start(ap, fmt);
            va_start(ap_dest, cache->fmt);

            // Actually copy the arguments from our stack to the buffer
            copy_va_args(fmt, &ap, ap_dest);

            va_end(ap);
            va_end(ap_dest);

            // Allocate a copy of the format string
            cache->fmt = strdup(fmt);

            // If failed to allocate the string, reverse allocations and
            // pointers
            if (!cache->fmt)
            {
                free(cache);
                cache = NULL;
            }
        }
    }

    return cache;
}

/*
 * free_printf_cache - frees the cache and any dynamic members
 *
 */
void free_printf_cache(struct cached_printf_args * cache)
{
    if (cache)
        free((char *)cache->fmt);
    free(cache);
}

/*
 * print_from_cache -- calls vprintf() with arguments stored in the
 *                     allocated argument cache
 *
 *
 * In order to compile on gcc, this function must be declared to
 * accept variable arguments.  Otherwise, use of the va_start()
 * macro is not allowed.  If additional arguments are passed to
 * this function, they will not be read.
 */
int print_from_cache(struct cached_printf_args * cache, ...)
{
    va_list arg;

    va_start(arg, cache->fmt);
    vprintf(cache->fmt, arg);
    va_end(arg);
}

int main(int argc, char *argv)
{
    struct cached_printf_args * cache;

    // Allocates a cache of the variable arguments and copy of the format string.
    cache = printf_later("All %d of these arguments will be %s fo%c later use, perhaps in %g seconds.", 10, "stored", 'r', 2.2);

    // Demonstrate the time-line with some commentary to the output.
    printf("This statement intervenes between creation of the cache and its journey to the display.\n"

    // THIS is the call which actually displays the output from the cached printf.
    print_from_cache(cache);

    // Don't forget to return dynamic memory to the free store
    free_printf_cache(cache);

    return 0;

}


Dav*_*ler 7

存储va_list本身并不是一个好主意; 标准只要求va_list参数使用va_start(),va_arg()va_end().据我所知,va_list不保证是可复制的.

但你不需要存储va_list.将提供的参数复制到另一个数据结构中,例如vector(可能是void*),然后以通常的方式检索它们.您需要注意类型,但C++中的printf样式函数总是如此.