Blame view

server/kernel/src/File/FileFormatManager.cpp 4.15 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
#include "FileFormatManager.h"

#include <cstring>

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

using namespace TREPS::Common;

namespace TREPS
{
	namespace File
	{
		FileFormatManagerClass::FileFormatManagerClass(void) : app(NULL), loader(NULL)
		{
			this->app = ApplicationClass::getInstance();
		}

		FileFormatManagerClass::~FileFormatManagerClass(void)
		{
			if (this->loader != NULL)
			{
				this->loader->close();
				delete this->loader;
				this->loader = NULL;
			}
		}

		bool FileFormatManagerClass::init(const char *format_path)
		{
			//create xml loader
			if (this->loader == NULL)
				this->loader = new XMLManagerClass();

			this->loader->close();

			if (!this->loader->isExist(format_path))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot find formats file : " << format_path);
				return false;
			}

			//load xml file
			if (!this->loader->loadFile(format_path))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Cannot load formats file : " << format_path);
				return false;
			}

			//file validation with xsd
			if (!this->loader->isValid(TREPS_FORMATS_XSD))
			{
				LOG4CXX_ERROR(this->app->getLog()->getPtr(),"Invalid formats file : " << format_path);
				return false;
			}

			return true;
		}

		t_FileFormat FileFormatManagerClass::getFileFormatFromId(const char *format_id)
		{
			if (strcmp(format_id,"ascii") == 0)
				return FF_ASCII;
			else if (strcmp(format_id,"votable") == 0)
				return FF_VOTABLE;
			else if (strcmp(format_id,"cdf") == 0)
				return FF_CDF;
			else if (strcmp(format_id,"netcdf") == 0)
				return FF_NETCDF;

			return FF_NONE;
		}

		string FileFormatManagerClass::getFileFormatIdFromFileFormat(t_FileFormat format)
		{
			switch (format)
			{
				case FF_ASCII :
					return "ascii";
				case FF_VOTABLE :
					return "votable";
				case FF_CDF :
					return "cdf";
				case FF_NETCDF :
					return "netcdf";
				case FF_NONE :
					return "";
			}

			return "";
		}

		t_FileFormat FileFormatManagerClass::detectFileFormat(const char *filePath)
		{
			string type = getFileMIMEType(filePath);

			LOG4CXX_INFO(this->app->getLog()->getPtr(),"File MIME Type : " << type);

			//remove charset
			size_t pos;
			pos = type.find(";");

			if (pos != string::npos)
				type = type.substr(0,pos);

054ff63d   Laurent BEIGBEDER   update format det...
105
106
107
108
109
			// detect voTable with "application/xml" but also "text/xml"
			//VOTable detection (a VOTable is a xml file)
             if (type.compare("application/xml") == 0 || type.compare("text/xml") == 0 )
				return FF_VOTABLE;

346b85c6   Benjamin Renard   First commit with...
110
111
112
113
			//ASCII file detection
			if (type.find("text/") != string::npos)
				return FF_ASCII;

054ff63d   Laurent BEIGBEDER   update format det...
114
115
            //detect voTable with "application/xml" but also "text/xml" 
            //- move detection before ASCII file detection
346b85c6   Benjamin Renard   First commit with...
116
			//VOTable detection (a VOTable is a xml file)
054ff63d   Laurent BEIGBEDER   update format det...
117
118
119
120
121
122
123
124
			//if (type.compare("application/xml") == 0)
			//	return FF_VOTABLE;

            // new kind of MINE found for netcdf
            // File MIME Type : application/x-cdf\012- application/octet-stream; charset=binary
            //ERROR  - File format not supported
			//if ((type.compare("application/octet-stream") == 0))
			if ((type.find("application/octet-stream") != string::npos))
346b85c6   Benjamin Renard   First commit with...
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
165
166
			{
				//netcdf or cdf file detection
				
				//read magic number
				char buf[5];

				FILE *file = fopen(filePath, "rb");
				fread(buf, sizeof(char), 4, file);
				fclose(file);

				buf[4] = '\0';

				//get magic number (read a big-endian integer from a byte array)
				int magicNum1 = ((buf[0] & 0xff ) << 24 | ( buf[1] & 0xff ) << 16 | ( buf[2] & 0xff ) << 8 | ( buf[3] & 0xff ) << 0);

				//test magic number
				if ((magicNum1 & 0xfff00000) == 0xcdf00000)  //CDF >= 2.6
				{
					LOG4CXX_INFO(this->app->getLog()->getPtr(),"CDF detected - Version >= 2.6");
					return FF_CDF;
				}
				else if (magicNum1 == 0x0000ffff)
				{
					LOG4CXX_INFO(this->app->getLog()->getPtr(),"CDF detected - Version < 2.6");
					return FF_CDF;
				}

				//test key word
				buf[3] = '\0';
				if (strcmp(buf,"CDF") == 0) //NetCDF (it's not an error, the key word for netCDF is CDF ...)
				{
					LOG4CXX_INFO(this->app->getLog()->getPtr(),"NetCDF detected");
					return FF_NETCDF;
				}
			}

			LOG4CXX_ERROR(this->app->getLog()->getPtr(),"File format not supported");

			return FF_NONE;
		}
	}
}