librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_cpp.cxx File Reference

Nonsensical example that merely test compiling and linking a C++ applciation agains librnr C library. More...

#include <iostream>
#include <string>
#include <libgen.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/opts.h"
#include "version.h"
#include "rnr/assoc.h"
#include "rnr/char.h"
#include "rnr/config.h"
#include "rnr/dliststr.h"
#include "rnr/hash.h"
#include "rnr/new.h"

Go to the source code of this file.

Enumerations

enum  BodyId_T {
  unknown,
  Sun,
  Mercury,
  Venus,
  Earth,
  Moon,
  Mars,
  Phobos,
  Deimos,
  Ceres,
  Jupiter,
  Io,
  Europa,
  Ganymede,
  Callisto,
  Saturn,
  Titan,
  Uranus,
  Titania,
  Neptune,
  Triton,
  Pluto,
  Charon,
  Eris,
  NumBodies,
  unknown,
  Sun,
  Mercury,
  Venus,
  Earth,
  Moon,
  Mars,
  Phobos,
  Deimos,
  Ceres,
  Jupiter,
  Io,
  Europa,
  Ganymede,
  Callisto,
  Saturn,
  Titan,
  Uranus,
  Titania,
  Neptune,
  Triton,
  Pluto,
  Charon,
  Eris,
  NumBodies
}
 Major and minor solar body ids (partial, of course).
 

Functions

static void logtest_num1 (void *p, const char *sFruit, int n, int half, double f, int hex, bool_t bAreRipe)
 Log test number 1. More...
 
static int logtest_num2 (int count)
 Log test number 2. More...
 
static int runlogtests ()
 Run log test(s). More...
 
void test_assoc (BodyId_T oid)
 Test value to name association. More...
 
void test_assoc (const char *name)
 Test name to value association. More...
 
void test_char ()
 Test character routine(s).
 
void test_config (string inifile)
 Test INI configuration routines.
 
void test_dlist ()
 Test doubly-linked list routines.
 
void del_node_data (void *key, void *val)
 Delete node data - both key and value are dynamically allocated. More...
 
void test_hash ()
 Test hash and new routines.
 
static int init (int argc, char *argv[])
 Main initialization. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

static char * Argv0
 the command
 
static int OptsTest = 1
 test option value
 
static OptsPgmInfo_T LogExamplePgmInfo
 Program information. More...
 
static OptsInfo_T LogExampleOptsInfo []
 
Nvp_T SolarBodyNames []
 Solar body name-id pairs (partial). More...
 
static string Colorado14ers [][2]
 Colorado's 14,000+ foot mountains. More...
 

Detailed Description

Nonsensical example that merely test compiling and linking a C++ applciation agains librnr C library.

$LastChangedDate$ $Rev$

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file example_cpp.cxx.

Function Documentation

void del_node_data ( void *  key,
void *  val 
)

Delete node data - both key and value are dynamically allocated.

Parameters
keyData key.
valData value.

Definition at line 410 of file example_cpp.cxx.

Referenced by test_hash().

411 {
412  delete((char *)key);
413  delete((char *)val);
414 }
static int init ( int  argc,
char *  argv[] 
)
static

Main initialization.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Returns
1 on success, exits on failure.

Definition at line 465 of file example_cpp.cxx.

References Argv0, CHKEXPR_INT, LOG_GET_LOGFP(), LOG_LEVEL_DIAG1, LOGABLE, LOGDIAG1, OptsGet(), OptsTest, and PkgInfo.

Referenced by main().

466 {
467  FILE *fp;
468  int i;
469 
470  // name of this process
471  Argv0 = basename(argv[0]);
472 
473  // parse input options
475  &argc, argv);
476 
477  // test log helper macro
478  CHKEXPR_INT(OptsTest, ((OptsTest >= 0) || (OptsTest <= 3)), 0);
479 
480  // test complicated logging
481  if( LOGABLE(LOG_LEVEL_DIAG1) )
482  {
483  LOGDIAG1("Post options processed non-option arguments:");
484  fp = LOG_GET_LOGFP();
485  for(i=0; i<argc; ++i)
486  {
487  fprintf(fp, " argv[%d]=\"%s\"\n", i, argv[i]);
488  }
489  }
490 
491  return 1;
492 }
#define CHKEXPR_INT(val, expr,...)
check integer
Definition: log.h:688
static char * Argv0
the command
Definition: example_cpp.cxx:61
FILE * LOG_GET_LOGFP()
Get current logging output stream file pointer.
Definition: log.c:301
static OptsInfo_T LogExampleOptsInfo[]
Command line options information.
Definition: example_log.c:76
#define LOGDIAG1(fmt,...)
Standard Diagnostic Level 1 logging.
Definition: log.h:407
static OptsPgmInfo_T LogExamplePgmInfo
Program information.
Definition: example_cpp.cxx:75
#define LOGABLE(level)
Test if given level is logable at current threshold.
Definition: log.h:195
static int OptsTest
test option value
Definition: example_cpp.cxx:62
static const PkgInfo_T PkgInfo
Definition: version.h:45
char ** OptsGet(const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
Gets, validates, and sets all command line options.
Definition: opts.c:861
#define LOG_LEVEL_DIAG1
diagnostic level 1
Definition: log.h:181
static void logtest_num1 ( void *  p,
const char *  sFruit,
int  n,
int  half,
double  f,
int  hex,
bool_t  bAreRipe 
)
static

Log test number 1.

Parameters
pVoid pointer.
sFruitCharacter pointer.
nInteger.
halfShort.
fDouble.
hexInteger in hex.
bAreRipeBoolean.

Definition at line 128 of file example_cpp.cxx.

References _TBOOL, _TFLOAT, _THEX, _TINT, _TPTR, _TSHORT, _TSTR, LOGDIAG1CALL, and LOGDIAG2.

Referenced by runlogtests().

130 {
131  // test log function call diagnostics 1
132  LOGDIAG1CALL(_TPTR(p), _TSTR(sFruit), _TINT(n), _TSHORT(half), _TFLOAT(f),
133  _THEX(hex), _TBOOL(bAreRipe));
134 
135  // test diagnostic 2 test
136  if( bAreRipe )
137  {
138  LOGDIAG2("There are %d juicy %s in %s", n, sFruit, "Tuscany");
139  }
140  else
141  {
142  LOGDIAG2("There are %d green %s in %s", n, sFruit, "Utah");
143  }
144 }
#define _THEX(var)
int (hex)
Definition: log.h:581
#define _TINT(var)
int (decimal)
Definition: log.h:582
#define _TSHORT(var)
short int (decimal)
Definition: log.h:584
#define LOGDIAG2(fmt,...)
Standard Diagnostic Level 2 logging.
Definition: log.h:400
#define _TBOOL(var)
boolean
Definition: log.h:588
#define _TSTR(var)
string variable
Definition: log.h:578
#define _TPTR(var)
pointer
Definition: log.h:587
#define _TFLOAT(var)
float
Definition: log.h:586
#define LOGDIAG1CALL(...)
Standard Diagnostic Level 1 function call tracing.
Definition: log.h:460
static int logtest_num2 ( int  count)
static

Log test number 2.

Parameters
countCount.

Definition at line 151 of file example_cpp.cxx.

References _TINT, LOGDIAG2CALL, LOGDIAG4, LOGERROR, LOGSYSERROR, and NULL.

Referenced by runlogtests().

152 {
153  FILE *fp;
154  const char *sFileName = "PackingShedOnStrike.newsflash";
155 
156  // test log function call diagnostics 2
157  LOGDIAG2CALL(_TINT(count));
158 
159  // test error logging
160  while( count-- > 0 )
161  {
162  LOGERROR("Salmonella scare %d...", count);
163  }
164 
165  // test system error logging
166  if( (fp = fopen(sFileName, "r")) == NULL )
167  {
168  LOGSYSERROR("%s", sFileName);
169  }
170  else
171  {
172  LOGDIAG4("%s: what a pleasant surprise", sFileName);
173  fclose(fp);
174  }
175 }
#define _TINT(var)
int (decimal)
Definition: log.h:582
#define NULL
null pointer
Definition: rnrconfig.h:199
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
#define LOGDIAG2CALL(...)
Standard Diagnostic Level 2 function call tracing.
Definition: log.h:454
int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 503 of file example_cpp.cxx.

References init(), runlogtests(), test_assoc(), test_char(), test_config(), test_dlist(), and test_hash().

504 {
505  cout << "Testing librnr Linkage" << endl
506  << "----------------------" << endl;
507 
508  cout << "* Validate options and logging." << endl;
509  if( !init(argc, argv) )
510  {
511  return 3;
512  }
513  runlogtests();
514 
515  cout << "* Validate associative mapping." << endl;
516  test_assoc(Titania);
517  test_assoc("Moon");
518  cout << endl;
519 
520  cout << "* Validate char." << endl;
521  test_char();
522  cout << endl;
523 
524  cout << "* Validate config (ini)." << endl;
525  test_config("example.ini");
526  cout << endl;
527 
528  cout << "* Validate string dlist." << endl;
529  test_dlist();
530  cout << endl;
531 
532  cout << "* Validate hash and new." << endl;
533  test_hash();
534  cout << endl;
535 
536  return 0;
537 }
static int runlogtests()
Run log test(s).
void test_config(string inifile)
Test INI configuration routines.
void test_dlist()
Test doubly-linked list routines.
void test_assoc(BodyId_T oid)
Test value to name association.
void test_hash()
Test hash and new routines.
static int init(int argc, char *argv[])
Main initialization.
void test_char()
Test character routine(s).
static int runlogtests ( )
static

Run log test(s).

Returns
1 on success, > 0 on failure.

Definition at line 182 of file example_cpp.cxx.

References LOGERROR, logtest_num1(), logtest_num2(), and OptsTest.

Referenced by main().

183 {
184  short h = 10;
185  int i = 12;
186 
187  switch( OptsTest )
188  {
189  case 0:
190  break;
191  case 1:
192  logtest_num1(&i, "apples", 53, h, 123.5, 0xdead, false);
193  break;
194  case 2:
195  logtest_num2(3);
196  break;
197  case 3:
198  logtest_num1(&h, "pears", 12, h, -1.5E-2, 0xf1, true);
199  logtest_num2(3);
200  break;
201  default:
202  LOGERROR("bug: %d: unexpected test", OptsTest);
203  return 0;
204  }
205  return 1;
206 }
static int logtest_num2(int count)
Log test number 2.
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
static int OptsTest
test option value
Definition: example_cpp.cxx:62
static void logtest_num1(void *p, const char *sFruit, int n, int half, double f, int hex, bool_t bAreRipe)
Log test number 1.
void test_assoc ( BodyId_T  oid)

Test value to name association.

Parameters
oidValue.

Definition at line 273 of file example_cpp.cxx.

References arraysize, and NvpVal2Name().

Referenced by main().

274 {
275  cout << oid << " --> "
277  << endl;
278 }
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
Nvp_T SolarBodyNames[]
Solar body name-id pairs (partial).
void test_assoc ( const char *  name)

Test name to value association.

Parameters
nameName.

Definition at line 285 of file example_cpp.cxx.

References arraysize, and NvpName2Val().

286 {
287  cout << name << " --> "
289  << endl;
290 }
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
Nvp_T SolarBodyNames[]
Solar body name-id pairs (partial).
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.
Definition: assoc.c:64

Variable Documentation

string Colorado14ers[][2]
static
Initial value:
=
{
{"mt_elbert", "14,433"},
{"mt_massive", "14,421"},
{"mt_harvard", "14,420"},
{"blanca_peak", "14,345"},
{"la_plata_peak", "14,336"},
{"uncompahgre_peak", "14,309"},
{"crestone_peak", "14,294"},
{"mt_lincoln", "14,286"},
{"grays_peak", "14,270"},
{"mt_antero", "14,269"},
{"torreys_peak", "14,267"},
{"castle_peak", "14,265"},
{"quandary_peak", "14,265"},
{"mt_evans", "14,264"},
{"longs_peak", "14,255"},
{"mt_wilson", "14,246"}
}

Colorado's 14,000+ foot mountains.

N.B. there are 53 of them, I'm stopping at the highest 16.

Definition at line 384 of file example_cpp.cxx.

Referenced by test_hash().

OptsInfo_T LogExampleOptsInfo[]
static
Initial value:
=
{
{
"test",
't',
required_argument,
true,
"<test>",
"Set the logging test(s) to conduct.\n"
"%A is one of:\n"
" 0 - No tests.\n"
" 1 - Test #1.\n"
" 2 - Test #2.\n"
" 3 - All tests."
},
{NULL, }
}
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
Definition: opts.c:1215
#define NULL
null pointer
Definition: rnrconfig.h:199
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Definition: opts.c:1065
static int OptsTest
test option value
Definition: example_cpp.cxx:62

Definition at line 93 of file example_cpp.cxx.

OptsPgmInfo_T LogExamplePgmInfo
static
Initial value:
=
{
"Example of C++ using librnr grab bag of goodies.",
"The %P command demonstrates compiling-linking C++ with librnr facilities.",
}
#define NULL
null pointer
Definition: rnrconfig.h:199

Program information.

Note
The dot initialization of C is far superior to C++ fixed order for complex fixed structures.

Definition at line 75 of file example_cpp.cxx.

Nvp_T SolarBodyNames[]
Initial value:
=
{
{"unknown", unknown},
{"Sun", Sun},
{"Mercury", Mercury},
{"Venus", Venus},
{"Earth", Earth},
{"Moon", Moon},
{"Mars", Mars},
{"Phobos", Phobos},
{"Deimos", Deimos},
{"Jupiter", Jupiter},
{"Io", Io},
{"Europa", Europa},
{"Ganymede", Ganymede},
{"Callisto", Callisto},
{"Saturn", Saturn},
{"Titan", Titan},
{"Uranus", Uranus},
{"Titania", Titania},
{"Neptune", Neptune},
{"Triton", Triton},
{"Ceres", Ceres},
{"Pluto", Pluto},
{"Charon", Charon}
}

Solar body name-id pairs (partial).

Definition at line 241 of file example_cpp.cxx.