GzipHeader.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 "GzipHeader.h"
00019 #include <iostream>
00020 
00021 #include <cstring>
00022 
00023 // Constructor to initialize member data to 0.
00024 GzipHeader::GzipHeader()
00025 {
00026     // clear the union via memset:
00027     memset(headerBuffer, 0, sizeof(headerBuffer));
00028 }
00029 
00030 
00031 // Desctructor - nothing to do.
00032 GzipHeader::~GzipHeader()
00033 {
00034 }
00035 
00036 
00037 // Method to read the gzip header from a file.
00038 // Returns true if the file is a gzip file, false, otherwise.
00039 bool GzipHeader::readHeader(FILE* filePtr)
00040 {
00041     bool isGzip = false;
00042 
00043     // If the file is not already open, return false.
00044     if (filePtr == NULL)
00045     {
00046         // File is not open, so return false - not a gzip file.
00047         return(false);
00048     }
00049 
00050     // Try to read a header from the file.
00051     //   if(144 == fread(buffer, 1, 144, filePtr))
00052     if (GZIP_HEADER_SIZE == fread(buffer, 1, GZIP_HEADER_SIZE, filePtr))
00053     {
00054         memcpy(headerBuffer, buffer, GZIP_HEADER_SIZE);
00055 
00056         // Successfully read enough bytes, so check to see if it is a GzipFile.
00057         if (isGzipFile())
00058         {
00059             // It is a gzip file.
00060             isGzip = true;
00061         }
00062     }
00063 
00064     return isGzip;
00065 }
00066 
00067 
00068 // Determine if the file is a gzip file.
00069 bool GzipHeader::isGzipFile()
00070 {
00071     if ((id1 == 31) && (id2 == 139))
00072     {
00073         return true;
00074     }
00075     return false;
00076 }
00077 
00078 
00079 // Determine if the file is a BGZF compressed file.
00080 bool GzipHeader::isBgzfFile()
00081 {
00082     if (isGzipFile() && (si1 == 66) && (si2 == 67))
00083     {
00084         return true;
00085     }
00086     return false;
00087 }
00088 
Generated on Wed Nov 17 15:38:28 2010 for StatGen Software by  doxygen 1.6.3