AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 166 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12  Next
Author Message
PostPosted: September 20th, 2010, 7:01 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
GZIP_DecompressBuffer()
Decompression for an in-memory 'single-file no-name GZIP' using 'Microsoft GZIP Compression DLL'

To create a single-file no-name GZIP, we may do it as
gzip.exe -k --no-name sourcefilename.extension
..and to uncompress it
gzip.exe -d sourcefilename.extension.gz

.. but this function was written for a different purpose. Certain websites use GZIP for 'content encoding' and we do not feel a thing because every popular browser decompresses them automatically ... to display the content as well when we click select a file download. Even DoFileDownload() uncompresses the file before saving it to disk.

The problem arises when we use UrlDownloadToFile. We have to use gzip.exe or compatibles to uncompress the content. I use InternetFileRead() for downloading.. and Microsoft does provide a WININET solution, but unfortunately for Vista and above.. ( I'm on XP ).. and so, I had to create this function for in-memory decompression.

About 'Microsoft GZIP Compression DLL' : gzip.dll, is a compact DLL ( 31.5 KiB ) and is only dependent on Kernel32.dll.. It definitely resides somewhere deep inside Windows folder, atleast that is what I guess..

Here follows the function along with a testing example:

Code:
GZIP_DecompressBuffer( ByRef var ) { ; 'Microsoft GZIP Compression DLL' SKAN 20-Sep-2010
; Decompress routine for 'no-name single file GZIP', available in process memory.
; Forum post : www.autohotkey.com/forum/viewtopic.php?p=384875#384875
 nSz := VarSetCapacity( var ), vSz :=  NumGet( var,nsz-4 ), VarSetCapacity( out,vsz,0 )
 DllCall( "GZIP\InitDecompression" )
 DllCall( "GZIP\CreateDecompression", UIntP,CTX, UInt,1 )
 If ( DllCall( "GZIP\Decompress", UInt,CTX, UInt,&var, UInt,nsz, UInt,&Out, UInt,vsz
    , UIntP,input_used, UIntP,output_used ) = 0 && ( Ok := ( output_used = vsz ) ) )
      VarSetCapacity( var,64 ), VarSetCapacity( var,0 ), VarSetCapacity( var,vsz,32 )
    , DllCall( "RtlMoveMemory", UInt,&var, UInt,&out, UInt,vsz )
 DllCall( "GZIP\DestroyDecompression", UInt,CTX ),  DllCall( "GZIP\DeInitDecompression" )
Return Ok ? vsz : 0
}

; Usage Example :

SetWorkingDir, %A_ScriptDir%
IfNotExist,gzip.dll, URLDownloadToFile
 , http://www.autohotkey.net/~goyyah/GZIP/gzip.dll, gzip.dll ; 32256 bytes
IfNotExist,gpl.gz, URLDownloadToFile
 , http://www.autohotkey.net/~goyyah/GZIP/gpl.gz,   gpl.gz   ;  6794 bytes

DllCall( "LoadLibrary", Str,"gzip.dll" ) ; Load GZIP library

FileRead, Var, gpl.gz
sz := GZIP_DecompressBuffer( Var )
MsgBox, 0, Expanded Size: %sz% bytes, %Var%
Return


PS: I have not written a GZIP_CompressBuffer() because VarZ wrapper is superior, in the sense that it uses documented functions.




Report this post
Top
 Profile  
Reply with quote  
 Post subject: Beep() for PC Speaker
PostPosted: October 2nd, 2010, 2:21 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
Beep()
Accepts pairs of Frequency & Duration to produce series of beeps on 'PC Speaker'.
The second parameter is useful for suppressing Beep() with a global variable.

Code:
Beep( S1="1000,100", F=1 ) {     ; Beep() for PC Speaker, by SKAN  |  Created: 02-Oct-2010
 Loop, Parse, S%F%,`,`;,%A_Space% ; www.autohotkey.com/forum/viewtopic.php?p=384875#384875
  Mod(A_Index,2) ? Frq:=A_LoopField  : DllCall( "Beep", UInt,Frq, UInt,Durn:=A_LoopField )
}

; Usage Example:

Beep( "600,250; 600,250; 650,500; 600,500; 800,500; 750,1000" )  ; 'Happy Birthday to you'



Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 5th, 2010, 12:17 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
AccuSleep()
Sleeps for specified seconds, accurately, and returns the expired time in seconds.


Code:
AccuSleep( Seconds=0.001 ) { ;  High-Resolution Sleep,  accurate upto a millisecond
 Static Freq      ; By SKAN, www.autohotkey.com/forum/viewtopic.php?p=388198#388198
 _BatchLines := A_Batchlines ;  Created : 05-Oct-2010 | Last Modified : 24-08-2011c

 IfEqual,Freq,, SetEnv, ErrorLevel, % DllCall( "QueryPerformanceFrequency", Int64P,Freq )

 DllCall( "QueryPerformanceCounter",Int64P,T1 )
 If ( Seconds > 0.032 ) {
   Sleep %  ( Seconds - 0.016 ) * 1000
   DllCall( "QueryPerformanceCounter",Int64P,T2 ), Seconds := Seconds - ((T2-T1)/Freq)
 }
 SetBatchlines, -1
 DllCall( "QueryPerformanceCounter", Int64P,pTick ), cTick := pTick
 While( ( (Tick:=(pTick-cTick)/Freq)) < Seconds ) {
   DllCall( "QueryPerformanceCounter", Int64P,pTick )
   Sleep -1
 }
 DllCall( "QueryPerformanceCounter",Int64P,T3 )
 SetBatchlines, %_BatchLines%
Return Round( (T3-T1)/Freq,3 )
}





Edit: 24-Aug-2011 Code was not AHK_Lw compatible, now rectified


Last edited by SKAN on August 24th, 2011, 1:22 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 5th, 2010, 1:52 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
@SKAN: What's the difference among AccuSleep(), QPX() and Delay()?

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 5th, 2010, 9:15 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
What's the difference among AccuSleep(), QPX() and Delay()?


Delay() never sleeps... It peaks out processor owing to the Loop used.
Using it for 5 seconds as in 'Delay( 5.000 )' will keep the processor peaked for the whole 5 seconds..
Delay() is best used to induce an accurate wait period, less than 50ms.
Anything more than 50ms is not worth the processor strain.

Sleep command Vs DllCall( "Sleep", .. )

AHK's Sleep is a wonderful command, It is asynchronous and keeps the processor at peace.
The only drawback is that the wait period will never be accurate. Refer Documentation
DllCall( "Sleep", .. ) offers same functionality as Sleep but is Synchronous. Never use it!
The following snippet demonstrates synchronous vs asynchronous sleep:
Code:
Gui, Show, w480 h240, Sleep 5000 active`, you may move the window
Sleep 5000
Gui, Show,, DllCall( "Sleep"`,UInt`,5000 ) active`, you CANNOT move the window
DllCall( "Sleep", UInt,5000 )
Gui, Show,, GUI Normal now


AccuSleep()

AccuSleep() is a combination of AHK's Sleep command and Delay()
It first uses Sleep command to induce a 'processor friendly' wait state
and then, for correcting the inaccuracy,
it runs a high-speed loop which will be for a short period less than 50ms

8)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 5th, 2010, 9:36 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Great explanation. :)
What about QPX()?

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 5th, 2010, 10:00 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
er.. other than the fact that QPX() is also based on QueryPerformanceCounter, it has nothing to do with AccuSleep() or Delay()


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 5th, 2010, 10:06 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
I know, I just wanted to take the opportunity and ask about it, so people who don't really understand what that means may have a more noob-friendly explanation. :)

I've already seen people reporting that Sleep's inaccuracy may become very problematic when dealing with older computers. Have you (or anyone else) tried AccuSleep() in this scenario? Would it definitely solve the problem?

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 6th, 2010, 8:10 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
MasterFocus wrote:
I've already seen people reporting that Sleep's inaccuracy may become very problematic when dealing with older computers. Have you (or anyone else) tried AccuSleep() in this scenario?


Valid point. No, I have not stress-tested the function. I do not have any video conversions right now. Will test it soon and reply. Thanks for bringing this. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Replicate()
PostPosted: April 11th, 2011, 10:30 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
Replicate()
Concatenates multiple copies of a string, without using a loop.

Code:
Replicate( Str, Count ) {     ; By SKAN  / CD:12-04-2011
; www.autohotkey.com/forum/viewtopic.php?p=435990#435990
  VarSetCapacity( S, Count * ( A_IsUnicode ? 2:1 ), 1  )
  StringReplace, S, S, % SubStr( S,1,1 ), %Str%, All
Return SubStr( S, 1, Count * StrLen(Str) )
}

; Usage Examples

MsgBox, % "[" Replicate( A_Space, 100 ) "]"
MsgBox, % Replicate( "B@BY" , 100 )




Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2011, 11:35 pm 
Now that's clever! :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2011, 4:02 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Awesome! You should probably mention it here.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 13th, 2011, 7:32 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
MasterFocus wrote:
You should probably mention it here.


Non-ahk coders will never understand it.
I feel the current loop example given there suits the rest-of-the-world.

Thanks. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: StrSet()
PostPosted: April 14th, 2011, 4:34 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
StrSet()
Aligns text by padding characters on left/right/both as specified in parameters

Code:
StrSet( Str, Width, Align=0, PadChar=" " ) {      ; Align  => Left=0, Right=1, Center=2
 If ( StrLen( Str ) >= Width )              ; By SKAN / CD:12-Apr-2011 / LM:14-Apr-2011
  Return SubStr( Str,1,Width ) ; www.autohotkey.com/forum/viewtopic.php?p=436843#436843
 PL := ( Width - StrLen(Str) ) // ( Align=2 ? 2:1 )
 VarSetCapacity( S, PL * ( A_IsUnicode ? 2:1 ), 1 ),   S := SubStr( S,1,PL )
 StringReplace, S, S, % SubStr( S,1,1 ),  % PadChar := SubStr( PadChar,1,1 ), All
Return Align=2 ? SubStr( S Str S PadChar,1,Width ) :  Align=1 ?  S Str : Str S
}                       

; Usage Examples

MsgBox, % "["  StrSet("Align Left"  , 20, 0 ) "]"
MsgBox, % "["  StrSet("Align Right" , 20, 1 ) "]"
MsgBox, % "["  StrSet("Align Center", 20, 2 ) "]"


MsgBox, % StrSet( A_Tab "Center padded string" A_Tab, 40, 2, "@" )
MsgBox, % "Numerical zero padding:`t" StrSet( 1234, 10, 1, "0" )




Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2011, 2:05 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Just FYI, the first two examples can be done with format(), the prototype formatting function I've included with the v2 alpha download. (It will be built in eventually.)
Code:
MsgBox, % format("[{1:-20s}]", "Align Left")
MsgBox, % format("[{1:20s}]",  "Align Right")
The syntax is a mixture of .NET's String.Format (i.e. {arg:format}) and C format specifiers minus the percent signs. It's not quite as flexible as StrSet (for padding), though.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 166 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: nomissenrojb, Rajat and 57 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