Reading a Windows format ".ini" file

Windows specific questions.
KenHorse
Posts: 40
Joined: Jan 27, 2012 0:08

Reading a Windows format ".ini" file

Postby KenHorse » Jan 27, 2012 0:16

Before I try to reinvent the wheel, has anyone already worked out a routine to read a Windows type .ini format file?

For example, the contents of a file with entries such as:

[Field]
blah blah blah....

[A different field]

blah, blah blah.

I got an existing file in that format I need to parse
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Reading a Windows format ".ini" file

Postby MichaelW » Jan 27, 2012 2:31

The Windows API provides GetPrivateProfileString and similar functions. Windows XP still supports these “provided only for compatibility with 16-bit Windows-based applications” functions, but I’m not sure that the most recent versions of Windows do.
KenHorse
Posts: 40
Joined: Jan 27, 2012 0:08

Re: Reading a Windows format ".ini" file

Postby KenHorse » Jan 27, 2012 16:34

Yup, I'm familiar with the GetPrivateProfileString method as I (still) program quite a bit in VB.

The file in question isn't really an .ini file but rather a data file that is in that format, hence my question. I guess I will just have to parse it with string functions within FB
Coolman
Posts: 218
Joined: Nov 05, 2010 15:09

Re: Reading a Windows format ".ini" file

Postby Coolman » Jan 27, 2012 21:51

will have one example in PureBasic here, it should not be difficult to adapt a FreeBasic:

http://www.purearea.net/pb/CodeArchiv_v ... d&Write.pb
http://www.purearea.net/pb/CodeArchiv_v ... INIFile.pb
KenHorse
Posts: 40
Joined: Jan 27, 2012 0:08

Re: Reading a Windows format ".ini" file

Postby KenHorse » Jan 27, 2012 21:59

Thanks very much for the example code

Except....my program will eventually be run on Linux, hence there are no Window APIs available

(sorry, perhaps I should have mentioned the Linux part before!)
marcov
Posts: 2840
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Reading a Windows format ".ini" file

Postby marcov » Jan 27, 2012 22:07

MichaelW wrote:The Windows API provides GetPrivateProfileString and similar functions. Windows XP still supports these “provided only for compatibility with 16-bit Windows-based applications” functions, but I’m not sure that the most recent versions of Windows do.


Afaik they are deprecated by then, and you are not supposed to use them according to MS.

Most apps that do use them, have own libraries to manage them since afaik the API calls still have various limitations
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Re: Reading a Windows format ".ini" file

Postby Sisophon2001 » Jan 28, 2012 11:33

I thought it would be useful to have a function to do this for one of my programs, so I just made one now. It is only tested on a single example, so it may still have problems.

Garvan


Code: Select all

#include "vbcompat.bi"

const FALSE = 0
const TRUE = NOT FALSE

''
'' File not found         return -1
'' Section not found      return -2
'' Key not found          return -3
'' Key found              return  0
'' returnvalue            value of key or defaultvalue if key is not found
''
function get_section_key_value (filename as string, _
                     section as string, _
                     key as string, _
                     returnvalue as string, _
                     defaultvalue as string = "") as integer

   dim as integer filenumber, foundsection = FALSE
   dim as string src, isrc, tmp
   
   section = "[" + trim(section) + "]"
   key = trim(key)
   defaultvalue = trim(defaultvalue)
   returnvalue = defaultvalue
   
   
   filenumber = freefile
   
   if Open (filename, for input, As filenumber) then return -1         'error opening file
   
   do until eof(filenumber)
      line input #filenumber,  isrc
      src = trim(lcase(isrc))
      if left(src,1) = "#" then continue do
      if left(src,1) = "[" then
         if foundsection then return -3
         foundsection = FALSE
      end if
      if src = section then foundsection = TRUE
      if foundsection then
         if left(src, len(key)) = key then
            tmp = trim(mid(src,len(key)+1))
            if left(tmp,1) = "=" then
               foundsection = TRUE
               returnvalue = trim(mid(tmp, 2))
               return 0
            end if
         end if
      end if
   loop
   close filenumber

   return -2
end function

'' File not found         return -1
'' key set                return  0
''
function set_section_key_value (filename as string, _
                     section as string, _
                     key as string, _
                     keyvalue as string) as integer

   dim as integer filenumber1, filenumber2, foundsection = FALSE, update = FALSE
   dim as string src, isrc, tmp
   
   section = "[" + trim(section) + "]"
   key = trim(key)
   keyvalue = trim(keyvalue)
   
   filenumber1 = freefile
   if Open (filename, for input, As filenumber1) then return -1         'error opening file
   filenumber2 = freefile
   if Open (filename + ".tmp", for output, As filenumber2) then close :return -1         'error opening file
   
   do until eof(filenumber1)
      line input #filenumber1,  isrc
      src = trim(lcase(isrc))
      if left(src,1) = "#" then
         print #filenumber2, isrc
         continue do
      end if
      if left(src,1) = "[" then
         if foundsection and update = FALSE then
            '' print out new key
            print #filenumber2, key + " = " + keyvalue
            print #filenumber2, ""
            print #filenumber2, isrc
            update = TRUE
            continue do
         end if
         foundsection = FALSE
      end if
      if src = section then foundsection = TRUE
      if foundsection then
         if left(src, len(key)) = key then
            tmp = trim(mid(src,len(key)+1))
            if left(tmp,1) = "=" then
               'print out new value
               print #filenumber2, key + " = " + keyvalue
               update = TRUE
               continue do
            end if
         end if
      end if
      print #filenumber2, isrc
   loop
   if update = FALSE then
      if not foundsection then print #filenumber2, section
      print #filenumber2, key + " = " + keyvalue
   end if
   close filenumber1
   close filenumber2
   filecopy  filename, filename + ".bak"
   filecopy  filename + ".tmp", filename
   return 0
end function

dim as string fn, rtn
dim as integer r
fn = exepath + "/skb.ini"
r = get_section_key_value(fn, "themenames", "theme3", rtn, "not found")
r = set_section_key_value(fn, "themenames", "theme3", "testing")
r = set_section_key_value(fn, "test1", "theme3", "testing")
r = set_section_key_value(fn, "test1", "theme7", "testing")

print r
print rtn


Update: Bug fix & more tests.
Last edited by Sisophon2001 on Jan 29, 2012 5:53, edited 1 time in total.
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Reading a Windows format ".ini" file

Postby rdc » Jan 28, 2012 17:25

I wrote one of these a while ago for my own needs. You can take a look here.

Even though the file type is deprecated in Windows, I like to use the format for my own programs. It is simple, yet expressive, and makes a nice little database for things like options and such. It also has the advantage (and disadvantage) of being human readable and editable which is not possible in a binary format.
KenHorse
Posts: 40
Joined: Jan 27, 2012 0:08

Re: Reading a Windows format ".ini" file

Postby KenHorse » Jan 29, 2012 1:13

Thanks guys.. exactly what I was looking for!
ytwinky
Posts: 217
Joined: Dec 03, 2005 12:44
Location: MD, Germany

Re: Reading a Windows format ".ini" file

Postby ytwinky » Jan 30, 2012 0:44

hi,
@rdc: if ever you need a german translation of your inobject.pdf
hal @ http://ytwinky.freebasic-portal.de/4all ... ect-de.pdf
As you can guess it's for all (fbusers)..
I tried to be as close as possible to the english version ;-))
regards
ytwinky
rdc
Posts: 1713
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Re: Reading a Windows format ".ini" file

Postby rdc » Jan 30, 2012 5:57

ytwinky wrote:hi,
@rdc: if ever you need a german translation of your inobject.pdf
hal @ http://ytwinky.freebasic-portal.de/4all ... ect-de.pdf
As you can guess it's for all (fbusers)..
I tried to be as close as possible to the english version ;-))
regards
ytwinky


That is very cool. My first translated document (as far as I know). :) Thanks; I'll add it to the distro.
Makoto WATANABE
Posts: 118
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Reading a Windows format ".ini" file

Postby Makoto WATANABE » Sep 27, 2016 5:42

Dear rdc;

Thank you for publishing your Ini-Object.
I translated your contents into Japanese and introduced to Japanese people.
http://makoto-watanabe.main.jp/freebasic/tipsWindowsIni.html

Please consent to this.
marcov
Posts: 2840
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Reading a Windows format ".ini" file

Postby marcov » Sep 27, 2016 8:43

rdc wrote:Even though the file type is deprecated in Windows, I like to use the format for my own programs. It is simple, yet expressive, and makes a nice little database for things like options and such. It also has the advantage (and disadvantage) of being human readable and editable which is not possible in a binary format.


I use XML for application configuration. The advantage is that it nests levels better, is still text, and the hierarchy makes it easier to stream an OO structur to it.

Return to “Windows”

Who is online

Users browsing this forum: MSN [Bot] and 1 guest