 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
majkinetor
Joined: 24 May 2006 Posts: 3644 Location: Belgrade
|
Posted: Wed Sep 13, 2006 2:20 pm Post subject: Detecting INI section changes |
|
|
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: _________________
 |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Wed Sep 13, 2006 2:26 pm Post subject: |
|
|
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  |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3644 Location: Belgrade
|
Posted: Wed Sep 13, 2006 2:53 pm Post subject: |
|
|
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 ? _________________
 |
|
| Back to top |
|
 |
Roland
Joined: 08 Jun 2006 Posts: 244
|
Posted: Wed Sep 13, 2006 4:25 pm Post subject: |
|
|
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
} |
|
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3644 Location: Belgrade
|
Posted: Wed Sep 13, 2006 5:13 pm Post subject: |
|
|
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 _________________
 |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Wed Sep 13, 2006 8:08 pm Post subject: |
|
|
| 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 |
|
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3644 Location: Belgrade
|
Posted: Thu Sep 14, 2006 7:20 am Post subject: |
|
|
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. _________________
 |
|
| Back to top |
|
 |
stevep
Joined: 28 Aug 2006 Posts: 20 Location: Ukraine
|
Posted: Thu Sep 14, 2006 10:36 am Post subject: |
|
|
| 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
| 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"  |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5376 Location: /b/
|
Posted: Thu Sep 14, 2006 11:03 am Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3644 Location: Belgrade
|
Posted: Thu Sep 14, 2006 5:27 pm Post subject: |
|
|
| 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  _________________
 |
|
| 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
|