/* 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 #include #include "gfcursor.hpp" void ErrorNotification(void); void ErrorNotification(void) { cerr << "Error encountered." << endl; } 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; char newchar; char *str; unsigned char *buffer; unsigned long filelen; CURSOR::OTYPE objtype; CURSOR *c = new CURSOR("newfile"); c->eCursorQueryValid(&valid); if (!valid) exit(-1); /* Check that the file has contents. */ c->eObjQueryAggregateEmpty(&answer); if (answer) exit(-1); c->eCursorMoveIn(); c->eCursorQueryOnObject(&answer); while (answer) { c->eObjQueryType(&objtype); switch (objtype) { case CURSOR::OT_AGGR: /* aggregate object */ cout << "aggregate" << endl; break; case CURSOR::OT_SINT: c->eObjQueryDataSize(&objsize); switch (objsize) { case 1: /* signed char */ c->eObjRead(&sc); cout << "sc = " << sc << endl; break; case 2: /* short */ c->eObjRead(&s); cout << "s = " << s << endl; break; case 4: /* long */ c->eObjRead(&l); cout << "l = " << l << endl; break; default: /* Programmer decides not to try to handle odd-sized */ /* signed integers other than to indicate an error. */ ErrorNotification(); } /* end OT_SINT switch */ break; case CURSOR::OT_UINT: c->eObjQueryDataSize(&objsize); switch (objsize) { case 1: /* unsigned char */ c->eObjRead(&uc); cout << "uc = " << uc << endl; break; case 2: /* unsigned short */ c->eObjRead(&us); cout << "us = " << us << endl; break; case 4: /* unsigned long */ c->eObjRead(&ul); cout << "ul = " << ul << endl; break; default: /* Programmer decides not to try to handle odd-sized */ /* unsigned integers other than to indicate an error.*/ ErrorNotification(); } /* end OT_UINT switch */ break; case CURSOR::OT_FLT: c->eObjQueryDataSize(&objsize); switch (objsize) { case 4: /* float */ c->eObjRead(&f); cout << "f = " << f << endl; break; case 8: /* double */ c->eObjRead(&d); cout << "d = " << d << endl; 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 CURSOR::OT_CHAR: /* character */ c->eObjRead(&newchar); cout << "newchar = " << newchar << endl; break; case CURSOR::OT_STR: /* string */ c->eObjQueryDataSize(&objsize); str = new char[objsize]; c->eObjRead(str, objsize); cout << "str = " << str << endl; delete str; break; case CURSOR::OT_BIN: /* binary */ c->eObjQueryDataSize(&objsize); buffer = new unsigned char[objsize]; c->eObjRead(buffer, &objsize); cout << "buffer object" << endl; delete buffer; break; /* The programmer decides to simply read the */ /* length of the stored OSFile object. */ case CURSOR::OT_OSF: /* OS File */ c->eObjQueryOSFileLength(&filelen); cout << "File length = " << filelen << endl; break; case CURSOR::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 (c->eCursorMoveNext()) answer = 0; } /* end while statement */ delete c; return 0; }