Reading from dat, or ini

Posted By: log2

Reading from dat, or ini - 09/22/08 02:28

so far I only know of one way to read from an INI, or DAT, but I would like to know if there is a way to read from one of those files in a standard ini format..

In case you're not familiar with the standard ini format it looks something like this:
Quote:
[topic]
subject=answer
subject2=answer2
[topic2]
subject=answer


So in other words I'd like to know if I could specify the topic, then obviously specify the subject and receive the answer..

Or even if there is a way to do it without the topics would definitely help, so I can just call out the subject and receive the answer

Any help appreciated, thanks
Posted By: testDummy

Re: Reading from dat, or ini - 09/22/08 05:02

The following is probably not helpful at all:

A6.60
http://testdummy93.tripod.com/3DGS/3DGS_index.html
tDe.zip -> tDe.dll + tDe.wdl

Code:
STRING sAnswer[256];
// set path
ifdef DEVELOP;
inif_wp(work_dir, "questions.ini");
ifelse;
inif_wp(exe_dir, "questions.ini");
endif;

inif_wSect("topic"); 	// set section
inif_rs("subject", "?", sAnswer);	// read string
// ...
inif_rs("subject2", "?", sAnswer);	// read string
// ...
inif_wSect("topic2");	// set section
inif_rs("subject", "?", sAnswer);  // read string
// ...

Posted By: log2

Re: Reading from dat, or ini - 09/22/08 05:16

thanks looks good, but I take it there is currently no way to do it without a plugin?
Posted By: Anonymous

Re: Reading from dat, or ini - 09/22/08 05:47

read:
1. read the ini file in a text object
2. normalize the text object
3. search for your [topic]
4. search for your key=
5. return the value

add/delete:
1. read the ini file in a text object
2. create a temp dest. textobject
3. copy untouched entries to the dest textobject
4. when found your [topic] your key=
change the value(change) or skip copying the line(del)
6. write the dest. textobject

Posted By: MrGuest

Re: Reading from dat, or ini - 09/24/08 19:00

have a look at
Code:
var_for_name()

Posted By: log2

Re: Reading from dat, or ini - 09/25/08 05:23

hmmm can't really tie that to an answer
Originally Posted By: "From Manual"
Returns a pointer to a variable with the given name. If the name string contains a '=' followed by a number, the variable is set to that number. Otherwise a '=' and the current variable value is appended to the string. This function can be used to display or alter variables during gameplay for debugging purposes.


Also mercuryus, do you have any code example, I can only assume from your message that you mean to open the ini file in a
Code:
TEXT whatever = {}
however I can't really find any reference in the manual as to how to search an ini/txt file or how to open the ini file in a text object

but I appreciate both of your help, and am awaiting a reply, thanks a bunch
Posted By: testDummy

Re: Reading from dat, or ini - 09/25/08 17:55

Code:
// not complete; just an example
// tested quickly; may have errors
TEXT ini_t1 {
	strings = 256;

}
var ini_nLinesC = 0;
var ini_bSect = 0;
STRING ini_sValue[256];
STRING ini_s1[256];
STRING ini_s2[256];
STRING ini_s3[256];
var ini_n1 = 0;
var ini_n2 = 0;

function inif_get(_sSection, _sKey) {
	ini_n1 = 0;
	str_cpy(ini_sValue, "");
	ini_bSect = 0;
	while(ini_n1 < ini_nLinesC) {
		if (str_stri(ini_t1.string[ini_n1], "[") == 1) {
			if (ini_bSect) {
				return(0);
			}
			str_cpy(ini_s1, ini_t1.string[ini_n1]);
			str_clip(ini_s1, 1);
			str_trunc(ini_s1, 1);
			if (str_cmpi(_sSection, ini_s1)) {
				ini_bSect = 1;
			}
		} else {
			if (!ini_bSect) { goto(next1); }
			ini_n2 = str_stri(ini_t1.string[ini_n1], "=");
			if (ini_n2 == 0) { goto(next1); } 
			str_cpy(ini_s2, ini_t1.string[ini_n1]);
			str_cpy(ini_s3, ini_s2);
			str_trunc(ini_s2, (str_len(ini_s2) - ini_n2) + 1);
			if (str_cmpi(_sKey, ini_s2)) {
				str_clip(ini_s3, ini_n2);
				str_cpy(ini_sValue, ini_s3);
				return(1);
			}
		}
		next1:
		ini_n1 += 1;
	}
	return(0);
}

function inif_load(_sFile) {
	ini_nLinesC = txt_load(ini_t1, _sFile);
	return(ini_nLinesC);
}

//...
inif_load("questions.ini");
inif_get("topic2", "subject");
// do something with ini_sValue

Posted By: log2

Re: Reading from dat, or ini - 09/26/08 04:45

Great, thanks testDummy, I'm gonna try that out right away, again thanks a lot very much appreciated

EDIT: Just getting back, seems to be working perfectly, gonna do some more testing with it, but right now it seems to be working perfectly, than you so much testDummy laugh
© 2024 lite-C Forums