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 = (int)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 = (int)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 }
00097 
00098 #if defined(VCL_WIN32) && VXL_USE_WIN_WCHAR_T
00099 //  --------------------------------------------------------------------------------
00100 //  Windows' wchar_t overloading version
00101 //
00102 //
00103 vil_stream *vil_open(wchar_t const* what, char const* how)
00104 {
00105   // check for null pointer or empty strings.
00106   if (!what || !*what)
00107     return 0;
00108 
00109   // try to open as file first.
00110 #ifdef VIL_USE_FSTREAM64
00111   vil_stream *is = new vil_stream_fstream64(what, how);
00112 #else //VIL_USE_FSTREAM64
00113   vil_stream *is = new vil_stream_fstream(what, how);
00114 #endif //VIL_USE_FSTREAM64
00115 
00116   if (!is->ok()) {
00117     // this will delete the stream object.
00118     is->ref();
00119     is->unref();
00120     is = 0;
00121   }
00122 
00123 #if 0  // TODO: add wchar_t support in vil_stream_core
00124   if (!is) {
00125     // hacked check for filenames beginning "gen:".
00126     int l = wcslen(what);
00127     if (l > 4 && wcsncmp(what, L"gen:", 4) == 0) {
00128       if (vcl_strcmp(how, "r") == 0) {
00129         // Make an in-core stream...
00130         vil_stream_core *cis = new vil_stream_core();
00131         cis->write(what, l+1);
00132         is = cis;
00133       }
00134       else {
00135         vcl_cerr << __FILE__ ": cannot open gen:* for writing\n";
00136       }
00137     }
00138   }
00139   if (is && !is->ok()) {
00140     // this will delete the stream object.
00141     is->ref();
00142     is->unref();
00143     is = 0;
00144   }
00145 #endif
00146 
00147 #if 0  // TODO: add wchar_t support in vil_stream_url
00148   if (!is) {
00149     // maybe it's a URL?
00150     int l = vcl_strlen(what);
00151     if (l > 4 && vcl_strncmp(what, "http://", 7) == 0) {
00152       if (vcl_strcmp(how, "r") == 0) {
00153         is = new vil_stream_url(what);
00154       }
00155       else
00156         vcl_cerr << __FILE__ ": cannot open URL for writing (yet)\n";
00157     }
00158   }
00159   if (is && !is->ok()) {
00160     // this will delete the stream object.
00161     is->ref();
00162     is->unref();
00163     is = 0;
00164   }
00165 #endif
00166 
00167   return is;
00168 }
00169 
00170 #endif //defined(VCL_WIN32) && VXL_USE_WIN_WCHAR_T