09-01-2006 04:13 AM
09-01-2006 04:51 AM
Here some hint to design and use a function vith variable number of arguments (by means of va_xxx macros you already mentioned). I used this function to create a history of operation in a plant controller: depending on the type of event to record, there were different parameters to store.
// Types of parameters passed to the function (see "stdarg.h")
#define INTPRM 1
#define DBLPRM 2
#define CHRPRM 3
int parms[10]; // Array of arguments for History ()
// Function definition
void History (int op, int stn, int *parms, ...);
// USAGE: Record and event in program history
// HINT: always add a parms[x] = 0 at the end of the list!
parms[0] = INTPRM; parms[1] = DBLPRM; parms[2] = 0;
History (id, op, parms, data1, data2);
// History () function
// This function writes resord in program activity list
// This function accepts and handles a variable number of arguments
void History (int recordType, int station, int *parms, ...)
{
int fH, error = 0;
char type, msg[256];
SYSTEMTIME lt;
va_list ap;
// Start variable parms handling
va_start (ap, parms);
// Open history file (handle file I/O error)
....
// Date / time of record
GetLocalTime (<);
sprintf (msg, "%02d/%02d/%04d %02d:%02d", lt.wDay, lt.wMonth, lt.wYear,
lt.wHour, lt.wMinute);
sprintf (msg, "%s,%s,%d,%d", msg, operator, station, recordType);
// Variable list of parameters
while ((type = *parms++) != 0) {
switch (type) {
case INTPRM: sprintf (msg, "%s,%d", msg, va_arg (ap, int)); break;
case DBLPRM: sprintf (msg, "%s,%.1f", msg, va_arg (ap, double)); break;
case CHRPRM: sprintf (msg, "%s,%s", msg, va_arg (ap, char *)); break;
}
}
// Write to file and close it (handle file I/O error)
strcat (msg, "\n");
...
Error:
CloseFile (fH);
// Stop variable parms handling
va_end (ap);
if (error) {
// Error handling
}
return;
}
Hope this helps you a little
Roberto
09-01-2006 08:00 AM
09-05-2006 03:44 AM
On my side I use this kind of simple function. this allow to add text in the text box.
I have also a variant with rich text box.
Bext Regards,
int UI_Printf(char *form_str, ...) /* a printf emulation ! */
{
va_list list_arg;
char strbox[500];
int iTraceFileHandle = 0;
strbox[0]='\0';
// translate form_str with list_arg into str
va_start(list_arg, form_str);
vsprintf(strbox, form_str, list_arg);
va_end(list_arg);
SetCtrlVal (uiPanel, UI_TEXT_BOX_INFO, strbox);
ProcessSystemEvents();
return 0;
}
10-27-2006 04:22 AM