Copyright © 2003-2005, Peter Seibel

24. Practical: Parsing Binary Files

In this chapter I’ll show you how to build a library that you can use to write code for reading and writing binary files. You’ll use this library in Chapter 25 to write a parser for ID3 tags, the mechanism used to store metadata such as artist and album names in MP3 files. This library is also an example of how to use macros to extend the language with new constructs, turning it into a special-purpose language for solving a particular problem, in this case reading and writing binary data. Because you’ll develop the library a bit at a time, including several partial versions, it may seem you’re writing a lot of code. But when all is said and done, the whole library is fewer than 150 lines of code, and the longest macro is only 20 lines long.