1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2011, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
**************************************************************************/
/* end stub */
/*! \file file/nresources.h
* \brief Provides methods for accessing old-style binary file-like resources.
* \deprecated The preferred way of representing data is using XML
* which is accessed using text I/O streams.
*/
#ifndef __NRESOURCES_H
#ifndef __DOXYGEN
#define __NRESOURCES_H
#endif
#include "regina-core.h"
#include <cstring>
#include <fstream>
namespace regina {
/**
* \weakgroup file
* @{
*/
/**
* Provides a standard random access interface for old-style binary
* file-like resources.
*
* \deprecated The preferred way of representing data is using XML
* which is accessed using text I/O streams.
*
* \ifacespython Not present.
*/
class REGINA_API NRandomAccessResource {
public:
/**
* Specifies the current state of a resource.
*/
enum mode {
CLOSED = 0,
/**< The resource is closed. */
READ = 1,
/**< The resource is open for reading. */
WRITE = 2
/**< The resource is open for writing. */
};
public:
/**
* Destroys this resource.
*/
virtual ~NRandomAccessResource();
/**
* Open the resource in read mode.
* This routine should fail if the resource does not exist.
*
* \pre The resource is currently closed.
*
* @return \c true on success, \c false on failure.
*/
virtual bool openRead() = 0;
/**
* Open the resource in write mode.
* If the resource already exists, any existing contents should
* be deleted.
*
* \pre The resource is currently closed.
*
* @return \c true on success, \c false on failure.
*/
virtual bool openWrite() = 0;
/**
* Close the resource.
* If the resource is already closed, this routine should do
* nothing.
*/
virtual void close() = 0;
/**
* Returns the current state of the resource.
* If the resource is open, the mode in which it was opened will
* be returned (either \c READ or \c WRITE). If the file is
* closed, 0 (alternatively, \c CLOSED) will be returned.
*
* @return the current state of the resource.
*/
virtual mode getOpenMode() const = 0;
/**
* Reads a character from the current position in the resource
* and moves on to the next position.
*
* \pre The resource is currently open in read mode.
*
* @return the character read.
*/
virtual char getChar() = 0;
/**
* Writes the given character to the resource at the current
* position and moves on to the next position.
*
* \pre The resource is currently open in write mode.
*
* @param c the character to write.
*/
virtual void putChar(char c) = 0;
/**
* Returns the current position in the resource.
*
* \pre The resource is currently open.
*
* @return the current positon, as counted in bytes.
*/
virtual long getPosition() = 0;
/**
* Moves to the given position in the resource.
*
* \pre The resource is currently open.
*
* @param pos the position to which to move, as counted in bytes.
*/
virtual void setPosition(long pos) = 0;
};
/**
* A random access resource that is simply a local file.
*
* \deprecated The preferred way of representing data is using XML
* which is accessed using text I/O streams.
*
* \ifacespython Not present.
*/
class REGINA_API NLocalFileResource : public NRandomAccessResource {
private:
std::ifstream infile;
/**< The file that is being read from. */
std::ofstream outfile;
/**< The file that is being written to. */
mode openMode;
/**< The current state of the file. */
char* fileName;
/**< The pathname of the file that is this resource. */
public:
/**
* Creates a new resource referring to the given file.
* The file will not be accessed until one of the <tt>open...</tt>
* routines is called.
*
* \i18n This routine makes no assumptions about the character
* encoding used in the given file \e name, and simply passes it
* through unchanged to low-level C/C++ file I/O routines.
*
* \pre Parameter \a newFileName is not an empty string.
*
* @param newFileName the pathname of the file that is this
* resource.
*/
NLocalFileResource(const char* newFileName);
/**
* Destroys this resource reference, closing the corresponding
* file if necessary.
*/
virtual ~NLocalFileResource();
/**
* Returns the system file mode for opening a binary file for reading.
*
* @return the system file mode for reading.
*/
static std::ios::openmode sysModeRead();
/**
* Returns the system file mode for opening a binary file for writing.
*
* @return the system file mode for writing.
*/
static std::ios::openmode sysModeWrite();
virtual bool openRead();
virtual bool openWrite();
virtual void close();
virtual mode getOpenMode() const;
virtual char getChar();
virtual void putChar(char c);
virtual long getPosition();
virtual void setPosition(long pos);
};
/*@}*/
// Inline functions for NRandomAccessResource
inline NRandomAccessResource::~NRandomAccessResource() {
}
// Inline functions for NLocalFileResource
inline NLocalFileResource::NLocalFileResource(const char* newFileName) :
openMode(CLOSED), fileName(new char[strlen(newFileName) + 1]) {
strcpy(fileName, newFileName);
}
inline NLocalFileResource::~NLocalFileResource() {
close();
delete[] fileName;
}
inline std::ios::openmode NLocalFileResource::sysModeRead() {
return std::ios::in | std::ios::binary;
}
inline std::ios::openmode NLocalFileResource::sysModeWrite() {
return std::ios::out | std::ios::trunc | std::ios::binary;
}
inline NRandomAccessResource::mode NLocalFileResource::getOpenMode() const {
return openMode;
}
inline char NLocalFileResource::getChar() {
return static_cast<char>(infile.get());
}
inline void NLocalFileResource::putChar(char c) {
outfile.put(c);
}
} // namespace regina
#endif
|