/* Gamelon(tm) File I/O Library Sample Code  C Language   */

/* (C)1994 Menai Corporation(tm)  Released for public use */

 

/* Example 9b. How to read the contents of stored objects */

/* when structure of the file is unknown.                 */

 

/* If the structure of the file is unknown, the program   */

/* must first determine the datatype of each object       */

/* before selecting the appropriate function with which   */

/* to read the value within the object. In this example,  */

/* we have used the variable sizes common to personal     */

/* computers.                                             */

 

/* Beginning file layout:                        */

/*   {                                       }   */

/*    [14]['F']["Wherefore art thou, Romeo?"]    */

 

#include <stdlib.h>

#include <stdio.h>

#include "gfcursor.h"

 

void ErrorNotification(void);

void ErrorNotification(void) {

printf("Error encountered.\n");

}

 

int main(void) {

/* Declare a few variables which will be used. */

int answer;

int valid;

size_t objsize;

signed char sc;

short s;

long l;

unsigned char uc;

unsigned short us;

unsigned long ul;

float f;

double d;

long double ld;

char newchar;

char *str;

unsigned char *buffer;

unsigned long filelen;

enum OTYPE objtype;

CURSOR *c = new_CURSOR(NULL,

"newfile",

AM_SHARED,

NULL,

0);

eCursorQueryValid(c, &valid);

if (!valid)

exit(1);

/* Check that the file has contents. */

eObjQueryAggregateEmpty(c, &answer);

if (answer)

exit(1);

eCursorMoveIn(c);

eCursorQueryOnObject(c, &answer);

while (answer) {

eObjQueryType(c, &objtype, NULL);

switch (objtype) {

case OT_SINT:

eObjQueryDataSize(c, &objsize);

switch (objsize) {

case 1: /* signed char */

eObjReadSignedChar(c, &sc);

printf("sc = %d\n", (int)sc);

break;

case 2: /* short */

eObjReadShort(c, &s);

printf("s = %hd\n", s);

break;

case 4: /* long */

eObjReadLong(c, &l);

printf("l = %ld\n", l);

break;

default:

/* Programmer decides not to try to handle oddsized */

/* signed integers other than to indicate an error. */

ErrorNotification();

} /* end OT_SINT switch */

break;

case OT_UINT:

eObjQueryDataSize(c, &objsize);

switch (objsize) {

case 1: /* unsigned char */

eObjReadUnsignedChar(c, &uc);

printf("uc = %u\n", (unsigned int)uc);

break;

case 2: /* unsigned short */

eObjReadUnsignedShort(c, &us);

printf("us = %hu\n", us);

break;

case 4: /* unsigned long */

eObjReadUnsignedLong(c, &ul);

printf("ul = %lu\n", ul);

break;

default:

/* Programmer decides not to try to handle oddsized */

/* unsigned integers other than to indicate an error.*/

ErrorNotification();

} /* end OT_UINT switch */

break;

case OT_FLT:

eObjQueryDataSize(c, &objsize);

switch (objsize) {

case 4: /* float */

eObjReadFloat(c, &f);

printf("f = %f\n", f);

break;

case 8: /* double */

eObjReadDouble(c, &d);

printf("d = %f\n", d);

break;

case 10: /* long double */

eObjReadLongDouble(c, &ld);

printf("ld = %Lf\n", ld);

break;

default:

/* Programmer decides not to try to handle odd */

/* sized floats other than to indicate an error.*/

ErrorNotification();

} /* end OT_FLT switch */

break;

case OT_CHAR: /* character */

eObjReadChar(c, &newchar);

printf("newchar = %c\n", newchar);

break;

case OT_STR: /* string */

eObjQueryDataSize(c, &objsize);

str = malloc(objsize);

eObjReadString(c, str, objsize);

printf("str = %s\n", str);

free (str);

break;

case OT_BIN: /* binary */

eObjQueryDataSize(c, &objsize);

buffer = malloc(objsize);

eObjReadBinary(c, buffer, &objsize, 0);

printf("buffer object\n");

free (buffer);

break;

/* The programmer decides to simply read the */

/* length of the stored OSFile object. */

case OT_OSF: /* OS File */

eObjQueryOSFileLength(c, &filelen);

printf("File length = %lu\n", filelen);

break;

case OT_BAD: /* bad object */

/* Programmer decides not to try to handle bad  */

/* objects other than to indicate an error.     */

ErrorNotification();

break;

default:

/* Programmer decides not to try to handle unrecog */

/* nized objects other than to indicate an error.    */

ErrorNotification();

} /* end objtype switch */

/* Do something with the value just read. */

if (eCursorMoveNext(c, OT_OBJ))

answer = 0;

} /* end while statement */

delete_CURSOR (c);

return 0;

}

Download this sample source code

Return to Samples List

 Home | Product | Consulting | Programming | Reviews | Company | Site Map | Guest Book

 Menai Corporation, 1010 El Camino Real, Suite 300, Menlo Park, California 94025-4335
 800-GAMELON (800-426-3566) (US only), 650-853-6450 (voice), 650-853-6453 (fax)
 Questions about our products and services:
info@menai.com.
 Comments about this web site:
webmaster@menai.com.

Copyright © 1996-98 Menai Corporation. All Rights Reserved. Menai, Gamelon and gamelon(stylized) are worldwide trademarks of Menai Corporation, registered in the United States of America, and the .[g] logo is a worldwide trademark of Menai Corporation. All other trademarks are owned by their respective owners.