[Win32utils-devel] Using strtok via win32/api

Daniel Berger djberg96 at gmail.com
Thu Oct 25 08:39:59 EDT 2007


Park Heesob wrote:
> Hi,
> ----- Original Message ----- 
> From: "Daniel Berger" <djberg96 at gmail.com>
> To: "win32utils-devel" <win32utils-devel at rubyforge.org>
> Sent: Thursday, October 25, 2007 11:44 AM
> Subject: [Win32utils-devel] Using strtok via win32/api
> 
> 
>> 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
>>
> I think it is a bug.
> 
> The source code line #457 of api.c in win32-api module
> 
>            v_return = rb_str_new2((TCHAR*)return_value);
> 
> shoule be
> 
>            v_return = (return_value==0) ? Qnil : rb_str_new2((TCHAR*)return_value);

Aha! I actually changed it to this:

if(!return_value)
    v_return = Qnil;
else
    v_return = rb_str_new2((TCHAR*)return_value);

That should cover both 0 and NULL. :)

Many thanks,

Dan

PS - Win32API suffers from the same bug (unless they patched it recently).


More information about the win32utils-devel mailing list