cfile_tools.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. cfile_tools.h
  3. A small library containing tools for dealing with files
  4. that are compressed with different compressors or not compressed
  5. at all. The idea is to recognize the compression automagically
  6. and transparently. Files can be opened for reading or writing,
  7. but not both. Reading and writing is by different function classes.
  8. Access is sequential only.
  9. Copyright (C) 2004 by Arno Wagner <arno.wagner@acm.org>
  10. Distributed under the Gnu Public License version 2 or the modified
  11. BSD license (see file COPYING)
  12. Support for gzip added by Bernhard Tellenbach <bernhard.tellenbach@gmail.com>
  13. Function prefixes are:
  14. cfr_ = compressed file read
  15. Supported:
  16. Reading:
  17. - type recognition from file name extension
  18. - standard input (filename: '-' )
  19. - no compression
  20. - bzip2
  21. - gzip
  22. */
  23. #ifndef _CFILE_TOOLS_DEFINES
  24. #define _CFILE_TOOLS_DEFINES
  25. #define _GNU_SOURCE
  26. #define _FILE_OFFSET_BITS 64
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #ifndef DONT_HAVE_BZ2
  30. #include <bzlib.h>
  31. #endif
  32. #ifndef DONT_HAVE_GZ
  33. #include <zlib.h>
  34. #endif
  35. // Types
  36. struct _CFRFILE {
  37. int format; // 0 = not open, 1 = uncompressed, 2 = bzip2, 3 = gzip
  38. int eof; // 0 = not eof
  39. int closed; // indicates whether fclose has been called, 0 = not yet
  40. int error1; // errors from the sytem, 0 = no error
  41. int error2; // for error messages from the compressor
  42. FILE * data1; // for filehandle of the system
  43. void * data2; // addtional handle(s) of the compressor
  44. // compressor specific stuff
  45. int bz2_stream_end; // True when a bz2 stream has ended. Needed since
  46. // further reading returns error and not eof.
  47. };
  48. typedef struct _CFRFILE CFRFILE;
  49. // Formats
  50. #ifndef DONT_HAVE_BZ2
  51. #ifndef DONT_HAVE_GZ
  52. #define CFR_NUM_FORMATS 4
  53. #else
  54. #define CFR_NUM_FORMATS 3
  55. #endif
  56. #else
  57. #define CFR_NUM_FORMATS 2
  58. #endif
  59. // Functions
  60. CFRFILE * cfr_open(const char *path);
  61. int cfr_close(CFRFILE *stream);
  62. size_t cfr_read(void *ptr, size_t size, size_t nmemb, CFRFILE *stream);
  63. size_t cfr_read_n(CFRFILE *stream, void *ptr, size_t bytes);
  64. ssize_t cfr_getline(char **lineptr, size_t *n, CFRFILE *stream);
  65. int cfr_eof(CFRFILE *stream);
  66. int cfr_error(CFRFILE *stream);
  67. char * cfr_strerror(CFRFILE *stream);
  68. const char * cfr_compressor_str(CFRFILE *stream);
  69. #ifndef DONT_HAVE_BZ2
  70. const char * _bz2_strerror(int err);
  71. #endif
  72. #endif