00001
00007 #include <string>
00008 #include <iostream>
00009 #include <fstream>
00010 #include <vector>
00011 #include <sstream>
00012
00013 #include "LifeVariables.h"
00014 #include "RawReader.h"
00015 #include "Error.h"
00016 #include "osdir.h"
00017
00018 using namespace std;
00019
00020 RawReader::RawReader() {
00021 this->openFlag = false;
00022 this->openDirFlag = false;
00023 this->voxelData = 0;
00024 this->size = 0;
00025 fileInfoString = "";
00026 try {
00027 this->openPath(DEFAULT_PATH);
00028 }
00029 catch(Error e) {
00030 cout << e.getErrorMsg();
00031 }
00032 }
00033
00034 void RawReader::openPath(string pathName) throw(Error){
00035 oslink::directory dataDir(pathName);
00036 string tempString;
00037 if(dataDir != NULL) {
00038
00039 if (this->fileList.size()> 0) {
00040 if(this->openFlag) {
00041 close();
00042 }
00043
00044 this->fileList.clear();
00045 }
00046 while(dataDir) {
00047
00048 tempString = dataDir.next();
00049
00050 int pos = string::npos;
00051 if((pos = tempString.rfind(".dat")) != string::npos) {
00052 this->fd = new FileDescription();
00053 this->fd->name = tempString.substr(0, pos);
00054 this->fd->path = pathName+tempString;
00055 this->fileList.push_back(fd);
00056
00057 }
00058 tempString = "";
00059 }
00060
00061 if(this->fileList.size() > 1) {
00062 this->fileList.pop_back();
00063 }
00064 else if(this->fileList.size() == 0) {
00065 this->openFlag = false;
00066 this->openDirFlag = false;
00067 throw Error("The directory '"+pathName+"' does not contain any valid files.");
00068 }
00069 }
00070 else {
00071 this->openFlag = false;
00072 this->openDirFlag = false;
00073 throw Error("The directory '"+pathName+"' does not exist.");
00074 }
00075 this->openFlag = false;
00076 this->openDirFlag = true;
00077 }
00078
00079
00080 void RawReader::open(int index) throw(Error) {
00081 if(index > this->fileList.size()) {
00082 this->openFlag = false;
00083 throw Error("File is not in the directory.");
00084 }
00085 if(openFlag) {
00086 this->close();
00087 }
00088
00089 unsigned short *cPtr = 0;
00090 long ColNum = 0;
00091 long RowNum = 0;
00092
00093
00094 string fileName = this->fileList.at(index)->path;
00095
00096
00097 this->fileStream = new ifstream(fileName.c_str(), ios::in|ios::binary);
00098 if(this->fileStream->is_open()) {
00099
00100
00101
00102 this->fileStream->read((char*)&dimX, 2);
00103 this->fileStream->read((char*)&dimY, 2);
00104 this->fileStream->read((char*)&dimZ, 2);
00105
00106 ostringstream infoStream;
00107 infoStream << dimX << " x " << dimY << " x " << dimZ;
00108 fileInfoString = infoStream.str();
00109
00110
00111 this->size = dimX*dimY*dimZ;
00112
00113 this->voxelData = new unsigned short[size];
00114
00115 if(this->voxelData!=0) {
00116 this->fileStream->read((char*)voxelData, sizeof(unsigned short)*size);
00117
00118
00119
00120
00121
00122 this->fileStream->close();
00123 }
00124 else {
00125 throw Error("File '"+ fileName + "' might be to big for loading.");
00126 }
00127
00128
00129 this->openFlag = true;
00130 }
00131 else {
00132 this->openFlag = false;
00133 this->fileInfoString="-";
00134 throw Error("File '"+ fileName + "' could not be loaded.");
00135 }
00136 }
00137
00138
00139 void RawReader::close() {
00140 if(openFlag) {
00141
00142
00143
00144
00145 this->openFlag = false;
00146 this->fileInfoString="-";
00147 }
00148 }
00149
00150
00151 char* RawReader::getFileNameAt(int index) throw(Error){
00152 if(index > fileList.size()) {
00153 throw Error("The filename is not in the list");
00154 }
00155 else {
00156 return &((FileDescription*)fileList.at(index))->name[0];
00157 }
00158 }
00159