News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

GetFileExtension

Started by Vortex, July 14, 2007, 07:11:30 AM

Previous topic - Next topic

Vortex

Here is a GetFileExtension procedure. It reads the extension of a filename including the dot separator. The only condition to use the procedure is to avoid NULL pointers.

.386
.model flat,stdcall
option casemap:none

.data

GFEtable db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
           db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
.code

GetFileExtension PROC uses esi _file:DWORD


mov esi,_file
mov edx,OFFSET GFEtable
xor ecx,ecx
l0:
mov eax,esi ; The procedure returns the address of the filename
@@: ; if there is no extension
add esi,1
mov cl,BYTE PTR [esi]
cmp BYTE PTR [edx+ecx],0
je @b
cmp cl,'.'
je l0
ret

GetFileExtension ENDP

END



[attachment deleted by admin]

jdoe

Vortex,

You must scan the path backward to find the extension.

Your function will fail on this path...

C:\TEST.TEST.TXT

And your function should be called GetExtension because the returned extension could be one of a folder.

:wink


arjuns

Exactly, above code fails to work on the scenerio that jdoe.I had that problem once but managed to make it  :bdg
regards

Vortex

jdoe,

The extension of the file TEST.TEST.TXT located on the root of partition C

C:\TEST.TEST.TXT

is

.TXT

This is what the procedure returns : Extension = .TXT

Where is the problem? On my Win Xp Pro Sp2 machine, I can see that the extension of this file .txt



MichaelW

On my Windows 2000 SP4 system, for "C:\TEST.TEST.TXT" I get "Extension = .TXT". The logic looks correct to me and I get correct results for all of the filenames I tried.
eschew obfuscation

Vortex

Hi Michael,

Many thanks for testing the routine.

jdoe

Sorry Vortex, it's my mistake. I read the code too fast.

:red


Vortex

No problem Jdoe, everything is fine.

Grincheux

What about "Test..txt" ?
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

Vortex

Quote from: Grincheux on July 21, 2007, 02:31:00 PM
What about "Test..txt" ?

The routine gives the correct result :

.txt

There is no problem with that file name as the algo scans for the last dot in the string.

Grincheux

I have no time to make test, but it seems ther is a problem with a file named "..txt" because the second table byte is 0.
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

ramguru

I made a little thinking, and something came out  :lol
I cannot say that this routine is faster (MichaelW's timer routines show - no, Agner's - yes),
at least it's creative one...

.data

GFEtable2 db 22,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,6,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
db 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
.code

GetFileExtensionR PROC uses esi edi _file:DWORD

mov edx, OFFSET GFEtable2
mov esi, _file
mov edi, $
nop
; :
mov eax, esi
; :
add esi, 1
xor ecx, ecx
mov cl, BYTE PTR [esi]
mov cl, BYTE PTR [edx+ecx]
add ecx, edi
jmp ecx

ret

GetFileExtensionR ENDP

Grincheux

Is there an infinite loop ?
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

Vortex

Quote from: Grincheux on July 21, 2007, 04:16:35 PM
I have no time to make test, but it seems ther is a problem with a file named "..txt" because the second table byte is 0.

Grincheux,

Please test the routine before making comments about the functionnality of the algo, OK?

There is no problem with the file name ..txt

The correct result by executing the procedure :

Extension = .txt

Vortex

Quote from: Grincheux on July 21, 2007, 06:12:07 PM
Is there an infinite loop ?

No. There is no any infinite loop. Depending on the array member's value, ecx is adjusted to jump to mov eax, esi , add esi, 1 or pop edi You can verify this by examining the procedure with Ollydbg. Ramguru, thanks for your contribution. Nice desing.