core/vil/vil_open.cxx

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

Generated on Mon Nov 23 05:08:37 2009 for core/vil by  doxygen 1.5.1