NEURON
file.cpp
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2 #include <../../nrnconf.h>
3 #endif
4 #if carbon
5 #undef MAC
6 #endif
7 
8 /*
9  * Copyright (c) 1991 Stanford University
10  * Copyright (c) 1991 Silicon Graphics, Inc.
11  *
12  * Permission to use, copy, modify, distribute, and sell this software and
13  * its documentation for any purpose is hereby granted without fee, provided
14  * that (i) the above copyright notices and this permission notice appear in
15  * all copies of the software and related documentation, and (ii) the names of
16  * Stanford and Silicon Graphics may not be used in any advertising or
17  * publicity relating to the software without the specific, prior written
18  * permission of Stanford and Silicon Graphics.
19  *
20  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
22  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
25  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
26  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
28  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29  * OF THIS SOFTWARE.
30  */
31 
32 // =======================================================================
33 //
34 // 1.5
35 // 1999/07/05 15:34:58
36 //
37 // Windows 3.1/NT InterViews Port
38 // Copyright (c) 1993 Tim Prinzing
39 //
40 // Permission to use, copy, modify, distribute, and sell this software and
41 // its documentation for any purpose is hereby granted without fee, provided
42 // that (i) the above copyright notice and this permission notice appear in
43 // all copies of the software and related documentation, and (ii) the name of
44 // Tim Prinzing may not be used in any advertising or publicity relating to
45 // the software without the specific, prior written permission of Tim Prinzing.
46 //
47 // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
49 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
50 //
51 // IN NO EVENT SHALL Tim Prinzing BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
52 // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
53 // RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
54 // POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR
55 // IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 //
57 // =======================================================================
58 
59 #include <OS/file.h>
60 #include <OS/string.h>
61 #include <OS/types.h>
62 #include <assert.h>
63 #include <fcntl.h>
64 #include <stdio.h>
65 
66 #ifndef MAC
67  #include <sys/stat.h>
68 #endif
69 
70 #ifdef HAVE_SYS_MMAN_H
71 extern "C" {
72 #include <sys/mman.h>
73 } // extern "C"
74 #endif
75 
76 #ifdef WIN16
77 #define WIN32
78 #endif
79 
80 #ifdef WIN32
81 #include <io.h>
82 #endif
83 
84 #ifdef CYGWIN
85 extern "C" {
86 // These are the POSIX definitions. Hopefully they won't conflict.
87  extern int _close(int);
88  extern int _read(int, void*, unsigned int);
89 } // extern "C"
90 #endif
91 
92 #if !defined(__GNUC__) || !defined (WIN32) && !defined (MAC)
93 
94 /* no standard place for these */
95 // Yes there is. Posix says read and close are in unistd.h.
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #else
99 extern "C" {
100 // These are the POSIX definitions. Hopefully they won't conflict.
101  extern int close(int);
102  extern int read(int, void*, unsigned int);
103 } // extern "C"
104 #endif
105 //#if defined(SGI)
106 //#endif
107 //#if defined(sun) && !defined(__SYSENT_H)
108 // extern int read(int, void*, unsigned int);
109 //#endif
110 //#if defined(AIXV3)
111 // extern int read(int, char*, unsigned int);
112 //#endif
113 //#if defined(apollo)
114 // extern long read(int, void*, unsigned int);
115 //#endif
116 //#if defined(__DECCXX)
117 // extern int read(int, void*, unsigned int);
118 //#endif
119 //}
120 #endif /* WIN32 */
121 
122 class FileInfo {
123 #ifndef MAC
124 public:
126  int fd_;
127  char* map_;
128  struct stat info_;
129  off_t pos_;
130  char* buf_;
131  unsigned int limit_;
132 
133  FileInfo(CopyString*, int fd);
134 #endif
135 };
136 
137 #ifndef MAC
139  name_ = s;
140  fd_ = fd;
141  pos_ = 0;
142  limit_ = 0;
143  map_ = nil;
144  buf_ = nil;
145 }
146 #endif
147 
148 
150  assert(i != nil);
151  rep_ = i;
152 }
153 
155  close();
156 #ifndef MAC
157  delete rep_->name_;
158  delete rep_;
159 #endif
160 }
161 
162 const String* File::name() const {
163 #ifndef MAC
164  return rep_->name_;
165 #else
166  return nil;
167 #endif
168 }
169 
170 long File::length() const {
171 #ifndef MAC
172  return rep_->info_.st_size;
173 #else
174  return 0;
175 #endif
176 }
177 
178 void File::close() {
179 #ifndef MAC
180  FileInfo* i = rep_;
181  if (i->fd_ >= 0) {
182  if (i->map_ != nil) {
183 #ifdef HAVE_SYS_MMAN_H // #if defined(SGI) || defined(__alpha)
184  munmap(i->map_, int(i->info_.st_size));
185 #endif
186  }
187  if (i->buf_ != nil) {
188  delete [] i->buf_;
189  }
190 #ifdef WIN32
191  _close(i->fd_);
192 #else
193  ::close(i->fd_);
194 #endif
195  i->fd_ = -1;
196  }
197 #endif
198 }
199 
200 void File::limit(unsigned int buffersize) {
201 #ifndef MAC
202  rep_->limit_ = buffersize;
203 #endif
204 }
205 
206 FileInfo* File::rep() const {
207 #ifndef MAC
208  return rep_;
209 #else
210  return nil;
211 #endif
212 }
213 
214 /* class InputFile */
215 
218 
220  CopyString* s = new CopyString(name);
221 #ifndef MAC
222 
223 #if defined(WIN32) && !defined(__MWERKS__) && !defined(CYGWIN)
224  int fd = _open(s->string(), O_RDONLY);
225 #else
226  /* cast to workaround DEC C++ prototype bug */
227  int fd = ::open(s->string(), O_RDONLY);
228 #endif
229  if (fd < 0) {
230  delete s;
231  return nil;
232  }
233  FileInfo* i = new FileInfo(s, fd);
234  if (fstat(fd, &i->info_) < 0) {
235  delete s;
236  delete i;
237  return nil;
238  }
239  return new InputFile(i);
240 #else
241  return nil;
242 #endif
243 }
244 
245 int InputFile::read(const char*& start) {
246 #ifndef MAC
247  FileInfo* i = rep();
248  int len = (int)(i->info_.st_size);
249  if (i->pos_ >= len) {
250  return 0;
251  }
252  if (i->limit_ != 0 && len > i->limit_) {
253  len = (int)(i->limit_);
254  }
255 #if HAVE_SYS_MMAN_H // #if defined(SGI) || defined(__alpha)
256  i->map_ = mmap(0, len, PROT_READ, MAP_PRIVATE, i->fd_, i->pos_);
257  if ((long)(i->map_) == -1) {
258  return -1;
259  }
260  start = i->map_;
261 #else
262  if (i->buf_ == nil) {
263  i->buf_ = new char[len];
264  }
265  start = i->buf_;
266 #ifdef WIN32
267  len = _read(i->fd_, i->buf_, len);
268 #else
269  len = ::read(i->fd_, i->buf_, len);
270 #endif /* WIN32 */
271 
272 #endif
273  i->pos_ += len;
274  return len;
275 #else
276  return 0;
277 #endif
278 }
279 
280 /* class StdInput */
281 
282 #if !MAC
283 StdInput::StdInput() : InputFile(new FileInfo(new CopyString("-stdin"), 0)) { }
284 #endif
286 
287 long StdInput::length() const { return -1; }
288 
289 int StdInput::read(const char*& start) {
290 #ifndef MAC
291  FileInfo* i = rep();
292  if (i->buf_ == nil) {
293  if (i->limit_ == 0) {
294  i->limit_ = BUFSIZ;
295  }
296  i->buf_ = new char[i->limit_];
297  }
298 
299 #ifdef WIN32
300  int nbytes = _read(i->fd_, i->buf_, i->limit_);
301 #else
302  int nbytes = ::read(i->fd_, i->buf_, i->limit_);
303 #endif /* WIN32 */
304 
305  if (nbytes > 0) {
306  start = (const char*)(i->buf_);
307  }
308  return nbytes;
309 #else
310  return 0;
311 #endif
312 }
#define assert(ex)
Definition: hocassrt.h:26
Definition: file.h:36
virtual void limit(unsigned int buffersize)
Definition: file.cpp:200
char * buf_
Definition: file.cpp:130
FileInfo * rep() const
Definition: file.cpp:206
int fd_
Definition: file.cpp:126
Definition: file.h:56
FileInfo(CopyString *, int fd)
Definition: file.cpp:138
void start()
Definition: hel2mos.cpp:205
static InputFile * open(const String &name)
Definition: file.cpp:219
const char * string() const
Definition: string.h:139
virtual ~File()
Definition: file.cpp:154
_CONST char * s
Definition: system.cpp:74
virtual long length() const
Definition: file.cpp:170
File(FileInfo *)
Definition: file.cpp:149
int
Definition: nrnmusic.cpp:71
#define CopyString
Definition: _defines.h:2
int read(int, void *, unsigned int)
virtual ~StdInput()
Definition: file.cpp:285
#define FileInfo
Definition: _defines.h:6
StdInput()
Definition: file.cpp:283
struct stat info_
Definition: file.cpp:128
CopyString * name_
Definition: file.cpp:125
virtual long length() const
Definition: file.cpp:287
#define nil
Definition: enter-scope.h:36
InputFile(FileInfo *)
Definition: file.cpp:216
virtual const String * name() const
Definition: file.cpp:162
off_t pos_
Definition: file.cpp:129
virtual ~InputFile()
Definition: file.cpp:217
unsigned int limit_
Definition: file.cpp:131
#define i
Definition: md1redef.h:12
virtual int read(const char *&start)
Definition: file.cpp:245
Definition: string.h:34
virtual int read(const char *&start)
Definition: file.cpp:289
int close(int)
virtual void close()
Definition: file.cpp:178
char * map_
Definition: file.cpp:127