home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0948.lha / Snoopy / Ini / Examples / dbase.c < prev    next >
C/C++ Source or Header  |  1993-12-20  |  5KB  |  133 lines

  1. /*
  2. ** small database program using the ini.library
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <dos/dos.h>
  8. #include <dos/dosasl.h>
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11.  
  12. #include "/libraries/ini.h"
  13. #include "/clib/ini_protos.h"
  14. #include "/pragmas/ini_pragmas.h"
  15.  
  16. #define ARG_TEMPLATE    "DBASE/A,INDEX/S,FIND/K,ADD/K,REMOVE/K,CONTENTS/K"
  17. #define ARGOPT_DBASE    0
  18. #define ARGOPT_INDEX    1
  19. #define ARGOPT_FIND     2   
  20. #define ARGOPT_ADD      3
  21. #define ARGOPT_REMOVE   4
  22. #define ARGOPT_CONTENTS 5
  23. #define ARGOPT_COUNT    6
  24.  
  25. #define DBASE_SECTION   "DBASE"
  26.  
  27. static UBYTE *VersTag = "\0$VER: dbase 1.0 (Oct.1,1993)";
  28. struct RDArgs *args = NULL;
  29. LONG opts[ ARGOPT_COUNT ];
  30. struct IniBase *IniBase;
  31. STRPTR fileName;
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.     if( ( args = ReadArgs( ARG_TEMPLATE, opts, NULL ) ) == NULL )
  36.     {
  37.         PrintFault( IoErr(), NULL );
  38.         return( RETURN_ERROR );
  39.     }
  40.  
  41.     if( ( IniBase = (struct IniBase *)OpenLibrary( "ini.library", 0 ) ) != NULL )
  42.     {
  43.         INIPARSER parser; 
  44.         INILINEINFO *info;
  45.         INIERROR rc;
  46.  
  47.         fileName = (STRPTR)(opts[ARGOPT_DBASE]);
  48.  
  49.         /* open the dbase */
  50.         if( ( rc = ini_New( fileName, &parser ) ) == INIERROR_NONE )
  51.         {
  52.             /* now that the ini.library has opened our small database, we have 
  53.                four options available
  54.                
  55.                 * "INDEX" = print out a list of all contents
  56.                 * "FIND" = find an entry and show it
  57.                 * "ADD" = add a new entry
  58.                 * "REMOVE" = remove an existing entry
  59.  
  60.             */
  61.             
  62.             if( opts[ ARGOPT_INDEX ] ) 
  63.             {
  64.                 /* find section header */
  65.                 if( ( info = ini_GetHeader( &parser, DBASE_SECTION ) ) != NULL )
  66.                 {
  67.                     /* the first element in the list is the first after the header */
  68.                     for( info = (INILINEINFO *)info->node.ln_Succ;
  69.                          info && (info->flags!=INIFLAG_HEADER);
  70.                          info = (INILINEINFO *)info->node.ln_Succ )
  71.                     {
  72.                         /* show only variables */
  73.                         if( info->flags == INIFLAG_VARIABLE )
  74.                         {
  75.                             printf("%s=%s\n", info->variable, info->contents );
  76.                         }
  77.                     }
  78.                 }
  79.                 else printf("ERROR, cannot find DBASE section\n");
  80.             }           
  81.             else if( opts[ ARGOPT_FIND ] )
  82.             {
  83.                 char buffer[256];
  84.  
  85.                 /* we use ini_GetString() for this simple task */
  86.                 ini_GetString( &parser,
  87.                                DBASE_SECTION,
  88.                                (STRPTR)opts[ ARGOPT_FIND ],
  89.                                "<not found>",
  90.                                buffer,
  91.                                256 );
  92.  
  93.                 printf("%s=%s\n", (STRPTR)opts[ ARGOPT_FIND ], buffer );               
  94.             }
  95.             else if( opts[ ARGOPT_REMOVE ] )
  96.             {
  97.                 if( ( rc = ini_RemoveVariable( &parser, DBASE_SECTION,
  98.                                             (STRPTR)opts[ ARGOPT_REMOVE ] ) ) == INIERROR_NONE )
  99.                 {
  100.                     if( ( rc = ini_Save( fileName, &parser ) ) != INIERROR_NONE )
  101.                         printf( "INIERROR:\"%s\" returned from ini_Save()\n", ini_ErrorString( rc ) );                   
  102.                 }
  103.                 else printf( "INIERROR:\"%s\" returned from ini_RemoveVariable()\n", ini_ErrorString( rc ) );
  104.             }
  105.             else if( opts[ ARGOPT_ADD ] )
  106.             {
  107.                 /* add string "<ARGOPT_ADD>=<ARGOPT_CONTENTS>". We can use
  108.                    ini_ChangeString() for that matter, because this function
  109.                    automagically appends the name if it doesn't yet exist */
  110.  
  111.                 STRPTR newContents = opts[ARGOPT_CONTENTS] ? (STRPTR)opts[ ARGOPT_CONTENTS ] : (STRPTR)"<unused>";
  112.                 
  113.                 if( ( rc = ini_ChangeString( &parser, DBASE_SECTION,
  114.                             (STRPTR)opts[ ARGOPT_ADD ], newContents ) ) == INIERROR_NONE )
  115.                 {
  116.                     if( ( rc = ini_Save( fileName, (INIPARSER *)&parser ) ) != INIERROR_NONE )
  117.                         printf( "INIERROR:\"%s\" returned from ini_Save()\n", ini_ErrorString( rc ) );
  118.                 }
  119.                 else printf( "INIERROR:\"%s\" returned from ini_ChangeString()\n", ini_ErrorString( rc ) );
  120.             }
  121.  
  122.             ini_Delete( &parser );
  123.         }
  124.         else printf( "INIERROR:\"%s\" returned from ini_New()\n", ini_ErrorString( rc ) );
  125.  
  126.         CloseLibrary( (struct Library *)IniBase );
  127.     }
  128.  
  129.     FreeArgs( args );
  130.  
  131.     return( RETURN_OK );
  132. }
  133.