[Win32utils-devel] Global substitution with win32-mmap

Daniel Berger djberg96 at gmail.com
Mon Apr 21 23:16:21 EDT 2008


Hi all,

I'm trying to figure out how to do an inline file substitute/replace 
using the address. In short, I'm trying to replace "assert_raises" with 
"assert_raise" throughout a file.

I tried this:

require 'win32/mmap'
require 'windows/msvcrt/string'
require 'windows/msvcrt/buffer'
include Win32
include Windows::MSVCRT::String
include Windows::MSVCRT::Buffer

StrlenL = API.new('strlen', 'L', 'L', 'msvcrt')

MMap.new(:file => 'test.rb') do |addr|
    old_str = 'assert_raises'
    old_len = old_str.length
    new_str = 'assert_raise'
    new_len = new_str.length

    ptr1 = ptr2 = ptr3 = [strstr(addr,old_str)].pack('p*').unpack('l')[0]

    while ptr1 && ptr1 != 0
       ptr2 += new_len
       ptr3 += old_len
       memmove(ptr2, ptr3, 1 + StrlenL.call(ptr3))
       memcpy(ptr1, new_str, new_len)
       ptr1 = ptr2 = ptr3 = [strstr(ptr2, 
old_str)].pack('p*').unpack('l')[0]
    end
end

I based it off of this thread:

http://tinyurl.com/5qvbnw

But, that doesn't seem to be working. Any ideas?

Thanks,

Dan

# test.rb
class Foo < Test::Unit::TestCase
    def setup
       @foo = Foo.new
    end

    def test_bar_expected_errors
       assert_raises(ArgumentError){ 1 == 2 }
       assert_raise(StandardError){ 2 == 1 }
    end

    def test_foo_expected_errors
       assert_raises(ArgumentError){ 1 == 2 }
       assert_raise(StandardError){ 2 == 1 }
    end

    def teardown
       @foo = nil
    end
en


More information about the win32utils-devel mailing list