libStatGen Software  1
BgzfFileType.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifdef __ZLIB_AVAILABLE__
19 
20 #include <iostream>
21 #include <string.h>
22 
23 #include "BgzfFileType.h"
24 
25 // Default to require the EOF block at the end of the file.
26 bool BgzfFileType::ourRequireEofBlock = true;
27 
28 BgzfFileType::BgzfFileType(const char * filename, const char * mode)
29 {
30  // If the file is for write and is '-', then write to stdout.
31  if(((mode[0] == 'w') || (mode[0] == 'W')) &&
32  (strcmp(filename, "-") == 0))
33  {
34  // Write to stdout.
35  bgzfHandle = bgzf_dopen(fileno(stdout), mode);
36  }
37  else if(((mode[0] == 'r') || (mode[0] == 'R')) &&
38  (strcmp(filename, "-") == 0))
39  {
40  // read from stdin
41  bgzfHandle = bgzf_dopen(fileno(stdin), mode);
42  }
43  else
44  {
45  bgzfHandle = bgzf_open(filename, mode);
46  }
47 
48  myStartPos = 0;
49  if (bgzfHandle != NULL)
50  {
51  // Check to see if the file is being opened for read, if the eof block
52  // is required, and if it is, if it is there.
53  if ((mode[0] == 'r' || mode[0] == 'R') && (strcmp(filename, "-") != 0)
54  && ourRequireEofBlock && (bgzf_check_EOF(bgzfHandle) != 1))
55  {
56  std::cerr << "BGZF EOF marker is missing in " << filename << std::endl;
57  // the block is supposed to be there, but isn't, so close the file.
58  close();
59  }
60  else
61  {
62  // Successfully opened a properly formatted file, so get the start
63  // position.
64  myStartPos = bgzf_tell(bgzfHandle);
65  }
66  }
67 
68  myEOF = false;
69 }
70 
71 
72 // Set whether or not to require the EOF block at the end of the
73 // file. True - require the block. False - do not require the block.
74 void BgzfFileType::setRequireEofBlock(bool requireEofBlock)
75 {
76  ourRequireEofBlock = requireEofBlock;
77 }
78 
79 #endif