Open-RJ C/C++ User's Journal Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

example_c_3.c

C example program for the Open-RJ core library. Demonstrates searching for fields in records.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_3.c
00003  *
00004  * Purpose:     C example program for the Open-RJ core library. Demonstrates
00005  *              searching for fields in records.
00006  *
00007  * Created:     12th May 2006
00008  * Updated:     15th May 2006
00009  *
00010  * www:         http://www.openrj.org/
00011  *
00012  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00013  *              All rights reserved.
00014  *
00015  *              (Licensed under the Synesis Software Open License)
00016  *
00017  *              This source code is placed into the public domain 2006
00018  *              by Synesis Software Pty Ltd. There are no restrictions
00019  *              whatsoever to your use of the software.
00020  *
00021  * ////////////////////////////////////////////////////////////////////////// */
00022 
00023 
00024 /* Open-RJ Header Files */
00025 #include <openrj/openrj.h>
00026 
00027 /* Standard C Library Files */
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 /* /////////////////////////////////////////////////////////////////////////////
00033  * Globals
00034  */
00035 
00036 /* Sample database */
00037 static const char   contents[] =
00038 
00039     "%% Sample Open-RJ database - Cats and Dogs\n"
00040     "%% Created:   28th September 2004\n"
00041     "%% Updated:   29th September 2004\n"
00042     "Name:      Barney\n"
00043     "Species:   Dog\n"
00044     "Breed:     Bijon \\\n"
00045     "           Frieze\n"
00046     "%%\n"
00047     "Name:      Elsa\n"
00048     "Species:   Dog\n"
00049     "Breed:     Mixed\n"
00050     "%%\n"
00051     "Name:      Fluffy Kitten\n"
00052     "Species:   Cat\n"
00053     "%%\n"
00054     "Name:      Moet\n"
00055     "Species:   Dog\n"
00056     "Breed:     Boxer\n"
00057     "%%\n"
00058     "Name:      Rebel\n"
00059     "Species:   Dog\n"
00060     "Breed:     German \\\n"
00061     "           Shepherd\n"
00062     "%%\n"
00063     "Name:      Sparky\n"
00064     "Species:   Cat\n"
00065     "%%\n";
00066 
00067 /* ////////////////////////////////////////////////////////////////////////// */
00068 
00069 int main()
00070 {
00071     unsigned            flags   =   ORJ_FLAG_ELIDEBLANKRECORDS | ORJ_FLAG_IGNORECASEONLOOKUP;
00072     ORJDatabase const   *db;
00073     ORJError            error;
00074     ORJRC               rc;
00075 
00076     /* 1. Create a database instance on memory, so use memory_database */
00077     rc = ORJ_CreateDatabaseFromMemory(contents, sizeof(contents), NULL, flags, &db, &error);
00078 
00079     if(0 != rc)
00080     {
00081         printf("Error at line %ld, column %ld: %s\n", (long)error.invalidLine, (long)error.invalidColumn, ORJ_GetErrorStringA(rc));
00082 
00083         return EXIT_FAILURE;
00084     }
00085     else
00086     {
00087         size_t  iRecord;
00088 
00089         printf( "Database has %ld lines in %ld fields in %ld records\n"
00090             ,   (long)ORJ_Database_GetNumLinesA(db)
00091             ,   (long)ORJ_Database_GetNumFieldsA(db)
00092             ,   (long)ORJ_Database_GetNumRecordsA(db));
00093 
00094         printf("Enumerating all records in the database, and displaying the \"Breed\" fields:\n");
00095 
00096         for(iRecord = 0; iRecord < ORJ_Database_GetNumRecordsA(db); ++iRecord)
00097         {
00098             ORJRecordA const    *record;
00099             ORJFieldA const     *breed;
00100 
00101             ORJ_Database_GetRecordA(db, iRecord, &record);
00102 
00103             breed   =   ORJ_Record_FindFieldByNameA(record, "BREED", NULL);
00104 
00105             if(NULL == breed)
00106             {
00107                 printf("record does not contain a field called \"Breed\"\n");
00108             }
00109             else
00110             {
00111                 ORJFieldA const     *name       =   ORJ_Record_FindFieldByNameA(record, "NAME", NULL);
00112                 ORJFieldA const     *species    =   ORJ_Record_FindFieldByNameA(record, "SPECIES", NULL);
00113                 ORJStringA const    *comment;
00114 
00115                 ORJ_Record_GetCommentA(record, &comment);
00116 
00117                 printf("record-#%ld; %.*s (%ld fields)\n", (long)iRecord, (int)comment->len, comment->ptr, (long)ORJ_Record_GetNumFieldsA(record));
00118                 printf("  %.*s=%.*s\n", (int)name->name.len, name->name.ptr, (int)name->value.len, name->value.ptr);
00119                 printf("  %.*s=%.*s\n", (int)species->name.len, species->name.ptr, (int)species->value.len, species->value.ptr);
00120                 printf("  %.*s=%.*s\n", (int)breed->name.len, breed->name.ptr, (int)breed->value.len, breed->value.ptr);
00121             }
00122         }
00123 
00124         /* Finally, free the database */
00125         ORJ_FreeDatabase(db);
00126     }
00127 
00128     return EXIT_SUCCESS;
00129 }
00130 
00131 /* /////////////////////////////////////////////////////////////////////////////
00132  * end of file
00133  */

Open-RJ Library documentation © Synesis Software Pty Ltd, 2004-2005