Warning, /include/Geant4/tools/fileis is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_fileis
0005 #define tools_fileis
0006
0007 #include "signature"
0008 #include <cstring>
0009
0010 namespace tools {
0011 namespace file {
0012
0013 inline bool is_zip(const std::string& a_file,bool& a_is){
0014 unsigned char head[4];
0015 {unsigned int num = 4;
0016 if(!signature(a_file,head,num)) {a_is = false;return false;}
0017 if(num!=4) {a_is = false;return true;}}
0018 if(head[0]!='P') {a_is = false;return true;}
0019 if(head[1]!='K') {a_is = false;return true;}
0020 if(head[2]!=3) {a_is = false;return true;}
0021 if(head[3]!=4) {a_is = false;return true;}
0022 a_is = true;
0023 return true;
0024 }
0025
0026 inline bool is_jpeg(const std::string& a_file,bool& a_is){
0027 unsigned char head[4];
0028 {unsigned int num = 4;
0029 if(!signature(a_file,head,num)) {a_is = false;return false;}
0030 if(num!=4) {a_is = false;return true;}}
0031 if(head[0]!=255) {a_is = false;return true;}
0032 if(head[1]!=216) {a_is = false;return true;}
0033 if(head[2]!=255) {a_is = false;return true;}
0034 //if(head[3]!=224) {a_is = false;return true;} //LRI.jpg is 225 !
0035 a_is = true;
0036 return true;
0037 }
0038
0039 inline bool is_ico(const std::string& a_file,bool& a_is){
0040 unsigned char head[4];
0041 {unsigned int num = 4;
0042 if(!signature(a_file,head,num)) {a_is = false;return false;}
0043 if(num!=4) {a_is = false;return true;}}
0044 if(head[0]!=0) {a_is = false;return true;}
0045 if(head[1]!=0) {a_is = false;return true;}
0046 if(head[2]!=1) {a_is = false;return true;}
0047 if(head[3]!=0) {a_is = false;return true;}
0048 a_is = true;
0049 return true;
0050 }
0051
0052 inline bool is_png(const std::string& a_file,bool& a_is){
0053 unsigned char head[4];
0054 {unsigned int num = 4;
0055 if(!signature(a_file,head,num)) {a_is = false;return false;}
0056 if(num!=4) {a_is = false;return true;}}
0057 if(head[0]!=137) {a_is = false;return true;}
0058 if(head[1]!='P') {a_is = false;return true;}
0059 if(head[2]!='N') {a_is = false;return true;}
0060 if(head[3]!='G') {a_is = false;return true;}
0061 a_is = true;
0062 return true;
0063 }
0064
0065 inline bool is_root(const std::string& a_file,bool& a_is){
0066 unsigned char head[4];
0067 {unsigned int num = 4;
0068 if(!signature(a_file,head,num)) {a_is = false;return false;}
0069 if(num!=4) {a_is = false;return true;}}
0070 if(head[0]!='r') {a_is = false;return true;}
0071 if(head[1]!='o') {a_is = false;return true;}
0072 if(head[2]!='o') {a_is = false;return true;}
0073 if(head[3]!='t') {a_is = false;return true;}
0074 a_is = true;
0075 return true;
0076 }
0077
0078 inline bool is_iv(const std::string& a_file,bool& a_is){
0079 unsigned char head[9];
0080 {unsigned int num = 9;
0081 if(!signature(a_file,head,num)) {a_is = false;return false;}
0082 if(num!=9) {a_is = false;return true;}}
0083 if(head[0]!='#') {a_is = false;return true;}
0084 if(head[1]!='I') {a_is = false;return true;}
0085 if(head[2]!='n') {a_is = false;return true;}
0086 if(head[3]!='v') {a_is = false;return true;}
0087 if(head[4]!='e') {a_is = false;return true;}
0088 if(head[5]!='n') {a_is = false;return true;}
0089 if(head[6]!='t') {a_is = false;return true;}
0090 if(head[7]!='o') {a_is = false;return true;}
0091 if(head[8]!='r') {a_is = false;return true;}
0092 a_is = true;
0093 return true;
0094 }
0095
0096 inline bool is_wrl(const std::string& a_file,bool& a_is){
0097 unsigned char head[5];
0098 {unsigned int num = 5;
0099 if(!signature(a_file,head,num)) {a_is = false;return false;}
0100 if(num!=5) {a_is = false;return true;}}
0101 if(head[0]!='#') {a_is = false;return true;}
0102 if(head[1]!='V') {a_is = false;return true;}
0103 if(head[2]!='R') {a_is = false;return true;}
0104 if(head[3]!='M') {a_is = false;return true;}
0105 if(head[4]!='L') {a_is = false;return true;}
0106 a_is = true;
0107 return true;
0108 }
0109
0110 inline bool is_fog(const std::string& a_file,bool& a_is){
0111 unsigned char head[256];
0112 {unsigned int num = 256;
0113 if(!signature(a_file,head,num)) {a_is = false;return false;}
0114 if(num!=256) {a_is = false;return true;}}
0115 head[255] = 0; //to have a C string.
0116 a_is = ::strstr((const char*)head,"#nb super-volumes")?true:false;
0117 return true;
0118 }
0119
0120 inline bool is_dot(const std::string& a_file,bool& a_is){
0121 unsigned char head[8];
0122 {unsigned int num = 7;
0123 if(!signature(a_file,head,num)) {a_is = false;return false;}
0124 if(num!=7) {a_is = false;return true;}}
0125 head[7] = 0; //to have a C string.
0126 a_is = ::strcmp((const char*)head,"digraph")?false:true;
0127 return true;
0128 }
0129
0130 inline bool is_dcm(const std::string& a_file,bool& a_is){
0131 unsigned char head[132];
0132 {unsigned int num = 132;
0133 if(!signature(a_file,head,num)) {a_is = false;return false;}
0134 if(num!=132) {a_is = false;return true;}}
0135 if(head[128]!='D') {a_is = false;return true;}
0136 if(head[129]!='I') {a_is = false;return true;}
0137 if(head[130]!='C') {a_is = false;return true;}
0138 if(head[131]!='M') {a_is = false;return true;}
0139 a_is = true;
0140 return true;
0141 }
0142
0143 inline bool is_gdml(const std::string& a_file,bool& a_is){
0144 //NOTE : it assumes that the file is not compressed.
0145 unsigned char head[1024];
0146 {unsigned int num = 1024;
0147 if(!signature(a_file,head,num)) {a_is = false;return false;}
0148 if(num!=1024) {a_is = false;return true;}}
0149 head[1023] = 0; //to have a C string.
0150 a_is = ::strstr((const char*)head,"<gdml")?true:false;
0151 return true;
0152 }
0153
0154 inline bool is_exsg(unsigned int a_sz,const char* a_buffer){
0155 if(a_sz<5) return false;
0156 if(a_buffer[0]!='<') return false;
0157 if(a_buffer[1]!='e') return false;
0158 if(a_buffer[2]!='x') return false;
0159 if(a_buffer[3]!='s') return false;
0160 if(a_buffer[4]!='g') return false;
0161 return true;
0162 }
0163
0164 inline bool is_exsg(const std::string& a_file,bool& a_is){
0165 unsigned char head[5];
0166 {unsigned int num = 5;
0167 if(!signature(a_file,head,num)) {a_is = false;return false;}
0168 if(num!=5) {a_is = false;return true;}}
0169 a_is = is_exsg(5,(const char*)head);
0170 return true;
0171 }
0172
0173 inline bool is_bsg(unsigned int a_sz,const char* a_buffer){
0174 if(a_sz<7) return false;
0175 if(a_buffer[0]!='i') return false;
0176 if(a_buffer[1]!='n') return false;
0177 if(a_buffer[2]!='e') return false;
0178 if(a_buffer[3]!='x') return false;
0179 if(a_buffer[4]!='b') return false;
0180 if(a_buffer[5]!='s') return false;
0181 if(a_buffer[6]!='g') return false;
0182 return true;
0183 }
0184
0185 inline bool is_bsg(const std::string& a_file,bool& a_is){
0186 unsigned char head[7];
0187 {unsigned int num = 7;
0188 if(!signature(a_file,head,num)) {a_is = false;return false;}
0189 if(num!=7) {a_is = false;return true;}}
0190 a_is = is_bsg(7,(const char*)head);
0191 return true;
0192 }
0193
0194 inline bool is_scenarios(const std::string& a_file,bool& a_is){
0195 unsigned char head[10];
0196 {unsigned int num = 10;
0197 if(!signature(a_file,head,num)) {a_is = false;return false;}
0198 if(num!=10) {a_is = false;return true;}}
0199 if(head[0]!='<') {a_is = false;return true;}
0200 if(head[1]!='s') {a_is = false;return true;}
0201 if(head[2]!='c') {a_is = false;return true;}
0202 if(head[3]!='e') {a_is = false;return true;}
0203 if(head[4]!='n') {a_is = false;return true;}
0204 if(head[5]!='a') {a_is = false;return true;}
0205 if(head[6]!='r') {a_is = false;return true;}
0206 if(head[7]!='i') {a_is = false;return true;}
0207 if(head[8]!='o') {a_is = false;return true;}
0208 if(head[9]!='s') {a_is = false;return true;}
0209 a_is = true;
0210 return true;
0211 }
0212
0213 inline bool is_slides(const std::string& a_file,bool& a_is){
0214 unsigned char head[7];
0215 {unsigned int num = 7;
0216 if(!signature(a_file,head,num)) {a_is = false;return false;}
0217 if(num!=7) {a_is = false;return true;}}
0218 if(head[0]!='<') {a_is = false;return true;}
0219 if(head[1]!='s') {a_is = false;return true;}
0220 if(head[2]!='l') {a_is = false;return true;}
0221 if(head[3]!='i') {a_is = false;return true;}
0222 if(head[4]!='d') {a_is = false;return true;}
0223 if(head[5]!='e') {a_is = false;return true;}
0224 if(head[6]!='s') {a_is = false;return true;}
0225 a_is = true;
0226 return true;
0227 }
0228
0229 inline bool is_fits(const std::string& a_file,bool& a_is){
0230 unsigned char head[6];
0231 {unsigned int num = 6;
0232 if(!signature(a_file,head,num)) {a_is = false;return false;}
0233 if(num!=6) {a_is = false;return true;}}
0234 if(head[0]!='S') {a_is = false;return true;}
0235 if(head[1]!='I') {a_is = false;return true;}
0236 if(head[2]!='M') {a_is = false;return true;}
0237 if(head[3]!='P') {a_is = false;return true;}
0238 if(head[4]!='L') {a_is = false;return true;}
0239 if(head[5]!='E') {a_is = false;return true;}
0240 a_is = true;
0241 return true;
0242 }
0243
0244 inline bool is_hdf(const std::string& a_file,bool& a_is){
0245 unsigned char head[4];
0246 {unsigned int num = 4;
0247 if(!signature(a_file,head,num)) {a_is = false;return false;}
0248 if(num!=4) {a_is = false;return true;}}
0249 if(head[0]!=137) {a_is = false;return true;}
0250 if(head[1]!='H') {a_is = false;return true;}
0251 if(head[2]!='D') {a_is = false;return true;}
0252 if(head[3]!='F') {a_is = false;return true;}
0253 a_is = true;
0254 return true;
0255 }
0256
0257 inline bool is_ps(const std::string& a_file,bool& a_is){
0258 unsigned char head[4];
0259 {unsigned int num = 4;
0260 if(!signature(a_file,head,num)) {a_is = false;return false;}
0261 if(num!=4) {a_is = false;return true;}}
0262 if(head[0]!='%') {a_is = false;return true;}
0263 if(head[1]!='!') {a_is = false;return true;}
0264 if(head[2]!='P') {a_is = false;return true;}
0265 if(head[3]!='S') {a_is = false;return true;}
0266 a_is = true;
0267 return true;
0268 }
0269
0270 inline bool is_simbad(const std::string& a_file,bool& a_is){
0271 unsigned char head[10];
0272 {unsigned int num = 10;
0273 if(!signature(a_file,head,num)) {a_is = false;return false;}
0274 if(num!=10) {a_is = false;return true;}}
0275 if(head[0]!=':') {a_is = false;return true;}
0276 if(head[1]!=':') {a_is = false;return true;}
0277 if(head[2]!='s') {a_is = false;return true;}
0278 if(head[3]!='c') {a_is = false;return true;}
0279 if(head[4]!='r') {a_is = false;return true;}
0280 if(head[5]!='i') {a_is = false;return true;}
0281 if(head[6]!='p') {a_is = false;return true;}
0282 if(head[7]!='t') {a_is = false;return true;}
0283 if(head[8]!=':') {a_is = false;return true;}
0284 if(head[9]!=':') {a_is = false;return true;}
0285 a_is = true;
0286 return true;
0287 }
0288
0289 }}
0290
0291 #include "fsize"
0292
0293 namespace tools {
0294 namespace file {
0295
0296 inline bool is_aida(const std::string& a_file,bool& a_is){
0297 long sz;
0298 if(!size(a_file,sz)) {a_is = false;return false;}
0299
0300 //NOTE : it assumes that the file is not compressed.
0301 unsigned char head[1024];
0302 {unsigned int num = 1024;
0303 if(!signature(a_file,head,num)) {a_is = false;return false;}
0304 if(num!=1024) {a_is = false;return true;}}
0305 head[1023] = 0; //to have a C string.
0306 a_is = ::strstr((const char*)head,"<aida")?true:false;
0307 return true;
0308 }
0309
0310 inline bool is_jive(const std::string& a_file,bool& a_is){
0311 long sz;
0312 if(!size(a_file,sz)) {a_is = false;return false;}
0313
0314 //NOTE : it assumes that the file is not compressed.
0315 unsigned char head[1024];
0316 {unsigned int num = 1024;
0317 if(!signature(a_file,head,num)) {a_is = false;return false;}
0318 if(num!=1024) {a_is = false;return true;}}
0319 head[1023] = 0; //to have a C string.
0320 if(::strstr((const char*)head,"<?ATLAS")) {a_is = true;return true;}
0321 a_is = ::strstr((const char*)head,"<!DOCTYPE Event")?true:false;
0322 return true;
0323 }
0324
0325 inline bool is_heprep(const std::string& a_file,bool& a_is){
0326 long sz;
0327 if(!size(a_file,sz)) {a_is = false;return false;}
0328
0329 //NOTE : it assumes that the file is not compressed.
0330 unsigned char head[1024];
0331 {unsigned int num = 1024;
0332 if(!signature(a_file,head,num)) {a_is = false;return false;}
0333 if(num!=1024) {a_is = false;return true;}}
0334 head[1023] = 0; //to have a C string.
0335 a_is = ::strstr((const char*)head,"<heprep")?true:false;
0336 return true;
0337 }
0338
0339 }}
0340
0341 #include <climits>
0342
0343 namespace tools {
0344 namespace file {
0345
0346 inline bool is_shp(const std::string& a_file,bool& a_is){
0347 long sz;
0348 if(!size(a_file,sz)) {a_is = false;return false;}
0349 // below logic from shapelib-1.5.0/shpopen.c.
0350 unsigned char head[100];
0351 {unsigned int num = 100;
0352 if(!signature(a_file,head,num)) {a_is = false;return false;}
0353 if(num!=100) {a_is = false;return true;}}
0354 unsigned int _sz = (static_cast<unsigned int>(head[24])<<24)|(head[25]<<16)|(head[26]<<8)|head[27];
0355 if(_sz<(UINT_MAX/2)) _sz *= 2;
0356 else _sz = (UINT_MAX/2)*2;
0357 a_is = sz==long(_sz)?true:false;
0358 return true;
0359 }
0360
0361 }}
0362
0363 #endif