libStatGen Software  1
FileType.h
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __FILETYPE_H__
19 #define __FILETYPE_H__
20 
21 #include <stdint.h>
22 
23 class FileType
24 {
25 public:
26  FileType();
27  virtual ~FileType();
28 
29  virtual bool operator == (void * rhs) = 0;
30 
31  virtual bool operator != (void * rhs) = 0;
32 
33  // Close the file.
34  virtual int close() = 0;
35 
36  // Reset to the beginning of the file.
37  virtual void rewind() = 0;
38 
39  // Check to see if we have reached the EOF.
40  virtual int eof() = 0;
41 
42  // Check to see if the file is open.
43  virtual bool isOpen() = 0;
44 
45  // Write to the file.
46  virtual unsigned int write(const void * buffer, unsigned int size) = 0;
47 
48  // Read into a buffer from the file.
49  virtual int read(void * buffer, unsigned int size) = 0;
50 
51  // Get current position in the file.
52  // -1 return value indicates an error.
53  virtual int64_t tell() = 0;
54 
55  // Seek to the specified offset from the origin.
56  // origin can be any of the following:
57  // Note: not all are valid for all filetypes.
58  // SEEK_SET - Beginning of file
59  // SEEK_CUR - Current position of the file pointer
60  // SEEK_END - End of file
61  // Returns true on successful seek and false on a failed seek.
62  virtual bool seek(int64_t offset, int origin) = 0;
63 
64  // Set by the InputFile to inform this class if buffering
65  // is used. Maybe used by child clases (bgzf) to disable
66  // tell. NOTE: this class does no buffering, the
67  // buffering is handled by the calling class.
68  void setBuffered(bool buffered);
69 
70  //
71  // When caller catches an exception, it may call this method.
72  // It is implemented only in BgzfFileTypeRecovery.
73  //
74  virtual bool attemptRecoverySync(bool (*checkSignature)(void *data) , int length);
75 
76 protected:
77  // Set by the InputFile to inform this class if buffering
78  // is used. Maybe used by child clases (bgzf) to disable
79  // tell.
80  bool myUsingBuffer;
81 };
82 
83 #endif
84