Specialised Projects > Assembler/Compiler Technology

how to implement auto sizing strings

(1/2) > >>

mhmooney:
Have a hobby parser, code generator that shells to masm32 to create a exe. Im trying to figure out the best way to do a basic string, bstr, ansi, maybe unicode, something that I can automatically resize as needed. I assume id create a  pointer to a memory block to hold the string. Guess my question is should I use stack based, malloc, or heap. Im interested in any intuition on how to implement and other considerations.

fearless:
This might help: https://www.quora.com/How-does-C++-implement-a-string
Have a field that contains a pointer to the buffer that contains the text and a field that contains the size of the buffer used in the "string". Can then code functions to take the address and look for the pointer and size for fields based on that structure.

TouEnMasm:
THe number of bstr is limited by the size in dword of bstr_array

--- Quote ---; #########################################################################

      .386                      ; force 32 bit code
      .model flat, stdcall      ; memory model & calling convention
      option casemap :none      ; case sensitive

      ; -------------------------------------------------------------------
      ; Les fichiers inclus içi ,AUCUN .lib c'est au programme utilisateur
      ;   de les fournir
      ; -------------------------------------------------------------------
       include \masm32\include\windows.inc
   include \masm32\include\kernel32.inc
   include \masm32\include\oleaut32.inc
      ; include \masm32\include\gdi32.inc
      ; include \masm32\include\user32.inc
      ;   include \masm32\include\kernel32.inc
      ; include perso32.inc
      ;  include macros.txt   

      ; ---------------------------------------------------------
      ; Les PROTO sont tous regroupés dans un fichier .inc du même
      ; nom que la librairie .lib
      ; ---------------------------------------------------------

      ; CreateBstr PROTO :DWORD
   ; FreeBSTR PROTO
   ;UtoA PROTO  :DWORD
   ;AtoU PROTO  :DWORD

   ;BSTR MACRO arg
   ;   invoke CreateBstr,SADR(arg)
   ;   EXITM<eax>
   ;ENDM

      ;exemples de definitions externes
      ;    EXTERNDEF Copier :PROTO  :DWORD,:DWORD
      ;    EXTERNDEF EtatMemoirePile :MEMORY_BASIC_INFORMATION
      ;    EXTERNDEF PoriginePile:DWORD         
   
   .data   
bstr_array dd 50 dup (0)
buffer db MAX_PATH dup (0)
ubuffer dw MAX_PATH dup (0)
    .code

;################################################################
UtoA PROC  pChain:DWORD
   invoke WideCharToMultiByte,CP_ACP,NULL,pChain,\
      -1,addr buffer,LENGTHOF buffer,NULL,NULL
   .if eax != 0
      mov edx,eax
      lea eax,buffer
   .endif              
         ret
UtoA endp

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

AtoU PROC  pChain:DWORD
   invoke MultiByteToWideChar,CP_ACP,NULL,pChain,\
      -1,addr ubuffer,LENGTHOF ubuffer
   .if eax != 0
      mov edx,eax
      lea eax,ubuffer
   .endif       
         ret
AtoU endp


;################################################################
;use bstr_array in data
CreateBstr PROC  pChain:DWORD 
         Local  retour:DWORD
         Local   phrase[MAX_PATH]:WORD
   mov eax,pChain
   invoke MultiByteToWideChar,CP_ACP,NULL,pChain,\
      -1,addr phrase,LENGTHOF phrase 
   invoke SysAllocString,addr phrase
   mov retour,eax   
   .if eax == 0
      ;invoke MessageBox,NULL,SADR("FAILED"),SADR("MultiByteToWideChar"),MB_OK
      jmp FindeCreateBstr
   .endif
   ;---------------- save pointer in an array BSTR -----------------
   lea edx,bstr_array
   .LISTALL
   mov ecx,LENGTHOF bstr_array
   .NOLIST
   @@:
   .if dword ptr [edx] == 0
      mov [edx],eax
   .else
      dec ecx
      .if ecx != 0   ;not the end of array
         add edx,4
         jmp @B
      .else
         ;failed to save bstr pointer         
         ;invoke MessageBox,NULL,SADR("BSTR array too small"),SADR("ERREUR"),MB_OK
         invoke SysFreeString,retour
         mov retour,0         
      .endif
   .endif
 FindeCreateBstr:
         mov eax,retour
         ret
CreateBstr endp

;################################################################
;use bstr_array in data
FreeBSTR PROC uses esi
         Local count:DWORD
         Local  retour:DWORD
         mov retour,1
   mov ecx,LENGTHOF bstr_array
   mov count,ecx
   lea esi,bstr_array
   @@:
   .if dword ptr [esi] != 0
      mov edx,dword ptr [esi]
      invoke SysFreeString,edx
      mov dword ptr [esi],0
      add esi,4
      dec count
      jnz @B
   .endif
 FindeFreeBSTR:
         mov eax,retour
         ret
FreeBSTR endp


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

end


--- End quote ---

mhmooney:
Thanks for the responses, gives me a starting point to consider.

TouEnMasm:
Very simple.

--- Quote ---invoke CreateBstr,addr phrase  ; phrase db "bla blate ",0     could be repeat size of bstr_array in dword(50 times)
                                              ;the bstr is in eax (or 0 if failed) and in the bstr_array
;When you have finished used them
invoke FreeBSTR         ;all bstr in bstr_array are free and the bstr_array is initialized to 0

--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version