00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef _LIGO_FRAMEIO_H
00040 #define _LIGO_FRAMEIO_H
00041
00042
00043 #include <time.h>
00044 #include <string>
00045
00046
00047 namespace framefast {
00048
00049
00060
00061
00063 class basic_frame_storage {
00064 public:
00066 basic_frame_storage () {
00067 }
00069 virtual ~basic_frame_storage() {
00070 }
00072 virtual void reset() = 0;
00074 virtual const char* data() const = 0;
00076 virtual int size() const = 0;
00078 virtual bool empty() const {
00079 return data() != 0; }
00080
00082 const char* name() const {
00083 return fName.c_str(); }
00085 void setname (const char* n) {
00086 fName = n ? n : ""; }
00087 protected:
00089 std::string fName;
00090
00091 private:
00092 basic_frame_storage (const basic_frame_storage&);
00093 basic_frame_storage& operator= (const basic_frame_storage&);
00094 };
00095
00096
00098 class memory_frame_storage : public basic_frame_storage {
00099 public:
00101 memory_frame_storage (const char* data, int len, bool own = true)
00102 : fOwn (own), fData (data), fLength (len) {
00103 }
00105 virtual ~memory_frame_storage() {
00106 reset(); }
00108 virtual void reset();
00110 virtual const char* data() const {
00111 return fData; }
00113 virtual int size() const {
00114 return fLength; }
00115
00116 protected:
00118 bool fOwn;
00120 const char* fData;
00122 int fLength;
00123 };
00124
00125
00127 class file_frame_storage : public memory_frame_storage {
00128 public:
00130 explicit file_frame_storage (const char* filename = 0)
00131 : memory_frame_storage (0, 0) {
00132 if (filename) load (filename); }
00134 bool load (const char* filename);
00135 };
00136
00137
00139 class mmap_frame_storage : public basic_frame_storage {
00140 public:
00142 explicit mmap_frame_storage (const char* filename = 0)
00143 : fAddr (0), fLength (0) {
00144 if (filename) map (filename); }
00146 virtual ~mmap_frame_storage() {
00147 reset(); }
00149 virtual void reset();
00151 virtual const char* data() const {
00152 return fAddr; }
00154 virtual int size() const {
00155 return fLength; }
00157 bool map (const char* filename);
00158
00159 protected:
00161 const char* fAddr;
00163 int fLength;
00164 };
00165
00166
00168 class frame_storage_ptr {
00169 public:
00171 frame_storage_ptr () : fFrame (0) {
00172 }
00174 explicit frame_storage_ptr (basic_frame_storage* frame) :
00175 fFrame (frame) {
00176 }
00178 frame_storage_ptr (const frame_storage_ptr& fptr) {
00179 fFrame = fptr.release(); }
00181 frame_storage_ptr& operator= (const frame_storage_ptr& fptr) {
00182 if (this != &fptr) {
00183 reset(); fFrame = fptr.release(); }
00184 return *this; }
00186 ~frame_storage_ptr () {
00187 reset(); }
00189 void set (basic_frame_storage* frame) {
00190 reset(); fFrame = frame; }
00192 void reset () {
00193 if (fFrame) delete fFrame; fFrame = 0; }
00195 basic_frame_storage* release () const {
00196 basic_frame_storage* frame = fFrame; fFrame = 0;
00197 return frame; }
00199 const char* data () const {
00200 return fFrame ? fFrame->data() : 0; }
00202 int size () const {
00203 return fFrame ? fFrame->size() : 0; }
00205 const char* name () const {
00206 return fFrame ? fFrame->name() : 0; }
00207
00208 protected:
00210 mutable basic_frame_storage* fFrame;
00211 };
00212
00213
00214
00216 class basic_frameout {
00217 public:
00219 struct src_dest_t {
00221 const char* fAddr;
00223 int fLen;
00224 };
00225
00227 basic_frameout() : fSofar (0) {
00228 }
00230 virtual ~basic_frameout() {
00231 }
00236 virtual bool open (int len = 0) = 0;
00239 virtual void close() = 0;
00245 virtual bool write (const char* p, int len) = 0;
00251 virtual bool write (const src_dest_t* s, int slen) {
00252 for (int i = 0; i < slen; i++) {
00253 if (!write (s[i].fAddr, s[i].fLen))
00254 return false; }
00255 return true; }
00259 virtual int length() const {
00260 return fSofar; }
00261
00262 protected:
00264 int fSofar;
00265 };
00266
00267
00272 class memory_out : public basic_frameout, public memory_frame_storage {
00273 public:
00279 memory_out (char* data = 0, int maxsize = 0, bool own = false)
00280 : memory_frame_storage (data, maxsize, own) {
00281 }
00284 virtual ~memory_out () {
00285 close (); }
00290 virtual bool open (int len = 0);
00293 virtual void close() {
00294 }
00300 virtual bool write (const char* p, int len);
00306 virtual bool write (const src_dest_t* s, int slen) {
00307 return basic_frameout::write (s, slen); }
00308 };
00309
00310
00315 class desc_out : public basic_frameout {
00316 public:
00320 desc_out (int filedesc) : fd (filedesc) {
00321 }
00326 virtual bool open (int len = 0) {
00327 return true; }
00330 virtual void close() {
00331 }
00337 virtual bool write (const char* p, int len);
00343 virtual bool write (const src_dest_t* s, int slen);
00344
00345 protected:
00347 int fd;
00348 };
00349
00350
00355 class file_out : public desc_out {
00356 public:
00360 file_out (const char* filename)
00361 : desc_out (-1), fFilename (filename) {
00362 }
00365 virtual ~file_out () {
00366 close (); }
00371 virtual bool open (int len = 0);
00374 virtual void close();
00375
00376 protected:
00378 std::string fFilename;
00379 };
00380
00381
00383
00384 }
00385
00386 #endif // _LIGO_FRAMEIO_H
00387
00388