00001 #include "vuFileHelper.h"
00002 #include <string.h>
00003
00004 vuString vuFileHelper::getFileType(const char* filename)
00005 {
00006 vuString type = getvuFileType(filename);
00007 if (type.getLength() == 0) {
00008 type = getdatFileType(filename);
00009 }
00010 return type;
00011 }
00012
00013 vuString vuFileHelper::getdatFileType(const char* filename)
00014 {
00015 FILE* file = fopen(filename, "rb");
00016 if (file == NULL) return "";
00017 int i = getTwoByte(file);
00018 int size = i;
00019 i = getTwoByte(file);
00020 size *= i;
00021 i = getTwoByte(file);
00022 size *= i;
00023 size = size*2 + 6;
00024 fseek(file, 0, SEEK_END);
00025 int fsize = ftell(file);
00026 if (fsize >= size) {
00027 cerr << "Found a CG-TUWien file ...\n";
00028 return "1112211";
00029 } else {
00030 cerr << "Error while checking a CG-TUWien file ...\n";
00031 return "";
00032 }
00033 }
00034
00035 int vuFileHelper::getTwoByte(FILE* file)
00036 {
00037 int result = 0;
00038 result = result + fgetc(file);
00039 result = result + (fgetc(file) << 8);
00040 return result;
00041 }
00042
00043 vuString vuFileHelper::getvuFileType(const char* filename)
00044 {
00045 FILE *file = fopen(filename,"rb");
00046 if (file == NULL) return "";
00047
00048
00049 vuString FileFormat;
00050 char type = getType(file);
00051 if (type != '\0')
00052 {
00053 FileFormat += type;
00054 type = getGeometry(file);
00055 }
00056 if (type != '\0')
00057 {
00058 FileFormat += type;
00059 type = getModality(file);
00060 }
00061 if (type != '\0')
00062 {
00063 FileFormat += type;
00064 type = getDimension(file);
00065 }
00066 if (type != '\0')
00067 {
00068 FileFormat += type;
00069 type = getImplementation(file);
00070 }
00071
00072 fclose(file);
00073
00074
00075 if (type != '\0') FileFormat += type;
00076
00077 return FileFormat;
00078 }
00079
00080 char vuFileHelper::getType(FILE *file)
00081 {
00082 int len = 0;
00083 char header[257];
00084
00085 if (( fscanf(file,"# vu DataFile Version 1.0%n",&len) == 0) &&
00086 (len == 25) && (fgetc(file) == '\n'))
00087 ;
00088 else
00089 return '\0';
00090 len = 0;
00091
00092
00093 if ( fgets(header,257,file) == NULL ) return '\0';
00094
00095
00096 if (( fscanf(file,"ASCII %n",&len) == 0 ) && (len >= 6) )
00097 ;
00098 else if (( fscanf(file,"BINARY %n",&len) == 0 ) && (len >= 7) )
00099 ;
00100 else
00101 return '\0';
00102
00103 return '1';
00104 }
00105
00106 char vuFileHelper::getGeometry(FILE *file)
00107 {
00108 int ret = 0;
00109 char geometryName[64] = "";
00110
00111
00112 ret = fscanf(file,"DATASET %s\n", geometryName);
00113 if (ret != 1) return '\0';
00114
00115 if (strcmp(geometryName,"STRUCTURED_POINTS") == 0) return '1';
00116 else if (strcmp(geometryName,"BCC_POINTS") == 0) return '5';
00117 else if (strcmp(geometryName,"LIGHTFIELD") == 0) return '6';
00118 else if (strcmp(geometryName,"FOURIER") == 0) return '7';
00119
00120 return '\0';
00121 }
00122
00123 char vuFileHelper::getModality(FILE *file)
00124 {
00125 int len = 0;
00126
00127
00128 if (( fscanf(file,"UNIMODAL %n",&len) == 0) && (len >= 9) )
00129 return '1';
00130 else if (( fscanf(file,"MULTIMODAL %n",&len) == 0) && (len >= 11) )
00131 return '2';
00132 else
00133 return '\0';
00134 }
00135
00136 char vuFileHelper::getDimension(FILE *file)
00137 {
00138 int ret = 0;
00139 char type;
00140
00141
00142 dword d[4] = {0,0,0,0};
00143 ret = fscanf(file,"DIMENSIONS %lu %lu %lu %lu ",&d[0],&d[1],&d[2],&d[3]);
00144
00145 if (ret != 4)
00146 return _getKindOfLightfield(file);
00147
00148
00149 if (d[0]>1 && d[1]>1 && d[2]==1 && d[3]==1)
00150 type = '1';
00151 else if (d[0]>1 && d[1]>1 && d[2]>1 && d[3]==1)
00152 type = '2';
00153 else if (d[0]>1 && d[1]>1 && d[2]>1 && d[3]>1)
00154 type = '3';
00155 else
00156 return '\0';
00157
00158
00159 int o[4]={0,0,0,0};
00160 ret = fscanf(file,"ORIGIN %i %i %i %i ",&o[0],&o[1],&o[2],&o[3]);
00161 if (ret != 4) return '\0';
00162
00163
00164 dword s[4]={0,0,0,0};
00165 ret = fscanf(file,"SPACING %lu %lu %lu %lu ",&s[0],&s[1],&s[2],&s[3]);
00166 if (ret != 4) return '\0';
00167
00168 return type;
00169 }
00170
00171 char vuFileHelper::getImplementation(FILE *file)
00172 {
00173 int len = 0, ret = 0;
00174 char header[257]="";
00175 char type;
00176
00177
00178 dword size=0;
00179 ret = fscanf(file,"POINT_DATA %lu ",&size);
00180 if (ret != 1) return _getFieldsDataType(file);
00181
00182
00183 ret = fscanf(file,"SCALARS %s ",header);
00184 if (ret != 1) return '\0';
00185
00186
00187 if (( fscanf(file,"byte %n",&len) == 0) && (len >= 5) )
00188 type = '1';
00189 else if (( fscanf(file,"word %n",&len) == 0) && (len >= 5) )
00190 type = '2';
00191 else if (( fscanf(file,"dword %n",&len) == 0) && (len >= 6) )
00192 type = '3';
00193 else
00194 return '\0';
00195 len = 0;
00196
00197
00198 if (( fscanf(file,"LOOKUP_TABLE default%n",&len) == 0)
00199 && (len == 20) && (fgetc(file) == '\n'))
00200 ;
00201 else
00202 return '\0';
00203
00204 return type;
00205 }
00206
00207
00208
00209
00210
00211 char vuFileHelper::_getFieldsDataType(FILE *file)
00212 {
00213 int ret = 0;
00214
00215
00216 dword size=0;
00217 ret = fscanf(file,"DATA_SIZE %lu",&size);
00218 if (ret != 1) return '\0';
00219
00220
00221 char dataName[64] = "";
00222 char typeName[32] = "";
00223 int typeDim = 0;
00224
00225 ret = fscanf(file,"\nFIELDS %s %d %s ",dataName, &typeDim, typeName);
00226 if (ret != 3) return '\0';
00227
00228 if (strcmp(typeName,"byte") == 0) {
00229 if (typeDim == 1) return '1';
00230 else if (typeDim == 2) return '2';
00231 else if (typeDim == 3) return '3';
00232 else return '\0';
00233 }
00234 else if (strcmp(typeName,"float") == 0) {
00235 if (typeDim == 1) return 'A';
00236 else if (typeDim == 2) return 'B';
00237 else if (typeDim == 3) return 'C';
00238 else return '\0';
00239 }
00240 return '\0';
00241 }
00242
00243 char vuFileHelper::_getKindOfLightfield(FILE *file)
00244 {
00245 int ret = 0;
00246
00247
00248 dword d[3] = {0,0,0};
00249 ret = fscanf(file,"SPHERIC %lu %lu %lu ",&d[0],&d[1],&d[2]);
00250
00251 if (ret == 3) return '1';
00252
00253 return '\0';
00254 }