LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ini files in CVI

Hello,

i have a question regarding CVI and the use of ini files.
I know that you can use the instrument driver located in C:\Program Files\National Instruments\CVI80\toolslib\toolbox\inifile.fp for editing and reading ini files.

I wanted to know if it is possible to read directly a structure custom definied?
for instance:
<device>
     name="aa"
     channel="bb"
     max=10

I want a struct with this parameter and read device directly?

Thanks a lot ;o)

0 Kudos
Message 1 of 4
(9,965 Views)

I'm not aware of a way to automatically save a struct to an INI file keeping the structure detail in plain text.

You could dump the structure to a string variable and store in the ini file but you will loose the readability of it. This is an example of how I accomplish this task with a dummy structure:

#include <utility.h>
#include <formatio.h>
#include "inifile.h"
typedef struct {
 char name[5];
 char channel[10];
 int  max;
} test;


int main (int argc, char *argv[])
{
 int  i, ch;
 char msg[256];
 test xt, yt;
 IniText T = 0;

 // Fill in struct fields
 strcpy (xt.name, "ABC");
 strcpy (xt.channel, "Channel1");
 xt.max = 123;

 // Create a string with struct content
 Fmt (msg, "%*i[zb1w2p0r16]", sizeof (test), (char *)&xt);

 // Write the ini file
 T = Ini_New (0);
 Ini_PutString (T, "General", "Struct", msg);
 Ini_WriteToFile (T, "Output.dat");
 Ini_Dispose (T);

 // Clear the string
 memset (msg, 0, 256);

 // Read back from the ini file
 T = Ini_New(0);
 Ini_ReadFromFile (T, "Output.dat");
 Ini_GetStringIntoBuffer (T, "General", "Struct", msg, 256);
 Ini_Dispose (T);

 // Rebuild original structure
 if (strlen (msg)) {
  for (i = 0; i < strlen(msg) / 2 - 2; i++) {
   Scan (msg, "%s[i*w2]>%i[r16]", i * 2, &ch);
   sprintf ((char *)&yt+i, "%c", ch);
  }
 }

 DebugPrintf ("yt.name = %s\n", yt.name);
 DebugPrintf ("yt.channel = %s\n", yt.channel);
 DebugPrintf ("yt.max = %d\n", yt.max);

 return 0;
}

This the resulting INI file:

[General]
Struct = "41424300eb4368616e6e656c310014007b000000"

 



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?
Message 2 of 4
(9,931 Views)
Hi
Thanks man for the response...
It is a workaround nice if you can edit the file as you want but not when the ini files is yet defined and can be modified by the operator...

Thank you ;o)
0 Kudos
Message 3 of 4
(9,856 Views)
i am not sure to understand clearly what you want. what do you mean by "a structure custom defined" ? i can understand this sentence in 4 different ways...

if the structure is clearly defined and the operator only edits the parameters in the file (name, channel, max in your case) then there is no problem. write a function like this:

typedef struct
{
    char name[10];
    char channel[10]; // maybe channel is an int...
    int max;
} DEVICE;

int device_read( char *ini_filename, DEVICE *device )
{
    IniText handle = Ini_New( 0 );
    Ini_ReadFromFile( handle, ini_filename );
    Ini_GetStringIntoBuffer( handle, "device", "name", device->name, sizeof( device->name ) );
    Ini_GetStringIntoBuffer( handle, "device", "channel", device->channel, sizeof( device->channel) );
    Ini_GetInt(handle, "device", "max", &device->max );
    Ini_Dispose( handle );

    // handle all possible error case and return a value according to the real outcome of the function
    return 0;
}

(this one is really too easy: i hope it is not what you were looking for)

now the operator might also rename the section in the file. like instead of having a section named "device" it is named "io" for example, all other parameter names are the same. then you use "Ini_NumberOfSections" to find the number of sections defined in the file, then call "Ini_NthSectionName" to retrieve each device. then Ini_GetStringIntoBuffer or Ini_GetInt passing the retrieved section name instead of "device".

now the worst part: you don't know the structure of the file because the operator might write anything in the file and you want to programmatically define a structure to hold those data.
"Ini_NumberOfSections", "Ini_NthSectionName", "Ini_NumberOfItems" and "Ini_NthItemName" will allow you to figure out the content of the file regardless of what's inside as long as the syntax conforms to an ini file. but you will not be able to define a neat structure like the one defined above for handling the data: C is not a dynamic language. in this case you may use a dictionnary to hold key/value pairs (write your own or find one already written).

last, i noticed that you don't use the standard notation for ini files: you wrote "<device>" instead of "[device]". if you want the ini instrument file to parse another file format out of the box, you can't. but you can edit the source code for the instrument and modify it to handle your own format...




0 Kudos
Message 4 of 4
(9,849 Views)