libStatGen Software  1
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 #ifdef __ZLIB_AVAILABLE__
00019 
00020 #include <iostream>
00021 #include <string.h>
00022 
00023 #include "BgzfFileType.h"
00024 
00025 // Default to require the EOF block at the end of the file.
00026 bool BgzfFileType::ourRequireEofBlock = true;
00027 
00028 BgzfFileType::BgzfFileType(const char * filename, const char * mode)
00029 {
00030     // If the file is for write and is '-', then write to stdout.
00031     if(((mode[0] == 'w') || (mode[0] == 'W')) && 
00032        (strcmp(filename, "-") == 0))
00033     {
00034         // Write to stdout.
00035         bgzfHandle = bgzf_dopen(fileno(stdout), mode);
00036     }
00037     else if(((mode[0] == 'r') || (mode[0] == 'R')) && 
00038        (strcmp(filename, "-") == 0))
00039     {
00040         // read from stdin
00041         bgzfHandle = bgzf_dopen(fileno(stdin), mode);
00042     }
00043     else
00044     {
00045         bgzfHandle = bgzf_open(filename, mode);
00046     }
00047 
00048     myStartPos = 0;
00049     if (bgzfHandle != NULL)
00050     {
00051         // Check to see if the file is being opened for read, if the eof block
00052         // is required, and if it is, if it is there.
00053         if ((mode[0] == 'r' || mode[0] == 'R') && (strcmp(filename, "-") != 0)
00054             && ourRequireEofBlock && (bgzf_check_EOF(bgzfHandle) != 1))
00055         {
00056             std::cerr << "BGZF EOF marker is missing in " << filename << std::endl;
00057             // the block is supposed to be there, but isn't, so close the file.
00058             close();
00059         }
00060         else
00061         {
00062             // Successfully opened a properly formatted file, so get the start
00063             // position.
00064             myStartPos = bgzf_tell(bgzfHandle);
00065         }
00066     }
00067 
00068     myEOF = false;
00069 }
00070 
00071 
00072 // Set whether or not to require the EOF block at the end of the
00073 // file.  True - require the block.  False - do not require the block.
00074 void BgzfFileType::setRequireEofBlock(bool requireEofBlock)
00075 {
00076     ourRequireEofBlock = requireEofBlock;
00077 }
00078 
00079 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends