News:

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

Handling a File

Started by Farabi, May 05, 2005, 07:38:38 AM

Previous topic - Next topic

Farabi

This is for handling file. Check the new function named OpenFileFrb. That function will create a structure of file and set everything. This is the old explanation for this. Remember, this is the old explanation. If it not work, you must find your own way to make it work. If software loading are above 5 second or there are errors, please tell me.

Hi, thank you for your attention. This time I want to share my knowledge with you. Text handling such as seek char, seek word and get word or set word is not an easy thing. If you want, you can do a search word from the begining of file to the end of file. But the counsequence is your program can be very slow.
We can make the search time is better if on the beginning of file we made a structure line each line and word per word.

Put this code on your project.


Buka proc tumpukan:dword,namafile:dword,lpukuran:dword,ukr:dword
  LOCAL hfile:dword
 
;invoke Hapus,tumpukan
invoke CreateFile,namafile,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL
mov hfile,eax
invoke ReadFile,hfile,tumpukan,ukr,lpukuran,NULL 

brs:
invoke CloseHandle,hfile

ret
Buka endp

CreateOffsetTable proc uses esi edi  lpSou:dword,lpTbl:dword,FileSize:dword,lnCnt:dword
;LOCAL rslt,Pos:dword


mov esi,lpSou
mov edi,lpTbl

xor ecx,ecx
mov [edi],ecx
add edi,4
dloop:
mov dl,[esi]
inc ecx
cmp dl,10
jnz go
inc eax
mov [edi],ecx
add edi,4
go:
inc esi
cmp ecx,FileSize
jl dloop


ret
CreateOffsetTable endp

OpenFileFrb proc uses esi edi lptz:dword
LOCAL w,p,pf:dword

; Memory for structur
; header = txt1
; lpmem file
; lpmem line table
; dword size
; line count
; for use with process function
; for use with word function

invoke GlobalAlloc,GMEM_ZEROINIT,32
mov p,eax
mov esi,eax
; Header write on
mov dword ptr[esi],'TXT1'
;lpmem file
invoke GlobalAlloc,GMEM_ZEROINIT,500000
mov [esi+4],eax
invoke Buka,[esi+4],lptz,addr w,500000
; Line Table
invoke LineCount,[esi+4],w
shl eax,2
invoke GlobalAlloc,GMEM_ZEROINIT,eax
mov [esi+8],eax
invoke CreateOffsetTable,[esi+4],[esi+8],w,NULL
cmp eax,0
jnz @f
invoke GlobalFree,p
invoke GlobalFree,[esi+4]
invoke GlobalFree,[esi+8]
mov eax,0
ret
@@:
; size
mov eax,w
mov dword ptr[esi+12],eax
; line count
invoke LineCount,[esi+4],[esi+12]
mov [esi+16],eax

invoke CreateWordTable,p

mov eax,p


ret
OpenFileFrb endp

TextFromFile proc uses esi edi nInd:dword,lpfile:dword

mov esi,lpfile

invoke GetOffsetTable,nInd,[esi+8],[esi+16]
add eax,[esi+4]

push eax
invoke LineLength,eax,200000
pop edx


ret
TextFromFile endp

Word_Count proc uses esi edi lptz:dword,nSize:dword
LOCAL stts,sizech,p:dword

mov esi,lptz
mov ecx,nSize

xor eax,eax
mov stts,eax
mov edx,eax

cmp nSize,0
jnz @f
xor eax,eax
ret
@@:

loop_each_word:
mov al,[esi]
cmp al,039h ; 39 is '1'
ja not_number
cmp al,030h
jl not_number
; mov eax,stts
; cmp eax,1
; jz not_number
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_number:

mov al,[esi]
cmp al,05ah ; 059h is 'Z'
ja not_char_up
cmp al,041h
jl not_char_up
; mov eax,stts
; cmp eax,1
; jz not_char_up
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_char_up:

mov al,[esi]
cmp al,07ah ; 07ah is 'z'
ja not_char_low
cmp al,061h
jl not_char_low
; cmp stts,1
; jz not_char_low
mov sizech,esi
mov stts,1
jmp not_ilegal_char
not_char_low:

mov eax,stts
cmp eax,1
jnz not_ilegal_char
inc edx
dec eax
mov stts,eax
sub sizech,esi
xchg edx,sizech
sub edx,esi
xchg edx,sizech

not_ilegal_char:
inc esi
dec ecx
jnz loop_each_word

mov eax,edx

ret
Word_Count endp


Word_position proc uses esi edi nPos:dword,lptz:dword,nSize:dword
LOCAL stts,sizech,p,ttc:dword

mov esi,lptz
mov ecx,nSize

xor eax,eax
mov stts,eax
mov sizech,eax
mov edx,eax

cmp nSize,0
jnz @f
xor eax,eax
ret
@@:

loop_each_word:
mov al,[esi]

cmp al,'a'
jnae not_low
cmp al,'z'
jnle not_low
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_low:

cmp al,'A'
jnae not_high
cmp al,060h
jnle not_high
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_high:

cmp al,021h
jnae not_number
cmp al,040h
jnle not_number
cmp stts,1
jz point_jmp
mov stts,1
mov sizech,esi
jmp point_jmp
not_number:

cmp stts,1
jnz point_jmp
inc edx
mov stts,0
cmp edx,nPos
jz done
point_jmp:
inc esi
dec ecx
jnz loop_each_word
xor eax,eax
ret

done:
mov edx,sizech
mov eax,sizech
sub esi,eax
mov eax,esi

ret
Word_position endp

CreateWordTable proc uses esi edi lpsf:dword
LOCAL tmp[20]:dword
LOCAL w_cnt,lew_cnt,w_ptr,td,ta:dword
LOCAL td1,tc1,offst:dword
; This function will create a word table filled with offset and size

mov esi,lpsf
lea edi,tmp


mov edx,[esi+16]
shl edx,2
invoke GlobalAlloc,GMEM_ZEROINIT,edx
mov [esi+24],eax
mov dword ptr[eax],"NANO"


mov td1,1
mov ecx,[esi+16]
loop_each_lines:
push ecx
invoke TextFromFile,td1,lpsf
invoke Word_Count,edx,eax
mov w_cnt,eax
cmp eax,0
jle no_word
xchg eax,edx
shl edx,2
invoke GlobalAlloc,GMEM_ZEROINIT,edx
mov w_ptr,eax
mov edx,[esi+24]
mov ecx,td1
dec ecx
mov [edx+ecx*4],eax

mov tc1,1
loop_each_word:
push eax
invoke TextFromFile,td1,lpsf
invoke Word_position,tc1,edx,eax
push edx
push eax
invoke GlobalAlloc,GMEM_ZEROINIT,20
mov ecx,eax
pop eax
pop edx
mov dword ptr[ecx],0
mov [ecx+4],edx
mov [ecx+8],eax
pop eax

mov edx,tc1
dec edx
mov [eax+edx*4],ecx

inc tc1
dec w_cnt
jnz loop_each_word
no_word:
pop ecx
inc td1
dec ecx
jnz loop_each_lines


ret
CreateWordTable endp

GetWord proc uses esi edi lpsf:dword,colm:dword,ind:dword

mov esi,lpsf

mov edx,[esi+24] ; lplinetbl

mov ecx,colm ; line count
cmp ecx,[esi+16]
jae no_word
dec ecx ;
mov edx,[edx+ecx*4] ; line structure, edx = lpword
mov ecx,ind ; word count
dec ecx
mov edx,[edx+ecx*4] ; word structure, edx=the structur
ret
no_word:
xor eax,eax
dec eax

ret
GetWord endp

LineCount proc uses esi edi lpTheFile:dword,FileSize:dword

mov esi,lpTheFile


xor ecx,ecx
xor eax,eax
inc eax
inc eax

dloop:
mov dl,[esi]
cmp dl,13
jnz @f
inc eax
@@:
inc esi
inc ecx
cmp ecx,FileSize
jl dloop


ret
LineCount endp



By put this function on WM_CREATE event like this.


.if uMsg == WM_CREATE
invoke OpenFileFrb,addr str_1
mov fhnd,eax
.endif


Now you have a file structured on you memory. Because the structure is not a constant structure, I will not explain to you what the structure is. If you want to get a word from a line you must use the GetWord function where the return value  is eax =-1 if the word you are searching is not found or the collumn you are use is not valid.

Edx will return to you a structure like this.
[edx+0] used for future
[edx+4] offset
[edx+8] size
[edx+12] I forgot

Dont forgot that are not every line have a word, before you use the function, use WordCount function first. And dont continue if there is no word on a line.
Believe me, handling with text is not easy and sometime it is annoying. Specially when you miss take the handle.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"