News:

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

How is "strtok" done?

Started by David, January 19, 2010, 05:28:31 PM

Previous topic - Next topic

David

Hi, how is the function strtok done in mASM?  Is there another function that splits strings, or is it done manually?  If it is done manually, can you give me an example?

Vortex

Hi David,

You can use the strtok function provided by the C run-time library msvcrt.lib :


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib


.data

mystr       db 'This is a strtok demo. Example coded with Masm.',0
format1     db '%s',13,10,0

delimiters  db ' .',0

.code

start:

;   The C run-time functions are prefixed with the tag crt_ to avoid conflicts
;   with reserved Masm keywords.

    invoke  crt_strtok,ADDR mystr,ADDR delimiters
@@:
    invoke  crt_printf,ADDR format1,eax
    invoke  crt_strtok,NULL,ADDR delimiters
    test    eax,eax
    jnz     @b

    invoke  ExitProcess,0

END start

mitchi

I'm sure it's an easy function to write. You basically write a zero when you encounter the symbol you tokenize the string with, and you return a pointer to that token.
It's a good exercise!

hutch--

Look at the two tokenisers in the masm32 library, a line tokeniser and a word tokeniser. You will not have to hold your breath waiting on either of them.

In the masm32 library help file,


ltok  Tokenise lines of text "in place" in a text file.
wtok  Tokenise words "in place" in a text file.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php