Programming with Rudiments using the Wrapper Classes

Using the file 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 device 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 serialport 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 shmfile 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 environment Class

The environment class allows you to get or set the value of environment variables. The following example gets, sets and resets the environment variable TEST.

// 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 datetime Class

One of the most difficult things to do in a Posix environment is deal with dates and times. The "standard" functions and structures associated with dates and times are complex, vary widely from platform to platform and in many cases are not thread safe. The datetime class attempts to rectify this situation.

The datetime class allows you to query and set the system clock. If your platform has a working real-time-clock (/dev/rtc), then you can query or set the hardware clock as well. Note that your program must run as root in order to set the system or hardware clock. Since it is common for the system clock to be set to the local time zone and the hardware clock to be set to GMT, a method is provided for converting the hardware clock time to the system's time zone.

Additionally there is a method for switching the time zone of the time currently represented in the class.

The datetime class also provides methods for converting between several common ways of representing time. Such as a formatted string, the number of seconds since 1970 and "struct tm".

There are also methods for adding discrete amounts of time to the time currently stored in the class. You can, for example add 150 seconds to "12:30:50 01/02/2003 EST" and get "12:32:20 01/02/2003 EST". You can add negative numbers to subtract time.

Below is some code illustrating the use of the datetime 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 timezonefile Class

While I was working on timezone support in the datetime class I originally thought that it might be useful to be able to parse timezone files. I could not find any standard C functions for parsing them, so I wrote a class that parses them.

It turned out to be of very limited value, but it works and I never removed it. So, if you need such functionality, it exists.

// 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 directory 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 filesystem Class

The filesystem class provides methods for collecting statistics about a filesystem. The "standard" posix function for getting filesystem statistics is either statfs or statvfs. Few operating systems implement statvfs though and the structure returned by statfs varies greatly between operating systems. The filesystem class attempts to remedy this situation. However, no operating system supports every method in this 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 logger Class

The logger class and associated logdestination classes provide a framework for generating log messages from applications. An application can define a set of logdestinations and attach them to an instance of the logger class. Then, when the application calls one of the write() methods of the logger class, the log message is written to each of the logdestinations. For example, an application could simultaneously log to stderr and to a file. Currently stdout, stderr, file and syslog logdestinations are supported. If an application needs to send one set of log messages to one destination and another set to a different destinations, it can create two instances of the logger class and use one for each set of messages.

The following example illustrates use of the logger 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 permissions Class

The permissions class provides simple methods for generating permissions. The output of these methods can be used whenever a function takes an argument of type mode_t. Below is some code illustrating the use of the permissions 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 randomnumber Class

Functions for generating random numbers vary from platform to platform. The randomnumber class attempts to rectify this situation. Below is some code illustrating the use of the randomnumber 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 regularexpression Class

Regular expressions allow a programmer to perform complex string matching but methods for using regular expressions vary from platform to platform. The regularexpression class attempts to rectify this situation. Below is some code illustrating the use of the regularexpression 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 crypt 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 dynamiclib 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 error 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 intervaltimer 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 mutex 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 process 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 system 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 serialportprofile 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 snooze 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 Signal Classes

Signals allow processes to interrupt the execution of other processes. Signal handlers allow processes to intercept and react to the signals sent to them.

Rudiments provides 3 classes for working with signals: signalset, signalmanager and signalhandler.

A signalset is just a collection of signals. The signalset class allows a programmer to build up a collection of signals.

The signalmanager class provides methods for sending signals, ignoring signals, waiting for signals and examining blocked signals.

The signalhandler class provides methods for catching and handling signals.

Below is some code illustrating the use of all three classes. Note that you'll have to kill this program with a -9.

// 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 sharedmemory Class

Shared memory allows seperate processes to access a common block of memory. The standard functions and structures for managing shared memory segments are complex. The sharedmemory class attempts to rectify this situation. Below is some code illustrating the use of the sharedmemory class.

There are methods in the sharedmemory class that allow you to get and set user/group ownership and permissions of a segment that are not documented here, but they are straightforward and rarely used.

This program puts some data into shared memory then goes to sleep, giving another program time to access the segment.

// 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) {
}

This program reads the data from shared memory.

// 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 semaphoreset Class

Semaphores allow seperate processes or threads to synchronize activities. The standard functions and structures for managing semaphores are complex. The sempahoreset class attempts to rectify this situation. Below is some code illustrating the use of the semaphoreset class.

There are methods in the semaphoreset class that allow you to get and set user/group ownership and permissions of a semaphore set that are not documented here, but they are straightforward and rarely used.

The first program prints out 1 and 3, the second program prints out 2 and 4. They use a set of 2 semaphores to synchronize these activities. No matter what order the programs are started in, they will always print out:

1
2
3
4
1
2
3
4
etc.

These programs must both be run to the background.

Using the memorymap 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 character 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 charstring Class

The charstring class contains some commonly needed string manipulation and evaluation functions. Below is some code illustrating the use of the string 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 rawbuffer 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 passwdentry Class

The passwdentry class allows you to look up entries from /etc/passwd or from elsewhere if you're using the Name Service Switch.

// 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 shadowentry Class

The shadowentry class allows you to look up entries from /etc/shadow or from elsewhere if you're using the Name Service Switch.

// 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 groupentry Class

The groupentry class allows you to look up entries from /etc/group or from elsewhere if you're using the Name Service Switch.

// 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 hostentry Class

The hostentry class allows you to look up entries from /etc/hosts or from elsewhere if you're using the Name Service Switch.

// 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 protocolentry Class

The protocolentry class allows you to look up entries from /etc/protocols or from elsewhere if you're using the Name Service Switch.

// 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 serviceentry Class

The serviceentry class allows you to look up entries from /etc/services or from elsewhere if you're using the Name Service Switch.

// 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 rpcentry Class

The rpcentry class allows you to look up entries from /etc/rpc or from elsewhere if you're using the Name Service Switch.

// 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) {
}