PDA

View Full Version : Can't get IniOpen to work.


MagNet
10-05-2006, 03:22 PM
I have these two procedures, I have already saved something to options.ini with the first one and now I'm trying to load with the second one and just pop up a message with the first item in the list for testing but everytime I run it I get an access violation from the ListToStruct function.

proc OptSave
list(opt$);
add(opt$, bs$, mov$, sph$, ms$);
IniSave(AppPath$ + "options.ini", opt$);
clear(opt$);
end;

proc OptLoad
list(opt$);
IniOpen(AppPath$ + "options.ini", &opt$);
struct(optstr$, "a", "b", "c", "d");
ListToStruct(opt$, optstr$);
ShowMessage(optstr.a$);
clear(opt$);
end;

What am I doing wrong?

kornalius
10-05-2006, 03:26 PM
Here is a solution:

list(opt$);
IniOpen(AppPath$ + "options.ini", &opt$);
ShowMessage(opt$);
struct(optstr$, "a", "b", "c", "d");
ListToStruct(opt$, optstr$);
ShowMessage(@optstr.a$);

Don't forget the &opt$ in IniOpen, you need the list to be modified therefore need to pass it as a pointer.

You also need the @ to be used to view the string value of optstr.a$. The a,b,c members are all integer values and don't hold strings only a pointer to a string from the list.

MagNet
10-05-2006, 03:30 PM
Oh well, I'll remember that @ thing but the problem is, that it isn't the ShowMessage what's bugging. It's most probably the ListToStruct where you haven't changed anything thus it still doesn't work :(

MagNet
10-05-2006, 03:33 PM
Oh another thing, I tried changing the code to this...

list(opt$);
add(opt$, bs$, mov$, sph$, ms$);
struct(optstr$, "a", "b", "c", "d");
ListToStruct(opt$, optstr$);
ShowMessage(@optstr.a$);
clear(opt$);

leaving out the IniOpen out to test if the rest's working and now I get access violation on ShowMessage if I use the @... might it be because what I'm adding to the list are integers?

kornalius
10-05-2006, 03:39 PM
What are bs$, mov$, sph$ and ms$ holding? String values or numeric values?

MagNet
10-05-2006, 03:45 PM
Integers, numeric.

But ShowMessage isn't the problem, it keeps having problems with ListToStruct.

PointOfLight
10-05-2006, 07:10 PM
I took your last code posting and populated the variables myself, so here's the result:

bs$ = 1;
mov$ = 10;
sph$ = 23;
ms$ = 5;

list(opt$);
add(opt$, bs$, mov$, sph$, ms$);
struct(optstr$, "a", "b", "c", "d");
ListToStruct(opt$, optstr$);
ShowMessage(optstr.a$);
clear(opt$);

This ran just fine for me, and the ShowMessage displayed 1 just as I would have expected it to. I just tested this on the PC, not on the PPC.

MagNet
10-05-2006, 07:31 PM
This does run, fine, yes. But it doesn't load the ini :D
I think I said this was running for me aswell... just bugged when I added the @ because that's for strings.

kornalius
10-05-2006, 07:54 PM
Make sure it's pointing to the right place.

Btw, the @ is needed, the ini is loaded as strings not integers.

kornalius
10-05-2006, 07:56 PM
I saw in your first post that & opts$ in the IniOpen line, make sure there are no spaces: &opts$

MagNet
10-05-2006, 07:56 PM
Well yeah, but when I'm loading the ini I'm not having problems with the ShowMessage function. But the ListToStruct one.

PointOfLight
10-05-2006, 08:49 PM
Sorry Magnet. When you said you were working with integers at one point it threw me off. Anyway, I modified my code as such:

bs$ = "This is some text";
mov$ = "ini line 2";
sph$ = "Hello there world";
ms$ = "bob";

list(opt$);
add(opt$, bs$, mov$, sph$, ms$);
struct(optstr$, "a", "b", "c", "d");
ListToStruct(opt$, optstr$);
ShowMessage(@optstr.a$);
clear(opt$);

Still no errors, and "This is some text" is displayed. To me this makes the IniOpen function suspect. Instead of using ListToStruct, try ListToStr and print out the resulting string to see if the results are as you'd expect. Do this:

IniOpen(AppPath$ + "options.ini", &opt$);
s$ = ListToStr(opt$, "\n", "", "");
ShowMessage(s$);
clear(opt$);

and see what you get.

MagNet
10-05-2006, 09:05 PM
Says [_empty_]... that's what it puts in the list when it can't load the file right? At least it looks so from the inifile.ppl ... no good.

PointOfLight
10-05-2006, 09:28 PM
Can you post a sample of what your ini file looks like?

PointOfLight
10-05-2006, 09:43 PM
Do you have a section header in your ini file? If not, then you will get the [empty] string. Your ini file must look something like this:

[Header1]
key=value
key1=value
key2=value

[Header2]
key3=value
key4=value
key5=value

PointOfLight
10-05-2006, 09:48 PM
I figured out the problem you were having with ListToStruct as well. I will post a new thread.

MagNet
10-05-2006, 10:06 PM
Oh well... I have no header there, how to add one?
Do I simply add it as a first item to the list?

EDIT: Yay!

bmanske
10-06-2006, 06:01 AM
Magnet, </p>

I've read through all of this plus POL's solution in the newer thread and I'm still scratching my head and wondering what this has to do with INI files? Sure you used INIOpen and INISave but at heart all they really do is read a list from a file and write a list to a file. The real meat of the ini code is in the IniRead and IniWrite functions. </p>

Are you trying to implemet your own INI code? Is there something missing in our implementation that you need? </p>

I use INI functions in all of the option dialogs. Check out Editor.ppl in the editor project for some examples like the findDlg and SetOptions. </p>

Here is a quick example:</p><pre><span style=&quot;font-family: courier new,courier&quot;>#ifdef _WIN32_WCE
#define STARTUP_SECTION_NAME** &quot;Startup&quot;
#else
#define STARTUP_SECTION_NAME** &quot;StartupPC&quot;
#endif
// These are here to save space in the code.
func StartupSectionName
* return(STARTUP_SECTION_NAME);
end;
* local(IniData$);
list(IniData$);
* IniOpen(UserAppPath$ + INI_FILE_NAME, &amp;IniData$);
* // Make sure the needed sections exist in the ini file
* if (IniSectionExists(IniData$, StartupSectionName) == -1)
*** IniAddSection(&amp;IniData$, StartupSectionName);
* end;
// write out a key
*IniWriteString(&amp;IniData$, StartupSectionName, &quot;Reopen&quot;, startupStr$);
* // read the value from the Reopen key in the StartupSectionName
* startupInt$ = Int(IniReadString(IniData$, StartupSectionName, &quot;Reopen&quot;, &quot;0&quot;));
iniSave(...);
</span></pre>

The ini functions handle things if*no section is found.</p>

Brad</p>

PointOfLight
10-06-2006, 04:07 PM
Actually, the more I think about it, I agree with bmanske. I'm not really sure what you're needing that you need to use an INI file for. If all you're going to do is load a bunch of values from a text file to a structure, maybe you'd be better off doing something like this:

s$ = LoadStr(AppPath$ + &quot;options.ini&quot;, size$);
StrToList(s$, &quot;\n&quot;, lst$);
struct(optstr$, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;);
ListToStruct(lst$, optstr$);

Then to save you could do:

list(lst$);
StructToList(optstr$, lst$);
s$ = ListToStr(lst$, &quot;\n&quot;, &quot;&quot;, &quot;&quot;);
SaveStr(AppPath$ + &quot;options.ini&quot;, s$, -1);


Then your config file could look something like this:
Val1
Val2
Val3
Val4
etc...

Is this more along the lines of what you want to accomplish?

MagNet
10-06-2006, 06:54 PM
Hmm... maybe...
What's ini for when not for saving a bunch o' values?

PointOfLight
10-07-2006, 04:47 AM
An INI file is certainly designed for storing lots of values. The only reason I suggested the other method is because it seemed like you might not care if the values were keyed in the file, which is the main benefit of using an INI file over a plain data file. If there's no benefit to having a file where each field is laid out as key=value, then there's not much gain with using an INI file versus a text file. Either way will work just fine, though.

MagNet
10-07-2006, 01:28 PM
Hm, ok.
My code is now like this:
proc OptSave
opt$ = (bs$+&quot;\n&quot;+mov$+&quot;\n&quot;+sph$+&quot;\n&quot;+ms$);
SaveStr(OPTFILE, opt$, -1);
clear(opt$);
end;

proc OptLoad
if (FileExists(OPTFILE))
opt$ = LoadStr(OPTFILE, z$);
StrToList(opt$, &quot;\n&quot;, optlst$);
bs$ = Int(optlst$);
next(optlst$); mov$ = Int(optlst$);
next(optlst$); sph$ = Int(optlst$);
next(optlst$); ms$ = Int(optlst$);
end;
end;