The Embedded Warrior Library for C++ has a custom implementation of messages.
Example code to open a catalog:
typedef std::messages<char> Msg;
const Msg& ct = std::use_facet<Msg>(std::locale::classic());
Msg::catalog cat = ct.open("my_messages",
std::locale::classic());
if (cat < 0)
{
std::cout << "Can't open message file\n";
std::exit(1);
}
The first line simply type defines messages<char> for easier reading or typing. The second line extracts the messages facet from the "C" locale. The third line instructs the messages facet to look for a file named "my_messages" and read message set data out of it using the classic ("C") locale (one could specify a locale with a specialized codecvt facet for reading the data file). If the file is not found, the open method returns -1. The facet messages<char> reads data from a narrow file ( ifstream). The facet messages<wchar_t> reads data from a wide file ( wifstream).
The messages data file can contain zero or more message data sets of the format:
The keyword $set begins a message data set. The setid is the set number. It can be any int. Set id's do not need to be contiguous. But the set id must be unique among the sets in this catalog.
The msgid is the message id number. It can be any int. Message id's do not need to be contiguous. But the message id must be unique among the messages in this set.
The message is an optionally quoted (") string that is the message for this setid and msgid. If the message contains white space, it must be quoted. The message can have characters represented escape sequences using the hexadecimal or universal format. For example (see also String Syntax ):
"\u0048\u0069\u0020\u0054\u0068\u0065\u0072\u0065\u0021"
The message data set terminates when the data is not of the form
msgid message
Thus, there are no syntax errors in this data. Instead, a syntax error is simply interpreted as the end of the data set. The catalog file can contain data other than message data sets. The messages facet will scan the file until it encounters $set setid.
An example message data file might contain: $set 1 1 "First Message" 2 "Error in foo" 3 Baboo 4 "\u0048\u0069\u0020\u0054\u0068\u0065\u0072\u0065\u0021" $set 2 1 Ok 2 Cancel
A program that uses messages to read and output this file follows:
#include <locale>
#include <iostream>
int main()
{
typedef std::messages<char> Msg;
const Msg& ct = std::use_facet<Msg>(std::locale::classic());
Msg::catalog cat = ct.open("my_messages",
std::locale::classic());
if (cat < 0)
{
std::cout << "Can't open message file\n";
return 1;
}
std::string eof("no more messages");
for (int set = 1; set <= 2; ++set)
{
std::cout << "set " << set << "\n\n";
for (int msgid = 1; msgid < 10; ++msgid)
{
std::string msg = ct.get(cat, set, msgid, eof);
if (msg == eof)
break;
std::cout << msgid << "\t" << msg << '\n';
}
std::cout << '\n';
}
ct.close(cat);
}
The output of this program is:
set 1
1 First Message 2 Error in foo 3 Baboo 4 Hi There!
set 2
1 Ok 2 Cancel