1#include <sys/types.h> 2#include <sys/stat.h> 3#include <unistd.h> 4#include <fcntl.h> 5#include <dlfcn.h> 6#include <stdio.h> 7#include <string.h> 8#include <stdlib.h> 9#include <dirent.h> 10 11#define MAX_STRING_LENGTH 255 12 13 14bool IsEnd(int handler) 15{ 16 int nPos; 17 18 nPos = lseek(handler,0,SEEK_CUR); 19 20 if ( nPos < 0 ) 21 return true; // means some error 22 23 struct stat st; 24 if(fstat(handler, &st) < 0) 25 return true; 26 27 if ( st.st_size == nPos ) 28 return true; 29 else 30 return false; 31} 32 33 34char* ReadLine(int handler, char* s, int size ) 35{ 36 37 char c = 0; 38 int nPos = 0; 39 40 while ( read(handler, &c, 1 ) != -1 && !IsEnd(handler) ) 41 { 42 s[nPos] = c; 43 nPos++; 44 45 if ( c == '\n' ) 46 break; 47 } 48 s[nPos] = 0; 49 return s; 50} 51 52 53void ConvertFile(const char * pPath, const char * pFileName, const char * pExtension, bool bTextFile) 54{ 55 56 57 char full_path_in[MAX_STRING_LENGTH]; 58 char full_path_out_c[MAX_STRING_LENGTH]; 59 char full_path_out_h[MAX_STRING_LENGTH]; 60 char signature[MAX_STRING_LENGTH]; 61 char str[MAX_STRING_LENGTH]; 62 char str_size[20]; 63 int size; 64 65 int len = pExtension == NULL ? 0 : strlen(pExtension)+1; 66 67 strcpy(full_path_in,pPath); 68 strcat(full_path_in,"/"); 69 strcat(full_path_in,pFileName); 70 71 memset(signature,0,MAX_STRING_LENGTH); 72 memset(str,0,MAX_STRING_LENGTH); 73 74 strncpy(signature,pFileName,strlen(pFileName)-len); 75 76 int file_handler_in; 77 file_handler_in = open(full_path_in, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); 78 if ( file_handler_in == -1 ) 79 { 80 printf("Unable open file %s\n",full_path_in); 81 return; 82 } 83 84 struct stat st; 85 if(fstat(file_handler_in, &st) < 0) 86 { 87 printf("Unable get size of file %s\n",full_path_in); 88 return; 89 } 90 91 size = st.st_size; 92 93 if ( pExtension ) 94 { 95 strcat(signature,"_"); 96 strcat(signature,pExtension); 97 } 98 99 strcpy(full_path_out_c,signature); 100 strcpy(full_path_out_h,signature); 101 102 strcat(full_path_out_c,".c"); 103 strcat(full_path_out_h,".h"); 104 105 strcpy(str,"#include \""); 106 strcat(str,signature); 107 strcat(str,".h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); 108 strcat(str,"static UINT8 "); 109 strcat(str,signature); 110 strcat(str,"[] = {\n"); 111 112 int file_handler_c; 113 int file_handler_h; 114 115 file_handler_c = open(full_path_out_c, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); 116 if ( file_handler_c == -1 ) 117 { 118 printf("Unable open file %s\n",full_path_out_c); 119 return; 120 } 121 122 123 file_handler_h = open(full_path_out_h, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); 124 if ( file_handler_h == -1 ) 125 { 126 printf("Unable open file %s\n",full_path_out_h); 127 return; 128 } 129 130 write(file_handler_c,str,strlen(str)); 131 132 int current_byte_in_line = 0; 133 int current_byte = 0; 134 char hex_byte[20]; 135 int i; 136 137 if ( pExtension && strcmp(pExtension,"bmdf") == 0 ) 138 { 139 int value = 0; 140 unsigned char value1[4]; 141 142 read(file_handler_in,(unsigned char*)value1,4); 143 if ( value1[0] == '[' && (value1[1] == '/' || value1[2] == '.') ) 144 { 145 printf("BMDF: file %s not recognized as a bmdf file\n",full_path_in); 146 return; 147 } 148 149 for (i=0; i<4; i++) 150 { 151 value |= value1[i] << (i*8); 152 } 153 if ( value != size ) 154 { 155 printf("corrupted mdf file %s ,size %d\n",full_path_in,value); 156 return; 157 } 158 printf("mdf file %s ,size %d\n",full_path_in,value); 159 lseek(file_handler_in,0,SEEK_SET); 160 } 161 162 163 if ( bTextFile == true ) 164 { 165 char line[300]; 166 int new_size = 0; 167 int line_len = 0; 168 while ( !IsEnd(file_handler_in) ) 169 { 170 ReadLine(file_handler_in,line,300); 171 if ( line[0] == '#' ) 172 continue; 173 174 line_len = strlen(line); 175 while ( line_len > 0 && (line[line_len-1]=='\r' || line[line_len-1]=='\n' || 176 line[line_len-1]==' ' || line[line_len-1]=='\t' ) ) 177 line[--line_len]='\0'; 178 179 if ( line_len == 0 ) 180 continue; 181 182 char * new_line = line; 183 184 while ( ( *new_line == ' ' || *new_line == '\t') && *new_line != '\0' ) 185 new_line ++; 186 187 line_len = strlen(new_line); 188 189 if ( line_len == 0 ) 190 continue; 191 192 line[line_len] = '\0'; 193 for (i = 0; i<=line_len; i++) 194 { 195 if ( line[i] == '\t' ) 196 line[i] = ' '; 197 current_byte_in_line++; 198 new_size++; 199 if ( i < line_len ) 200 { 201 if ( current_byte_in_line != 30 ) 202 sprintf(hex_byte,"%#4x,",line[i]); 203 else 204 { 205 sprintf(hex_byte,"%#4x,\n",line[i]); 206 current_byte_in_line = 0; 207 } 208 } 209 else 210 { 211 if ( IsEnd(file_handler_in) ) 212 sprintf(hex_byte,"%#4x\n",line[i]); 213 else 214 { 215 if ( current_byte_in_line != 30 ) 216 sprintf(hex_byte,"%#4x,",line[i]); 217 else 218 { 219 sprintf(hex_byte,"%#4x,\n",line[i]); 220 current_byte_in_line = 0; 221 } 222 } 223 } 224 write(file_handler_c,hex_byte,strlen(hex_byte)); 225 } 226 } 227 size = new_size; 228 } 229 else 230 { 231 232 for (int i=0; i<size; i++) 233 { 234 read(file_handler_in,¤t_byte,1); 235 current_byte_in_line++; 236 if ( i != size - 1 ) 237 { 238 if ( current_byte_in_line != 30 ) 239 sprintf(hex_byte,"%#4x,",current_byte); 240 else 241 { 242 sprintf(hex_byte,"%#4x,\n",current_byte); 243 current_byte_in_line = 0; 244 } 245 } 246 else 247 sprintf(hex_byte,"%#4x\n",current_byte); 248 write(file_handler_c,hex_byte,strlen(hex_byte)); 249 } 250 } 251 252 strcpy(str,"};\n\n"); 253 254 write(file_handler_c,str,strlen(str)); 255 256 sprintf(str_size,"%d",size); 257 258 strcpy(str,"#ifndef "); 259 strcat(str,signature); 260 strcat(str,"_H\n#define "); 261 strcat(str,signature); 262 strcat(str,"_H\n\n#include \"xpl_Types.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); 263 264 write(file_handler_h,str,strlen(str)); 265 266 267 strcpy(str,"const UINT8 * dmGet_"); 268 strcat(str,signature); 269 strcat(str,"(UINT32 * size)"); 270 271 write(file_handler_h,str,strlen(str)); 272 write(file_handler_c,str,strlen(str)); 273 274 strcpy(str,";\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n\n"); 275 276 write(file_handler_h,str,strlen(str)); 277 278 strcpy(str,"\n{\n if ( size )\n {\n *size = "); 279 strcat(str,str_size); 280 strcat(str,";\n return "); 281 strcat(str,signature); 282 strcat(str,";\n }\n else\n {\n return NULL;\n }\n}\n\n#ifdef __cplusplus\n}\n#endif\n\n"); 283 284 write(file_handler_c,str,strlen(str)); 285 286 close(file_handler_c); 287 close(file_handler_h); 288 close(file_handler_in); 289 290} 291 292 293void ConvertFiles(const char * pPath, const char * pExtension, bool bTextFile) 294{ 295 296 DIR *dir = NULL; 297 struct dirent *de = NULL; 298 299 300 dir = opendir(pPath); 301 if ( dir == NULL ) 302 { 303 printf("cannot open dir : %s\n",pPath); 304 return; 305 } 306 307 int nExtensionLen = pExtension == NULL ? 0 : strlen(pExtension); 308 while ( de = readdir(dir) ) 309 { 310 int nNameLen = strlen(de->d_name); 311 if (nNameLen > nExtensionLen && !strncmp((de->d_name + nNameLen - nExtensionLen), pExtension, nExtensionLen)) 312 { 313 ConvertFile(pPath,de->d_name,pExtension,bTextFile); 314 } 315 } 316 closedir(dir); 317 318} 319 320int main(int argc, char** argv) 321{ 322 323 324 char * settings_path = getenv("dm_setting_root"); 325 char * plugin_path = getenv("dm_setting_plugin"); 326 327 if ( settings_path == NULL && plugin_path == NULL ) 328 { 329 printf("env variables are not set :dm_setting_root, dm_setting_plugin\n"); 330 return 0; 331 } 332 333 if ( settings_path ) 334 { 335 printf("dm settings : %s\n",settings_path); 336 337 char * s = (char*)strchr( (const char *)settings_path, ':' ); 338 if ( s ) 339 s = '\0'; 340 341 ConvertFiles(settings_path,"bmdf",false); 342 ConvertFile(settings_path,"fstab",NULL,true); 343 } 344 if ( plugin_path ) 345 { 346 printf("plugin settings : %s\n",plugin_path ); 347 ConvertFiles(plugin_path,"ini",true); 348 } 349 return 0; 350} 351