E57 Foundation API v1.1.312  Aug. 10, 2011
Functions
examples/Cancel.cpp File Reference

example: stop writing after discover an error More...

Include dependency graph for Cancel.cpp:

Functions

void testOpen (ImageFile imf)
 Print whether ImageFile is open.
int main (int, char **)
 Example use of ImageFile::cancel.

Detailed Description

example: stop writing after discover an error

Also see listing at end of this page for source without line numbers (to cut&paste from).

00001 /*** Cancel.cpp example: stop writing after discover an error */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 void testOpen(ImageFile imf)
00011 {
00012     if (imf.isOpen())
00013         cout << "ImageFile is open" << endl;
00014     else
00015         cout << "ImageFile is closed" << endl;
00016 }
00017 
00019 int main(int /*argc*/, char** /*argv*/) {
00020     try {
00021         ImageFile imf("temp._e57", "w");
00022 
00023         testOpen(imf);
00024         if (/*need to stop writing file*/ 1) {
00025             cout << "Canceling write to " << imf.fileName() << endl;
00026             imf.cancel();
00027             testOpen(imf);
00028             return(-1);
00029         }
00030            
00031         imf.close(); // don't forget to explicitly close the ImageFile
00032     } catch(E57Exception& ex) {
00033         ex.report(__FILE__, __LINE__, __FUNCTION__);
00034         return(-1);
00035     }
00036     return(0);
00037 }

This example program shows how to stop the writing of an ImageFile and gracefully remove any partially written files from the disk. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

The ImageFile is opened for writing in source line 21. Source line 23 calls a function to print out whether ImageFile is open (which it is). Some error is encountered (illustrated by source line 24), and ImageFile::cancel is called on source line 26. This call causes the disk file to be unlinked (deleted) and the ImageFile isOpen state changes to false. The state is printed on the console by function called on source line 27. At this point, most of the calls to API functions (those that require the ImageFile to be open) will fail with an exception. So there is not much to do except communicate the abort to the caller.

The following console output is produced:

There is no XML section of the temp._e57 E57 file, because it was deleted by the program during the abort.

Here is the source code without line numbers to cut&paste from:

/*** Cancel.cpp example: stop writing after discover an error */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

void testOpen(ImageFile imf)
{
    if (imf.isOpen())
        cout << "ImageFile is open" << endl;
    else
        cout << "ImageFile is closed" << endl;
}

int main(int /*argc*/, char** /*argv*/) {
    try {
        ImageFile imf("temp._e57", "w");

        testOpen(imf);
        if (/*need to stop writing file*/ 1) {
            cout << "Canceling write to " << imf.fileName() << endl;
            imf.cancel();
            testOpen(imf);
            return(-1);
        }
           
        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines