Solved

va_list/end/...

Posted on 2004-10-28
1,109 Views
Last Modified: 2008-01-09
[code]wxString SQLite3_Db::Format( const wxString& format, ... )
{
  va_list marker;
  va_start(marker, format);
 
  char * t = sqlite3_vmprintf( format.mb_str(wxConvUTF8).data(), marker );

  va_end(marker);

  wxString retVal( t, wxConvUTF8 );

  sqlite3_free( t );

  return retVal;
}[/code]

This is code I have to format a string. For some this might look Chinese, but let me explain.
I want to send something like 'printf('SELECT * FROM %s', 'tablename')'.
the sqlite3_vmprintf() function should do that (because it has the added value of having the '%q' which inserts a string, escaping the "dangerous" characters.

Now when I do this, the first va_arg that I got into the sqlite-function is the pointer of the second argument.

What I mean is:

Sending:
::Format("%d %s %s", 1, "test", "test2");

In the sqlite3_ functions I receive the correct format, and then sqlite starts parsing. but in %d the va_arg() is taking from "test", so it displays the integer value of a pointer... Which is obviously not what I want to achieve.


I already asked on the sqlite mailing list, but they opt that it would be a problem in the va_* functions.


Can someone of you help me with this please?

Thx
0
Question by:G00fy
    8 Comments
     
    LVL 30

    Expert Comment

    by:Axter
    Please post code that shows how you're calling this function.
    0
     

    Author Comment

    by:G00fy
       SQLite3_Db::Format( wxT("INSERT INTO table(f1, f2, f3, f4, f5) ")
                            wxT("VALUES (        %ld, '%q',     '%q',      '%q',    '%q');"),
                            ID, name.mb_str(wxConvUTF8), group.mb_str(wxConvUTF8), section.mb_str(wxConvUTF8),
                            info.mb_str(wxConvUTF8)
                          )
    0
     

    Author Comment

    by:G00fy
    f1 = int,
    f2->5 = wxString (converted to multi-byte string (char*) by the function .mb_str(wxConvUTF8)
    0
     
    LVL 30

    Expert Comment

    by:Axter
    Are you compiling this in a UNICODE project?

    If it's not a UNICODE project, the above Format will fail with wide string.
    0
     
    LVL 30

    Expert Comment

    by:Axter
    For wide strings, the code should be using one of the following:

    vwprintf
    _vsnwprintf

    It will fail to work if it uses
    vprintf
    _vsnprintf
    0
     

    Author Comment

    by:G00fy
    It's not really a problem of widestrings and so on...

    SQLite uses it's own v(w)printf-function.

    The problem is that in the '%ld' variable, the pointer of 'f2' is put... So I think more of a var_list problem I did not spot?
    0
     

    Author Comment

    by:G00fy
    I already found the solution to my problem.

    It is because of the "wxString format" I pass... When I pass a char* and do exactly the same it works like a charm... So I changed the function header and it's ok.

    Thanks for any help!
    0
     
    LVL 1

    Accepted Solution

    by:
    PAQd, 300 points refunded.

    GhostMod
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now