libStatGen Software  1
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 // Method to read the gzip header from a file.
00069 // Returns true if the file is a gzip file, false, otherwise.
00070 bool GzipHeader::readHeader(UncompressedFileType& file)
00071 {
00072     bool isGzip = false;
00073 
00074     // If the file is not already open, return false.
00075     if (!file.isOpen())
00076     {
00077         // File is not open, so return false - not a gzip file.
00078         return(false);
00079     }
00080 
00081     // Try to read a header from the file.
00082     //   if(144 == file.read(buffer, 1, 144, filePtr))
00083     if ((int)GZIP_HEADER_SIZE == file.read(buffer, GZIP_HEADER_SIZE))
00084     {
00085         memcpy(headerBuffer, buffer, GZIP_HEADER_SIZE);
00086 
00087         // Successfully read enough bytes, so check to see if it is a GzipFile.
00088         if (isGzipFile())
00089         {
00090             // It is a gzip file.
00091             isGzip = true;
00092         }
00093     }
00094 
00095     return isGzip;
00096 }
00097 
00098 
00099 // Determine if the file is a gzip file.
00100 bool GzipHeader::isGzipFile()
00101 {
00102     if ((id1 == 31) && (id2 == 139))
00103     {
00104         return true;
00105     }
00106     return false;
00107 }
00108 
00109 
00110 // Determine if the file is a BGZF compressed file.
00111 bool GzipHeader::isBgzfFile()
00112 {
00113     if (isGzipFile() && (si1 == 66) && (si2 == 67))
00114     {
00115         return true;
00116     }
00117     return false;
00118 }
00119 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends