| 70 | }; |
| 71 | |
| 72 | FileIndexedIO::StreamFile::StreamFile( const std::string &filename, IndexedIO::OpenMode mode ) : StreamIndexedIO::StreamFile(mode), m_filename( filename ), m_endPosition(0) |
| 73 | { |
| 74 | if (mode & IndexedIO::Write) |
| 75 | { |
| 76 | std::fstream *f = new std::fstream(filename.c_str(), std::ios::trunc | std::ios::binary | std::ios::in | std::ios::out); |
| 77 | |
| 78 | if (! f->is_open() ) |
| 79 | { |
| 80 | throw IOException( "FileIndexedIO: Cannot open '" + filename + "' for writing" ); |
| 81 | } |
| 82 | setInput( f, true, ""); |
| 83 | } |
| 84 | else if (mode & IndexedIO::Append) |
| 85 | { |
| 86 | if (!fs::exists( filename.c_str() ) ) |
| 87 | { |
| 88 | /// Create new file |
| 89 | std::fstream *f = new std::fstream(filename.c_str(), std::ios::trunc | std::ios::binary | std::ios::in | std::ios::out ); |
| 90 | |
| 91 | if (! f->is_open() ) |
| 92 | { |
| 93 | throw IOException( "FileIndexedIO: Cannot open '" + filename + "' for append" ); |
| 94 | } |
| 95 | setInput( f, true, "" ); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | /// Read existing file |
| 100 | std::fstream *f = new std::fstream(filename.c_str(), std::ios::binary | std::ios::in | std::ios::out ); |
| 101 | |
| 102 | if (! f->is_open() ) |
| 103 | { |
| 104 | throw IOException( "FileIndexedIO: Cannot open '" + filename + "' for read " ); |
| 105 | } |
| 106 | |
| 107 | try |
| 108 | { |
| 109 | setInput( f, false, filename); |
| 110 | } |
| 111 | catch ( Exception &e ) |
| 112 | { |
| 113 | e.prepend( "Opening file \"" + filename + "\" : " ); |
| 114 | throw; |
| 115 | } |
| 116 | catch (...) |
| 117 | { |
| 118 | throw IOException( "FileIndexedIO: Caught error reading file '" + filename + "'" ); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | assert( mode & IndexedIO::Read ); |
| 126 | std::fstream *f = new std::fstream(filename.c_str(), std::ios::binary | std::ios::in ); |
| 127 | |
| 128 | if (! f->is_open() ) |
| 129 | { |
nothing calls this directly
no test coverage detected