| View previous topic :: View next topic |
| Author |
Message |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
|
| Back to top |
|
 |
AtheistCrusader Grandmaster Cheater
Reputation: 6
Joined: 23 Sep 2006 Posts: 681
|
Posted: Sat Dec 15, 2007 10:26 am Post subject: |
|
|
Option Explicit
Public Function ReadIniValue(INIpath As String, KEY As String, Variable As String) As String
Dim NF As Integer
Dim Temp As String
Dim LcaseTemp As String
Dim ReadyToRead As Boolean
AssignVariables:
NF = FreeFile
ReadIniValue = ""
KEY = "[" & LCase$(KEY) & "]"
Variable = LCase$(Variable)
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
While Not EOF(NF)
Line Input #NF, Temp
LcaseTemp = LCase$(Temp)
If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
If LcaseTemp = KEY Then ReadyToRead = True
If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
If InStr(LcaseTemp, Variable & "=") = 1 Then
ReadIniValue = Mid$(Temp, 1 + Len(Variable & "="))
Close NF: Exit Function
End If
End If
Wend
Close NF
End Function
IN MODULE1
Option Explicit
Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
Dim Temp As String
Dim LcaseTemp As String
Dim ReadKey As String
Dim ReadVariable As String
Dim LOKEY As Integer
Dim HIKEY As Integer
Dim KEYLEN As Integer
Dim VAR As Integer
Dim VARENDOFLINE As Integer
Dim NF As Integer
Dim X As Integer
AssignVariables:
NF = FreeFile
ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
KEYLEN = Len(ReadKey)
ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
Temp = Input$(LOF(NF), NF)
Temp = vbCrLf & Temp & "[]"
Close NF
LcaseTemp = LCase$(Temp)
LogicMenu:
LOKEY = InStr(LcaseTemp, ReadKey)
If LOKEY = 0 Then GoTo AddKey:
HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
GoTo RenewVariable:
AddKey:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
GoTo TrimFinalString:
AddVariable:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Left$(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(Temp, LOKEY + KEYLEN + 1)
GoTo TrimFinalString:
RenewVariable:
Temp = Left$(Temp, Len(Temp) - 2)
VARENDOFLINE = InStr(VAR, Temp, Chr$(13))
Temp = Left$(Temp, VAR) & PutVariable & "=" & PutValue & Mid$(Temp, VARENDOFLINE)
GoTo TrimFinalString:
TrimFinalString:
Temp = Mid$(Temp, 2)
Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Do Until Right$(Temp, 1) > Chr$(13)
Temp = Left$(Temp, Len(Temp) - 1)
Loop
Do Until Left$(Temp, 1) > Chr$(13)
Temp = Mid$(Temp, 2)
Loop
OutputAmendedINIFile:
Open INIpath For Output As NF
Print #NF, Temp
Close NF
End Function
IN MODULE2..
Then you call it like this
WriteIniValue App.Path & "\MyTest.ini", "Default", "Text1", Text1.Text
to write
Text1.Text = ReadIniValue(App.Path & "\Settings.ini", "Data", "name")
to read
|
|
| Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
Posted: Sat Dec 15, 2007 10:43 am Post subject: |
|
|
im having some errors
(!!!!!! indicate errors)
WriteIniValue !!!!! App.Path & "\MyTest.ini", "Default", "Text1", Text1.Text
Text1.Text = ReadIniValue(App.Path & "\Settings.ini", "Data", "name")
_________________
you and me baby ain't nothing but mammals so lets do it like they do on the discovery channel |
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Sat Dec 15, 2007 11:03 am Post subject: |
|
|
| Or create a delphi lib and use vb to link to it.
|
|
| Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
Posted: Sat Dec 15, 2007 11:07 am Post subject: |
|
|
i dun have delphi since i didnt get me new 80 mb harddisk
(i ran out space)
_________________
you and me baby ain't nothing but mammals so lets do it like they do on the discovery channel |
|
| Back to top |
|
 |
pimpstonie Advanced Cheater
Reputation: 0
Joined: 27 Sep 2007 Posts: 70
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Dec 15, 2007 7:45 pm Post subject: |
|
|
@masterkert3: You should really use the API instead of writing your own parser to read and write to INI files. Just easier to use in the long run and less of a worry for errors.
_________________
- Retired. |
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Sat Dec 15, 2007 8:58 pm Post subject: |
|
|
What API??? I don't know that api.
Im gonna use the API viewer brb.
Edit: Cant find windows api for that all I found was RegRead... Etc
ANd I do remember delphi having that TIniRead Class.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Dec 16, 2007 12:44 am Post subject: |
|
|
GetPrivateProfileString
WritePrivateProfileString
Heres a quick toss-together example. Put this code into a module:
| Code: | Option Explicit
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'//
'// INI File Name
'//
Private Const cINIFile = "testing.ini"
'//
'// Read Section Key
'//
Public Function ReadSectionKey(strSection As String, strKey As String) As String
Dim strReturn As String
Dim strFile As String
strFile = App.Path & "/" & cINIFile
strReturn = String(255, Chr(0))
ReadSectionKey = Left(strReturn, GetPrivateProfileString(strSection, ByVal strKey, "", strReturn, Len(strReturn), strFile))
End Function
'//
'// Write Section Key
'//
Public Function WriteSectionKey(strSection As String, strKey As String, strNewVal As String) As Boolean
Dim dwReturn As Long
Dim strFile As String
strFile = App.Path & "/" & cINIFile
dwReturn = WritePrivateProfileString(strSection, strKey, strNewVal, strFile)
WriteSectionKey = CBool(dwReturn)
End Function
|
Then change the following line to the name of your INI file:
| Code: | | Private Const cINIFile = "testing.ini" |
Next, to use the code you would do the following. Lets use this example ini for this:
| Code: | [READING]
Key1= 0
Key2= Hello!
Key3= 12345
[WRITING]
Key1=
Key2=
Key3= |
So we have two sections, READING and WRITING each with 3 keys. So to read the keys we can do:
| Code: | Dim strKeyVal1 As String
Dim strKeyVal2 As String
Dim strKeyVal3 As String
strKeyVal1 = ReadSectionKey("READING", "Key1")
strKeyVal2 = ReadSectionKey("READING", "Key2")
strKeyVal3 = ReadSectionKey("READING", "Key3") |
And an example to write new values:
| Code: | Call WriteSectionKey("WRITING", "Key1", "New value for key 1")
Call WriteSectionKey("WRITING", "Key2", "New value for key 2")
Call WriteSectionKey("WRITING", "Key3", "New value for key 3") |
If the key does not exist when you are writing it, it will create it automatically. Same goes for the section as well. If reading fails, the return will be empty.
This has no error handling and so on so like I said, basic toss together example that I just wrote.
_________________
- Retired. |
|
| Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
Posted: Sun Dec 16, 2007 4:44 am Post subject: |
|
|
What buttons/textboxes do i need ?
_________________
you and me baby ain't nothing but mammals so lets do it like they do on the discovery channel |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Dec 16, 2007 9:44 am Post subject: |
|
|
| haxory' wrote: | | What buttons/textboxes do i need ? |
Depends on how you plan to use the INI file. You might not need any at all. Read the values into variables if anything then use the variable as needed.
_________________
- Retired. |
|
| Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
Posted: Mon Dec 17, 2007 8:01 am Post subject: |
|
|
im not following
_________________
you and me baby ain't nothing but mammals so lets do it like they do on the discovery channel |
|
| Back to top |
|
 |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Mon Dec 17, 2007 8:05 am Post subject: |
|
|
| haxory' wrote: | | im not following |
|
|
| Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
|
| Back to top |
|
 |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Mon Dec 17, 2007 8:19 am Post subject: |
|
|
| haxory' wrote: | if its so
you may help me |
He means you can use variables (Like dim ____ as string) to save data, temporarily.
|
|
| Back to top |
|
 |
|