AutoHotkey Community

It is currently May 27th, 2012, 11:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 129 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9
Author Message
 Post subject:
PostPosted: September 8th, 2011, 9:35 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
djvj wrote:
Is there anyway of changing the final section name to something else while using ini_replaceSection?

You can rename it. ini_replaceSection() checks if the section name exists. If it exists, then it replaces the whole section with the replace string, which includes the section name.

Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

ini =
(
[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)
MsgBox %ini%
ini_replaceSection(ini, "Tip", "[test]`nkey=value")
MsgBox %ini%

Or can you show me with such a simple example what you mean, if not that is what you are looking for?

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2011, 11:40 pm 
Offline

Joined: September 19th, 2010, 6:30 am
Posts: 18
I think the way I explained it there makes it sound like I want to change it. It's the opposite. My original section name is [standard_Keyboard]. My destination section name in another ini is called [Keyboard]. I want to copy all the keys and values from my original section to my destination section w/o changing the section name from [Keyboard] to [standard_Keyboard].


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2011, 9:06 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Ah ok. The library does not cover such a functionality yet. Here is a workaround rename section function. It is a quick and dirty one. So if you experience problems, post it here. A test script with the function:
Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

ini_1 =
(
[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)
ini_2 =
(
[test]
Some = Thing
)

section := ini_getSection(ini_1, "Tip")
rename_section(section, "test")
ini_replaceSection(ini_2, "test", section)
MsgBox %ini_2%

rename_section(ByRef _ini, _newName)
{
    _ini := RegExReplace(_ini, "`aisUS)(*BSR_ANYCRLF)(?=(?:\R|^)\s*)\[[^\r\n]+]", "[" . _newName . "]")
}

The function renames first found section name in the ini string. Have not tested it yet, just with the test script.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


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

Joined: September 19th, 2010, 6:30 am
Posts: 18
Thank you very much tuncay, that worked!

I guess what you could do for the future is add an optional parameter to ini_replaceSection where if it is provided, it renames the section to what is supplied, or even just keeps it the same as what's existing.

The way it is now, after the script is ran once, you could never run it again because the destination section name has changed. So the output parameter of the function is never found.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2011, 6:33 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Ok, here is another workaround for that. :D I do not know how to make it simpler without changing the lib. The idea is to use replaceSection and insertSection. I changed the rename_section function so it deletes the first found section header name, for use with insertSection. I know this is complicated and uncomfortable. At least it solves your problem (I hope so).

Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

ini_1 =
(
[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)
ini_2 =
(
[test]
Some = Thing
)

section := ini_getSection(ini_1, "Tip")
rename_section(section)
ini_replaceSection(ini_2, "test", "")
MsgBox %ini_2%
ini_insertSection(ini_2, "test", section)
MsgBox %ini_2%

rename_section(ByRef _ini, _newName="")
{
    if (_newName = "")
    {
        _ini := RegExReplace(_ini, "`aisUS)(*BSR_ANYCRLF)(?=(?:\R|^)\s*)\[[^\r\n]+]\R")
    }
    else
    {
        _ini := RegExReplace(_ini, "`aisUS)(*BSR_ANYCRLF)(?=(?:\R|^)\s*)\[[^\r\n]+]", "[" . _newName . "]")
    }
}

replaceSection deletes the section. Then the extracted sections header will be deleted via rename_section (may be later an option at getSection to exclude the header will be added). After that the section without its header can be added via insertSection again.

I can see a need for such a functionality.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


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

Joined: September 19th, 2010, 6:30 am
Posts: 18
Thanks, but the last reply works fine. I was just adding that your library now has a very limited use because it changes the section name. You didn't have to make another method for my script. Thanks anyways


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 4th, 2011, 8:01 pm 
It would be much better if you'd make
SetValue instead of ReplaceValue
It would check if...

1. ...section exists -> if it doesn't -> create it
2. ...key exists -> if it doesn't -> create it
3. ...set value to key

Same goes for SetKey and SetSection


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 4th, 2011, 8:25 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
@Chavlji
Thats correct. This would need a major update and rework. But I do not work on it anymore.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 6th, 2011, 7:29 pm 
Offline

Joined: October 5th, 2011, 8:13 am
Posts: 5
NEW FUNCTIONS:

- ini_setSection ( content, ini )

- ini_setKey( section, key, value, ini )

just put the following code at the end of ini.ahk.
I think they are more convinient than replace functions.

Code:
ini_setSection( content, ByRef ini ) {
; place section into ini
; if it already exists it is replaced
   StringTrimLeft,section,content,1 ; section name is string between [ ]
   section := strUntilChr( section, "]" )
   if ini_getSection( ini, section )
      ini_replaceSection( ini, section, content ) ; if section exists we replace it
   else
      ini := ini . "`n" . content ; else we add content to end of ini
   }

; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ini_setKey( section, key, value, ByRef ini ) {
; place key into ini
; if it already exists it is replaced
   newLine := key . "=" . value
   if (ini_getKey( ini, section, key ) )
      ini_replaceKey( ini, section, key, newLine ) ; if key exists within section, we replace it
   else {
      secContent := ini_getSection( ini, content )
      if (secContent)
         secContent := secContent . "`n" . newLine ; if section exists and key doesn't we add it to end
      else
         secContent := "[" . section . "]`n" . newLine ; if even section doesn't exist, we create one
      ini_setSection( secContent, ini )  ; use upper function to set content of section with key.
      }
}

; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strUntilChr( str, char ) {
; returns string from first char from str until it finds char
Loop, parse, str
   if (A_LoopField<>char)
      result := result . A_LoopField
   else
      return result
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 129 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, MSN [Bot], nomissenrojb, Rajat and 58 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