FilePublisher.cpp

00001 #include "ScanResultWriter.h"
00002 #include "dataTypes.h"
00003 #include "Sct/Exception.h"
00004 #include "ipc/core.h"
00005 
00006 #include <CommonWithDsp/processor.h>
00007 #include <Sct/AbcdModule.h>
00008 #include <Sct/AbcdScans.h>
00009 
00010 #include <cstdio>
00011 #include <cstring>
00012 
00013 using namespace std;
00014 using namespace SctData;
00015 
00016 //Prototypes
00017 void handleFile(const char* name, int i);
00018 
00019 //We assume we are given a list of files containing binary slice data
00020 //Then we fake a header.
00021 int main(int argc, char** argv) {
00022     Sct::setExceptionHandlers(argv[0]);
00023     IPCCore::init(argc, argv);
00024 
00025     for (int i=1; i<argc; ++i) {    
00026     handleFile(argv[i], i); 
00027     }
00028     return 0;
00029 }
00030     
00031 //Handle the file
00032 //Alogorithm is discover file size, allocate RAM
00033 //Use the structs to fake the data then publish!
00034 void handleFile(const char* name, int i) {
00035     try {
00036     FILE * pFile;
00037     long lSize;
00038     
00039     pFile = fopen (name, "rb" );
00040     if (pFile==NULL) throw FileException(name, "File Not Found", __FILE__, __LINE__);
00041 
00042     // obtain file size.
00043     fseek (pFile , 0 , SEEK_END);
00044     lSize = ftell (pFile);
00045     rewind (pFile);
00046 
00047     //New way of reading
00048     scan_result_ptrs scanResult;
00049     
00050     //read in header
00051     int n = fread(&scanResult.header, sizeof(ScanHeader), 1, pFile);
00052     
00053     //scanResult.header.scanNumber = i;
00054     
00055     //Read in scan points
00056     scanResult.points = new FLOAT32[scanResult.header.npoints];
00057     scanResult.nEvents = new UINT32[scanResult.header.npoints];
00058     scanResult.nErrorEvents = new UINT32[scanResult.header.npoints];
00059     
00060     fseek(pFile, scanResult.header.pntPoints, SEEK_SET);
00061     n = fread(scanResult.points, sizeof(FLOAT32), scanResult.header.npoints, pFile);
00062     if (n != scanResult.header.npoints) { 
00063         ostringstream oss;
00064         oss << "Failed to read in ScanPoints.  Expected: " << scanResult.header.npoints << " Read: " << n;
00065         throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00066     }
00067     
00068     fseek(pFile, scanResult.header.pntEvents, SEEK_SET);
00069     n = fread(scanResult.nEvents, sizeof(UINT32), scanResult.header.npoints, pFile);
00070     if (n != scanResult.header.npoints) { 
00071         ostringstream oss;
00072         oss << "Failed to read in ScanPoints, nEvents.  Expected: " << scanResult.header.npoints << " Read: " << n;
00073         throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00074     }
00075     
00076     fseek(pFile, scanResult.header.pntErrors, SEEK_SET);
00077     n = fread(scanResult.nErrorEvents, sizeof(UINT32), scanResult.header.npoints, pFile);
00078     if (n != scanResult.header.npoints) { 
00079         ostringstream oss;
00080         oss << "Failed to read in ScanPoints, nErrorEvents.  Expected: " << scanResult.header.npoints << " Read: " << n;
00081         throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00082     }
00083     
00084     
00085     //Read in data
00086     scanResult.data = new short[scanResult.header.size];
00087     //cout << scanResult.header.size << endl;
00088     fseek(pFile, scanResult.header.pntData, SEEK_SET);
00089     n = fread(scanResult.data, sizeof(UINT16), scanResult.header.size, pFile);
00090     if (n != scanResult.header.size) { 
00091         ostringstream oss;
00092         oss << "Failed to read in data.  Expected: " << scanResult.header.size << " Read: " << n;
00093         throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00094     }
00095     
00096     //All done
00097     fclose (pFile);
00098     
00099     ScanResultWriter::publish(scanResult);
00100 
00101     //We leak here if something bad happens, I know, but this isn't meant to be a big or long program!
00102     delete [] scanResult.points;
00103     delete [] scanResult.nEvents;
00104     delete [] scanResult.nErrorEvents;
00105     delete [] (short*)scanResult.data;  
00106     } catch (Sct::Throwable& t) {
00107     t.sendToMrs(MRS_ERROR);
00108     }
00109 }

Generated on Mon Feb 6 14:01:19 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6