vil_open.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_open.cxx
2 //:
3 // \file
4 // \author fsm
5 
6 #include <iostream>
7 #include <cstring>
8 #include <fstream>
9 #include "vil_open.h"
10 
11 
12 #include <vil/vil_stream_fstream.h>
13 
14 #include <vil/vil_stream_core.h>
15 #include <vil/vil_stream_url.h>
16 // not used? #include <vcl_compiler.h>
17 
18 vil_stream *vil_open(char const* what, char const* how)
19 {
20  // check for null pointer or empty strings.
21  if (!what || !*what)
22  return nullptr;
23 
24  // try to open as file first.
25 #ifdef VIL_USE_FSTREAM64
26  vil_stream *is = new vil_stream_fstream64(what, how);
27 #else //VIL_USE_FSTREAM64
28  vil_stream *is = new vil_stream_fstream(what, how);
29 #endif //VIL_USE_FSTREAM64
30 
31 #if 0
32  // unfortunately, the following doesn't work because (note typo)
33  // vil_open<("/tmp/foo.jgp")
34  // will create a new file, permissions allowing, instead of opening
35  // the intended file /tmp/foo.jpg. suggest long-term solution is to
36  // make a new vil_open<*() set of functions which can open an image
37  // for reading and/or writing depending on the caller's need and that
38  // vil_load() just does a vil_open<() for reading. i do not think people
39  // expect "loading an image" to open the disk file for writing by
40  // default. -- fsm
41 #ifdef VIL_USE_FSTREAM64
42  vil_stream *is = new vil_stream_fstream64(what, "r+");
43 #else //VIL_USE_FSTREAM64
44  vil_stream *is = new vil_stream_fstream(what, "r+");
45 #endif //VIL_USE_FSTREAM64
46 #endif
47  if (!is->ok()) {
48  // this will delete the stream object.
49  is->ref();
50  is->unref();
51  is = nullptr;
52  }
53 
54  if (!is) {
55  // hacked check for filenames beginning "gen:".
56  int l = (int)std::strlen(what);
57  if (l > 4 && std::strncmp(what, "gen:", 4) == 0) {
58  if (std::strcmp(how, "r") == 0) {
59  // Make an in-core stream...
60  auto *cis = new vil_stream_core();
61  cis->write(what, l+1);
62  is = cis;
63  }
64  else {
65  std::cerr << __FILE__ ": cannot open gen:* for writing\n";
66  }
67  }
68  }
69  if (is && !is->ok()) {
70  // this will delete the stream object.
71  is->ref();
72  is->unref();
73  is = nullptr;
74  }
75 
76  if (!is) {
77  // maybe it's a URL?
78  int l = (int)std::strlen(what);
79  if (l > 4 && std::strncmp(what, "http://", 7) == 0) {
80  if (std::strcmp(how, "r") == 0) {
81  is = new vil_stream_url(what);
82  }
83  else
84  std::cerr << __FILE__ ": cannot open URL for writing (yet)\n";
85  }
86  }
87  if (is && !is->ok()) {
88  // this will delete the stream object.
89  is->ref();
90  is->unref();
91  is = nullptr;
92  }
93 
94  return is;
95 }
96 
97 #if defined(_WIN32) && VXL_USE_WIN_WCHAR_T
98 // --------------------------------------------------------------------------------
99 // Windows' wchar_t overloading version
100 //
101 //
102 vil_stream *vil_open(wchar_t const* what, char const* how)
103 {
104  // check for null pointer or empty strings.
105  if (!what || !*what)
106  return 0;
107 
108  // try to open as file first.
109 #ifdef VIL_USE_FSTREAM64
110  vil_stream *is = new vil_stream_fstream64(what, how);
111 #else //VIL_USE_FSTREAM64
112  vil_stream *is = new vil_stream_fstream(what, how);
113 #endif //VIL_USE_FSTREAM64
114 
115  if (!is->ok()) {
116  // this will delete the stream object.
117  is->ref();
118  is->unref();
119  is = 0;
120  }
121 
122 #if 0 // TODO: add wchar_t support in vil_stream_core
123  if (!is) {
124  // hacked check for filenames beginning "gen:".
125  int l = wcslen(what);
126  if (l > 4 && wcsncmp(what, L"gen:", 4) == 0) {
127  if (std::strcmp(how, "r") == 0) {
128  // Make an in-core stream...
129  vil_stream_core *cis = new vil_stream_core();
130  cis->write(what, l+1);
131  is = cis;
132  }
133  else {
134  std::cerr << __FILE__ ": cannot open gen:* for writing\n";
135  }
136  }
137  }
138  if (is && !is->ok()) {
139  // this will delete the stream object.
140  is->ref();
141  is->unref();
142  is = 0;
143  }
144 #endif
145 
146 #if 0 // TODO: add wchar_t support in vil_stream_url
147  if (!is) {
148  // maybe it's a URL?
149  int l = std::strlen(what);
150  if (l > 4 && std::strncmp(what, "http://", 7) == 0) {
151  if (std::strcmp(how, "r") == 0) {
152  is = new vil_stream_url(what);
153  }
154  else
155  std::cerr << __FILE__ ": cannot open URL for writing (yet)\n";
156  }
157  }
158  if (is && !is->ok()) {
159  // this will delete the stream object.
160  is->ref();
161  is->unref();
162  is = 0;
163  }
164 #endif
165 
166  return is;
167 }
168 
169 #endif //defined(_WIN32) && VXL_USE_WIN_WCHAR_T
vil_streampos write(void const *buf, vil_streampos n) override
Write n bytes from buf. Returns number of bytes written.
A vil_stream implementation using std::fstream.
open an URL
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
A vil_stream implementation using std::fstream.
void ref()
up/down the reference count.
Definition: vil_stream.h:45
void unref()
Definition: vil_stream.cxx:31
An in-core vil_stream implementation.
virtual bool ok() const =0
Return false if the stream is broken.
make a vil_stream from a filename, an URL, etc.
vil_stream * vil_open(char const *what, char const *how="r")
make a vil_stream from a filename, an URL, etc.
Definition: vil_open.cxx:18
An in-core vil_stream implementation.
open an URL.