core/vil1/vil1_open.cxx

Go to the documentation of this file.
00001 // This is core/vil1/vil1_open.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vil1_open.h"
00010 
00011 #include <vcl_cstring.h>  // strncmp()
00012 
00013 #include <vil1/vil1_stream_fstream.h>
00014 #include <vil1/vil1_stream_core.h>
00015 #include <vil1/vil1_stream_url.h>
00016 
00017 vil1_stream *vil1_open(char const* what, char const* how)
00018 {
00019   // check for null pointer or empty strings.
00020   if (!what || !*what)
00021     return 0;
00022 
00023   // try to open as file first.
00024   vil1_stream *is = new vil1_stream_fstream(what, how);
00025 #if 0
00026   // unfortunately, the following doesn't work because (note typo)
00027   //    vil1_open("/tmp/foo.jgp")
00028   // will create a new file, permissions allowing, instead of opening
00029   // the intended file /tmp/foo.jpg. suggest long-term solution is to
00030   // make a new vil1_open*() set of functions which can open an image
00031   // for reading and/or writing depending on the caller's need and that
00032   // vil1_load() just does a vil1_open() for reading. i do not think people
00033   // expect "loading an image" to open the disk file for writing by
00034   // default. -- fsm
00035   vil1_stream *is = new vil1_stream_fstream(what, "r+");
00036 #endif
00037   if (!is->ok()) {
00038     // this will delete the stream object.
00039     is->ref();
00040     is->unref();
00041     is = 0;
00042   }
00043 
00044   if (!is) {
00045     // hacked check for filenames beginning "gen:".
00046     int l = vcl_strlen(what);
00047     if (l > 4 && vcl_strncmp(what, "gen:", 4) == 0) {
00048       if (vcl_strcmp(how, "r") == 0) {
00049         // Make an in-core stream...
00050         vil1_stream_core *cis = new vil1_stream_core();
00051         cis->write(what, l+1);
00052         is = cis;
00053       }
00054       else {
00055         vcl_cerr << __FILE__ ": cannot open gen:* for writing\n";
00056       }
00057     }
00058   }
00059   if (is && !is->ok()) {
00060     // this will delete the stream object.
00061     is->ref();
00062     is->unref();
00063     is = 0;
00064   }
00065 
00066   if (!is) {
00067     // maybe it's a URL?
00068     int l = vcl_strlen(what);
00069     if (l > 4 && vcl_strncmp(what, "http://", 7) == 0) {
00070 #ifdef __APPLE__
00071       vcl_cerr << __FILE__ ": cannot open URL for writing (yet)\n";
00072 #else
00073       if (vcl_strcmp(how, "r") == 0) {
00074         is = new vil1_stream_url(what);
00075       }
00076       else vcl_cerr << __FILE__ ": cannot open URL for writing (yet)\n";
00077 #endif
00078     }
00079   }
00080   if (is && !is->ok()) {
00081     // this will delete the stream object.
00082     is->ref();
00083     is->unref();
00084     is = 0;
00085   }
00086 
00087   return is;
00088 }

Generated on Mon Mar 8 05:09:33 2010 for core/vil1 by  doxygen 1.5.1