AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: September 13th, 2006, 3:20 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
I want to check if specific section in INI file is changed
Other application can change other sections at will at any time.

SO, ini looks like this:
    [Section1]
    ...
    ...
    [SectionN]
    ...
    ...
    [SectionM]

...
where M > N

Are there any suggestion how to do this?
I was thinking to read entire section in the string then compare strings on next read (first by len, then if equal char by char), but I am concerned by the speed of the reading in AHK.
FileReadLine sounds slow and FileRead can only read entire file. Then, I can use IniRead, but I suspect this to be the worst case.

Some API might help, I didn't check it out.

I would appriciate any suggestions:

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2006, 3:26 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I guess it would be simpler if you check modification time of the ini file first. If changed you can read the sections with iniread and compare them to previous read.
I read somewhere that the iniread commands are optimzed for speed. Thus it should be so much slower then FileRead in combination with string manipulation.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2006, 3:53 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Date Time can be checked but only to speed up a little, since other application using the same ini will change other sections at will.

I will of course add this, but I need a solution how to check is my section changed when file is changed.

How about adapthing this: http://www.autohotkey.com/forum/viewtopic.php?t=6113

is loop fast enough ?

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2006, 5:25 pm 
Offline

Joined: June 8th, 2006, 9:38 pm
Posts: 307
Something like this, maybe?

Code:
 
#SingleInstance force
#Persistent

SetWorkingDir % a_scriptDir

ini = TestIni.ini
section = TestSection

SetTimer, Check, 1000
return

Check:
If ( SectionChanged(ini, section) )
  Msgbox, Section changed!
return

SectionChanged(ini, section) {
static prevSection
FileRead, str, %ini%
StringTrimLeft, str, str, InStr(str, "[" section "]")-1
p := InStr(str, "`r`n[")
If ( p )
  StringLeft, str, str, p-3
If ( str!=prevSection )
  changed=1
prevSection=%str%
return changed
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2006, 6:13 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
2 Roland
Speed is the only concern here, since I have to do lengthy thing if section is changed. That means that I have to check section changes as fast as possible.

I will check out how your code behaves.
Thanx

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2006, 9:08 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
majkinetor wrote:
Speed is the only concern here

Here's something to start you off, I have no experience in structs so I don't know how to use 'OVERLAPPED':
Code:
#Persistent
file = %A_Temp%\ini.txt
struct_OVERLAPPED := 0x0
sec = SectionN

hfile := DllCall("CreateFile", Str, file, UInt, 8 << 28, UInt, 3, UInt, 0, UInt, 3, UInt, 4 << 28, UInt, 0)
OnExit, Close
size := DllCall("GetFileSize", UInt, hfile, UInt, 0)
DllCall("GetFileTime", UInt, hfile, UInt, 0, UInt, 0, UIntP, xmod)
SetTimer, Monitor, 1000
Return

Monitor:
DllCall("GetFileTime", UInt, hfile, UInt, 0, UInt, 0, UIntP, mod)
If (mod = xmod)
   Return
xmod := mod
DllCall("ReadFile", UInt, hfile, UInt, c, UInt, size, UIntP, size, UInt, struct_OVERLAPPED)
StringReplace, c, c, `r, , A
t := InStr(c, "`n[" . sec . "]`n")
If !t
   Return
StringTrimLeft, c, c, t
t := InStr(c, "`n[")
If t
   StringLeft, c, c, t
MsgBox, %c%
Return

Close:
DllCall("CloseHandle", UInt, hfile)
ExitApp


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

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Thanx Titan, the thing you just did for me was what I was initialy planning.
I will check out how it behaves, but I guess it can not be much better then that.

Thank you for doing my work. Of course, there is that OVERLAPED thingy :) I will post you here equivalent "AHK structure" if you want.

_________________
Image


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

Joined: August 28th, 2006, 6:20 pm
Posts: 22
Location: Ukraine
Quote:
Thank you for doing my work
Titan, the funny thing is that you accomplished the suggestions I made for majkinetor in his FavMenu topic :lol:
Quote:
there should be 2 tests (the smart update):
1. compare File Modification Date (for cases when TC even not got focus).
2. if change detected, load wincmd.ini to a variable, seek to the [DirMenu] section, then seek to the next section with simple pattern "`n[" if not found, then to EOF and compare this pieces. If the pieces are different, recreate the Menu.

http://www.autohotkey.com/forum/viewtopic.php?t=12412&postdays=0&postorder=asc&start=32

No, I don't want to say that majkinetor is lazy. The Modular FavMenu is a great job along with the "Constructive Language Criticism & Script Merging" great article. My contributions are only "under development" :lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2006, 12:03 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
majkinetor wrote:
Of course, there is that OVERLAPED thingy :) I will post you here equivalent "AHK structure" if you want.
Please do, not knowing this makes me feel like a noob.

stevep wrote:
Titan, the funny thing is that you accomplished the suggestions I made for majkinetor in his FavMenu topic Laughing
I didn't accomplish anything, I just showed one method of monitoring files and reading them quickly :P


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 14th, 2006, 6:27 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
Please do, not knowing this makes me feel like a noob.

I will send you the code when I adapt it for the Favmenu. Don't worry about the n00b thing... we are only humans...


Thanx again to you, and to Roland who did the same, just without API calls.


EDIT: After examing the code with more detail, I must say that I don't understand why did you use OVERLAPPED thingy at all. In MSDN it say:
Quote:
If hFile is opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure

I suspect that you wanted to find the pointer to the start of the section, then read until the end of the file. However, I am not sure if that will produce correct result since parent application may choose to delete some ini enteries thus moving the start of the section up. Because of this, you can never know where is section located after parent did changes to the ini.

Anyway, to create overlaped struct in the AHK you just have to count how many butes it ocupy. U may be confused by the c "union" keyword witch means that the same field can be used for variables of different type as needed.

Code:
union
  {   
         struct
         {     
            DWORD Offset;     
            DWORD OffsetHigh;   
         };   
         PVOID Pointer;
  }


This means that in the field of the struct where the union resides can find its place one of those 2 things inside its definiton - either unnamed struct with offsets or Pointer. This makes sense since offsets are used only for "file devices" in witch case apperently reserved field Pointer is not used, while when device is not part of file system, system keeps here some pointer for internal usage.

Be aware that the length of a union is the length of its longest variable. In this case it is 2xDWORD = 8 bytes. After sum all contained field in the original struct you will get the size you have to reserve for AHK variable. Then you use insert/extract functions to set values in this buffer on special positions (I hope Chris will create something more human)

I hope you feel just a little less n00b now :)

_________________
Image


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Maestr0 and 64 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