x2c source

I finally got around to putting up the source code for x2c under GPL. No, you haven’t heard of this thing and it may not seem immediately useful, but when it is useful, it’s incredibly useful. The hardest thing is coming up with full samples of what it can do, so I’ll just outline it right here.

x2c stands for “XML to Code”, and it’s an interpreter for a little language I made with built-in commands to handle XML documents and write to plain text output files.

It started life as a tool to create VB and C# source code for data access layer classes, based on XML descriptions of an Oracle database. Another possibility is generating language tables from Excel spreadsheets, and I’ll tell you how:

Imagine an Excel spreadsheet with one sentence per row. In each column, the same sentence is written in another language, like Swedish, English, French, etc. Save the spreadsheet as an XML document. Now you can write a pretty short x2c script that reads these languages, column by column, and then produces a C++ header file with the right strings declared as constants. Great for products you want to recompile for a number of human languages.

Especially for this last use, I recently adapted the text output file command in x2c to allow output to ASCII, unicode (default), or any codepage you have installed on the Windows system you’re running this thing on. In the above script example, you see codepage 1251 used for Russian. In this case this was necessary since the C++ compiler used (Borland) couldn’t use unicode header files. This script runs under US or Swedish XP and Vista, as long as codepage 1251 is also installed on the system, and then produces the right MBCS file for Borland C++, resulting in binaries that will look real to russians running russian versions of Windows. Note that above is the complete script that is needed to convert the excel spreadsheet to four different C++ header files and it can easily be run from a build script.

The source is C++ in a VS 2008 solution. Have a go at it.

Leave a Reply

Your email address will not be published.