CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
BinaryFile.h
1 #ifndef __BINARY_FILE_H__
2 #define __BINARY_FILE_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2014, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "boost/shared_ptr.hpp"
32 
33 #include "AString.h"
34 
35 #include <stdint.h>
36 
37 namespace cifti {
38 
39  //class to hide difference between compressed and standard binary file reading, and to automate error checking (throws if problem)
40  class BinaryFile
41  {
42  public:
43  enum OpenMode
44  {
45  NONE = 0,
46  READ = 1,
47  WRITE = 2,
48  READ_WRITE = 3,//for convenience
49  TRUNCATE = 4,
50  WRITE_TRUNCATE = 6,//ditto
51  READ_WRITE_TRUNCATE = 7//ditto
52  };
53  BinaryFile() { }
55  BinaryFile(const AString& filename, const OpenMode& fileMode = READ);
56  void open(const AString& filename, const OpenMode& opmode = READ);
57  void close();
58  AString getFilename() const;//not a reference because when no file is open, m_impl is NULL
59  bool getOpenForRead();
60  bool getOpenForWrite();
61  void seek(const int64_t& position);
62  int64_t pos();
63  void read(void* dataOut, const int64_t& count, int64_t* numRead = NULL);//throw if numRead is NULL and (error or end of file reached early)
64  void write(const void* dataIn, const int64_t& count);//failure to complete write is always an exception
66  {
67  protected:
68  AString m_fileName;//filename is tracked here so error messages can be implementation-specific
69  public:
70  virtual void open(const AString& filename, const OpenMode& opmode) = 0;
71  virtual void close() = 0;
72  const AString& getFilename() const { return m_fileName; }
73  virtual void seek(const int64_t& position) = 0;
74  virtual int64_t pos() = 0;
75  virtual void read(void* dataOut, const int64_t& count, int64_t* numRead) = 0;
76  virtual void write(const void* dataIn, const int64_t& count) = 0;
77  virtual ~ImplInterface();
78  };
79  private:
80  boost::shared_ptr<ImplInterface> m_impl;
81  OpenMode m_curMode;//so implementation classes don't have to track it
82  };
83 } //namespace cifti
84 
85 #endif //__BINARY_FILE_H__
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:41
Definition: BinaryFile.h:40
Definition: BinaryFile.h:65