May 01, 2024, 03:15:36 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


*.INI Files

Started by LarryMc, September 25, 2013, 02:47:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

RitchieF asked me this in an email.
Quotehow do you handle the ini files?

Is it your own routine or has IWBasic built in commands to read/write ini-files I didn't find til now ?

Thanks

Richard

What I use is something I got off CodingMonkeys years ago.


Declare extern WritePrivateProfileInt Alias WritePrivateProfileStringA(lpAppName:String, lpKeyName:String, lpValue:Int, lpFileName:String),Int
   ' NOTE: lbValue declared as Pointer to allow passing a string pointer containing
   '       the value or a NULL pointer used by iniDelKey

global sub IReadI(iniFile:String, section:String, key:String, defaultInt:Int),int
   ' write an integer from the specified ini file
   int result = 0
   If (iniFile<>"" & section<>"" & key<>"")
      result = GetPrivateProfileIntA(section, key, defaultInt, iniFile)
   EndIf
   Return result
endsub

global sub IReadS(iniFile:String, section:String, key:String, defaultString:String),string
   ' write a string from the specified ini file
   istring buffer[2048], value[2048]=""
   int bufferLen=2048, result=-1
   If (iniFile<>"" & section<>"" & key<>"")
      buffer = Space$(bufferLen)
      result = GetPrivateProfileStringA(section, key, defaultString, buffer, bufferLen, iniFile)
      If result > 0
         value = Left$(buffer, result)
      Else
         value = defaultString
      EndIf
   EndIf
   Return value
endsub

global sub IWriteI(iniFile:String, section:String, key:String, value:Int),int
   ' write an integer to the specified ini file
   Return IWriteS(iniFile, section, key, Str$(value))
endsub

global sub IWriteS(iniFile:String, section:String, key:String, value:String),int
   ' write a string to the specified ini file
   If (iniFile<>"" & section<>"" & key<>"")
      Return WritePrivateProfileString(section, key, value, iniFile)
   EndIf
   Return -1
endsub

global sub IDelSect(iniFile:String, section:String),int
   ' attempts to remove the specified section from the ini file
   int result = -1
   If (iniFile<>"" & section<>"")
      Pointer p=null
      result = WritePrivateProfileSectionA(section, p, iniFile)
   EndIf
   Return (result <> 0)
endsub

global sub IDelKey(iniFile:String, section:String, key:String),int
   ' attempts to remove the specified key from the section
   int result = -1
   If (iniFile<>"" & section<>"" & key<>"")
      result = WritePrivateProfileInt(section, key, 0, iniFile)
  EndIf
   Return (result <> 0)
endsub

global sub EnumProfile(string iniPath, opt pointer pszSectionName=0),pointer
   pointer list = 0
   pointer memo = new(char,65536)

   if (memo)
      if (GetPrivateProfileString(pszSectionName, NULL, NULL, memo, 65536, iniPath))
         pointer pos = memo

         while (*<char>pos)

            int cch = len(*<string>pos)
            ' create the list only when we need it
            if (!list) then list = ListCreate()
            ' add *<string>pos to the list
            if (list)
               pointer pData = new(char,cch+1)
               if (pData)
                  *<string>pData = *<string>pos
                  ListAdd(list, pData)
               endif
            endif
            ' jump to the next string
            pos = *<byte>pos[cch+1]
         wend
      endif
      delete memo
   endif
   return list
endsub
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

RitchieF

Thanks for the informations, Larry!

Richard