AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Improved IniRead (reads spaces)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Kerry



Joined: 20 Jul 2006
Posts: 146
Location: Los Angeles

PostPosted: Sun Sep 03, 2006 11:16 pm    Post subject: Improved IniRead (reads spaces) Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Titan



Joined: 11 Aug 2004
Posts: 5376
Location: /b/

PostPosted: Sun Sep 03, 2006 11:33 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Kerry



Joined: 20 Jul 2006
Posts: 146
Location: Los Angeles

PostPosted: Mon Sep 04, 2006 8:33 am    Post subject: Reply with quote

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
Code:

[User]
Name= s s


-Kerry
_________________
String Manipulator - GrabIco
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Sep 04, 2006 8:44 am    Post subject: Reply with quote

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 Cool
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
MsgBox



Joined: 17 Nov 2005
Posts: 179
Location: Leicester, UK

PostPosted: Mon Sep 04, 2006 9:42 am    Post subject: Reply with quote

Kerry, don't feel bad Laughing
Back to top
View user's profile Send private message Visit poster's website
Kerry



Joined: 20 Jul 2006
Posts: 146
Location: Los Angeles

PostPosted: Mon Sep 04, 2006 10:20 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Titan



Joined: 11 Aug 2004
Posts: 5376
Location: /b/

PostPosted: Mon Sep 04, 2006 10:33 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Kerry



Joined: 20 Jul 2006
Posts: 146
Location: Los Angeles

PostPosted: Mon Sep 04, 2006 8:23 pm    Post subject: Reply with quote

Ok, cool, thank you!
_________________
String Manipulator - GrabIco
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Kerry



Joined: 20 Jul 2006
Posts: 146
Location: Los Angeles

PostPosted: Tue Sep 12, 2006 6:08 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group