Parsing CArchive (MFC classes) files in Ruby

余生颓废 提交于 2019-12-24 23:12:10

问题


I have a legacy app that seems to be exporting/saving files with CArchive (legacy MFC application).

We're currently refactoring the tool for the web. Is there a library I can look at in Ruby for parsing and loading these legacy files?

What possible libraries could I look into?

Problems with the file format according to XML serialization for MFC include: Non-robustness—your program will probably crash if you read an archive produced by another version of your program. This can be avoided by complex and unwieldly version management. By using XML, this can be largely avoided. - Heavy dependencies between your program object model and the archived data. Change the program model and it is almost impossible to read data from a previous version. - Archived data cannot be edited, understood, and changed, except with the associated application.

Also - 4 versions of the legacy software exists, how would I be able to overcome this ObjectModel, Archived data problem for the different versions? Total backward (import) capabilities are required.


回答1:


CArchive doesn't have a format that you can parse. It's just a binary file. You have to know what is in it to know how to read it. A library could make it easier to read some data types (CString, CArray, etc.) but I'm not sure you'll find anything like this.

CArchive works like this (storing part):

CArchive ar;
int i = 5;
float f = 5.42f;
CString str("string");
ar << i << f << str;

Then all this is dumped into binary file. You would have to read binary data and somehow interpret it. This is easy in C++ because MFC knows how to serialize types, including complex types like CString and CArray. But you'll have to do this on your own using Ruby.

For example you might read 4 bytes (because you know that int is that big) and interpret it as integer. Next four bytes for float. And then you have to see how to load CString, it stores the length first and then data, but you'll have to take a look at the exact format it uses. You could create utility functions for each type to make your life easier but don't expect this to be simple.




回答2:


You could write an exporter in C++ using the old functionality, that would read in the CArchive and then output an xml file or whatever of the contents. Reading CArchives directly from Ruby (or any other language than C++/MFC) is going to be a major project. Maybe you can get away with it if the data that is written is just a struct with a few ints or longs, but as soon as your CArchive contains UDT's you're in for a world of pain. For example I don't even think CArchive makes promises on alignment.



来源:https://stackoverflow.com/questions/2599056/parsing-carchive-mfc-classes-files-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!