03-04-2008 10:14 AM
03-05-2008 03:22 AM
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"
03-21-2008 11:33 AM
03-21-2008 12:14 PM