zfio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (c) 2009-2013 Genome Research Ltd.
  3. Author: James Bonfield <jkb@sanger.ac.uk>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
  12. Institute nor the names of its contributors may be used to endorse or promote
  13. products derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "io_lib_config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include "cram/os.h"
  31. #include "cram/zfio.h"
  32. /* ------------------------------------------------------------------------ */
  33. /* Some wrappers around FILE * vs gzFile *, allowing for either */
  34. /*
  35. * gzopen() works on both compressed and uncompressed data, but it has
  36. * a significant performance hit even for uncompressed data (tested as
  37. * 25s using FILE* to 46s via gzOpen and 66s via gzOpen when gzipped).
  38. *
  39. * Hence we use our own wrapper 'zfp' which is a FILE* when uncompressed
  40. * and gzFile* when compressed. This also means we could hide bzopen in
  41. * there too if desired.
  42. */
  43. off_t zftello(zfp *zf) {
  44. return zf->fp ? ftello(zf->fp) : -1;
  45. }
  46. int zfseeko(zfp *zf, off_t offset, int whence) {
  47. return zf->fp ? fseeko(zf->fp, offset, whence) : -1;
  48. }
  49. /*
  50. * A wrapper for either fgets or gzgets depending on what has been
  51. * opened.
  52. */
  53. char *zfgets(char *line, int size, zfp *zf) {
  54. if (zf->fp)
  55. return fgets(line, size, zf->fp);
  56. else
  57. return gzgets(zf->gz, line, size);
  58. }
  59. /*
  60. * A wrapper for either fputs or gzputs depending on what has been
  61. * opened.
  62. */
  63. int zfputs(char *line, zfp *zf) {
  64. if (zf->fp)
  65. return fputs(line, zf->fp);
  66. else
  67. return gzputs(zf->gz, line) ? 0 : EOF;
  68. }
  69. /*
  70. * Peeks at and returns the next character without consuming it from the
  71. * input. (Ie a combination of getc and ungetc).
  72. */
  73. int zfpeek(zfp *zf) {
  74. int c;
  75. if (zf->fp) {
  76. c = getc(zf->fp);
  77. if (c != EOF)
  78. ungetc(c, zf->fp);
  79. } else {
  80. c = gzgetc(zf->gz);
  81. if (c != EOF)
  82. gzungetc(c, zf->gz);
  83. }
  84. return c;
  85. }
  86. /* A replacement for either feof of gzeof */
  87. int zfeof(zfp *zf) {
  88. return zf->fp ? feof(zf->fp) : gzeof(zf->gz);
  89. }
  90. /* A replacement for either fopen or gzopen */
  91. zfp *zfopen(const char *path, const char *mode) {
  92. char path2[1024];
  93. zfp *zf;
  94. if (!(zf = (zfp *)malloc(sizeof(*zf))))
  95. return NULL;
  96. zf->fp = NULL;
  97. zf->gz = NULL;
  98. /* Try normal fopen */
  99. if (mode[0] != 'z' && mode[1] != 'z' &&
  100. NULL != (zf->fp = fopen(path, mode))) {
  101. unsigned char magic[2];
  102. if (2 != fread(magic, 1, 2, zf->fp)) {
  103. free(zf);
  104. return NULL;
  105. }
  106. if (!(magic[0] == 0x1f &&
  107. magic[1] == 0x8b)) {
  108. fseeko(zf->fp, 0, SEEK_SET);
  109. return zf;
  110. }
  111. fclose(zf->fp);
  112. zf->fp = NULL;
  113. }
  114. #ifdef HAVE_POPEN
  115. /*
  116. * I've no idea why, by gzgets is VERY slow, maybe because it handles
  117. * arbitrary seeks.
  118. * popen to gzip -cd is 3 times faster though.
  119. */
  120. if (*mode == 'w') {
  121. } else {
  122. if (access(path, R_OK) == 0) {
  123. sprintf(path2, "gzip -cd < %.*s", 1000, path);
  124. if (NULL != (zf->fp = popen(path2, "r")))
  125. return zf;
  126. }
  127. sprintf(path2, "gzip -cd < %.*s.gz", 1000, path);
  128. if (NULL != (zf->fp = popen(path2, "r")))
  129. return zf;
  130. printf("Failed on %s\n", path);
  131. } else {
  132. sprintf(path2, "gzip > %.*s", 1000, path);
  133. if (NULL != (zf->fp = popen(path2, "w")))
  134. return zf;
  135. }
  136. printf("Failed on %s\n", path);
  137. }
  138. #else
  139. /* Gzopen instead */
  140. if ((zf->gz = gzopen(path, mode)))
  141. return zf;
  142. sprintf(path2, "%.*s.gz", 1020, path);
  143. if ((zf->gz = gzopen(path2, mode)))
  144. return zf;
  145. #endif
  146. perror(path);
  147. free(zf);
  148. return NULL;
  149. }
  150. int zfclose(zfp *zf) {
  151. int r = (zf->fp) ? fclose(zf->fp) : gzclose(zf->gz);
  152. free(zf);
  153. return r;
  154. }