libStatGen Software  1
UncompressedFileType.h
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 #ifndef __UNCOMPRESSEDFILETYPE_H__
00019 #define __UNCOMPRESSEDFILETYPE_H__
00020 
00021 #include <iostream>
00022 #include <stdio.h>
00023 #include "FileType.h"
00024 #include "knetfile.h"
00025 
00026 class UncompressedFileType : public FileType
00027 {
00028 public:
00029     UncompressedFileType()
00030     {
00031         filePtr = NULL;
00032         kfilePtr = NULL;
00033         keof = false;
00034     }
00035 
00036     virtual ~UncompressedFileType()
00037     {
00038         if((filePtr != NULL) || (kfilePtr != NULL))
00039         {
00040             close();
00041         }
00042     }
00043 
00044     UncompressedFileType(const char * filename, const char * mode);
00045 
00046     bool operator == (void * rhs)
00047     {
00048         // No two file pointers are the same, so if rhs is not NULL, then
00049         // the two pointers are different (false).
00050         if (rhs != NULL)
00051             return false;
00052         // rhs is NULL.  They are the same if both filePtr & kfilePtr are NULL.
00053         return((filePtr == rhs) && (kfilePtr == rhs));
00054     }
00055 
00056     bool operator != (void * rhs)
00057     {
00058         // No two file pointers are the same, so if rhs is not NULL, then
00059         // the two pointers are different (true).
00060         if (rhs != NULL)
00061             return true;
00062         // rhs is NULL.  They are the different if either filePtr or kfilePtr
00063         // are not NULL.
00064         return((filePtr != rhs) || (kfilePtr != rhs));
00065     }
00066 
00067     // Close the file.
00068     inline int close()
00069     {
00070         if(filePtr != NULL)
00071         {
00072             if((filePtr != stdout) && (filePtr != stdin))
00073             {
00074                 int result = fclose(filePtr);
00075                 filePtr = NULL;
00076                 return result;
00077             }
00078             filePtr = NULL;
00079         }
00080         else if(kfilePtr != NULL)
00081         {
00082             int result = knet_close(kfilePtr);
00083             kfilePtr = NULL;
00084             return result;
00085         }
00086         return 0;
00087     }
00088 
00089 
00090     // Reset to the beginning of the file.
00091     inline void rewind()
00092     {
00093         // Just call rewind to move to the beginning of the file.
00094         if(filePtr != NULL)
00095         {
00096             ::rewind(filePtr);
00097         }
00098         else if (kfilePtr != NULL)
00099         {
00100             knet_seek(kfilePtr, 0, SEEK_SET);
00101         }
00102     }
00103 
00104     // Check to see if we have reached the EOF.
00105     inline int eof()
00106     {
00107         //  check the file for eof.
00108         if(kfilePtr != NULL)
00109         {
00110             return(keof);
00111         }
00112         else
00113         {
00114             return feof(filePtr);
00115         }
00116     }
00117 
00118     // Check to see if the file is open.
00119     virtual inline bool isOpen()
00120     {
00121         if((filePtr != NULL) || (kfilePtr != NULL))
00122         {
00123             // filePtr is not null, so the file is open.
00124             return(true);
00125         }
00126         return(false);
00127     }
00128 
00129     // Write to the file
00130     inline unsigned int write(const void * buffer, unsigned int size)
00131     {
00132         // knetfile is never used for writing.
00133         return fwrite(buffer, 1, size, filePtr);
00134     }
00135 
00136     // Read into a buffer from the file.  Since the buffer is passed in and
00137     // this would bypass the fileBuffer used by this class, this method must
00138     // be protected.
00139     inline int read(void * buffer, unsigned int size)
00140     {
00141         if(kfilePtr != NULL)
00142         {
00143             int bytesRead = knet_read(kfilePtr, buffer, size);
00144             if((bytesRead == 0) && (size != 0))
00145             {
00146                 keof = true;
00147             }
00148             else if((bytesRead != (int)size) && (bytesRead >= 0))
00149             {
00150                 // Less then the requested size was read and an error
00151                 // was not returned (bgzf_read returns -1 on error).
00152                 keof = true;
00153             }
00154             else
00155             {
00156                 keof = false;
00157             }
00158             return(bytesRead);
00159         }
00160         return fread(buffer, 1, size, filePtr);
00161     }
00162 
00163 
00164     // Get current position in the file.
00165     // -1 return value indicates an error.
00166     virtual inline int64_t tell()
00167     {
00168         if(kfilePtr != NULL)
00169         {
00170             return knet_tell(kfilePtr);
00171         }
00172         return ftell(filePtr);
00173     }
00174 
00175 
00176     // Seek to the specified offset from the origin.
00177     // origin can be any of the following:
00178     // Note: not all are valid for all filetypes.
00179     //   SEEK_SET - Beginning of file
00180     //   SEEK_CUR - Current position of the file pointer
00181     //   SEEK_END - End of file
00182     // Returns true on successful seek and false on a failed seek.
00183     virtual inline bool seek(int64_t offset, int origin)
00184     {
00185         int returnVal = 0;
00186         if(kfilePtr != NULL)
00187         {
00188             returnVal = knet_seek(kfilePtr, offset, origin);
00189             keof = false;
00190         }
00191         else
00192         {
00193             returnVal = fseek(filePtr, offset, origin);
00194         }
00195         // Check for success - 0 return value.
00196         if (returnVal == 0)
00197         {
00198             return true;
00199         }
00200         // Successful.
00201         return false;
00202     }
00203 
00204 
00205 protected:
00206     // A FILE Pointer is used.
00207     FILE* filePtr;
00208     knetFile *kfilePtr;
00209     bool keof;
00210 };
00211 
00212 #endif
00213 
00214 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends