Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Daniel Berger

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Using mmap to do a global search and replace on Windows

// Use win32-mmap to do a global search and replace on Windows

   1  
   2  require 'win32/mmap'
   3  require 'windows/msvcrt/buffer'
   4  include Windows::MSVCRT::Buffer
   5  
   6  Strlen = API.new('strlen', 'L', 'L', 'msvcrt')
   7  Strstr = API.new('strstr', 'LP', 'L', 'msvcrt')
   8  
   9  Dir["**/*.rb"].each{ |f|
  10     p f
  11     Win32::MMap.new(:file => f) do |addr|
  12        old_str = 'some_old_string'
  13        old_len = old_str.length
  14        new_str = 'some_other_string'
  15        new_len = new_str.length
  16  
  17        ptr1 = ptr2 = ptr3 = Strstr.call(addr,old_str)
  18  
  19        while ptr1 && ptr1 != 0
  20           ptr2 += new_len
  21           ptr3 += old_len
  22           memmove(ptr2, ptr3, 1 + Strlen.call(ptr3))
  23           memcpy(ptr1, new_str, new_len)
  24           ptr1 = ptr2 = ptr3 = Strstr.call(ptr2,old_str)
  25        end
  26     end
  27  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS