BgzfFileType.cpp

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <iostream>
00019 #include <string.h>
00020 
00021 #include "BgzfFileType.h"
00022 
00023 // Default to require the EOF block at the end of the file.
00024 bool BgzfFileType::ourRequireEofBlock = true;
00025 
00026 BgzfFileType::BgzfFileType(const char * filename, const char * mode)
00027 {
00028     // If the file is for write and is '-', then write to stdout.
00029     if(((mode[0] == 'w') || (mode[0] == 'W')) && 
00030        (strcmp(filename, "-") == 0))
00031     {
00032         // Write to stdout.
00033         bgzfHandle = bgzf_fdopen(fileno(stdout), mode);
00034     }
00035     else if(((mode[0] == 'r') || (mode[0] == 'R')) && 
00036        (strcmp(filename, "-") == 0))
00037     {
00038         // read from stdin
00039         bgzfHandle = bgzf_fdopen(fileno(stdin), mode);
00040     }
00041     else
00042     {
00043         bgzfHandle = bgzf_open(filename, mode);
00044     }
00045 
00046     myStartPos = 0;
00047     if (bgzfHandle != NULL)
00048     {
00049         // Check to see if the file is being opened for read, if the eof block
00050         // is required, and if it is, if it is there.
00051         if ((mode[0] == 'r' || mode[0] == 'R') && ourRequireEofBlock &&
00052                 (bgzf_check_EOF(bgzfHandle) == 0))
00053         {
00054             std::cerr << "BGZF EOF marker is missing in " << filename << std::endl;
00055             // the block is supposed to be there, but isn't, so close the file.
00056             close();
00057         }
00058         else
00059         {
00060             // Successfully opened a properly formatted file, so get the start
00061             // position.
00062             myStartPos = tell();
00063         }
00064     }
00065 
00066     myEOF = false;
00067 }
00068 
00069 
00070 // Set whether or not to require the EOF block at the end of the
00071 // file.  True - require the block.  False - do not require the block.
00072 void BgzfFileType::setRequireEofBlock(bool requireEofBlock)
00073 {
00074     ourRequireEofBlock = requireEofBlock;
00075 }
00076 
Generated on Wed Nov 17 15:38:28 2010 for StatGen Software by  doxygen 1.6.3