Using the propsax class
The propsax class provides a callback-based framework for parsing Java-style properties files. When it encounters a key, value, or comment, it calls one of its callback methods. These methods may be overridden by a child class to perform specific tasks. The propsax class is especially useful if you can't afford to load the entire file into memory and use the propdom class, or if you just need to extract specific data from a properties file.
The following example creates a child class of propsax that overrides the key(), value(), poundComment(), and exclamationComment() callback methods to print information about each component encountered during parsing.
#include <rudiments/propsax.h>
#include <rudiments/stdio.h>
// create a properties sax handler by inheriting from propsax
class myprophandler : public propsax {
protected:
bool key(const char *k) {
stdoutput.printf("key: %s",k);
return true;
}
bool value(const char *v) {
stdoutput.printf(" = %s\n",v);
return true;
}
bool poundComment(const char *c) {
stdoutput.printf("# %s\n",c);
return true;
}
bool exclamationComment(const char *c) {
stdoutput.printf("! %s\n",c);
return true;
}
};
int main(int argc, const char **argv) {
myprophandler handler;
// parse a properties string
stdoutput.write("parsing properties:\n\n");
handler.parseString(
"# animal types\n"
"! a simple list\n"
"dog=mammal\n"
"eagle=bird\n"
"snake=reptile\n");
}