AutoHotkey Community

It is currently May 27th, 2012, 10:14 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: September 4th, 2006, 12:16 am 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
Would anyone know how to do this or give me a headstart?

-Kerry

_________________
String Manipulator - GrabIco


Last edited by Kerry on September 4th, 2006, 1:08 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 12:33 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
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
}

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 9:33 am 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 9:44 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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 8)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 10:42 am 
Offline

Joined: November 17th, 2005, 10:14 pm
Posts: 196
Location: Leicester, UK
Kerry, don't feel bad :lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 11:20 am 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 11:33 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
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
}

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2006, 9:23 pm 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
Ok, cool, thank you!

_________________
String Manipulator - GrabIco


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2006, 7:08 am 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Maestr0 and 63 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group