Daniel Berger
2008-Apr-22 03:16 UTC
[Win32utils-devel] Global substitution with win32-mmap
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
Hi, 2008/4/22, Daniel Berger <djberg96 at gmail.com>:> > 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?You must define strstr like this: Strstr = API.new(''strstr'', ''LP'', ''L'', ''msvcrt'') and try this: 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) 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) end end Regads, Park Heesob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20080422/f9b876bb/attachment.html