ocssw  1.0
/disk01/web/ocssw/build/src/libaquarius/FileStager.cpp (r8218/r5509)
Go to the documentation of this file.
00001 #include "FileStager.h"
00002 #include <stdint.h>
00003 
00004 namespace Aq {
00005   namespace Fw {
00006     namespace Ws {
00007       namespace Io {
00008 
00009     using namespace std;
00010 
00016     void Error(const char* s) { printf("%s", s); }
00017     void Error(const char* s, int n1) { printf("%s %x\n", s, n1); }
00018         // void Error(const char* s, int n1, int n2) { printf("%s %x\n", s, n1, n2); }
00019 
00020 
00021 
00027     FileStager::FileStager(){
00028       bufptr = 0;
00029       startbyte = 0;
00030     };
00031 
00032     FileStager::~FileStager(){
00033       delete buffer;
00034     };
00035 
00036 
00037     // *** stageFile *** //
00038     NumBytesAvail FileStager::stageFile(const char* const name, 
00039                         NameType ntype) {
00040       fstream filestr;
00041 
00042       // Open file for input in binary mode
00043       if (ntype == NAME_IS_FILENAME) {
00044         filestr.open(name, fstream::in | fstream::binary);
00045         if (!filestr.is_open()) {
00046           Error("Error opening: ");
00047           Error(name);
00048           Error("\n");
00049           return 0;
00050         }
00051       }
00052 
00053       if (ntype == NAME_IS_ENV_VAR) {
00054         char * envVar;
00055         envVar = getenv (name);
00056         filestr.open(envVar, fstream::in | fstream::binary);
00057         if (!filestr.is_open()) {
00058           Error("Error opening: ");
00059           Error(envVar);
00060           Error("\n");
00061           return 0;
00062         }
00063       }
00064 
00065       // Get filesize
00066       filestr.seekg (0, ios::end);
00067       totalbytes = filestr.tellg();
00068       filestr.seekg (0, ios::beg);
00069 
00070       // Allocate & clear buffer (4-byte aligned)
00071       if ((totalbytes % 4) != 0) {
00072         buffer = new char[4*((totalbytes/4) + 1)];
00073         memset(buffer, 0, 4*((totalbytes/4) + 1));
00074       } else {
00075         buffer = new char[totalbytes];
00076         memset(buffer, 0, totalbytes);
00077       }
00078 
00079       // Read entire file into buffer
00080       filestr.read (buffer, totalbytes);
00081 
00082       if (filestr.eof()) printf("bad\n");
00083 
00084       // Close file
00085       filestr.close();
00086 
00087       bufptr = buffer;
00088 
00089       // Set totalbyes to 4-byte-aligned value
00090       if ((totalbytes % 4) != 0) totalbytes = 4*((totalbytes/4) + 1);
00091 
00092       // Return total bytes read
00093       return totalbytes;
00094     }
00095 
00096 
00097     // *** stageFile (with magic word search) *** //
00098     NumBytesAvail FileStager::stageFile(const char* const name, 
00099                         NameType ntype, int32_t magic) {
00100 
00101       // Stage file & return number of bytes in stream buffer
00102       NumBytesAvail n = this->stageFile(name, ntype);
00103 
00104       // Find magic word
00105       startbyte = 0;
00106       bool found = false;
00107       /*
00108       for (int32_t j=0; j<totalbytes/4; j++) {
00109         int32_t i;
00110         memcpy(&i, &buffer[4*j], sizeof(int32_t));
00111       */
00112       // Step every byte rather than 4-bytes JMG 08/14/2011
00113       for (int32_t j=0; j<totalbytes; j++) {
00114         int32_t i;
00115         memcpy(&i, &buffer[j], sizeof(int32_t));
00116 
00117         // If magic word found then break else increase startbyte counter
00118         if (i == magic) {
00119           found = true;
00120           break;
00121         } else {
00122           //          startbyte += sizeof(int32_t);
00123           startbyte += sizeof(uint8_t);
00124         }
00125       }
00126         
00127       // If magic word not found, exit
00128       if (found == false) {
00129           unsigned char *ptr;
00130           ptr = (unsigned char *) &magic;
00131           char strbuf[64];
00132           // Write magic word in hex format
00133           sprintf(strbuf, "Magic word: \"%x%x%x%x\" not found.\n",
00134               (int) ptr[0], (int) ptr[1], (int) ptr[2], (int) ptr[3]);
00135           Error(strbuf);
00136           exit(2);
00137       }
00138 
00139       // Set buffer pointer to startbyte
00140       bufptr = &buffer[startbyte];
00141 
00142       // Return number of bytes left in buffer 
00143       return (n - startbyte);
00144     }
00145 
00146 
00147     // *** getNext *** //
00148     // Seek
00149     char *FileStager::getNext(int bytesToRead) {
00150 
00151       bufptr += bytesToRead;
00152 
00153       if (bufptr >= &buffer[totalbytes]) {
00154         return NULL;
00155       } else {
00156         // Return current streambuffer position
00157         return bufptr;
00158       }
00159     }
00160 
00161 
00162     // *** getNext *** //
00163     // Read
00164     char *FileStager::getNext(int bytesToRead, char *varbuf) {
00165 
00166       // Record current stream buffer position
00167       int pos = bufptr - buffer;
00168 
00169       if ((totalbytes - pos) < bytesToRead) {
00170         return(0);
00171       }
00172 
00173       // Read next bytesToRead bytes into variable
00174       memcpy(varbuf, bufptr, bytesToRead);
00175       bufptr += bytesToRead;
00176 
00177       return bufptr;
00178     }
00179 
00180 
00181     // *** findSync *** //
00182     // Seek to next sync word
00183     char *FileStager::findSync( int32_t magic, int32_t offset) {
00184 
00185       int pos = bufptr - buffer;
00186 
00187       for (int32_t j=pos+1; j<totalbytes; j++) {
00188         int32_t i;
00189         memcpy(&i, &buffer[j], sizeof(int32_t));
00190 
00191         // If magic word found then break else increase startbyte counter
00192         if (i == magic) {
00193           bufptr = buffer + j + offset;
00194           break;
00195         }
00196       }
00197 
00198       if (bufptr >= &buffer[totalbytes]) {
00199         return NULL;
00200       } else {
00201         // Return current streambuffer position
00202         return bufptr;
00203       }
00204     }
00205 
00206 
00207     char *FileStager::rewind() {
00208       bufptr = buffer;
00209       return bufptr;
00210     }
00211 
00212 
00213     char *FileStager::rewind(int32_t magic) {
00214 
00215       // Find magic word
00216       startbyte = 0;
00217       bool found = false;
00218       /*
00219       for (int32_t j=0; j<totalbytes/4; j++) {
00220         int32_t i;
00221         memcpy(&i, &buffer[4*j], sizeof(int32_t));
00222       */
00223       // Step every byte rather than 4-bytes JMG 08/14/2011
00224       for (int32_t j=0; j<totalbytes; j++) {
00225         int32_t i;
00226         memcpy(&i, &buffer[j], sizeof(int32_t));
00227 
00228         // If magic word found then break else increase startbyte counter
00229         if (i == magic) {
00230           found = true;
00231           break;
00232         } else {
00233           //          startbyte += sizeof(int32_t);
00234           startbyte += sizeof(uint8_t);
00235         }
00236       }
00237         
00238       // If magic word not found, exit
00239       if (found == false) {
00240           unsigned char *ptr;
00241           ptr = (unsigned char *) &magic;
00242           char strbuf[64];
00243           // Write magic word in hex format
00244           sprintf(strbuf, "Magic word: \"%x%x%x%x\" not found.\n",
00245               (int) ptr[0], (int) ptr[1], (int) ptr[2], (int) ptr[3]);
00246           Error(strbuf);
00247           exit(2);
00248       }
00249 
00250       // Set buffer pointer to startbyte
00251       bufptr = &buffer[startbyte];
00252 
00253       return bufptr;
00254     }
00255 
00256 
00257     // *** bytesLeft *** //
00258     NumBytesAvail FileStager::bytesLeft() const {
00259 
00260       // Record current stream buffer position
00261       int pos = bufptr - buffer;
00262 
00263       return (totalbytes - pos);
00264     }
00265 
00266 
00267     // *** numRecs *** //
00268     // Determine # of records from record size
00269     int FileStager::numRecs(int recSize) {
00270       recordSize = recSize;
00271 
00272       int n = (totalbytes - startbyte) / recordSize;
00273 
00274       if (n * recordSize != (totalbytes - startbyte))
00275         printf("Warning: Incomplete records in data buffer\n");
00276 
00277       return n;
00278     }
00279 
00280 
00281 
00282     // *** createFile *** //
00283     NumBytesAvail FileStager::createFile(const char* const name, 
00284                          NameType ntype,
00285                          int bytesToWrite, char *buffer) {
00286       fstream filestr;
00287 
00288       // Open file for input in binary mode
00289       if (ntype == NAME_IS_FILENAME) {
00290         filestr.open(name, fstream::out | fstream::binary);
00291         if (!filestr.is_open()) {
00292           Error("Error opening: ");
00293           Error(name);
00294           Error("\n");
00295           return 0;
00296         }
00297       }
00298 
00299       if (ntype == NAME_IS_ENV_VAR) {
00300         char * envVar;
00301         envVar = getenv (name);
00302         filestr.open(envVar, fstream::out | fstream::binary);
00303         if (!filestr.is_open()) {
00304           Error("Error opening: ");
00305           Error(envVar);
00306           Error("\n");
00307           return 0;
00308         }
00309       }
00310 
00311       // Read entire file into buffer
00312       filestr.write (buffer, bytesToWrite);
00313 
00314       // Close file
00315       filestr.close();
00316 
00317       // Return total bytes written
00318       return bytesToWrite;
00319     }
00320 
00321       }
00322     }
00323   }
00324 }
00325 
00326