librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_checksum.c File Reference

Example of librnr support for checksum algorithms. More...

#include <stdio.h>
#include <libgen.h>
#include <time.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/opts.h"
#include "rnr/checksum.h"
#include "version.h"

Go to the source code of this file.

Functions

static int init (int argc, char *argv[])
 Main initialization. More...
 
static void starttimer (struct timespec *pstart)
 Start nano second timer. More...
 
static long stoptimer (struct timespec *pstart)
 Stop nano second timer. More...
 
static void profile (char *sText)
 Profile the various checksum algorithms. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

static char * Argv0
 the command
 
static bool_t OptsPrintPoem = false
 do [not] print poem
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command line options information. More...
 
static char * Jabberwocky
 Jabberwocky by Lewis Carroll.
 

Detailed Description

Example of librnr support for checksum algorithms.

LastChangedDate
2013-03-05 09:45:13 -0700 (Tue, 05 Mar 2013)
Rev
2729
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_checksum.c.

Function Documentation

static int init ( int  argc,
char *  argv[] 
)
static

Main initialization.

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

Definition at line 104 of file example_checksum.c.

References Argv0, OptsGet(), and PkgInfo.

Referenced by main().

105 {
106  // name of this process
107  Argv0 = basename(argv[0]);
108 
109  // parse input options
110  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
111 
112  return 1;
113 }
static char * Argv0
the command
static OptsPgmInfo_T PgmInfo
Program information.
static const PkgInfo_T PkgInfo
Definition: version.h:45
static OptsInfo_T OptsInfo[]
Command line options information.
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
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 237 of file example_checksum.c.

References init(), Jabberwocky, new_strdup(), OptsPrintPoem, and profile().

238 {
239  char *sText;
240  char c;
241  int k;
242  int i;
243 
244  if( !init(argc, argv) )
245  {
246  return 2;
247  }
248 
249  if( OptsPrintPoem )
250  {
251  printf("\n%s\n", Jabberwocky);
252  }
253 
254  //
255  // --- Profile 1
256  //
257  printf("Text: Jabberwocky -- Lewis Carroll\n");
258  printf("Length: %zu bytes\n", strlen(Jabberwocky));
259 
261 
262  printf("\n");
263 
264  //
265  // --- Profile 2
266  //
267  sText = new_strdup(Jabberwocky);
268 
269  c = sText[0];
270  sText[0] = sText[1];
271  sText[1] = c;
272 
273  printf("Text: Jabberwocky, 1 byte swapped -- Lewis Carroll\n");
274  printf("Length: %zu bytes\n", strlen(sText));
275 
276  profile(sText);
277 
278  printf("\n");
279 
280  delete(sText);
281 
282  //
283  // --- Profile 3
284  //
285  k = 25;
286 
287  sText = new(strlen(Jabberwocky) * (size_t)k + (size_t)1);
288 
289  for(i=0, *sText=0; i<k; ++i)
290  {
291  strcat(sText, Jabberwocky);
292  }
293 
294  printf("Text: Jabberwocky * %d -- Lewis Carroll\n", k);
295  printf("Length: %zu bytes\n", strlen(sText));
296 
297  profile(sText);
298 
299  printf("\n");
300 
301  delete(sText);
302 
303  return 0;
304 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
static int init(int argc, char *argv[])
Main initialization.
static bool_t OptsPrintPoem
do [not] print poem
static char * Jabberwocky
Jabberwocky by Lewis Carroll.
static void profile(char *sText)
Profile the various checksum algorithms.
static void profile ( char *  sText)
static

Profile the various checksum algorithms.

Parameters
sTextNull-terminated text string.

Definition at line 199 of file example_checksum.c.

References generate_checksum16(), generate_checksum32(), generate_checksum8(), generate_crc32(), starttimer(), and stoptimer().

Referenced by main().

200 {
201  struct timespec t;
202  uint_t chksum;
203  long nsec;
204 
205  printf("Algorithm Result nSecs\n");
206 
207  starttimer(&t);
208  chksum = (uint_t)generate_checksum8((byte_t *)sText, strlen(sText));
209  nsec = stoptimer(&t);
210  printf(" 8-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
211 
212  starttimer(&t);
213  chksum = (uint_t)generate_checksum16((byte_t *)sText, strlen(sText));
214  nsec = stoptimer(&t);
215  printf("16-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
216 
217  starttimer(&t);
218  chksum = (uint_t)generate_checksum32((byte_t *)sText, strlen(sText));
219  nsec = stoptimer(&t);
220  printf("32-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
221 
222  starttimer(&t);
223  chksum = (uint_t)generate_crc32((byte_t *)sText, strlen(sText));
224  nsec = stoptimer(&t);
225  printf("32-bit crc %10u 0x%08x %6ld\n", chksum, chksum, nsec);
226 }
static void starttimer(struct timespec *pstart)
Start nano second timer.
u32_t generate_checksum32(byte_t buf[], size_t len)
Computes the modular 32-bit checksum over buffer.
Definition: checksum.c:126
u32_t generate_crc32(byte_t buf[], size_t len)
Computes the 32-bit cyclic redundance check over buffer.
Definition: checksum.c:139
u32_t uint_t
32-bit unsigned integer
Definition: rnrconfig.h:181
u16_t generate_checksum16(byte_t buf[], size_t len)
Computes the modular 16-bit checksum over buffer.
Definition: checksum.c:113
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
u8_t generate_checksum8(byte_t buf[], size_t len)
Computes the modular 8-bit checksum over buffer.
Definition: checksum.c:100
static long stoptimer(struct timespec *pstart)
Stop nano second timer.
static void starttimer ( struct timespec *  pstart)
static

Start nano second timer.

Parameters
[out]pstartPointer to start timespec.

Definition at line 161 of file example_checksum.c.

Referenced by profile().

162 {
163  clock_gettime(CLOCK_REALTIME, pstart);
164 }
static long stoptimer ( struct timespec *  pstart)
static

Stop nano second timer.

Parameters
[in]pstartPointer to start timespec.
Returns
Elaspse time in nano seconds.

Definition at line 173 of file example_checksum.c.

Referenced by profile().

174 {
175  struct timespec tstop;
176  long nsec;
177 
178  clock_gettime(CLOCK_REALTIME, &tstop);
179 
180  nsec = (tstop.tv_sec - pstart->tv_sec) * 1000000000;
181 
182  if( tstop.tv_nsec >= pstart->tv_nsec )
183  {
184  nsec += (tstop.tv_nsec - pstart->tv_nsec);
185  }
186  else
187  {
188  nsec += (1000000000 - pstart->tv_nsec + tstop.tv_nsec);
189  }
190 
191  return nsec;
192 }

Variable Documentation

OptsInfo_T OptsInfo[]
static
Initial value:
=
{
{
.long_opt = "poem",
.short_opt = OPTS_NO_SHORT,
.has_arg = no_argument,
.has_default = true,
.opt_addr = &OptsPrintPoem,
.fn_cvt = OptsCvtArgBool,
.fn_fmt = OptsFmtBool,
.arg_name = NULL,
.opt_desc = "Do [not] print poem."
},
{NULL, }
}
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
Definition: opts.c:1018
#define NULL
null pointer
Definition: rnrconfig.h:199
#define OPTS_NO_SHORT
no short option equivalent
Definition: opts.h:99
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
Definition: opts.c:1247
static bool_t OptsPrintPoem
do [not] print poem

Command line options information.

Definition at line 78 of file example_checksum.c.

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
.synopsis = "Example of librnr checksum algorithms.",
.long_desc =
"The %P command runs serveral checksum algorithms over a fixed body of "
"text and prints the results to stdout."
}

Program information.

Definition at line 66 of file example_checksum.c.