comparison src/file-io.cc @ 5325:ecbe4aa87e51

[project @ 2005-04-29 20:48:35 by jwe]
author jwe
date Fri, 29 Apr 2005 20:48:36 +0000
parents 7b95f7fdf175
children ac8d64b9e76a
comparison
equal deleted inserted replaced
5324:d2d11284528e 5325:ecbe4aa87e51
49 #ifdef HAVE_UNISTD_H 49 #ifdef HAVE_UNISTD_H
50 #ifdef HAVE_SYS_TYPES_H 50 #ifdef HAVE_SYS_TYPES_H
51 #include <sys/types.h> 51 #include <sys/types.h>
52 #endif 52 #endif
53 #include <unistd.h> 53 #include <unistd.h>
54 #endif
55
56 #ifdef HAVE_ZLIB_H
57 #include <zlib.h>
54 #endif 58 #endif
55 59
56 #include "error.h" 60 #include "error.h"
57 #include "file-ops.h" 61 #include "file-ops.h"
58 #include "lo-ieee.h" 62 #include "lo-ieee.h"
124 unlink (filename.c_str ()); 128 unlink (filename.c_str ());
125 } 129 }
126 } 130 }
127 131
128 static std::ios::openmode 132 static std::ios::openmode
129 fopen_mode_to_ios_mode (const std::string& mode) 133 fopen_mode_to_ios_mode (const std::string& mode_arg)
130 { 134 {
131 std::ios::openmode retval = std::ios::in; 135 std::ios::openmode retval = std::ios::in;
132 136
133 if (! mode.empty ()) 137 if (! mode_arg.empty ())
134 { 138 {
135 // Could probably be faster, but does it really matter? 139 // Could probably be faster, but does it really matter?
136 140
137 if (mode == "rt") 141 std::string mode = mode_arg;
138 retval = std::ios::in; 142
139 else if (mode == "wt") 143 size_t pos = mode.find ('z');
140 retval = std::ios::out | std::ios::trunc; 144
141 else if (mode == "at") 145 if (pos != NPOS)
142 retval = std::ios::out | std::ios::app; 146 {
143 else if (mode == "r+t") 147 #if defined (HAVE_ZLIB)
144 retval = std::ios::in | std::ios::out; 148 mode.erase (pos, 1);
145 else if (mode == "w+t") 149 #else
146 retval = std::ios::in | std::ios::out | std::ios::trunc; 150 error ("this version of Octave does not support gzipped files");
147 else if (mode == "a+t") 151 #endif
148 retval = std::ios::in | std::ios::out | std::ios::ate; 152 }
149 else if (mode == "rb" || mode == "r") 153
150 retval = std::ios::in | std::ios::binary; 154 if (! error_state)
151 else if (mode == "wb" || mode == "w") 155 {
152 retval = std::ios::out | std::ios::trunc | std::ios::binary; 156 if (mode == "rt")
153 else if (mode == "ab" || mode == "a") 157 retval = std::ios::in;
154 retval = std::ios::out | std::ios::app | std::ios::binary; 158 else if (mode == "wt")
155 else if (mode == "r+b" || mode == "r+") 159 retval = std::ios::out | std::ios::trunc;
156 retval = std::ios::in | std::ios::out | std::ios::binary; 160 else if (mode == "at")
157 else if (mode == "w+b" || mode == "w+") 161 retval = std::ios::out | std::ios::app;
158 retval = (std::ios::in | std::ios::out | std::ios::trunc 162 else if (mode == "r+t")
159 | std::ios::binary); 163 retval = std::ios::in | std::ios::out;
160 else if (mode == "a+b" || mode == "a+") 164 else if (mode == "w+t")
161 retval = (std::ios::in | std::ios::out | std::ios::ate 165 retval = std::ios::in | std::ios::out | std::ios::trunc;
162 | std::ios::binary); 166 else if (mode == "a+t")
163 else 167 retval = std::ios::in | std::ios::out | std::ios::ate;
164 ::error ("invalid mode specified"); 168 else if (mode == "rb" || mode == "r")
169 retval = std::ios::in | std::ios::binary;
170 else if (mode == "wb" || mode == "w")
171 retval = std::ios::out | std::ios::trunc | std::ios::binary;
172 else if (mode == "ab" || mode == "a")
173 retval = std::ios::out | std::ios::app | std::ios::binary;
174 else if (mode == "r+b" || mode == "r+")
175 retval = std::ios::in | std::ios::out | std::ios::binary;
176 else if (mode == "w+b" || mode == "w+")
177 retval = (std::ios::in | std::ios::out | std::ios::trunc
178 | std::ios::binary);
179 else if (mode == "a+b" || mode == "a+")
180 retval = (std::ios::in | std::ios::out | std::ios::ate
181 | std::ios::binary);
182 else
183 ::error ("invalid mode specified");
184 }
165 } 185 }
166 186
167 return retval; 187 return retval;
168 } 188 }
169 189
384 oct_mach_info::float_format flt_fmt = 404 oct_mach_info::float_format flt_fmt =
385 oct_mach_info::string_to_float_format (arch); 405 oct_mach_info::string_to_float_format (arch);
386 406
387 if (! error_state) 407 if (! error_state)
388 { 408 {
389 FILE *fptr = ::fopen (name.c_str (), mode.c_str ()); 409 #if defined (HAVE_ZLIB)
390 410 std::string tmode = mode;
391 retval = octave_stdiostream::create (name, fptr, md, flt_fmt); 411
392 412 size_t pos = tmode.find ('z');
393 if (! fptr) 413
394 { 414 if (pos != NPOS)
395 using namespace std; 415 {
396 retval.error (::strerror (errno)); 416 tmode.erase (pos, 1);
397 } 417
418 gzFile fptr = ::gzopen (name.c_str (), tmode.c_str ());
419
420 if (fptr)
421 retval = octave_zstdiostream::create (name, fptr, md, flt_fmt);
422 else
423 {
424 using namespace std;
425 retval.error (::strerror (errno));
426 }
427 }
428 else
429 #endif
430 {
431 FILE *fptr = ::fopen (name.c_str (), mode.c_str ());
432
433 if (fptr)
434 retval = octave_stdiostream::create (name, fptr, md, flt_fmt);
435 else
436 {
437 using namespace std;
438 retval.error (::strerror (errno));
439 }
440 }
441
398 } 442 }
399 } 443 }
400 444
401 return retval; 445 return retval;
402 } 446 }
499 \"b\" to open in binary mode. On Windows and Macintosh systems, text\n\ 543 \"b\" to open in binary mode. On Windows and Macintosh systems, text\n\
500 mode reading and writing automatically converts linefeeds to the\n\ 544 mode reading and writing automatically converts linefeeds to the\n\
501 appropriate line end character for the system (carriage-return linefeed\n\ 545 appropriate line end character for the system (carriage-return linefeed\n\
502 on Windows, carriage-returnn on Macintosh). The default if no mode is\n\ 546 on Windows, carriage-returnn on Macintosh). The default if no mode is\n\
503 specified is binary mode.\n\ 547 specified is binary mode.\n\
548 \n\
549 Additionally, you may append a \"z\" to the mode string to open a\n\
550 gzipped file for reading or writing. For this to be successful, you\n\
551 must also open the file in binary mode.\n\
504 \n\ 552 \n\
505 The parameter @var{arch} is a string specifying the default data format\n\ 553 The parameter @var{arch} is a string specifying the default data format\n\
506 for the file. Valid values for @var{arch} are:\n\ 554 for the file. Valid values for @var{arch} are:\n\
507 \n\ 555 \n\
508 @table @asis\n\ 556 @table @asis\n\