zfstream.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * A C++ I/O streams interface to the zlib gz* functions
  3. *
  4. * by Ludwig Schwardt <schwardt@sun.ac.za>
  5. * original version by Kevin Ruland <kevin@rodin.wustl.edu>
  6. *
  7. * This version is standard-compliant and compatible with gcc 3.x.
  8. */
  9. #include "zfstream.h"
  10. #include <cstring> // for strcpy, strcat, strlen (mode strings)
  11. #include <cstdio> // for BUFSIZ
  12. // Internal buffer sizes (default and "unbuffered" versions)
  13. #define BIGBUFSIZE BUFSIZ
  14. #define SMALLBUFSIZE 1
  15. /*****************************************************************************/
  16. // Default constructor
  17. gzfilebuf::gzfilebuf()
  18. : file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
  19. buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true)
  20. {
  21. // No buffers to start with
  22. this->disable_buffer();
  23. }
  24. // Destructor
  25. gzfilebuf::~gzfilebuf()
  26. {
  27. // Sync output buffer and close only if responsible for file
  28. // (i.e. attached streams should be left open at this stage)
  29. this->sync();
  30. if (own_fd)
  31. this->close();
  32. // Make sure internal buffer is deallocated
  33. this->disable_buffer();
  34. }
  35. // Set compression level and strategy
  36. int
  37. gzfilebuf::setcompression(int comp_level,
  38. int comp_strategy)
  39. {
  40. return gzsetparams(file, comp_level, comp_strategy);
  41. }
  42. // Open gzipped file
  43. gzfilebuf*
  44. gzfilebuf::open(const char *name,
  45. std::ios_base::openmode mode)
  46. {
  47. // Fail if file already open
  48. if (this->is_open())
  49. return NULL;
  50. // Don't support simultaneous read/write access (yet)
  51. if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
  52. return NULL;
  53. // Build mode string for gzopen and check it [27.8.1.3.2]
  54. char char_mode[6] = "\0\0\0\0\0";
  55. if (!this->open_mode(mode, char_mode))
  56. return NULL;
  57. // Attempt to open file
  58. if ((file = gzopen(name, char_mode)) == NULL)
  59. return NULL;
  60. // On success, allocate internal buffer and set flags
  61. this->enable_buffer();
  62. io_mode = mode;
  63. own_fd = true;
  64. return this;
  65. }
  66. // Attach to gzipped file
  67. gzfilebuf*
  68. gzfilebuf::attach(int fd,
  69. std::ios_base::openmode mode)
  70. {
  71. // Fail if file already open
  72. if (this->is_open())
  73. return NULL;
  74. // Don't support simultaneous read/write access (yet)
  75. if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
  76. return NULL;
  77. // Build mode string for gzdopen and check it [27.8.1.3.2]
  78. char char_mode[6] = "\0\0\0\0\0";
  79. if (!this->open_mode(mode, char_mode))
  80. return NULL;
  81. // Attempt to attach to file
  82. if ((file = gzdopen(fd, char_mode)) == NULL)
  83. return NULL;
  84. // On success, allocate internal buffer and set flags
  85. this->enable_buffer();
  86. io_mode = mode;
  87. own_fd = false;
  88. return this;
  89. }
  90. // Close gzipped file
  91. gzfilebuf*
  92. gzfilebuf::close()
  93. {
  94. // Fail immediately if no file is open
  95. if (!this->is_open())
  96. return NULL;
  97. // Assume success
  98. gzfilebuf* retval = this;
  99. // Attempt to sync and close gzipped file
  100. if (this->sync() == -1)
  101. retval = NULL;
  102. if (gzclose(file) < 0)
  103. retval = NULL;
  104. // File is now gone anyway (postcondition [27.8.1.3.8])
  105. file = NULL;
  106. own_fd = false;
  107. // Destroy internal buffer if it exists
  108. this->disable_buffer();
  109. return retval;
  110. }
  111. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  112. // Convert int open mode to mode string
  113. bool
  114. gzfilebuf::open_mode(std::ios_base::openmode mode,
  115. char* c_mode) const
  116. {
  117. bool testb = mode & std::ios_base::binary;
  118. bool testi = mode & std::ios_base::in;
  119. bool testo = mode & std::ios_base::out;
  120. bool testt = mode & std::ios_base::trunc;
  121. bool testa = mode & std::ios_base::app;
  122. // Check for valid flag combinations - see [27.8.1.3.2] (Table 92)
  123. // Original zfstream hardcoded the compression level to maximum here...
  124. // Double the time for less than 1% size improvement seems
  125. // excessive though - keeping it at the default level
  126. // To change back, just append "9" to the next three mode strings
  127. if (!testi && testo && !testt && !testa)
  128. strcpy(c_mode, "w");
  129. if (!testi && testo && !testt && testa)
  130. strcpy(c_mode, "a");
  131. if (!testi && testo && testt && !testa)
  132. strcpy(c_mode, "w");
  133. if (testi && !testo && !testt && !testa)
  134. strcpy(c_mode, "r");
  135. // No read/write mode yet
  136. // if (testi && testo && !testt && !testa)
  137. // strcpy(c_mode, "r+");
  138. // if (testi && testo && testt && !testa)
  139. // strcpy(c_mode, "w+");
  140. // Mode string should be empty for invalid combination of flags
  141. if (strlen(c_mode) == 0)
  142. return false;
  143. if (testb)
  144. strcat(c_mode, "b");
  145. return true;
  146. }
  147. // Determine number of characters in internal get buffer
  148. std::streamsize
  149. gzfilebuf::showmanyc()
  150. {
  151. // Calls to underflow will fail if file not opened for reading
  152. if (!this->is_open() || !(io_mode & std::ios_base::in))
  153. return -1;
  154. // Make sure get area is in use
  155. if (this->gptr() && (this->gptr() < this->egptr()))
  156. return std::streamsize(this->egptr() - this->gptr());
  157. else
  158. return 0;
  159. }
  160. // Fill get area from gzipped file
  161. gzfilebuf::int_type
  162. gzfilebuf::underflow()
  163. {
  164. // If something is left in the get area by chance, return it
  165. // (this shouldn't normally happen, as underflow is only supposed
  166. // to be called when gptr >= egptr, but it serves as error check)
  167. if (this->gptr() && (this->gptr() < this->egptr()))
  168. return traits_type::to_int_type(*(this->gptr()));
  169. // If the file hasn't been opened for reading, produce error
  170. if (!this->is_open() || !(io_mode & std::ios_base::in))
  171. return traits_type::eof();
  172. // Attempt to fill internal buffer from gzipped file
  173. // (buffer must be guaranteed to exist...)
  174. int bytes_read = gzread(file, buffer, buffer_size);
  175. // Indicates error or EOF
  176. if (bytes_read <= 0)
  177. {
  178. // Reset get area
  179. this->setg(buffer, buffer, buffer);
  180. return traits_type::eof();
  181. }
  182. // Make all bytes read from file available as get area
  183. this->setg(buffer, buffer, buffer + bytes_read);
  184. // Return next character in get area
  185. return traits_type::to_int_type(*(this->gptr()));
  186. }
  187. // Write put area to gzipped file
  188. gzfilebuf::int_type
  189. gzfilebuf::overflow(int_type c)
  190. {
  191. // Determine whether put area is in use
  192. if (this->pbase())
  193. {
  194. // Double-check pointer range
  195. if (this->pptr() > this->epptr() || this->pptr() < this->pbase())
  196. return traits_type::eof();
  197. // Add extra character to buffer if not EOF
  198. if (!traits_type::eq_int_type(c, traits_type::eof()))
  199. {
  200. *(this->pptr()) = traits_type::to_char_type(c);
  201. this->pbump(1);
  202. }
  203. // Number of characters to write to file
  204. int bytes_to_write = this->pptr() - this->pbase();
  205. // Overflow doesn't fail if nothing is to be written
  206. if (bytes_to_write > 0)
  207. {
  208. // If the file hasn't been opened for writing, produce error
  209. if (!this->is_open() || !(io_mode & std::ios_base::out))
  210. return traits_type::eof();
  211. // If gzipped file won't accept all bytes written to it, fail
  212. if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write)
  213. return traits_type::eof();
  214. // Reset next pointer to point to pbase on success
  215. this->pbump(-bytes_to_write);
  216. }
  217. }
  218. // Write extra character to file if not EOF
  219. else if (!traits_type::eq_int_type(c, traits_type::eof()))
  220. {
  221. // If the file hasn't been opened for writing, produce error
  222. if (!this->is_open() || !(io_mode & std::ios_base::out))
  223. return traits_type::eof();
  224. // Impromptu char buffer (allows "unbuffered" output)
  225. char_type last_char = traits_type::to_char_type(c);
  226. // If gzipped file won't accept this character, fail
  227. if (gzwrite(file, &last_char, 1) != 1)
  228. return traits_type::eof();
  229. }
  230. // If you got here, you have succeeded (even if c was EOF)
  231. // The return value should therefore be non-EOF
  232. if (traits_type::eq_int_type(c, traits_type::eof()))
  233. return traits_type::not_eof(c);
  234. else
  235. return c;
  236. }
  237. // Assign new buffer
  238. std::streambuf*
  239. gzfilebuf::setbuf(char_type* p,
  240. std::streamsize n)
  241. {
  242. // First make sure stuff is sync'ed, for safety
  243. if (this->sync() == -1)
  244. return NULL;
  245. // If buffering is turned off on purpose via setbuf(0,0), still allocate one...
  246. // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at
  247. // least a buffer of size 1 (very inefficient though, therefore make it bigger?)
  248. // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems)
  249. if (!p || !n)
  250. {
  251. // Replace existing buffer (if any) with small internal buffer
  252. this->disable_buffer();
  253. buffer = NULL;
  254. buffer_size = 0;
  255. own_buffer = true;
  256. this->enable_buffer();
  257. }
  258. else
  259. {
  260. // Replace existing buffer (if any) with external buffer
  261. this->disable_buffer();
  262. buffer = p;
  263. buffer_size = n;
  264. own_buffer = false;
  265. this->enable_buffer();
  266. }
  267. return this;
  268. }
  269. // Write put area to gzipped file (i.e. ensures that put area is empty)
  270. int
  271. gzfilebuf::sync()
  272. {
  273. return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0;
  274. }
  275. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  276. // Allocate internal buffer
  277. void
  278. gzfilebuf::enable_buffer()
  279. {
  280. // If internal buffer required, allocate one
  281. if (own_buffer && !buffer)
  282. {
  283. // Check for buffered vs. "unbuffered"
  284. if (buffer_size > 0)
  285. {
  286. // Allocate internal buffer
  287. buffer = new char_type[buffer_size];
  288. // Get area starts empty and will be expanded by underflow as need arises
  289. this->setg(buffer, buffer, buffer);
  290. // Setup entire internal buffer as put area.
  291. // The one-past-end pointer actually points to the last element of the buffer,
  292. // so that overflow(c) can safely add the extra character c to the sequence.
  293. // These pointers remain in place for the duration of the buffer
  294. this->setp(buffer, buffer + buffer_size - 1);
  295. }
  296. else
  297. {
  298. // Even in "unbuffered" case, (small?) get buffer is still required
  299. buffer_size = SMALLBUFSIZE;
  300. buffer = new char_type[buffer_size];
  301. this->setg(buffer, buffer, buffer);
  302. // "Unbuffered" means no put buffer
  303. this->setp(0, 0);
  304. }
  305. }
  306. else
  307. {
  308. // If buffer already allocated, reset buffer pointers just to make sure no
  309. // stale chars are lying around
  310. this->setg(buffer, buffer, buffer);
  311. this->setp(buffer, buffer + buffer_size - 1);
  312. }
  313. }
  314. // Destroy internal buffer
  315. void
  316. gzfilebuf::disable_buffer()
  317. {
  318. // If internal buffer exists, deallocate it
  319. if (own_buffer && buffer)
  320. {
  321. // Preserve unbuffered status by zeroing size
  322. if (!this->pbase())
  323. buffer_size = 0;
  324. delete[] buffer;
  325. buffer = NULL;
  326. this->setg(0, 0, 0);
  327. this->setp(0, 0);
  328. }
  329. else
  330. {
  331. // Reset buffer pointers to initial state if external buffer exists
  332. this->setg(buffer, buffer, buffer);
  333. if (buffer)
  334. this->setp(buffer, buffer + buffer_size - 1);
  335. else
  336. this->setp(0, 0);
  337. }
  338. }
  339. /*****************************************************************************/
  340. // Default constructor initializes stream buffer
  341. gzifstream::gzifstream()
  342. : std::istream(NULL), sb()
  343. { this->init(&sb); }
  344. // Initialize stream buffer and open file
  345. gzifstream::gzifstream(const char* name,
  346. std::ios_base::openmode mode)
  347. : std::istream(NULL), sb()
  348. {
  349. this->init(&sb);
  350. this->open(name, mode);
  351. }
  352. // Initialize stream buffer and attach to file
  353. gzifstream::gzifstream(int fd,
  354. std::ios_base::openmode mode)
  355. : std::istream(NULL), sb()
  356. {
  357. this->init(&sb);
  358. this->attach(fd, mode);
  359. }
  360. // Open file and go into fail() state if unsuccessful
  361. void
  362. gzifstream::open(const char* name,
  363. std::ios_base::openmode mode)
  364. {
  365. if (!sb.open(name, mode | std::ios_base::in))
  366. this->setstate(std::ios_base::failbit);
  367. else
  368. this->clear();
  369. }
  370. // Attach to file and go into fail() state if unsuccessful
  371. void
  372. gzifstream::attach(int fd,
  373. std::ios_base::openmode mode)
  374. {
  375. if (!sb.attach(fd, mode | std::ios_base::in))
  376. this->setstate(std::ios_base::failbit);
  377. else
  378. this->clear();
  379. }
  380. // Close file
  381. void
  382. gzifstream::close()
  383. {
  384. if (!sb.close())
  385. this->setstate(std::ios_base::failbit);
  386. }
  387. /*****************************************************************************/
  388. // Default constructor initializes stream buffer
  389. gzofstream::gzofstream()
  390. : std::ostream(NULL), sb()
  391. { this->init(&sb); }
  392. // Initialize stream buffer and open file
  393. gzofstream::gzofstream(const char* name,
  394. std::ios_base::openmode mode)
  395. : std::ostream(NULL), sb()
  396. {
  397. this->init(&sb);
  398. this->open(name, mode);
  399. }
  400. // Initialize stream buffer and attach to file
  401. gzofstream::gzofstream(int fd,
  402. std::ios_base::openmode mode)
  403. : std::ostream(NULL), sb()
  404. {
  405. this->init(&sb);
  406. this->attach(fd, mode);
  407. }
  408. // Open file and go into fail() state if unsuccessful
  409. void
  410. gzofstream::open(const char* name,
  411. std::ios_base::openmode mode)
  412. {
  413. if (!sb.open(name, mode | std::ios_base::out))
  414. this->setstate(std::ios_base::failbit);
  415. else
  416. this->clear();
  417. }
  418. // Attach to file and go into fail() state if unsuccessful
  419. void
  420. gzofstream::attach(int fd,
  421. std::ios_base::openmode mode)
  422. {
  423. if (!sb.attach(fd, mode | std::ios_base::out))
  424. this->setstate(std::ios_base::failbit);
  425. else
  426. this->clear();
  427. }
  428. // Close file
  429. void
  430. gzofstream::close()
  431. {
  432. if (!sb.close())
  433. this->setstate(std::ios_base::failbit);
  434. }