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-2 of 2 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

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

Strlen = API.new('strlen', 'L', 'L', 'msvcrt')
Strstr = API.new('strstr', 'LP', 'L', 'msvcrt')

Dir["**/*.rb"].each{ |f|
   p f
   Win32::MMap.new(:file => f) do |addr|
      old_str = 'some_old_string'
      old_len = old_str.length
      new_str = 'some_other_string'
      new_len = new_str.length

      ptr1 = ptr2 = ptr3 = Strstr.call(addr,old_str)

      while ptr1 && ptr1 != 0
         ptr2 += new_len
         ptr3 += old_len
         memmove(ptr2, ptr3, 1 + Strlen.call(ptr3))
         memcpy(ptr1, new_str, new_len)
         ptr1 = ptr2 = ptr3 = Strstr.call(ptr2,old_str)
      end
   end
}

Launching the default web browser on MS Windows

# This code will launch your default web browser to the URL you provide

require 'Win32API'

ShellExecute = Win32API.new('shell32', 'ShellExecute', 'LPPPPI', 'L')

unless ShellExecute.call(0, 'open', 'http://www.rubyforge.org', 0, 0, 1) > 0
   raise "ShellExecute failed"
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS