imgtools/romtools/rofsbuild/fsnode.cpp
changeset 647 53d1ab72f5bc
parent 617 3a747a240983
child 675 02e65118a746
equal deleted inserted replaced
641:8dd670a9f34f 647:53d1ab72f5bc
    23 #include <stdio.h> 
    23 #include <stdio.h> 
    24 #include <stdlib.h> 
    24 #include <stdlib.h> 
    25  
    25  
    26 #include <ctype.h> 
    26 #include <ctype.h> 
    27  
    27  
    28 
       
    29 #ifdef __LINUX__
       
    30 #include <dirent.h> 
       
    31 #include <sys/stat.h>
       
    32 #include <unistd.h>
       
    33 #define SPLIT_CHAR '/'
       
    34 #else
       
    35 #include <io.h> 
       
    36 #include <direct.h> //TODO: check under MinGW4 + stlport 5.2
       
    37 #include <conio.h> 
       
    38 #define SPLIT_CHAR '\\'
       
    39 #endif
       
    40  
       
    41 using namespace std;
    28 using namespace std;
    42 
    29 
    43 const TUint KBytesPerEntry = 13 ;
    30 const TUint KBytesPerEntry = 13 ;
    44 
    31 
    45 static inline bool is_a_long_file_name_char(unsigned char ch){
    32 static inline bool is_a_long_file_name_char(unsigned char ch){
   101 		free(iFileName) ;
    88 		free(iFileName) ;
   102 	if(iWideName)
    89 	if(iWideName)
   103 		delete iWideName;
    90 		delete iWideName;
   104 	if(iPCSideName)
    91 	if(iPCSideName)
   105 		free(iPCSideName);
    92 		free(iPCSideName);
   106 }
       
   107  
       
   108 TFSNode* TFSNode::CreateFromFolder(const char* aPath,TFSNode* aParent) { 
       
   109 	 
       
   110 	int len = strlen(aPath);  
       
   111 #ifdef __LINUX__
       
   112 	DIR* dir = opendir(aPath);
       
   113 	if(dir == NULL) {
       
   114 		cout << aPath << " does not contain any subfolder/file.\n";     
       
   115 			return aParent;
       
   116 	}
       
   117 	if(!aParent)
       
   118 		aParent = new TFSNode(NULL,"/",ATTR_DIRECTORY);
       
   119 	dirent*  entry; 
       
   120 	struct stat statbuf ;
       
   121 	char* fileName = new(nothrow) char[len + 200];
       
   122 	if(!fileName) return NULL ;
       
   123 	memcpy(fileName,aPath,len); 
       
   124 	fileName[len] = SPLIT_CHAR;
       
   125 	while ((entry = readdir(dir)) != NULL)  {
       
   126 		if(strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0)
       
   127 			continue ; 
       
   128 		strcpy(&fileName[len+1],entry->d_name);             
       
   129 		stat(fileName , &statbuf);         
       
   130 		TFSNode* pNewItem = new TFSNode(aParent,fileName,S_ISDIR(statbuf.st_mode) ? ATTR_DIRECTORY : 0);
       
   131 		pNewItem->Init(statbuf.st_ctime,statbuf.st_atime,statbuf.st_mtime,statbuf.st_size);         
       
   132 		if(S_ISDIR(statbuf.st_mode)){ 
       
   133 			CreateFromFolder(fileName,pNewItem);
       
   134 		} 
       
   135 	}	
       
   136 	delete []fileName ;
       
   137 	closedir(dir);
       
   138 #else
       
   139 	struct _finddata_t data ;
       
   140 	memset(&data, 0, sizeof(data)); 	
       
   141 	char* fileName = new(nothrow) char[len + 200];
       
   142 	if(!fileName) return NULL ;
       
   143 	memcpy(fileName,aPath,len); 
       
   144     fileName[len] = SPLIT_CHAR;
       
   145 	fileName[len+1] = '*';
       
   146 	fileName[len+2] = 0;
       
   147 	intptr_t hFind =  _findfirst(fileName,&data); 
       
   148  
       
   149 	if(hFind == (intptr_t)-1 ) {
       
   150 		cout << aPath << " does not contain any subfolder/file.\n";		
       
   151 		delete []fileName;
       
   152 		return aParent;
       
   153 	}	
       
   154 	if(!aParent)
       
   155 	    aParent = new TFSNode(NULL,"/",ATTR_DIRECTORY);	
       
   156 	
       
   157 	do {        
       
   158         if(strcmp(data.name,".") == 0 || strcmp(data.name,"..") == 0)
       
   159             continue ; 
       
   160         
       
   161         strcpy(&fileName[len+1],data.name); 
       
   162         TUint8 attr = 0;
       
   163         if(data.attrib & _A_SUBDIR)  
       
   164             attr |= ATTR_DIRECTORY;
       
   165         if(data.attrib & _A_RDONLY)
       
   166             attr |= ATTR_READ_ONLY ;
       
   167         if(data.attrib &  _A_HIDDEN)
       
   168             attr |= ATTR_HIDDEN ;
       
   169         if(data.attrib & _A_SYSTEM)
       
   170             attr |= ATTR_SYSTEM ;
       
   171         if(data.attrib & _A_ARCH)
       
   172             attr |= ATTR_ARCHIVE;      
       
   173         TFSNode* pNewItem = new TFSNode(aParent,data.name,attr,fileName);        
       
   174         pNewItem->Init(data.time_create,data.time_access,data.time_write,data.size);            
       
   175         if(data.attrib & _A_SUBDIR){ 
       
   176             CreateFromFolder(fileName,pNewItem);
       
   177         }  
       
   178  
       
   179     } while(-1 != _findnext(hFind, &data));
       
   180 	delete []fileName ;
       
   181     _findclose(hFind);
       
   182 #endif
       
   183  
       
   184 	return aParent;
       
   185 }
    93 }
   186  
    94  
   187 /** GenerateBasicName : Generate the short name according to long name 
    95 /** GenerateBasicName : Generate the short name according to long name 
   188 	* 
    96 	* 
   189 	* algorithm :
    97 	* algorithm :