News:

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

File parsing

Started by EddieB, November 02, 2006, 07:18:53 PM

Previous topic - Next topic

EddieB

Hello there,

I'm still fairly new to assembler and the hole setup, I know the basics and how to create a usable application. I'm in the process of writing an application that takes a file and splits it up, sort of like tokenizing I wish to end up with.

At the end of this tool, I would have liked to make it open up the window include file ( for C++ ;) ) and find strings starting with WM_, then  carry on and retrieve the hex value and the name. For example:

#define WM_MOVE                         0x0003

From winuser.h.

I can sort the rest of the applications, it's the file parsing that has me boggled, and introduction code to this with explanation of how it works would be much appreciated.

Cheers,
Eddie

Tedd

1. Memory-map the file
2. Make a function the returns one line of text on each call (from current offset, upto newline.)
3. Make a function that splits a string (or line of text :wink) into 'tokens' -- runs of characters separated by whitespace (spaces and tabs)
4. Use the above to parse the file a line at a time, and do whatever you choose with the tokens in each line :dance:

(You could join parts of these steps together, such as parsing the tokens while you find them, but this is a general outline of what you'll need to do.)
No snowflake in an avalanche feels responsible.

EddieB

I know the theory for sorting out this file, its just my lack of knowledge when it comes to ASM..

I found a short example that comes with masm that I should be able to follow :)

EddieB

Hey again,

Ive started with some code, It goes through the tokens and adds it to the edit box, resulting in the last word in the file.

Heres my code:

include \masm32\include\masm32rt.inc

; Prototype, Constants and Data sections, index to Declared Procedures
;-----------------------------------------------
DlgProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

;Data section
.data
token db "#define WM_",0

;Data? section
.data?
hInstance     HINSTANCE ?
CommandLine     LPSTR ?
posT            dd ?

;Const section
.const
IDC_ENAME       equ 1002
IDC_EVALUE      equ 1003
IDC_PREV        equ 1005
IDC_NEXT        equ 1006

;Code section
.code

start:
invoke GetModuleHandle, NULL
mov       hInstance,eax
invoke InitCommonControls
invoke DialogBoxParam, hInstance, 1000,NULL,ADDR DlgProc,NULL
invoke ExitProcess,eax


DlgProc PROC hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

      LOCAL hMem  :DWORD                                            ;loaded file memory
      LOCAL flen  :DWORD                                            ;loaded file length
      LOCAL hBuf  :DWORD                                            ;buffer handle
      LOCAL rpos  :DWORD                                            ;pointer to next read position
      LOCAL hArr  :DWORD                                            ;array handle
      LOCAL pmain :DWORD                                            ;array main memory handle
      LOCAL acnt  :DWORD                                            ;count of arguments returned by tokeniser
                                                                    ;loaded file memory
           
.IF uMsg==WM_CLOSE
invoke EndDialog, hWnd,NULL                        ;Close the dialog
.ELSEIF uMsg==WM_INITDIALOG
invoke LoadIcon,hInstance,200                      ;Load the icon
invoke SendMessage, hWnd, WM_SETICON, 1, eax       ;Set the icon

            mov hMem, InputFile("winuser.h")                        ;Open the file
            mov flen, ecx
            mov hBuf, alloc(flen)
            mov hArr, rv(create_array,64,1024)                      ;allow 64 args of 1k each
            mov pmain, ecx
            push esi
            mov rpos, 0 
                                                                       
        stlp:
            mov rpos, rv(get_ml,hMem,hBuf,rpos)                     ;get the next statement
            cmp rpos, 0
            jz stout
            mov acnt, rv(parse_line,hBuf,hArr)                      ;split it into tokens
            mov esi, hArr
        @@:
            add esi, 4
            sub acnt, 1
            jnz @B
            jmp stlp
            invoke SetDlgItemText,hWnd,IDC_ENAME,[esi]
      stout:
            pop esi                                                   
            free hArr
            free pmain
            free hMem
            free hBuf

      .ELSEIF uMsg == WM_COMMAND
                .IF wParam == 1006   
               

                                 
                .ENDIF
      .ELSE
mov eax,FALSE
ret
.ENDIF

mov eax,TRUE
ret
DlgProc ENDP

END start


I was wondering of some one could adapt and allow me to view the next token when the next button is pressed?

Cheers

Tedd

Move the 'cleanup' from initdialog into close -- the local variables used to store the various handles, memory pointers, etc, will need to be made into global variables (put them in the .data? section)
Now your file will stay open all the time the dialog is, and close when it closes.
Next step: when the button is actually pressed, do the same for getting the next string as you did in the initialisation -- except you don't need to re-open the file, or allocate the memory, or any of that setup. And obviously you'll need to remember where you get to in the file each time (another global variable.)


{Yes we could just do it for you, but you'll learn better - it should actually sink in :lol - if you struggle through it :wink}
No snowflake in an avalanche feels responsible.

EddieB

Yeah, Im getting there now :)

I have list boxes and have my file initiated.. working on the parsing now...

Cheers

EddieB

Sorry for one post after another...

I have my items loaded into a list box, but I want to filter whats added... can some one advise me on how to check strings in assembler?

Cheers

EddieB

I don't have the worlds greatest asm skills, but I produced what I needed and it works well.. My First asm project :)


include \masm32\include\masm32rt.inc

.data
cmpstrx db 'define',0

.data?
hInstance dd ?
hNameBox dd ?
hHexBox dd ?
IndexItem dd ?
buffer db 10 dup(?)

.const
IDD_MAINDIALOG equ 101
IDC_WMNAME equ 1002
IDC_WMHEX equ 1003


;Proc
DlgProc   PROTO   :HWND,:UINT,:WPARAM,:LPARAM

.code

start:

invoke GetModuleHandle,NULL
mov    hInstance,eax
    invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_MAINDIALOG,NULL,addr DlgProc,NULL
invoke ExitProcess,0

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


DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

LOCAL hMem  :DWORD                              ;loaded file memory
LOCAL flen  :DWORD                              ;loaded file length
LOCAL hBuf  :DWORD                              ;buffer handle
LOCAL rpos  :DWORD                              ;pointer to next read position
LOCAL hArr  :DWORD                              ;array handle
LOCAL pmain :DWORD                              ;array main memory handle
LOCAL acnt  :DWORD                              ;count of arguments returned by tokeniser
                                           
mov eax,uMsg
.if eax==WM_INITDIALOG
invoke GetDlgItem,hWin,1002
mov hNameBox,eax
invoke GetDlgItem,hWin,1003
mov hHexBox,eax

mov hMem, InputFile("winuser.h")           
        mov flen, ecx
        mov hBuf, alloc(flen)
        mov hArr, rv(create_array,64,1024)         
        mov pmain, ecx
        push esi
        mov rpos, 0   
  stlp:
    mov rpos, rv(get_ml,hMem,hBuf,rpos)         
    cmp rpos, 0
    jz stout
    mov acnt, rv(parse_line,hBuf,hArr)         
    mov esi, hArr
  @@:
  invoke szCmp,[esi],addr cmpstrx
  .if eax == 0
  jmp skip
  .endif
invoke SendMessage,hNameBox,LB_ADDSTRING,0,[esi+4] 
invoke SendMessage,hHexBox,LB_ADDSTRING,0,[esi+8]           
skip:       
    add esi, 4
    sub acnt,1
    jnz @B
    jmp stlp
   stout:
        pop esi                                                   
        free hArr
        free pmain
        free hMem
        free hBuf
.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0

.elseif uMsg==WM_COMMAND
mov edx,wParam
shr edx,16
.IF dx==LBN_SELCHANGE
invoke SendMessage,[lParam],LB_GETCURSEL,0,0   ;Get the cursors selection index
         mov IndexItem,eax
         invoke SendMessage,hNameBox,LB_SETCURSEL,IndexItem,0 ;Update both positions
  invoke SendMessage,hHexBox, LB_SETCURSEL,IndexItem,0
.ENDIF
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp

end start


EddieB

Heres the hole source with project files.

[attachment deleted by admin]

EddieB

Heres the FULL package, should be able to build this one ;)

[attachment deleted by admin]