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 
00025 class UncompressedFileType : public FileType
00026 {
00027 public:
00028     UncompressedFileType()
00029     {
00030         filePtr = NULL;
00031     }
00032 
00033     virtual ~UncompressedFileType()
00034     {
00035         if (filePtr != NULL)
00036         {
00037             close();
00038         }
00039     }
00040 
00041     UncompressedFileType(const char * filename, const char * mode);
00042 
00043     bool operator == (void * rhs)
00044     {
00045         // No two file pointers are the same, so if rhs is not NULL, then
00046         // the two pointers are different (false).
00047         if (rhs != NULL)
00048             return false;
00049         return (filePtr == rhs);
00050     }
00051 
00052     bool operator != (void * rhs)
00053     {
00054         // No two file pointers are the same, so if rhs is not NULL, then
00055         // the two pointers are different (true).
00056         if (rhs != NULL)
00057             return true;
00058         return (filePtr != rhs);
00059     }
00060 
00061     // Close the file.
00062     inline int close()
00063     {
00064         if((filePtr != stdout) && (filePtr != stdin))
00065         {
00066             int result = fclose(filePtr);
00067             filePtr = NULL;
00068             return result;
00069         }
00070         filePtr = NULL;
00071         return 0;
00072     }
00073 
00074 
00075     // Reset to the beginning of the file.
00076     inline void rewind()
00077     {
00078         // Just call rewind to move to the beginning of the file.
00079         ::rewind(filePtr);
00080     }
00081 
00082     // Check to see if we have reached the EOF.
00083     inline int eof()
00084     {
00085         //  check the file for eof.
00086         return feof(filePtr);
00087     }
00088 
00089     // Check to see if the file is open.
00090     virtual inline bool isOpen()
00091     {
00092         if (filePtr != NULL)
00093         {
00094             // filePtr is not null, so the file is open.
00095             return(true);
00096         }
00097         return(false);
00098     }
00099 
00100     // Write to the file
00101     inline unsigned int write(const void * buffer, unsigned int size)
00102     {
00103         return fwrite(buffer, 1, size, filePtr);
00104     }
00105 
00106     // Read into a buffer from the file.  Since the buffer is passed in and
00107     // this would bypass the fileBuffer used by this class, this method must
00108     // be protected.
00109     inline int read(void * buffer, unsigned int size)
00110     {
00111         return fread(buffer, 1, size, filePtr);
00112     }
00113 
00114 
00115     // Get current position in the file.
00116     // -1 return value indicates an error.
00117     virtual inline int64_t tell()
00118     {
00119         return ftell(filePtr);
00120     }
00121 
00122 
00123     // Seek to the specified offset from the origin.
00124     // origin can be any of the following:
00125     // Note: not all are valid for all filetypes.
00126     //   SEEK_SET - Beginning of file
00127     //   SEEK_CUR - Current position of the file pointer
00128     //   SEEK_END - End of file
00129     // Returns true on successful seek and false on a failed seek.
00130     virtual inline bool seek(int64_t offset, int origin)
00131     {
00132         int64_t returnVal = fseek(filePtr, offset, origin);
00133         // Check for success - 0 return value.
00134         if (returnVal == 0)
00135         {
00136             return true;
00137         }
00138         // Successful.
00139         return false;
00140     }
00141 
00142 
00143 protected:
00144     // A FILE Pointer is used.
00145     FILE* filePtr;
00146 };
00147 
00148 #endif
00149 
00150 
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3