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

example_c_2.c

C example program for the Open-RJ core library. Demonstrates enumeration of all non-empty records and their fields via structure members.

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

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