 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Kerry
Joined: 20 Jul 2006 Posts: 146 Location: Los Angeles
|
Posted: Sun Sep 03, 2006 11:16 pm Post subject: Improved IniRead (reads spaces) |
|
|
Would anyone know how to do this or give me a headstart?
-Kerry _________________ String Manipulator - GrabIco
Last edited by Kerry on Mon Sep 04, 2006 12:08 am; edited 1 time in total |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Sun Sep 03, 2006 11:33 pm Post subject: |
|
|
Try this function | Code: | IniRead(Filename, Section, Key, Default = "") {
FileRead, text, *t %Filename%
StringTrimLeft, text, text, InStr(text, "`n[" . Section . "]`n")
Loop, 8 {
sp := sp . " "
StringReplace, text, text, %Key%%sp%=, %Key%=
If ErrorLevel
Break
}
start := InStr(text, "`n" . Key . "=")
If !start
Return, Default
start += StrLen(Key) + 2
StringMid, Value, text, start, InStr(text, "`n", false, start) - start
Return, Value
} |
_________________
 |
|
| Back to top |
|
 |
Kerry
Joined: 20 Jul 2006 Posts: 146 Location: Los Angeles
|
Posted: Mon Sep 04, 2006 8:33 am Post subject: |
|
|
I tried it.. wasn't working for me:
test.ahk
| Code: |
IniRead(Filename, Section, Key, Default = "") {
MsgBox %Filename%
FileRead, text, *t %Filename%
StringTrimLeft, text, text, InStr(text, "`n[" . Section . "]`n")
Loop, 8 {
sp := sp . " "
StringReplace, text, text, %Key%%sp%=, %Key%=
If ErrorLevel
Break
}
start := InStr(text, "`n" . Key . "=")
If !start
Return, Default
start += StrLen(Key) + 2
StringMid, Value, text, start, InStr(text, "`n", false, start) - start
Return, Value
}
IniFile := "test.ini"
string := IniRead(IniFile, User, Name)
MsgBox |%string%|
IniRead NewString, test.ini, User, Name
MsgBox |%NewString%|
|
test.ini
-Kerry _________________ String Manipulator - GrabIco |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Mon Sep 04, 2006 8:44 am Post subject: |
|
|
| Kerry wrote: | IniFile := "test.ini"
string := IniRead(IniFile, User, Name)
MsgBox |%string%|
IniRead NewString, test.ini, User, Name
MsgBox |%NewString%|
[/code] | Command vs. function:
string := IniRead(IniFile, "User", "Name")
IniRead NewString, test.ini, User, Name
or add:
User = User
Name = Name
before the function call  _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
MsgBox
Joined: 17 Nov 2005 Posts: 179 Location: Leicester, UK
|
|
| Back to top |
|
 |
Kerry
Joined: 20 Jul 2006 Posts: 146 Location: Los Angeles
|
Posted: Mon Sep 04, 2006 10:20 am Post subject: |
|
|
Thanks MsgBox... lol
Well I tried both of those and it's still not working, but maybe I can redeem myself by fixing the function?
This worked (note the commented line)
And thanks for that PhilHo... I believe my lack of sleep is finally catching up with me
| Code: |
IniRead(Filename, Section, Key, Default = "") {
FileRead, text, *t %Filename%
StringTrimLeft, text, text, InStr(text, "`n[" . Section . "]`n")
Loop, 8 {
sp := sp . " "
StringReplace, text, text, %Key%%sp%=, %Key%=
If ErrorLevel
Break
}
start := InStr(text, "`n" . Key . "=")
If !start
Return, Default
start += StrLen(Key) + 2
StringMid, Value, text, start
;, InStr(text, "`n", false, start) - start
Return, Value
}
string := IniRead("test.ini", "User", "Name")
MsgBox |%string%|
|
_________________ String Manipulator - GrabIco |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Mon Sep 04, 2006 10:33 am Post subject: |
|
|
With that, you'll get wrong values with other keys. Use this improved version instead: | Code: | IniRead(Filename, Section, Key, Default = "") {
FileRead, text, *t %Filename%
text = `n%text%`n
StringTrimLeft, text, text, InStr(text, "`n[" . Section . "]`n")
Loop, 8 {
sp := sp . " "
StringReplace, text, text, %Key%%sp%=, %Key%=
If ErrorLevel
Break
}
start := InStr(text, "`n" . Key . "=")
If !start
Return, Default
start += StrLen(Key) + 2
StringMid, Value, text, start, InStr(text, "`n", false, start) - start
Return, Value
} |
_________________
 |
|
| Back to top |
|
 |
Kerry
Joined: 20 Jul 2006 Posts: 146 Location: Los Angeles
|
|
| Back to top |
|
 |
Kerry
Joined: 20 Jul 2006 Posts: 146 Location: Los Angeles
|
Posted: Tue Sep 12, 2006 6:08 am Post subject: |
|
|
Titan, would it be easy to write an IniWrite that has the same functionality? The same one will write spaces, but it won't get rid of them. In other words, it adds spaces, but doesn't set it to be the amount of spaces in a variable.
-Kerry _________________ String Manipulator - GrabIco |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|