I have made some progress. This is the current state of my function:
Code:
bool SQL_ResultWithValues(SQL_Output *SQL_Statement, const char *Typelist, ...)
{
int cType, Column;
char *TString = 0;
int *TInt = 0;
double *TDouble = 0;
va_list argp;
va_start(argp, Typelist);
for (int i = 1; i < strlen(Typelist); i += 2)
{
Column = (int)((i-1)/2);
if (!strncmp(Typelist + i - 1, "%", 1 ) )
{
cType = (int)Typelist[i];
switch (cType)
{
case 115 : case 83 : // s or S
TString = va_arg(argp, char*);
strcpy(TString, getResultAsString(SQL_Statement, Column) );
break;
case 100 : case 68 : // d or D
TInt = va_arg(argp, int*);
// getResultAsInt returns an int
TInt = (int*)getResultAsInt(SQL_Statement, Column);
break;
case 102 : case 70 : // f or F
TDouble = va_arg(argp, double*);
// next statement gives compiler error 'cannot convert double* to double'
// getResultAsDouble returns a double
TDouble = (double*)getResultAsDouble(SQL_Statement, Column);
break;
default :
printf("Unknown Type in Typelist: %s at position %d Token=%s\n",
Typelist, i, Typelist[i]);
va_end(argp);
return false;
break;
}
}
else
{ printf("Error parsing Typelist: %s at position %d\n", Typelist, i);
return false;
}
}
va_end(argp);
return true;
}
I am calling this function for example with:
Code:
int ID;
double Value;
char Remark[100];
DB->SQL_ResultWithValues(&SQLOut, "%d%f%s", &ID, &Value, &Remark);
printf("%d,%f,%s\n", ID, Value, Remark );
The Typelist parameter gives the number of arguments and it's type. (and yes the % characters aren't really necessary). I
agree with grumpy that there is a risk of mismatches between Typelist and actual types.
I am not familiar (yet) with 'boost::any'
For string type values everything works perfect.
For integers no values are returned.
For doubles I get an compiler error.
So I think i'm on the right track, but the code needs some tweeking.
I am using DevC with gcc 3.4.2