SourceForge.net Logo

Project TPFL

fmII

TPFL stands for Trivial Portable Framework Library. It includes several classes for command line and configuration files parsing and handling, strings and memory buffers handling, sockets workaround (server and client).

It features simple (trivial) usage of every class. This library was written
from API side, and then implemented in C++.

Command line and config file handling is very customizable: there is a
possibility of setting up various special properties such as quoting
characters, deliminator strings, file comment prefixes and others.
It supports several common data types such as booleans, long and string.

Socket communication classes are very intuitive and easy to use.

Strings and buffers classes makes usage of strings and buffers just trivial.

Get TPFL >>



Let's see examples below.

1. Parsing

Config file "~/.myapprc":

#
#       alarm client user config (and its a comment)
#

# Client options
host = "195.112.224.23:10000"           # alarm server hostname[:port]
tries = 3                               # how much tries is allowed

Application core code:

#include "tpfl/tpfl.hxx"


static const TPARSER_OPTION Options[] =
{
        // Socket options
        { "o", "host",          "host",         "alarm server hostname[:port]",  tParserFlag::Mandatory },
        { "t", "tries",         "tries",        "how much tries is allowed", tParserFlag::Optional }
};

// error handling
void parserErrorHandler(const tParser* par);

////////////////////////////////////////////////////////////
// error handlers

void parserErrorHandler(const tParser* par)
{
        tLog log(AppName);
        log.setConsole(Verb);
        tString errorDesc;
        log.write(LOG_ERR, par->getErrorDesc(errorDesc));
        exit(ALARM_ERROR_IO_PARSE);
}

////////////////////////////////////////////////////////////
// main

int main(int argc, char* argv[])
{
        // create a parser (simple one; there is a more features)
        tParser par(Options, sizeof(Options)/sizeof(TPARSER_OPTION));
        par.setErrorHandler(parserErrorHandler);
        par.setCmdLine(argc, argv);

        // get config file (simplified)
        tString configPath;
        tConfigPath path;

        configPath = path.getConfigPath(AppName);
        if (path.isGood((const char*)configPath))
            par.setResFileName((const char*)configPath);

        // define variables
        tString hostport;
        tString host = ALARM_SERVER_HOST;
        long tries = ALARM_CLIENT_TRIES;

        // open resource file
        par.openResFile();

        // parse it!
        par.found("t", tries);
        par.found("o", hostport);
      
        // close resource file
        par.closeResFile();
:
        // extract hostname and port number
        host = ((tStringNet)hostport).host();
        port = ((tStringNet)hostport).port();

.........

        return 0;
}


That's all. Simple? I think - yes. See package "alarmmon" for more advanced usage.
Get TPFL >>


2. Client sockets.

// callback
void socketErrorHandler(const tSocketBase* sock)
{
        tLog log(AppName);
        log.setConsole(Verb);
        tString errorDesc;
        log.write(LOG_ERR, sock->getErrorDesc(errorDesc));
}

// client socket
tSocketClient client;
client.setErrorHandler(socketErrorHandler);

client.socket(AF_INET, SOCK_STREAM, 0);
client.connect((const char*)host, port);

client.writeLine((const char*)line);

client.close();

And that's is all! Simple, I guess. See "alarmmon" projects for more.
Get TPFL >>


Konstantin N. Terskikh,
2003