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

example_c_6.c

C example program for the Open-RJ core library. Demonstrates processing of all database fields via API functions.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_c_6.c
00003  *
00004  * Purpose:     C example program for the Open-RJ core library. Demonstrates
00005  *              processing of all database fields via API functions.
00006  *
00007  * Created:     12th May 2006
00008  * Updated:     12th 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 <assert.h>
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   =   0;
00073     ORJDatabase const   *db;
00074     ORJError            error;
00075     ORJRC               rc;
00076 
00077     /* 1. Create a database instance on memory */
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  i;
00089 
00090         printf("Enumerating all fields in the database:\n");
00091 
00092         for(i = 0; i < db->numFields; ++i)
00093         {
00094             ORJField const  *field;
00095 
00096             rc = ORJ_Database_GetFieldA(db, i, &field);
00097 
00098             /* Asserting on a runtime condition is an absolute no-no. In this case it
00099              * is legitimate because we're working on a memory database whose contents
00100              * are fixed.
00101              */
00102             assert(ORJ_RC_SUCCESS == rc);
00103 
00104             printf("  %.*s=%.*s\n", (int)field->name.len, field->name.ptr, (int)field->value.len, field->value.ptr);
00105         }
00106 
00107         /* Free the database */
00108         ORJ_FreeDatabase(db);
00109     }
00110 
00111     return EXIT_SUCCESS;
00112 }
00113 
00114 /* /////////////////////////////////////////////////////////////////////////////
00115  * end of file
00116  */

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