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

example_c_8.c

C example program for the Open-RJ core library. Demonstrates use of processing instructions for field-name aliases.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_8.c
00003  *
00004  * Purpose:     C example program for the Open-RJ core library. Demonstrates
00005  *              use of processing instructions for field-name aliases.
00006  *
00007  * Created:     12th May 2006
00008  * Updated:     28th 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     "_OpenRJ.Field.Alias:   1:Name\n"
00043     "_OpenRJ.Field.Alias:   2:Species\n"
00044     "_OpenRJ.Field.Alias:   3:Breed\n"
00045     "%%\n"
00046     "1:  Barney\n"
00047     "2:  Dog\n"
00048     "3:  Bijon \\\n"
00049     "    Frieze\n"
00050     "%%\n"
00051     "1:  Elsa\n"
00052     "2:  Dog\n"
00053     "3:  Mixed\n"
00054     "%%\n"
00055     "1:  Fluffy Kitten\n"
00056     "2:  Cat\n"
00057     "%%\n"
00058     "1:  Moet\n"
00059     "2:  Dog\n"
00060     "3:  Boxer\n"
00061     "%%\n"
00062     "1:  Rebel\n"
00063     "2:  Dog\n"
00064     "3:  German \\\n"
00065     "    Shepherd\n"
00066     "%%\n"
00067     "1:  Sparky\n"
00068     "2:  Cat\n"
00069     "%%\n";
00070 
00071 /* ////////////////////////////////////////////////////////////////////////// */
00072 
00073 int main()
00074 {
00075     unsigned            flags   =   ORJ_FLAG_ELIDEBLANKRECORDS;
00076     ORJDatabase const   *db;
00077     ORJError            error;
00078     ORJRC               rc;
00079 
00080     /* 1. Create a database instance on memory, so use memory_database */
00081     rc = ORJ_CreateDatabaseFromMemory(contents, sizeof(contents), NULL, flags, &db, &error);
00082 
00083     if(0 != rc)
00084     {
00085         printf("Error at line %ld, column %ld: %s\n", (long)error.invalidLine, (long)error.invalidColumn, ORJ_GetErrorStringA(rc));
00086 
00087         return EXIT_FAILURE;
00088     }
00089     else
00090     {
00091         size_t  iRecord;
00092 
00093         printf( "Database has %ld lines in %ld fields in %ld records\n"
00094             ,   (long)db->numLines
00095             ,   (long)db->numFields
00096             ,   (long)db->numRecords);
00097 
00098         printf("Enumerating all fields in the database:\n");
00099 
00100         for(iRecord = 0; iRecord < db->numRecords; ++iRecord)
00101         {
00102             size_t              iField;
00103             ORJRecordA const    *record;
00104             ORJStringA const    *comment;
00105 
00106             record  =   &db->records[iRecord];
00107             comment =   &record->comment;
00108 
00109             if(0 != comment->len)
00110             {
00111                 printf("record-#%ld; %.*s (%ld fields)\n", (long)iRecord, (int)comment->len, comment->ptr, (long)record->numFields);
00112             }
00113             else
00114             {
00115                 printf("record-#%ld (%ld fields)\n", (long)iRecord, (long)record->numFields);
00116             }
00117 
00118             for(iField = 0; iField < record->numFields; ++iField)
00119             {
00120                 ORJFieldA const     *field;
00121                 ORJStringA const    *name;
00122                 ORJStringA const    *value;
00123 
00124                 field   =   &record->fields[iField];
00125                 name    =   &field->name;
00126                 value   =   &field->value;
00127 
00128                 printf("  field-#%ld %.*s=%.*s\n", (long)iField, (int)field->name.len, field->name.ptr, (int)field->value.len, field->value.ptr);
00129             }
00130         }
00131 
00132         /* Finally, free the database */
00133         ORJ_FreeDatabase(db);
00134     }
00135 
00136     return EXIT_SUCCESS;
00137 }
00138 
00139 /* /////////////////////////////////////////////////////////////////////////////
00140  * end of file
00141  */

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