Blame view

server/kernel/src/File/FileLoaderManager.cpp 3.92 KB
346b85c6   Benjamin Renard   First commit with...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "FileLoaderManager.h"

#include <cstring>

#include "FileFormatManager.h"
#include "FileLoaderASCII.h"
#include "FileLoaderVOTable.h"
#include "FileLoaderNetCDF.h"
#include "FileLoaderCDF.h"

#include "../FrameManager/FrameManager.h"
#include "../Common/Toolbox.h"

using namespace TREPS::Common;
using namespace TREPS::FrameManager;

namespace TREPS
{
	namespace File
	{
		FileLoaderManagerClass::FileLoaderManagerClass(void) : app(NULL), loader(NULL), errorMsg("")
		{
			this->app = ApplicationClass::getInstance();
		}
		
		FileLoaderManagerClass::~FileLoaderManagerClass(void)
		{
			if (this->loader != NULL)
			{
				this->loader->close();
				delete this->loader;
				this->loader = NULL;
			}
		}
		
		bool FileLoaderManagerClass::init(const char *file_path)
		{
			this->errorMsg = "";

			//detect file format
			string formatsFile = this->app->getConf()->getFormatsFilePath();

			if (formatsFile.compare("") == 0)
			{
				this->errorMsg = "Internal error - Cannot get files formats file path";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			//generate a file format manager to detect the file format
			FileFormatManagerClass *formatMgr = new FileFormatManagerClass();

			if (!formatMgr->init(formatsFile.c_str()))
			{
				this->errorMsg = "Cannot init file format manager";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				delete formatMgr;
				return false;
			}

			//init the file loader in relation with the file format
			t_FileFormat format = formatMgr->detectFileFormat(file_path);

			delete formatMgr;

			switch (format)
			{
				case FF_ASCII :
					this->loader = new FileLoaderASCIIClass();
					break;
				case FF_VOTABLE :
					this->loader = new FileLoaderVOTableClass();
					break;
				case FF_NETCDF :
					this->loader = new FileLoaderNetCDFClass();
					break;
				case FF_CDF :
					this->loader = new FileLoaderCDFClass();
					break;
				default :
					this->errorMsg = "Unknown file format";
					LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
					return false;
			}

			//open the file
			bool res = this->loader->open(file_path);

			if (!res)
			{
				this->errorMsg = "Cannot open file";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			return res;
		}

		bool FileLoaderManagerClass::getInfo(t_FileInfo &info)
		{
			this->errorMsg = "";

			if (!this->loader)
			{
				this->errorMsg = "Internal error - Cannot get file info. Loader not initialized.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			if (!this->loader->isOpened())
			{
				this->errorMsg = "Internal error - Cannot get file info. File not open.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			//get file structure information
			bool res = this->loader->getInfo(info);

			if (!res)
			{
				this->errorMsg = "Cannot get file info. Unable to load the file structure.";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
			}

			return res;
		}

		bool FileLoaderManagerClass::getData(int start, int limit, DataRecordListClass *data, int &total)
		{
			this->errorMsg = "";

			data->clear();
			
			if (!this->loader)
			{
				this->errorMsg = "Internal error - Cannot get file data. Loader not initialized!";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}

			if (!this->loader->isOpened())
			{
				this->errorMsg = "Internal error - Cannot get file data. File not open!";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
			
			//get file data records
			if (!this->loader->getData(start,limit,data,total))
			{
				this->errorMsg = "Cannot get file data. Unable to load the file structure";
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
				return false;
			}
			
			return true;
		}

		string FileLoaderManagerClass::getErrorMsg(void)
		{
			return this->errorMsg;
		}
	}
}