There are native functions to read/write ini files? [PC & WIZ]

Started by gigios, June 12, 2011, 10:49:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

gigios

There are native functions to read/write ini files?

I want to reading some settings from a standard ini files without writing a custom parser, if possible. All this also on Wiz.

Are this possible or I need to writing one custom lib?

FreeYourMind


gigios

Before writing the post I did a little search but I found nothing!

Unfortunately, the ini term is found for initialization/init and other words  :-[

I try to do another search.

edit:

I found this:
http://forum.bennugd.org/index.php?topic=2317.0

Sslaxx

Looks useful, if Spanish. I'll give a go translating the code and comments (so us non-Spanish speakers can hopefully understand how to use it).
Stuart "Sslaxx" Moore.

Sslaxx

Hmmm. It appears to not work.

Compiling it worked after getting the imports right. Running it, however:

Quote from: bgdi leeClaves.dcbSegmentation fault.

Running it with debug information:

Quote from: bgdi -d leeClaves.dcbLoading... mod_file.so
Loading... libsdlhandler.so
Loading... libkey.so
Loading... mod_key.so
Loading... mod_proc.so
Loading... mod_say.so
Loading... mod_string.so

***** INSTANCE MAIN(65536) ENTRY StackBase=0xa0ee670 StackPTR=0xa0ee674
                                             [   0] 000000FF 00000065          ------              leeClaves.prg:101 -> ModificaClave ("conf.ini", "OTRO", "Variable3", "Simio");

                                             [   2] 00000384 0000000E STRING   PUSH                14      

0000000E
                                             [   4] 00000384 0000000F STRING   PUSH                15      

0000000E 0000000F
                                             [   6] 00000384 00000010 STRING   PUSH                16      

0000000E 0000000F 00000010
                                             [   8] 00000384 00000011 STRING   PUSH                17      

0000000E 0000000F 00000010 00000011
                                             [  10] 00000089 00000001          CALL                MODIFICACLAVE (1)

***** INSTANCE MODIFICACLAVE(65537) ENTRY StackBase=0xa0f2678 StackPTR=0xa0f267c
                                             [   0] 000000FF 00000014          ------              leeClaves.prg:20 -> f1=fopen (ruta, O_READWRITE);   // Open up a file for reading/writing.

                                             [   2] 00000092 00000010          PRIVATE             16      

09FD9E30
                                             [   4] 00000397 00000000 STRING   GET_PRIVATE         0      

09FD9E30 0000000E
                                             [   6] 00000084 00000001          PUSH                1      

09FD9E30 0000000E 00000001
                                             [   8] 0000008A 00000006          SYSCALL             FOPEN    (6)

09FD9E30 00000000
                                             [  10] 00000047                   LETNP              

                                             [  11] 000000FF 00000015          ------              leeClaves.prg:21 -> encontrado=false; //por defecto false pero we.

                                             [  13] 00000092 00000024          PRIVATE             36      

09FD9E44
                                             [  15] 00000084 00000000          PUSH                0      

09FD9E44 00000000
                                             [  17] 00000047                   LETNP              

                                             [  18] 000000FF 00000016          ------              leeClaves.prg:22 -> longitud_clave=len (clave);

                                             [  20] 00000092 00000028          PRIVATE             40      

09FD9E48
                                             [  22] 00000397 00000008 STRING   GET_PRIVATE         8      

09FD9E48 00000010
                                             [  24] 0000008A 00000026          SYSCALL             LEN      (38)

09FD9E48 00000009
                                             [  26] 00000047                   LETNP              

                                             [  27] 000000FF 00000018          ------              leeClaves.prg:24 -> while (!(feof (f1)))

                                             [  29] 00000097 00000010          GET_PRIVATE         16      

00000000
                                             [  31] 0000008A 00000012          SYSCALL             FEOF     (18)

Segmentation fault

After a bit more investigation, if the file isn't there when the program is run it causes the segfault. If conf.ini is there, it runs but seemingly does nothing.

http://sslaxx.twu.net/leeClaves.prg is what there is so far.

Any advice?
Stuart "Sslaxx" Moore.

handsource-dyko

#5
I've also discovered Leeclaves.prg. I have translated it and I'm making some improvements to it.
This is what I have altered so far:



// include "DLLs/import.prg";

IMPORT "mod_string";
IMPORT "mod_file";
IMPORT "mod_key";
IMPORT "mod_say";
IMPORT "mod_proc";



//-----------------------------------------------------------------------------
// Function that modifies a key in an ini file.
//
// Entries: - string filename   : the name of the textfile to load
//          - string section    : the section word between [] to look for
//          - string key_string : the key to look for
//          - string value      : the value of the key parameter.
//
// Returns: -
//
//
// Notes: Format of the section
//
// [GENERAL]      <---section
// Experience=10  <---key_string = string_value
// lifes=20
// etc=10
//
//-----------------------------------------------------------------------------  
FUNCTION string modifykey(string filename,string section,string key_string,string value);


PRIVATE
int ini_file;              // handle for textfile
int count;                  
string text_line;          
string text_line_modified;  
string stored;
bool located;
int length_key;


BEGIN

ini_file=fopen(filename,O_READWRITE);
located=FALSE;
   
   // obtain the length of the textstring
length_key=len(key_string);

WHILE(NOT(feof(ini_file)))
   
       // get the text from the current line in the file
text_line=fgets(ini_file);

// find the section pattern to look for, sections are marked with [] around the string
IF(find(text_line,"[" + section + "]"))
  located=TRUE;
END

//if the section has been found
IF(located==TRUE)
//look for the key and change the value
IF(substr(text_line,0,length_key)==key_string)
text_line_modified=key_string + "=" + value;
stored=stored + text_line_modified + chr(13) + chr(10);
located=FALSE;
ELSE
IF(stored=="")
stored=text_line + chr(13) + chr(10);
ELSE
stored=stored + text_line + chr(13) + chr(10);
END
END
ELSE // we didn't find any section
IF(stored=="")
stored=text_line + chr(13) + chr(10);
ELSE
stored=stored+text_line + chr(13) + chr(10);
END
END
END

fclose(ini_file);
fopen(filename,O_WRITE);
   
   // write the line to the file
fputs(ini_file,stored);
fclose(ini_file);

END



//-----------------------------------------------------------------------------
// Function that reads a key in an ini file.
//
// Entries: - string filename   : the name of the textfile to load
//          - string section    : the section word between [] to look for
//          - string key_string : the key to look for
//
// Returns: - a string with the value of the key_string
//
//
// Notes: Format of the section
//
// [GENERAL]      <---section
// Experience=10  <---key_string = string_value
// lifes=20
// etc=10
//
//-----------------------------------------------------------------------------   
FUNCTION string read_key(string filename,string section,string key_string);


PRIVATE
int ini_file;               // handle for textfile
string text_line;
bool section_located=FALSE;
int length_key;
int found_position;

   
   
BEGIN
ini_file=fopen(filename,O_READ);
section_located=FALSE;
   
    // obtain the length of the textstring.
length_key=len(key_string);
   
   
WHILE (NOT(feof(ini_file)))
       
        // get the current line of text from the file.
text_line=fgets(ini_file);
        say("current line: "+text_line);
     
// find the section pattern to look for, sections are marked with [] around the string .         
IF ((find(text_line,"[" + section + "]")==-1) AND section_located==FALSE)
   section_located=FALSE;
           say("Section '" + section + "' not found!");               
        ELSE 
           section_located=TRUE;
           say(" section: " + section + " was located!");
        END
       
        // no match was found at the end of the file.
        IF (feof(ini_file) AND section_located==FALSE)
           RETURN ("no section match found in the entire file!");
        END
       
       
        //if the section has been found, we look for the key.
        IF (section_located==TRUE)
       
       //look for the value of the key.
       IF (substr(text_line,0,length_key)==key_string)
      found_position=find(text_line,"=");
             
              // check if there actually is a value, otherwise we have some "error condition", wich
              // can be salvaged by putting a zero at it's place .             
              IF (substr(text_line,found_position+1)=="")
                 say("The key '" + key_string + "' doesn't have a value, so it will become 0.");                 
                 RETURN ("0");
              ELSE // we do nothing, it's ok.
                 say("Key '" + text_line + "' located!");                 
              END
             
      RETURN (substr(text_line,found_position+1)); // returns the value of the key.
                           
              ELSE // key was not found, return an error message when the end of the file was reached.
              IF (feof(ini_file))
                 RETURN ("Key '" + key_string + "' in section '" + section + "' was not found.");
              END
       END
    END
       
END // end of while

   
END

PROCESS main();

PRIVATE

string output_string;

BEGIN
//modifykey("test.ini","GENERAL","Experiencia","10");
   
output_string=read_key("test.ini","GENERAL","Experiencia");
    //output_string=read_key("test.ini","ABBA","Experiencia");
    //output_string=read_key("test.ini","GENERAL","lifes");
    //output_string=read_key("test.ini","ADMINS","Admin1");
   
    say("output_string: "+output_string);
END




Please not that I have only looked at the read part so far, I haven't yet bothered about the write function.
The read function now works fine. The messed up formatting is due to copy-paste from wordpad++.

This is test.ini:


[GENERAL]
Experiencia=10
Oro=20

[ADMINS]
Admin1=Juan
Admin2=Martin


gecko

Just to be sure, this is the last version of LeerClaves that can be found aorund here?
Torres Baldi Studio
http://torresbaldi.com

handsource-dyko

yes, it's the last version, and I haven't looked at it since then. Actually, INI file handling would be a nice candidate for a dll.