The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: David on January 19, 2010, 05:28:31 PM

Title: How is "strtok" done?
Post by: David on January 19, 2010, 05:28:31 PM
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?
Title: Re: How is "strtok" done?
Post by: Vortex on January 19, 2010, 07:18:26 PM
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
Title: Re: How is "strtok" done?
Post by: mitchi on January 19, 2010, 08:58:36 PM
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!
Title: Re: How is "strtok" done?
Post by: hutch-- on January 20, 2010, 12:44:50 AM
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.