g4tools  5.4.0
fileis
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_fileis
5 #define tools_fileis
6 
7 #include "signature"
8 #include <cstring>
9 
10 namespace tools {
11 namespace file {
12 
13 inline bool is_zip(const std::string& a_file,bool& a_is){
14  unsigned char head[4];
15  {unsigned int num = 4;
16  if(!signature(a_file,head,num)) {a_is = false;return false;}
17  if(num!=4) {a_is = false;return true;}}
18  if(head[0]!='P') {a_is = false;return true;}
19  if(head[1]!='K') {a_is = false;return true;}
20  if(head[2]!=3) {a_is = false;return true;}
21  if(head[3]!=4) {a_is = false;return true;}
22  a_is = true;
23  return true;
24 }
25 
26 inline bool is_jpeg(const std::string& a_file,bool& a_is){
27  unsigned char head[4];
28  {unsigned int num = 4;
29  if(!signature(a_file,head,num)) {a_is = false;return false;}
30  if(num!=4) {a_is = false;return true;}}
31  if(head[0]!=255) {a_is = false;return true;}
32  if(head[1]!=216) {a_is = false;return true;}
33  if(head[2]!=255) {a_is = false;return true;}
34  //if(head[3]!=224) {a_is = false;return true;} //LRI.jpg is 225 !
35  a_is = true;
36  return true;
37 }
38 
39 inline bool is_ico(const std::string& a_file,bool& a_is){
40  unsigned char head[4];
41  {unsigned int num = 4;
42  if(!signature(a_file,head,num)) {a_is = false;return false;}
43  if(num!=4) {a_is = false;return true;}}
44  if(head[0]!=0) {a_is = false;return true;}
45  if(head[1]!=0) {a_is = false;return true;}
46  if(head[2]!=1) {a_is = false;return true;}
47  if(head[3]!=0) {a_is = false;return true;}
48  a_is = true;
49  return true;
50 }
51 
52 inline bool is_png(const std::string& a_file,bool& a_is){
53  unsigned char head[4];
54  {unsigned int num = 4;
55  if(!signature(a_file,head,num)) {a_is = false;return false;}
56  if(num!=4) {a_is = false;return true;}}
57  if(head[0]!=137) {a_is = false;return true;}
58  if(head[1]!='P') {a_is = false;return true;}
59  if(head[2]!='N') {a_is = false;return true;}
60  if(head[3]!='G') {a_is = false;return true;}
61  a_is = true;
62  return true;
63 }
64 
65 inline bool is_root(const std::string& a_file,bool& a_is){
66  unsigned char head[4];
67  {unsigned int num = 4;
68  if(!signature(a_file,head,num)) {a_is = false;return false;}
69  if(num!=4) {a_is = false;return true;}}
70  if(head[0]!='r') {a_is = false;return true;}
71  if(head[1]!='o') {a_is = false;return true;}
72  if(head[2]!='o') {a_is = false;return true;}
73  if(head[3]!='t') {a_is = false;return true;}
74  a_is = true;
75  return true;
76 }
77 
78 inline bool is_iv(const std::string& a_file,bool& a_is){
79  unsigned char head[9];
80  {unsigned int num = 9;
81  if(!signature(a_file,head,num)) {a_is = false;return false;}
82  if(num!=9) {a_is = false;return true;}}
83  if(head[0]!='#') {a_is = false;return true;}
84  if(head[1]!='I') {a_is = false;return true;}
85  if(head[2]!='n') {a_is = false;return true;}
86  if(head[3]!='v') {a_is = false;return true;}
87  if(head[4]!='e') {a_is = false;return true;}
88  if(head[5]!='n') {a_is = false;return true;}
89  if(head[6]!='t') {a_is = false;return true;}
90  if(head[7]!='o') {a_is = false;return true;}
91  if(head[8]!='r') {a_is = false;return true;}
92  a_is = true;
93  return true;
94 }
95 
96 inline bool is_wrl(const std::string& a_file,bool& a_is){
97  unsigned char head[5];
98  {unsigned int num = 5;
99  if(!signature(a_file,head,num)) {a_is = false;return false;}
100  if(num!=5) {a_is = false;return true;}}
101  if(head[0]!='#') {a_is = false;return true;}
102  if(head[1]!='V') {a_is = false;return true;}
103  if(head[2]!='R') {a_is = false;return true;}
104  if(head[3]!='M') {a_is = false;return true;}
105  if(head[4]!='L') {a_is = false;return true;}
106  a_is = true;
107  return true;
108 }
109 
110 inline bool is_fog(const std::string& a_file,bool& a_is){
111  unsigned char head[256];
112  {unsigned int num = 256;
113  if(!signature(a_file,head,num)) {a_is = false;return false;}
114  if(num!=256) {a_is = false;return true;}}
115  head[255] = 0; //to have a C string.
116  a_is = ::strstr((const char*)head,"#nb super-volumes")?true:false;
117  return true;
118 }
119 
120 inline bool is_dot(const std::string& a_file,bool& a_is){
121  unsigned char head[8];
122  {unsigned int num = 7;
123  if(!signature(a_file,head,num)) {a_is = false;return false;}
124  if(num!=7) {a_is = false;return true;}}
125  head[7] = 0; //to have a C string.
126  a_is = ::strcmp((const char*)head,"digraph")?false:true;
127  return true;
128 }
129 
130 inline bool is_dcm(const std::string& a_file,bool& a_is){
131  unsigned char head[132];
132  {unsigned int num = 132;
133  if(!signature(a_file,head,num)) {a_is = false;return false;}
134  if(num!=132) {a_is = false;return true;}}
135  if(head[128]!='D') {a_is = false;return true;}
136  if(head[129]!='I') {a_is = false;return true;}
137  if(head[130]!='C') {a_is = false;return true;}
138  if(head[131]!='M') {a_is = false;return true;}
139  a_is = true;
140  return true;
141 }
142 
143 inline bool is_gdml(const std::string& a_file,bool& a_is){
144  //NOTE : it assumes that the file is not compressed.
145  unsigned char head[1024];
146  {unsigned int num = 1024;
147  if(!signature(a_file,head,num)) {a_is = false;return false;}
148  if(num!=1024) {a_is = false;return true;}}
149  head[1023] = 0; //to have a C string.
150  a_is = ::strstr((const char*)head,"<gdml")?true:false;
151  return true;
152 }
153 
154 inline bool is_exsg(unsigned int a_sz,const char* a_buffer){
155  if(a_sz<5) return false;
156  if(a_buffer[0]!='<') return false;
157  if(a_buffer[1]!='e') return false;
158  if(a_buffer[2]!='x') return false;
159  if(a_buffer[3]!='s') return false;
160  if(a_buffer[4]!='g') return false;
161  return true;
162 }
163 
164 inline bool is_exsg(const std::string& a_file,bool& a_is){
165  unsigned char head[5];
166  {unsigned int num = 5;
167  if(!signature(a_file,head,num)) {a_is = false;return false;}
168  if(num!=5) {a_is = false;return true;}}
169  a_is = is_exsg(5,(const char*)head);
170  return true;
171 }
172 
173 inline bool is_bsg(unsigned int a_sz,const char* a_buffer){
174  if(a_sz<7) return false;
175  if(a_buffer[0]!='i') return false;
176  if(a_buffer[1]!='n') return false;
177  if(a_buffer[2]!='e') return false;
178  if(a_buffer[3]!='x') return false;
179  if(a_buffer[4]!='b') return false;
180  if(a_buffer[5]!='s') return false;
181  if(a_buffer[6]!='g') return false;
182  return true;
183 }
184 
185 inline bool is_bsg(const std::string& a_file,bool& a_is){
186  unsigned char head[7];
187  {unsigned int num = 7;
188  if(!signature(a_file,head,num)) {a_is = false;return false;}
189  if(num!=7) {a_is = false;return true;}}
190  a_is = is_bsg(7,(const char*)head);
191  return true;
192 }
193 
194 inline bool is_scenarios(const std::string& a_file,bool& a_is){
195  unsigned char head[10];
196  {unsigned int num = 10;
197  if(!signature(a_file,head,num)) {a_is = false;return false;}
198  if(num!=10) {a_is = false;return true;}}
199  if(head[0]!='<') {a_is = false;return true;}
200  if(head[1]!='s') {a_is = false;return true;}
201  if(head[2]!='c') {a_is = false;return true;}
202  if(head[3]!='e') {a_is = false;return true;}
203  if(head[4]!='n') {a_is = false;return true;}
204  if(head[5]!='a') {a_is = false;return true;}
205  if(head[6]!='r') {a_is = false;return true;}
206  if(head[7]!='i') {a_is = false;return true;}
207  if(head[8]!='o') {a_is = false;return true;}
208  if(head[9]!='s') {a_is = false;return true;}
209  a_is = true;
210  return true;
211 }
212 
213 inline bool is_slides(const std::string& a_file,bool& a_is){
214  unsigned char head[7];
215  {unsigned int num = 7;
216  if(!signature(a_file,head,num)) {a_is = false;return false;}
217  if(num!=7) {a_is = false;return true;}}
218  if(head[0]!='<') {a_is = false;return true;}
219  if(head[1]!='s') {a_is = false;return true;}
220  if(head[2]!='l') {a_is = false;return true;}
221  if(head[3]!='i') {a_is = false;return true;}
222  if(head[4]!='d') {a_is = false;return true;}
223  if(head[5]!='e') {a_is = false;return true;}
224  if(head[6]!='s') {a_is = false;return true;}
225  a_is = true;
226  return true;
227 }
228 
229 inline bool is_fits(const std::string& a_file,bool& a_is){
230  unsigned char head[6];
231  {unsigned int num = 6;
232  if(!signature(a_file,head,num)) {a_is = false;return false;}
233  if(num!=6) {a_is = false;return true;}}
234  if(head[0]!='S') {a_is = false;return true;}
235  if(head[1]!='I') {a_is = false;return true;}
236  if(head[2]!='M') {a_is = false;return true;}
237  if(head[3]!='P') {a_is = false;return true;}
238  if(head[4]!='L') {a_is = false;return true;}
239  if(head[5]!='E') {a_is = false;return true;}
240  a_is = true;
241  return true;
242 }
243 
244 inline bool is_hdf(const std::string& a_file,bool& a_is){
245  unsigned char head[4];
246  {unsigned int num = 4;
247  if(!signature(a_file,head,num)) {a_is = false;return false;}
248  if(num!=4) {a_is = false;return true;}}
249  if(head[0]!=137) {a_is = false;return true;}
250  if(head[1]!='H') {a_is = false;return true;}
251  if(head[2]!='D') {a_is = false;return true;}
252  if(head[3]!='F') {a_is = false;return true;}
253  a_is = true;
254  return true;
255 }
256 
257 inline bool is_ps(const std::string& a_file,bool& a_is){
258  unsigned char head[4];
259  {unsigned int num = 4;
260  if(!signature(a_file,head,num)) {a_is = false;return false;}
261  if(num!=4) {a_is = false;return true;}}
262  if(head[0]!='%') {a_is = false;return true;}
263  if(head[1]!='!') {a_is = false;return true;}
264  if(head[2]!='P') {a_is = false;return true;}
265  if(head[3]!='S') {a_is = false;return true;}
266  a_is = true;
267  return true;
268 }
269 
270 inline bool is_simbad(const std::string& a_file,bool& a_is){
271  unsigned char head[10];
272  {unsigned int num = 10;
273  if(!signature(a_file,head,num)) {a_is = false;return false;}
274  if(num!=10) {a_is = false;return true;}}
275  if(head[0]!=':') {a_is = false;return true;}
276  if(head[1]!=':') {a_is = false;return true;}
277  if(head[2]!='s') {a_is = false;return true;}
278  if(head[3]!='c') {a_is = false;return true;}
279  if(head[4]!='r') {a_is = false;return true;}
280  if(head[5]!='i') {a_is = false;return true;}
281  if(head[6]!='p') {a_is = false;return true;}
282  if(head[7]!='t') {a_is = false;return true;}
283  if(head[8]!=':') {a_is = false;return true;}
284  if(head[9]!=':') {a_is = false;return true;}
285  a_is = true;
286  return true;
287 }
288 
289 }}
290 
291 #include "fsize"
292 
293 namespace tools {
294 namespace file {
295 
296 inline bool is_aida(const std::string& a_file,bool& a_is){
297  long sz;
298  if(!size(a_file,sz)) {a_is = false;return false;}
299 
300  //NOTE : it assumes that the file is not compressed.
301  unsigned char head[1024];
302  {unsigned int num = 1024;
303  if(!signature(a_file,head,num)) {a_is = false;return false;}
304  if(num!=1024) {a_is = false;return true;}}
305  head[1023] = 0; //to have a C string.
306  a_is = ::strstr((const char*)head,"<aida")?true:false;
307  return true;
308 }
309 
310 inline bool is_jive(const std::string& a_file,bool& a_is){
311  long sz;
312  if(!size(a_file,sz)) {a_is = false;return false;}
313 
314  //NOTE : it assumes that the file is not compressed.
315  unsigned char head[1024];
316  {unsigned int num = 1024;
317  if(!signature(a_file,head,num)) {a_is = false;return false;}
318  if(num!=1024) {a_is = false;return true;}}
319  head[1023] = 0; //to have a C string.
320  if(::strstr((const char*)head,"<?ATLAS")) {a_is = true;return true;}
321  a_is = ::strstr((const char*)head,"<!DOCTYPE Event")?true:false;
322  return true;
323 }
324 
325 inline bool is_heprep(const std::string& a_file,bool& a_is){
326  long sz;
327  if(!size(a_file,sz)) {a_is = false;return false;}
328 
329  //NOTE : it assumes that the file is not compressed.
330  unsigned char head[1024];
331  {unsigned int num = 1024;
332  if(!signature(a_file,head,num)) {a_is = false;return false;}
333  if(num!=1024) {a_is = false;return true;}}
334  head[1023] = 0; //to have a C string.
335  a_is = ::strstr((const char*)head,"<heprep")?true:false;
336  return true;
337 }
338 
339 }}
340 
341 #include <climits>
342 
343 namespace tools {
344 namespace file {
345 
346 inline bool is_shp(const std::string& a_file,bool& a_is){
347  long sz;
348  if(!size(a_file,sz)) {a_is = false;return false;}
349  // below logic from shapelib-1.5.0/shpopen.c.
350  unsigned char head[100];
351  {unsigned int num = 100;
352  if(!signature(a_file,head,num)) {a_is = false;return false;}
353  if(num!=100) {a_is = false;return true;}}
354  unsigned int _sz = (static_cast<unsigned int>(head[24])<<24)|(head[25]<<16)|(head[26]<<8)|head[27];
355  if(_sz<(UINT_MAX/2)) _sz *= 2;
356  else _sz = (UINT_MAX/2)*2;
357  a_is = sz==long(_sz)?true:false;
358  return true;
359 }
360 
361 }}
362 
363 #endif
tools::file::is_png
bool is_png(const std::string &a_file, bool &a_is)
Definition: fileis:52
tools::file::is_exsg
bool is_exsg(unsigned int a_sz, const char *a_buffer)
Definition: fileis:154
fsize
tools::file::is_ico
bool is_ico(const std::string &a_file, bool &a_is)
Definition: fileis:39
tools::file::is_fits
bool is_fits(const std::string &a_file, bool &a_is)
Definition: fileis:229
tools::file::is_dot
bool is_dot(const std::string &a_file, bool &a_is)
Definition: fileis:120
tools::file::is_dcm
bool is_dcm(const std::string &a_file, bool &a_is)
Definition: fileis:130
tools::file::is_zip
bool is_zip(const std::string &a_file, bool &a_is)
Definition: fileis:13
tools::file::is_wrl
bool is_wrl(const std::string &a_file, bool &a_is)
Definition: fileis:96
tools::file::is_root
bool is_root(const std::string &a_file, bool &a_is)
Definition: fileis:65
tools::file::is_jpeg
bool is_jpeg(const std::string &a_file, bool &a_is)
Definition: fileis:26
tools::file::is_simbad
bool is_simbad(const std::string &a_file, bool &a_is)
Definition: fileis:270
tools::file::is_gdml
bool is_gdml(const std::string &a_file, bool &a_is)
Definition: fileis:143
tools::file::size
bool size(const std::string &a_file, long &a_size)
Definition: fsize:13
tools::file::is_slides
bool is_slides(const std::string &a_file, bool &a_is)
Definition: fileis:213
tools::file::is_aida
bool is_aida(const std::string &a_file, bool &a_is)
Definition: fileis:296
tools::file::is_fog
bool is_fog(const std::string &a_file, bool &a_is)
Definition: fileis:110
tools::file::is_jive
bool is_jive(const std::string &a_file, bool &a_is)
Definition: fileis:310
tools::file::signature
bool signature(const std::string &a_file, unsigned char a_head[], unsigned int &a_num)
Definition: signature:13
tools::file::is_heprep
bool is_heprep(const std::string &a_file, bool &a_is)
Definition: fileis:325
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::file::is_shp
bool is_shp(const std::string &a_file, bool &a_is)
Definition: fileis:346
tools::file::is_bsg
bool is_bsg(unsigned int a_sz, const char *a_buffer)
Definition: fileis:173
tools::file::is_scenarios
bool is_scenarios(const std::string &a_file, bool &a_is)
Definition: fileis:194
tools::file::is_ps
bool is_ps(const std::string &a_file, bool &a_is)
Definition: fileis:257
tools::file::is_iv
bool is_iv(const std::string &a_file, bool &a_is)
Definition: fileis:78
tools::file::is_hdf
bool is_hdf(const std::string &a_file, bool &a_is)
Definition: fileis:244
signature