heres a proc I wrote to get a files name from a file path if there is a better way please post.

FileNameFromFilePath PROC cFilePath:DWORD

mov esi, cFilePath
xor ecx, ecx
@@:
cmp BYTE ptr [esi], 0
je @f
inc esi
inc ecx
jmp @b
@@:
cmp ecx, 0
jbe done
cmp BYTE PTR [esi], '\'
je @f
cmp BYTE PTR [esi], '/'
je @f
dec esi
dec ecx
jmp @b
@@:
inc esi

done:
mov eax, esi
ret
FileNameFromFilePath endp
Posted on 2002-03-23 14:59:28 by Quantum
Posted on 2002-03-23 16:09:15 by bitRAKE
cmp BYTE PTR , '/'

?
maybe '\' ? funny I can not see my backslash :)
Posted on 2002-03-23 16:28:26 by The Svin
hmm not sure what happend to that backslash:eek:
Posted on 2002-03-23 16:47:35 by Quantum
Here is the standard one in the MASM32 library. I am sure you can shave a nanosecond of it somewhere but (YAWN) why bother. :)

Regards,

hutch@movsd.com

; ########################################################################


NameFromPath proc lpPath:DWORD,lpBuffer:DWORD

push edi

mov ecx, lpPath

; -------------------------------------------------------
; Scan path buffer for "\" and put each offset + 1 from
; ecx into edx. Offset of last occurrence of "\" + 1 will
; be in edx when loop exits on zero.
; -------------------------------------------------------

@@:
mov al, [ecx]
inc ecx
cmp al, 0 ; exit condition for 1st loop
je @F
cmp al, "\" ; test for "\"
jne @B
mov edx, ecx ; if "\" put ecx+1 offset in edx
jmp @B
@@:

sub ecx, lpPath ; length in ecx
add ecx, edx ; create exit condition

; ---------------------------
; copy file name to lpBuffer
; ---------------------------
mov edi, lpBuffer
@@:
mov al, [edx]
inc edx
mov [edi], al
inc edi
cmp edx, ecx
jne @B

pop edi

ret

NameFromPath endp

; #########################################################################

Posted on 2002-03-23 19:59:44 by hutch--
It's been months since I've looked at this code. I used GetModuleFileName to get the path to my exe, then subtract the exe name, then insert my file name after the appropriate \.
Added some extra fluff so you can see where I was going. If it looks bad, it was also in my first assem app.

invoke GetModuleFileName, NULL, addr filepath, SIZEOF filepath
sub eax, exestrlen
lea edx, filepath
add edx, eax
mov filepathend, edx

mov edi, filepathend
mov ebx, fpathlen
mov esi, offset fpath
@@:
mov eax,
mov , eax
dec ebx
jg @B

invoke CreateFile, addr fpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0
mov filehandle, eax

The nest thing is I learned that I didn't need to do that. Apparently windows know where files are in the app path implicitly. If I'm wrong, I'd like to know about this.


File STRUC
bytesread dd 0
fstart dd 0
fend dd 0
fsize dd 0
heaphandle dd 0
handle dd 0
File ENDS

file File<>

fpath db "data.dat", 0

invoke CreateFile, addr fpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
mov file.handle, eax

That's all I needed, to data file is in a direcotry above the exe, but CreatFile opened it up fine. This was for loading data directly into a buffer, hence the heaphandle member of File.
Posted on 2002-03-24 06:44:09 by ThoughtCriminal