Saturday, March 10, 2012

windows - How to use Ruby to replace text in a VC++ resource file ...

I have a plain managed VC++ project in a solution. It has a resource file, app.rc, that is used to store the assembly info (version, product, copyright, etc). If I open the file in my text editor, it says it's a Unicode (UTF-16 LE BOM). And Visual Studio only displays it correctly if I choose Unicode - Codepage 1200.

VS_VERSION_INFO VERSIONINFO  FILEVERSION 0,0,0,0  PRODUCTVERSION 0,0,0,0  FILEFLAGSMASK 0x3fL #ifdef _DEBUG  FILEFLAGS 0x1L #else  FILEFLAGS 0x0L #endif  FILEOS 0x40004L  FILETYPE 0x2L  FILESUBTYPE 0x0L BEGIN     BLOCK "StringFileInfo"     BEGIN         BLOCK "040904b0"         BEGIN             VALUE "CompanyName", "My Company, LLC"             VALUE "FileVersion", "0.0.0.0"             VALUE "InternalName", "myassembly.dll"             VALUE "LegalCopyright", "Copyright (C) 2011 My Company, LLC"             VALUE "OriginalFilename", "myassembly.dll"             VALUE "ProductName", "The Product"             VALUE "ProductVersion", "0.0.0.0"         END     END     BLOCK "VarFileInfo"     BEGIN         VALUE "Translation", 0x409, 1200     END END 

I am using Ruby/Rake to configure and run local builds. Part of that build is replacing the version in the assembly resources. I would read in the file, gsub the version numbers, and write the file back out. However, when I read in the file, I get garbage

irb(main):001:0> File.open("source\\myproject\\app.rc").read => "\xFF\xFE/\x00/\x00 \x00M\x00i\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00 \x00V\x00i\x00s\x 00u\x00a\x00l\x00 \x00C\x00+\x00+\x00 \x00g\x00e\x00n\x00e\x00r\x00a\x00t\x00e\x00d\x00 \x 00r\x00e\x00s\x00o\x00u\x00r\x00c\x00e\x00 \x00s\x00c\x00r\x00i\x00p\x00t\x00.\x00\n\x00\n \x00/\x00/\x00\n\x00\n\x00#\x00i\x00n\x00c\x00l\x00u\x00d\x00e\x00 \x00\"\x00r\x00e\x00s\x 

Ruby thinks it's an IBM437 encoding.

irb(main):006:0> File.open("source\\myproject\\app.rc").external_encoding => #<Encoding:IBM437> 

And if I write the contents out to another file, I get some strange encoding (my text editor swears it's Unicode (UTF-16 BE BOM)).

???????????????????????????????????????????????? ???? ??????????????????????? ?? 

How am I supposed to open/read this file properly?


Ok, so I can read the file properly now if I provide the correct encodings.

irb(main):003:0> File.open("source\\myproject\\app.rc", "rb:UTF-16LE") => #<File:source\myproject\app.rc> 

However, I cannot substitute the version strings out yet.

irb(main):003:0> c = File.open("source\\myproject\\app.rc", "rb:UTF-16LE").read irb(main):007:0> c.gsub("0.0.0.0", "0.0.5.0") Encoding::CompatibilityError: incompatible encoding regexp match (US-ASCII regexp with UTF-16LE string) 

But, I can't provide an encoding to gsub. Do I have to encode the pattern and replacement strings as well?

Source: http://stackoverflow.com/questions/9636118/how-to-use-ruby-to-replace-text-in-a-vc-resource-file-when-the-encoding-is-al

progeria cmas cmas tcu jackie robinson dr. oz heart attack grill las vegas

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.