开发者

Variable argument lists in C++

开发者 https://www.devze.com 2023-02-25 02:43 出处:网络
I have this method (by using code from vsnprintf man page): MYSQL_RES *nsDatabase::queryf(const char *fmt,...){

I have this method (by using code from vsnprintf man page):

MYSQL_RES *nsDatabase::queryf(const char *fmt,...){
  int n, size = 1024;
  char *query=NULL,*np;
  va_list ap;

  if (this->dbLink == NULL){
    return NULL;
  }

  query = (char *) malloc (size);
  if (query == NULL) return NULL;//memory error

  while (1) {
    va_start(ap, fmt);
    n=vsnprintf(query,size,fmt,ap);
    va_end(ap);
    if (n > -1 && n < size){// format string succeeded
      break;
    }
    if (n > -1)    /* glibc 2.1 */
       size = n+1; /* precisely what is needed */
    else           /* glibc 2.0 */
       size *= 2;  /* twice the old size */

    np = (char *) realloc (query, size);
    if (np == NULL) {
       printf("memory error\n");
       FREE(query);
       return NULL;//again memory error
    } else {
       query = np;
    }
  }

  MYSQL_RES *r = this->query(query);

  FREE(query);

  if(r == NULL){
    return NULL; //mysql error
  }

  return mysql_store_result(this->dbLink);
}

I want to make general function queryFormat which will calculate the size needed for formatted string, allocate memory, print format in it and return the string (not real code, pseudo code):

char *queryFormat(const char *fmt, va_list ap){
  // allocate memory
  // problem - can I use vsnprintf multiple times here (to determine the size of formatted string and allocate memory)
  return <formatted string>;
}

Then I need to call it from all functions that format queries (not real code):

queryRow(const char *fmt,...){
  va_start(ap, fmt);
  // I need to call vsnprintf many times in queryFormat ... Do I need to call va_start() before every call to fsnprintf ?
  char * formattedQuery = queryFormat(query, size, fmt, ap);
  va_end(ap);
  MYSQ开发者_Go百科L_RES *r = this->query(formattedQuery);
  free(formattedQuery);

  ...

}

I write for GCC under linux, but the code should be runnable under MinGW and CygWin.


See this - http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html.There is a good example in the book of Kernighan and Ritchie book.

EDIT Its the same in C++ too . you can have a look at this too. - http://www.cplusplus.com/reference/clibrary/cstdarg/va_start/

EDIT 2 http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/. I think this shall help you.


I believe this should help anyone who googled here in search of solution to the problem of how to "reuse" a va_list in a function that had it already passed as parameter:

// Incorrect:
/*void g(int foo, va_list ap) {
    vprintf("%s %s %s\n", ap);
    vprintf("%s %s %s\n", ap);
}*/

void g(int foo, va_list ap) {
    va_list copy;
    va_copy(copy, ap);
    vprintf("%s %s %s\n", copy);
    va_copy(copy, ap);
    vprintf("%s %s %s\n", copy);
}

void f(int foo, ...) {
    va_list ap;
    va_start(ap, foo);
    g(foo, ap);
    va_end(ap);
}

int main() {
    f(42, "aaa", "bbb", "ccc");
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消