[Win32utils-devel] Using strtok via win32/api

Daniel Berger djberg96 at gmail.com
Wed Oct 24 22:44:54 EDT 2007


Hi all,

I'm having a little trouble with strtok:

require 'win32/api'
include Win32

strtok = API.new('strtok', 'PP, 'P', 'msvcrt')

string = "A string\tof ,,tokens\nand some  more tokens";
seps = " ,\t\n";

puts "Tokens:"

token = strtok.call(string, seps)

while token
    puts token
    token = strtok.call(nil, seps)
end

This will work, except on the final call:

C:\>ruby strtok.rb
Tokens:
A
string
of
tokens
and
some
more
tokens
C:/ruby/lib/ruby/site_ruby/1.8/windows/api.rb:342:in `call': NULL 
pointer given (ArgumentError)
         from C:/ruby/lib/ruby/site_ruby/1.8/windows/api.rb:342:in `call'
         from 
C:/ruby/lib/ruby/site_ruby/1.8/windows/msvcrt/string.rb:69:in `strtok'
         from strtok.rb:13

I can see why it's doing this, but I'd like to make it work without a 
begin/rescue, if possible, in order to keep it fast. Any ideas?

Regards,

Dan

PS - Yes, I know strtok() is evil. Please humor me.


More information about the win32utils-devel mailing list