LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Variable type determination at runtime

Hi,
I want to implement a function that takes a label - value pair and writes it to a file. Ideally I wanted the function to be able to accept double, int and *char as the value parameter. I've had a few ideas but am not sure whether that is very professional:
a:
void WriteLabelValuePairToFile(int fileHdle, char *label, void *value, int dataType)
{
 if (dataType == DATATYPE_INT)
     some code to handle int
     FmtFile......(int)data;
.
.
.
}
concern: I've read that type casts are usually unnecessary and evidence of dodgy programming when done.....

b:

void WriteLabelValuePairToFile(int fileHdle, char *label, int iValue, double fValue, char *sValue,)
{
pass NULL for the not used variables check for NULL and handle
    if (iValue != NULL)
        some code to handle int
.
.
.
}

Any better idea would be much appreciated.
Is there a way to determine the datatype of a void* at runtime?
thanks heaps!


Message Edited by DocEye on 06-20-2008 01:52 AM

Message Edited by DocEye on 06-20-2008 01:53 AM
0 Kudos
Message 1 of 4
(3,068 Views)

In my opinion both of your solutions are good. I can add a couple of alternatives to handle this item.

1. Use variants. Variant is a datatype that can hold several different data; CVI ActiveX library has some functions CA_VariantHasX to query if a variant contains a stated data type, so that your solution a. could be migrated as follows:

void WriteLabelValuePairToFile(int fileHdle, char *label, VARIANT value)
{
  int    data;

 if (CA_VariantHasInt (&value)) {
     CA_VariantGetInt (&value, &data);
     FmtFile......(int)data;
 }
.
.
.
}

The caller of this function is responsible of assigning the correct value to the variane using proper CA_VariantSetX function:

CA_VariantSetInt (&value, intVal);


2. Use variable parameters macros: here a function that writes a logfile taking a variable list of arguments as a parameter:

void History (int operation, int station, int *parms, ...)
// Activity log on file
{
 int  fH, error = 0;
 char type, msg[256];
 SYSTEMTIME lt;
 va_list ap;

 // Start variable params management
 va_start (ap, parms);

 // Open log file
 fH = OpenFile ("Activity.log", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
 if (fH == -1) {
  error = GetFmtIOError ();
  goto Error;
 }

 // Date / time of record
 GetLocalTime (&lt);
 sprintf (msg, "%02d/%02d/%04d  %02d:%02d", lt.wDay, lt.wMonth, lt.wYear,
  lt.wHour, lt.wMinute);
 sprintf (msg, "%s,%d,%d", msg, station, operation);

 // Variable 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
 strcat (msg, "\n");
 if (WriteFile (fH, msg, strlen(msg)) < 0) {
  error = GetFmtIOError ();
  goto Error;
 }

Error:
 CloseFile (fH);
 // End variable parms management
 va_end (ap);

 if (error) {
  // Conveniently handle errors
 }
 return;
}

The caller of this function must pass a variable lengh list of parameters ended with a null element:

    parms[0] = INTPRM; parms[1] = 0; History (18, 1, parms, i);



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 4
(3,047 Views)
Hello DocEye.

If your aim is simply to save UI control values between sessions, I suggest you look at the User Interface library functions SavePanelState() and RecallPanelState().

Otherwise, you might save yourself some time by using CVI's IniFile instrument (inifile.fp), which includes functions such as Ini_PutDouble(), Ini_PutInt(), Ini_PutString() and Ini_WriteToFile(). Each Ini_PutXXX() call will generate one (or more) lines in the file.

Hope this helps,
Colin.

0 Kudos
Message 3 of 4
(3,020 Views)

Thanks to both!
it is data from an instrument that has to be saved in a format for easy and consistent evaluation by external software rather than saving panel states. I've made the final decision to use VARIANT as it seems the ideal data type for that and allows me to later extend my function.
Thanks heaps for your advice!!



0 Kudos
Message 4 of 4
(2,989 Views)