Hello

How do you parse a buffer.
I am asking this because I want to parse commands that I receive from a server socket.
I do it like this, but it seems that allocating a buffer each time the server sends me some text is not good. What is the best way to do this. The code is not tested but should be similar to this:



.data
szDelimeter db 13,10
.data?
szBuffer db 1024 dup (?)
.code

OnRead proc
LOCAL buflen:dword, lpBuffer:dword, index:dword

; append socket buffer to global buffer
invoke ioctlsocket, hsocket, FIONREAD, ADDR buflen
invoke GlobalAlloc, GPTR, buflen
mov lpBuffer, eax
invoke recv, hsocket, eax, buflen, 0
invoke lstrcat, ADDR szBuffer, lpBuffer
invoke GlobalFree, lpBuffer

; try to read complete command
invoke Instring, 1, ADDR szBuffer, ADDR szDelimeter
mov index, eax
.while (index > 0)
invoke GlocalAlloc, GPTR, eax
mov lpBuffer, eax
invoke lstrcpyn, eax, ADDR szBuffer, index
invoke ProcessCommand, lpBuffer
invoke GlobalFree, lpBuffer

; remove complete command + #13#10

invoke Instring, 1, ADDR szBuffer, ADDR szDelimeter
mov index, eax
.endw
OnRead endp


Thank you for helping
Posted on 2004-02-04 13:19:50 by maCo
Not sure if this is the best way to do this but:
I usually scan the buffer and change all spaces to NULL bytes. Then I use lstrcmpi on the buffer wich will only act on the first command found in the buffer since it is now null terminated. To get to the next command I use strlen function and add the return value to the buffer pointer +1 and then do lstrcmpi on the next command and so on untill I reach the end of the buffer. If you do this you should make sure the buffer is filled with zeros before putting data in it. You could receive data byte at a time saving it to a buffer untill you receive the new line charactor or copy it to a temporary bufffer.
Posted on 2004-02-04 17:35:38 by ENF