kvpTest.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "keyValuePair.h"

Go to the source code of this file.

Defines

#define MAX_STRINGS   10
#define MAX_STRING_LEN   100

Functions

int main (int argc, char *argv[])

Define Documentation

#define MAX_STRING_LEN   100

Definition at line 59 of file kvpTest.c.

Referenced by main().

#define MAX_STRINGS   10

Definition at line 58 of file kvpTest.c.

Referenced by main().


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 69 of file kvpTest.c.

References KVP_E_OK, kvpDumpInternals(), kvpError(), kvpGetDec(), kvpGetDouble(), kvpGetFloat(), kvpGetFloatArray(), kvpGetHex(), kvpGetInt(), kvpGetIntArray(), kvpGetString(), kvpGetStringArray(), kvpParse(), kvpPrintError(), kvpQueryLength(), kvpSetFloat(), kvpSetFloatArray(), kvpSetInt(), kvpSetIntArray(), kvpSetString(), kvpWrite(), MAX_STRING_LEN, MAX_STRINGS, nentries, and nkeys.

00070 {
00071    char record[1024] ;
00072    int nkeys ;
00073    int badtype ;
00074    char vtype[4] ;
00075    char key[80] ;
00076    int keysize ;
00077    int recordsize ;
00078    int nwritten ;
00079    char vString[200] ;
00080    int vsize ;
00081    double dVal ;
00082    int iVal ;
00083    int iarray[100] ;
00084    float farray[100] ;
00085    char sarray[MAX_STRINGS][MAX_STRING_LEN] ;
00086    int nentries ;
00087    int entry ;
00088    KvpErrorCode status ;
00089    char* tPtr ;
00090 
00091    printf ("Enter record : ") ;
00092    fgets (record, sizeof (record), stdin) ;
00093    recordsize = strlen (record) ;
00094    record[recordsize-1] = 0 ;
00095 
00096    if (record[0] != 0) {
00097       nkeys = kvpParse (record) ;
00098       if (nkeys >= 0) {
00099         printf ("record parsed into %d key value pairs\n", nkeys) ;
00100       }
00101       else {
00102         kvpPrintError (kvpError()) ;
00103         return (EXIT_FAILURE) ;
00104       }
00105    }
00106 
00107    printf ("***************** Enter additional keys *****\n") ;
00108    do {
00109       kvpDumpInternals();
00110       printf ("enter key : ") ;
00111       fgets (key, sizeof (key), stdin) ;
00112       keysize = strlen (key) ;
00113       key[keysize-1] = 0 ;
00114 
00115       if (key[0] != 0) {
00116          printf ("enter value : ") ;
00117          fgets (vString, sizeof (vString), stdin) ;
00118          vsize = strlen (vString) ;
00119          vString[vsize-1] = 0 ;
00120 
00121          do {
00122             printf ("enter value type s=string, i=int, f=float,"
00123                     " I=array of int : ") ;
00124             fgets (vtype, sizeof (vtype), stdin) ;
00125             badtype = 0 ;
00126             switch (vtype[0]) {
00127             case 's' :
00128                status = kvpSetString (key, vString) ;
00129                break ;
00130             case 'i' :
00131                iVal = strtol (vString, NULL, 0) ;
00132                status = kvpSetInt (key, iVal) ;
00133                break ;
00134             case 'I' :
00135                tPtr = vString ;
00136                nentries = 0 ;
00137                do {
00138                   iarray[nentries] = strtol (tPtr, &tPtr, 0) ;
00139                   if (*tPtr == ',') {
00140                      tPtr++ ;
00141                   }
00142                   nentries++ ;
00143                } while (*tPtr != 0) ;
00144                printf ("Adding %d element array of ints\n", nentries) ;
00145                status = kvpSetIntArray (key, iarray, nentries) ;
00146                break ;
00147             case 'f' :
00148                dVal = strtod (vString, NULL) ;
00149                status = kvpSetFloat (key, dVal) ;
00150                break ;
00151             case 'F' :
00152                tPtr = vString ;
00153                nentries = 0 ;
00154                do {
00155                   farray[nentries] = strtod (tPtr, &tPtr) ;
00156                   if (*tPtr == ',') {
00157                      tPtr++ ;
00158                   }
00159                   nentries++ ;
00160                } while (*tPtr != 0) ;
00161                printf ("Adding %d element array of floats\n", nentries) ;
00162                status = kvpSetFloatArray (key, farray, nentries) ;
00163                break ;
00164             default:
00165                badtype = 1 ;
00166             }
00167          } while (badtype) ;
00168 
00169          if (status == 0) {
00170             printf ("key inserted successfully\n") ;
00171          }
00172          else {
00173             kvpPrintError (kvpError()) ;
00174          }
00175       }
00176    } while (key[0] != 0) ;
00177 
00178    printf ("******************************************\n\n") ;
00179 
00180    nwritten = kvpWrite (1) ;
00181    if (nwritten <= 0) {
00182       perror ("write") ;
00183    }
00184    printf ("\n\n") ;
00185 
00186    printf ("************* Look up keys ****************\n") ;
00187    do {
00188       printf ("enter key : ") ;
00189       fgets (key, sizeof (key), stdin) ;
00190       keysize = strlen (key) ;
00191       key[keysize-1] = 0 ;
00192 
00193       if (key[0] != 0) {
00194 
00195          nentries = kvpQueryLength (key) ;
00196          if (nentries > 0) {
00197             printf ("Array of length %d found\n", nentries) ;
00198          }
00199 
00200          do {
00201             printf ("enter value type s=string, i=int, d=decimal, h=hex,"
00202                     " f=float, D=double\n"
00203                     "\tI=array of ints, F=array of floats : ") ;
00204             fgets (vtype, sizeof (vtype), stdin) ;
00205             badtype = 0 ;
00206 
00207             switch (vtype[0]) {
00208             case 's' :
00209                printf ("value=<%s>\n", kvpGetString (key)) ;
00210                break ;
00211             case 'i' :
00212                printf ("value=<%d>\n", kvpGetInt (key, -1)) ;
00213                break ;
00214             case 'd' :
00215                printf ("value=<%d>\n", kvpGetDec (key, -1)) ;
00216                break ;
00217             case 'h' :
00218                printf ("value=<%x>\n", kvpGetHex (key, -1)) ;
00219                break ;
00220 
00221             case 'S' :
00222                nentries = MAX_STRINGS ;
00223                status = kvpGetStringArray (key, sarray[0], MAX_STRING_LEN,
00224                                            &nentries) ;
00225               if (status == KVP_E_OK) {
00226                 printf ("array length %d, values: ", nentries) ;
00227                 for (entry = 0 ; entry < nentries ; entry++) {
00228                   printf ("  <%s>", sarray[entry]) ;
00229                 }
00230                 printf ("\n") ;
00231               }
00232               break ;
00233 
00234             case 'I' :
00235               nentries = sizeof (iarray) / sizeof (int) ;
00236               status = kvpGetIntArray (key, iarray, &nentries) ;
00237               if (status == KVP_E_OK) {
00238                 printf ("array length %d, values: ", nentries) ;
00239                 for (entry = 0 ; entry < nentries ; entry++) {
00240                   printf ("  %d", iarray[entry]) ;
00241                 }
00242                 printf ("\n") ;
00243               }
00244               break ;
00245             case 'f' :
00246                /*        fval = kvpGetFloat (key, HUGE) ;
00247                          printf ("value=<%g>\n", fval) ;*/
00248                printf ("value=<%g>\n", kvpGetFloat (key, HUGE)) ;
00249                break ;
00250             case 'D' :
00251                printf ("value=<%g>\n", kvpGetDouble (key, HUGE)) ;
00252                break ;
00253             case 'F' :
00254               nentries = sizeof (farray) / sizeof (float) ;
00255               status = kvpGetFloatArray (key, farray, &nentries) ;
00256               if (status == KVP_E_OK) {
00257                 printf ("array length %d, values: ", nentries) ;
00258                 for (entry = 0 ; entry < nentries ; entry++) {
00259                   printf ("  %g", farray[entry]) ;
00260                 }
00261                 printf ("\n") ;
00262               }
00263               break ;
00264 
00265             default:
00266                badtype = 1 ;
00267             }
00268          } while (badtype) ;
00269 
00270          kvpPrintError (kvpError ()) ;
00271       }
00272    } while (key[0] != 0) ;
00273    return (EXIT_SUCCESS) ;
00274 }


Generated on 18 Oct 2018 for loon by  doxygen 1.6.1