Programming with Rudiments using the Utility Classes

- Using the commandline Class
- Using the parameterstring Class
- Using the chat Class
- Using the memorypool Class
- Using the variablebuffer Class
- Using the stringbuffer Class
- Using the linkedlist Class
- Using the dictionary Class
- Using the xmldom and xmldomnode Classes
- Using the xmlsax Class
- Using the dtd Class

Using the commandline Class

Most programs take command line options in the option/value pair format:

> program -option value -option value ...

or

> program --option=value --option=value ...

The commandline class makes it easy to deal with command line options of either of these formats. Below is some code illustrating the use of the commandline class.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the parameterstring Class

Sometimes a function needs to take an arbitrary set of parameters. For example, a function for connecting to a database may need host, port, socket, username and password, any of which could be omitted depending on the database. Though C++ support methods which take an arbitrary number of parameters, sometimes it is more convenient to for the method to accept a single string parameter with name/value pairs in it instead.

The parameterstring class provides methods for parsing and accessing a parameter string of the following form:

> name1='value1';name2='value2';name3='value3'

The single quotes are optional. If a parameter needs to contain a single quote, then it can be escaped as follows:

> name='\'value\''

Backslashes can be similarly escaped:

> name='\\value\\'

    ...

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the chat Class

The chat class provides a framework for communications across file descriptors with a peer. This is typically a serial-attached device but technically it could be any device or process that can communicate over file descriptors. This class is mainly used internally by the modemserver and modemclient classes but could be used by itself as well.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the memorypool Class

The memorypool class implements a pool of memory that starts with an initial buffer and grows dynamically as needed. Each time the pool is freed, the dynamic buffers are deallocated. After a configurable number of free's, the initial buffer is resized to the average amount of memory that was requested between free's since the last time it resized.

The memorypool class is especially useful for applications that do the same task over and over where varying amounts of memory are required during each iteration. The class is particularly efficient in cases where memory requirements are fairly constant between iterations with sporadic requirements for lots of memory or where the memory requirements steadily get larger or smaller across iterations.

Using a memorypool can be faster than allocating memory on demand, then deallocating it later and more efficient than using static buffers that are large enough to contain the largest possible data.

The following code connects to a server on the local machine and reads variable/value pairs from it.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the variablebuffer Class

The variablebuffer class allows you to manipulate a buffer of arbitrary length containing binary data. You can append data to a variablebuffer or write data at arbitrary positions in the buffer.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the stringbuffer Class

The stringbuffer class allows you to manipulate strings of arbitrary length. You can append data to a stringbuffer or write data at arbitrary positions in the buffer.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the linkedlist Class

The linkedlist class allows you to store arbitrary data in a doubly-linked list.

Since lists of strings are commonly used, a stringlist typedef is provided for convenience as well.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the dictionary Class

The dictionary class allows you to store arbitrary key/value-pair data.

Since dictionaries with string and integer keys and dictionaries with string keys and values are commonly used, convenience classes are provided for each as well.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the xmldom and xmldomnode Classes

The xmldom and xmldomnode classes provide a framework for DOM parsing of XML documents. The xmldom class provides methods for parsing the document and accessing it's root node. Each node of the document is represented by an instance of the xmldomnode class. The xmldomnode class provides methods for accessing a node's data, attributes, child nodes, parent nodes and sibling nodes. Since the xmldom class creates a representation of the XML document in memory, it should not be used to process arbitrarily large documents which could exhaust system memory.

The following XML file contains an address book.

    <?xml version="1.0"?>
    <!DOCTYPE instances SYSTEM "adbook.dtd">
    <addressbook>
            <person firstname="David" middlename="Lee" lastname="Muse">
                    <phones>
                            <phone location="home" number="1-222-333-4444"/>
                            <phone location="work" number="1-333-444-5555"/>
                            <phone location="mobile" number="1-444-555-6666"/>
                    </phones>
                    <addresses>
                            <address location="home" address="1234 homestreet dr." city="mycity" state="GA" zip="12345"/>
                            <address location="work" address="2345 workstreet dr." city="mycity" state="GA" zip="23456"/>
                    </addresses>
                    <emails>
                            <email location="home" address="dmuse@firstworks.com"/>
                            <email location="work" address="dmuse@workemail.com"/>
                    </emails>
            </person>
    </addressbook>

The following program parses the addressbook and prints it to the screen.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}

Here is the output of the program above.

>     David Lee Muse
>     Phones:
>             home: 1-222-333-4444
>             work: 1-333-444-5555
>             mobile: 1-444-555-6666

>     Addresses:
>             home: 1234 homestreet dr. mycity, GA 12345
>             work: 2345 workstreet dr. mycity, GA 23456

>     Emails:
>             home: dmuse@firstworks.com
>             work: dmuse@workemail.com

Using the xmlsax Class

The xmlsax class provides a callback-based framework for parsing XML documents. The xmlsax class provides methods for parsing strings of XML or XML files. When it encounters a tag, attribute or other XML component, it calls one of it's callback methods. These methods may be overridden by a child class to perform specific tasks. The xmlsax class is especially useful if you can't afford to load the entire document into memory and use the xmldom class, or if you just need to extract specific data from an XML file.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the dtd Class

The dtd class provides methods for parsing an XML DTD. The result is an XML DOM tree representing the DTD. Once parsed, the tree can be accessed using the xmldom and xmldomnode classes.

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}
Using the codetree Class

...

// Copyright (c) 2012 David Muse
// See the file COPYING for more information
#ifdef RUDIMENTS_NAMESPACE
using namespace rudiments;
#endif
int main(int argc, const char **argv) {
}