1
0

cfile_tools.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <bzlib.h>
  30. #include <zlib.h>
  31. // Types
  32. struct _CFRFILE {
  33. int format; // 0 = not open, 1 = uncompressed, 2 = bzip2, 3 = gzip
  34. int eof; // 0 = not eof
  35. int closed; // indicates whether fclose has been called, 0 = not yet
  36. int error1; // errors from the sytem, 0 = no error
  37. int error2; // for error messages from the compressor
  38. FILE * data1; // for filehandle of the system
  39. void * data2; // addtional handle(s) of the compressor
  40. // compressor specific stuff
  41. int bz2_stream_end; // True when a bz2 stream has ended. Needed since
  42. // further reading returns error and not eof.
  43. };
  44. typedef struct _CFRFILE CFRFILE;
  45. // Formats
  46. #define CFR_NUM_FORMATS 4
  47. // Functions
  48. CFRFILE * cfr_open(const char *path);
  49. int cfr_close(CFRFILE *stream);
  50. size_t cfr_read(void *ptr, size_t size, size_t nmemb, CFRFILE *stream);
  51. size_t cfr_read_n(CFRFILE *stream, void *ptr, size_t bytes);
  52. ssize_t cfr_getline(char **lineptr, size_t *n, CFRFILE *stream);
  53. int cfr_eof(CFRFILE *stream);
  54. int cfr_error(CFRFILE *stream);
  55. char * cfr_strerror(CFRFILE *stream);
  56. const char * cfr_compressor_str(CFRFILE *stream);
  57. const char * _bz2_strerror(int err);
  58. #endif