| 1 | #include "sopnamsp.h" | 
|---|
| 2 | #include "rawstream.h" | 
|---|
| 3 | #include "pexceptions.h" | 
|---|
| 4 |  | 
|---|
| 5 | /*! | 
|---|
| 6 | \class SOPHYA::RawInOutStream | 
|---|
| 7 | \ingroup BaseTools | 
|---|
| 8 | This class defines the interface for a raw input/output stream. | 
|---|
| 9 | It has been designed primarily to be used by PPF streams | 
|---|
| 10 | (Sophya Persistence). | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | /* --Methode-- */ | 
|---|
| 14 | RawInOutStream::RawInOutStream() | 
|---|
| 15 | { | 
|---|
| 16 | } | 
|---|
| 17 |  | 
|---|
| 18 | /* --Methode-- */ | 
|---|
| 19 | RawInOutStream::~RawInOutStream() | 
|---|
| 20 | { | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | /* --Methode-- */ | 
|---|
| 24 | bool  RawInOutStream::isSeekable() const | 
|---|
| 25 | { | 
|---|
| 26 | return false; | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | /* --Methode-- */ | 
|---|
| 30 | int_8 RawInOutStream::tellg() | 
|---|
| 31 | { | 
|---|
| 32 | throw NotAvailableOperation("RawInOutStream::tellg() - Not implemented "); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | /* --Methode-- */ | 
|---|
| 36 | RawInOutStream& RawInOutStream::seekg(int_8, int) | 
|---|
| 37 | { | 
|---|
| 38 | throw NotAvailableOperation("RawInOutStream::seekg() - Not implemented "); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | /* --Methode-- */ | 
|---|
| 42 | RawInOutStream& RawInOutStream::read(char* s, uint_8 n) | 
|---|
| 43 | { | 
|---|
| 44 | throw NotAvailableOperation("RawInOutStream::read() - Not implemented "); | 
|---|
| 45 | } | 
|---|
| 46 | /* --Methode-- */ | 
|---|
| 47 | int_8 RawInOutStream::tellp() | 
|---|
| 48 | { | 
|---|
| 49 | throw NotAvailableOperation("RawInOutStream::tellp() - Not implemented "); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | /* --Methode-- */ | 
|---|
| 53 | RawInOutStream& RawInOutStream::seekp(int_8, int) | 
|---|
| 54 | { | 
|---|
| 55 | throw NotAvailableOperation("RawInOutStream::seekp() - Not implemented "); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /* --Methode-- */ | 
|---|
| 59 | RawInOutStream& RawInOutStream::write(const char* s, uint_8 n) | 
|---|
| 60 | { | 
|---|
| 61 | throw NotAvailableOperation("RawInOutStream::write() - Not implemented "); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | /*! | 
|---|
| 66 | \class SOPHYA::RawInFileStream | 
|---|
| 67 | \ingroup BaseTools | 
|---|
| 68 | Implements the input interface (reading) of  RawInOutStream | 
|---|
| 69 | on a disk file using the standard C library (fopen, fread, ...) | 
|---|
| 70 | */ | 
|---|
| 71 |  | 
|---|
| 72 | /* --Methode-- */ | 
|---|
| 73 | RawInFileStream::RawInFileStream(const char * path) | 
|---|
| 74 | : RawInOutStream() | 
|---|
| 75 | { | 
|---|
| 76 | fip = fopen(path,"rb"); | 
|---|
| 77 | if (fip == NULL) { | 
|---|
| 78 | string errmsg = "RawInFileStream() - fopen(rb) ERROR - File= "; | 
|---|
| 79 | errmsg += path; | 
|---|
| 80 | throw IOExc(errmsg); | 
|---|
| 81 | } | 
|---|
| 82 | _filename = path; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | /* --Methode-- */ | 
|---|
| 86 | RawInFileStream::RawInFileStream(string const & path) | 
|---|
| 87 | : RawInOutStream() | 
|---|
| 88 | { | 
|---|
| 89 | fip = fopen(path.c_str(),"rb"); | 
|---|
| 90 | if (fip == NULL) { | 
|---|
| 91 | string errmsg = "RawInFileStream() - fopen(rb) ERROR - File= "; | 
|---|
| 92 | errmsg += path; | 
|---|
| 93 | throw IOExc(errmsg); | 
|---|
| 94 | } | 
|---|
| 95 | _filename = path; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /* --Methode-- */ | 
|---|
| 99 | RawInFileStream::~RawInFileStream() | 
|---|
| 100 | { | 
|---|
| 101 | fclose(fip); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | /* --Methode-- */ | 
|---|
| 106 | bool  RawInFileStream::isSeekable() const | 
|---|
| 107 | { | 
|---|
| 108 | return true; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | /* --Methode-- */ | 
|---|
| 112 | int_8 RawInFileStream::tellg() | 
|---|
| 113 | { | 
|---|
| 114 | return ftell(fip) ; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /* --Methode-- */ | 
|---|
| 118 | RawInOutStream& RawInFileStream::seekg(int_8 off, int dir) | 
|---|
| 119 | { | 
|---|
| 120 | int fsd = SEEK_SET; | 
|---|
| 121 | if (dir == ios::beg)  fsd = SEEK_SET; | 
|---|
| 122 | else if (dir == ios::end)  fsd = SEEK_END; | 
|---|
| 123 | else fsd = SEEK_CUR; | 
|---|
| 124 | fseek(fip, (long int)off, fsd); | 
|---|
| 125 | return (*this); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | /* --Methode-- */ | 
|---|
| 129 | RawInOutStream& RawInFileStream::read(char* s, uint_8 n) | 
|---|
| 130 | { | 
|---|
| 131 | size_t rc = fread((void *)s, 1, (size_t)n, fip); | 
|---|
| 132 | if (rc !=  (size_t)n) { | 
|---|
| 133 | string errmsg = "RawInFileStream::read() ERROR - File= "; | 
|---|
| 134 | errmsg += _filename; | 
|---|
| 135 | throw IOExc(errmsg); | 
|---|
| 136 | } | 
|---|
| 137 | return (*this); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /*! | 
|---|
| 141 | \class SOPHYA::RawOutFileStream | 
|---|
| 142 | \ingroup BaseTools | 
|---|
| 143 | Implements the output interface (writing) of  RawInOutStream | 
|---|
| 144 | on a disk file using the standard C library (fopen, fwrite, ...) | 
|---|
| 145 | */ | 
|---|
| 146 |  | 
|---|
| 147 | /* --Methode-- */ | 
|---|
| 148 | RawOutFileStream::RawOutFileStream(const char * path) | 
|---|
| 149 | : RawInOutStream() | 
|---|
| 150 | { | 
|---|
| 151 | fip = fopen(path,"wb"); | 
|---|
| 152 | if (fip == NULL) { | 
|---|
| 153 | string errmsg = "RawOutFileStream() - fopen(wb) ERROR - File= "; | 
|---|
| 154 | errmsg += path; | 
|---|
| 155 | throw IOExc(errmsg); | 
|---|
| 156 | } | 
|---|
| 157 | _filename = path; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | /* --Methode-- */ | 
|---|
| 161 | RawOutFileStream::RawOutFileStream(string const & path) | 
|---|
| 162 | : RawInOutStream() | 
|---|
| 163 | { | 
|---|
| 164 | fip = fopen(path.c_str(),"wb"); | 
|---|
| 165 | if (fip == NULL) { | 
|---|
| 166 | string errmsg = "RawOutFileStream() - fopen(wb) ERROR - File= "; | 
|---|
| 167 | errmsg += path; | 
|---|
| 168 | throw IOExc(errmsg); | 
|---|
| 169 | } | 
|---|
| 170 | _filename = path; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /* --Methode-- */ | 
|---|
| 174 | RawOutFileStream::~RawOutFileStream() | 
|---|
| 175 | { | 
|---|
| 176 | fclose(fip); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 | /* --Methode-- */ | 
|---|
| 181 | bool  RawOutFileStream::isSeekable() const | 
|---|
| 182 | { | 
|---|
| 183 | return true; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | /* --Methode-- */ | 
|---|
| 187 | int_8 RawOutFileStream::tellp() | 
|---|
| 188 | { | 
|---|
| 189 | return ftell(fip) ; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /* --Methode-- */ | 
|---|
| 193 | RawInOutStream& RawOutFileStream::seekp(int_8 off, int dir) | 
|---|
| 194 | { | 
|---|
| 195 | int fsd = SEEK_SET; | 
|---|
| 196 | if (dir == ios::beg)  fsd = SEEK_SET; | 
|---|
| 197 | else if (dir == ios::end)  fsd = SEEK_END; | 
|---|
| 198 | else fsd = SEEK_CUR; | 
|---|
| 199 | fseek(fip, (long int)off, fsd); | 
|---|
| 200 | return (*this); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | /* --Methode-- */ | 
|---|
| 204 | RawInOutStream& RawOutFileStream::write(const char* s, uint_8 n) | 
|---|
| 205 | { | 
|---|
| 206 | size_t rc = fwrite((void *)s, 1, (size_t)n, fip); | 
|---|
| 207 | if (rc !=  (size_t)n) { | 
|---|
| 208 | string errmsg = "RawOutFileStream::write() ERROR - File= "; | 
|---|
| 209 | errmsg += _filename; | 
|---|
| 210 | throw IOExc(errmsg); | 
|---|
| 211 | } | 
|---|
| 212 | return (*this); | 
|---|
| 213 | } | 
|---|