xref: /aoo4110/main/l10ntools/source/directory.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_l10ntools.hxx"
27 #include <l10ntools/directory.hxx>
28 #include "tools/string.hxx"
29 #include <iostream>
30 #include <vector>
31 #include <algorithm>
32 
33 namespace transex
34 {
35 
Directory(const rtl::OUString sFullpath)36 Directory::Directory( const rtl::OUString sFullpath ) : bSkipLinks( false )
37 {
38     sFullName = sFullpath;
39 }
40 
Directory(const rtl::OUString sFullPath,const rtl::OUString sEntry)41 Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry ) : bSkipLinks( false )
42 {
43     sFullName       = sFullPath;
44     sDirectoryName  = sEntry;
45 }
46 
47 
Directory(const ByteString sFullPath)48 Directory::Directory( const ByteString sFullPath ) : bSkipLinks( false )
49 {
50     sDirectoryName = rtl::OUString( sFullPath.GetBuffer() , RTL_TEXTENCODING_UTF8 , sFullPath.Len() );
51 }
52 
lessDir(const Directory & rKey1,const Directory & rKey2)53 bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
54 {
55     rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
56     rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
57 
58     return sName1.compareTo( sName2 ) < 0 ;
59 }
60 
61 
dump()62 void Directory::dump()
63 {
64 
65     for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
66     {
67         std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
68     }
69 
70     for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
71     {
72         std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
73     }
74 
75 }
76 
scanSubDir(int nLevels)77 void Directory::scanSubDir( int nLevels )
78 {
79     readDirectory( sFullName );
80     dump();
81     if( nLevels > 0 ) {
82         for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )
83         {
84             ( *iter ).scanSubDir();
85         }
86     }
87 }
88 
setSkipLinks(bool is_skipped)89 void Directory::setSkipLinks( bool is_skipped )
90 {
91     bSkipLinks = is_skipped;
92 }
93 
readDirectory()94 void Directory::readDirectory()
95 {
96     readDirectory( sFullName );
97 }
98 
99 #ifdef WNT
100 #include <tools/prewin.h>
101 #include <windows.h>
102 #include <tools/postwin.h>
103 
readDirectory(const rtl::OUString & sFullpath)104 void Directory::readDirectory ( const rtl::OUString& sFullpath )
105 {
106     sal_Bool            fFinished;
107     HANDLE          hList;
108     TCHAR           szDir[MAX_PATH+1];
109     TCHAR           szSubDir[MAX_PATH+1];
110     WIN32_FIND_DATA FileData;
111 
112     rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
113     const char *dirname = sFullpathext.getStr();
114 
115     // Get the proper directory path
116 	sprintf(szDir, "%s\\*", dirname);
117 
118     // Get the first file
119     hList = FindFirstFile(szDir, &FileData);
120     if (hList == INVALID_HANDLE_VALUE)
121     {
122         //FindClose(hList);
123         //printf("No files found %s\n", szDir ); return;
124     }
125     else
126     {
127         fFinished = sal_False;
128         while (!fFinished)
129         {
130 
131 			sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
132 			rtl::OString myfile( FileData.cFileName );
133 			rtl::OString mydir( szSubDir );
134 
135             if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
136             {
137                 if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
138 					 (strcmp(FileData.cFileName, "..") != 0 ) )
139                 {
140 	                //sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
141 					transex::Directory aDir(	rtl::OStringToOUString( mydir , RTL_TEXTENCODING_UTF8 , mydir.getLength() ),
142 												rtl::OStringToOUString( myfile , RTL_TEXTENCODING_UTF8 , myfile.getLength() ) );
143 					aDirVec.push_back( aDir );
144                 }
145             }
146 			else
147 			{
148 				transex::File aFile(	rtl::OStringToOUString( mydir , RTL_TEXTENCODING_UTF8 , mydir.getLength() ),
149 										rtl::OStringToOUString( myfile , RTL_TEXTENCODING_UTF8 , myfile.getLength() ) );
150                 aFileVec.push_back( aFile );
151 			}
152             if (!FindNextFile(hList, &FileData))
153             {
154                 if (GetLastError() == ERROR_NO_MORE_FILES)
155                 {
156                     fFinished = sal_True;
157                 }
158             }
159         }
160     }
161 
162     FindClose(hList);
163 
164     ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
165     ::std::sort( aDirVec.begin()  , aDirVec.end()  , Directory::lessDir  );
166 }
167 
168 #else
169 
170 class dirholder
171 {
172 private:
173     DIR *mpDir;
174 public:
dirholder(DIR * pDir)175     dirholder(DIR *pDir) : mpDir(pDir) {}
close()176     int close() { int nRet = mpDir ? closedir(mpDir) : 0; mpDir = NULL; return nRet; }
~dirholder()177     ~dirholder() { close(); }
178 };
179 
readDirectory(const rtl::OUString & sFullpath)180 void Directory::readDirectory( const rtl::OUString& sFullpath )
181 {
182     struct stat     statbuf;
183     struct stat     statbuf2;
184     struct dirent   *dirp;
185     DIR             *dir;
186 
187     if( sFullpath.getLength() < 1 ) return;
188 
189     rtl::OString   sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() ).getStr();
190 
191     // stat
192 	if( stat( sFullpathext.getStr()  , &statbuf ) < 0 ){   printf("warning: Can not stat %s" , sFullpathext.getStr() ); return; }// error }
193 
194     if( S_ISDIR(statbuf.st_mode ) == 0 ) {  return; }// error }   return; // not dir
195 
196     if( (dir = opendir( sFullpathext.getStr() ) ) == NULL  ) {printf("readerror 2 in %s \n",sFullpathext.getStr()); return; } // error } return; // error
197 
198     dirholder aHolder(dir);
199 
200     sFullpathext += rtl::OString( "/" );
201 
202     const rtl::OString sDot ( "." ) ;
203     const rtl::OString sDDot( ".." );
204 
205     if ( chdir( sFullpathext.getStr() ) == -1 ) { printf("chdir error in %s \n",sFullpathext.getStr()); return; } // error
206 
207     while(  ( dirp = readdir( dir ) ) != NULL )
208     {
209         rtl::OString sEntryName(  dirp->d_name );
210 
211         if( sEntryName.equals( sDot )  || sEntryName.equals( sDDot ) )
212             continue;
213 
214         // add dir entry
215         rtl::OString sEntity = sFullpathext;
216         sEntity += sEntryName;
217 
218         // stat new entry
219         if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
220         {
221             printf("error on entry %s\n" , sEntity.getStr() ) ; // error
222             continue;
223         }
224 
225         // add file / dir to vector
226         switch( statbuf2.st_mode & S_IFMT )
227         {
228             case S_IFREG:
229 						{
230                             rtl::OString sFile = sFullpathext;
231                             sFile += sEntryName ;
232                             transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
233                                                  rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
234                                                );
235 
236 							aFileVec.push_back( aFile ) ;
237                             break;
238                          }
239             case S_IFLNK:
240                         {
241                             if( bSkipLinks )    break;
242                         }
243             case S_IFDIR:
244 						{
245                             rtl::OString sDir = sFullpathext;
246                             sDir += sEntryName ;
247 
248                             transex::Directory aDir(
249                                                      rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
250                                                      rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
251                                                    ) ;
252                             aDirVec.push_back( aDir ) ;
253                             break;
254                          }
255         }
256     }
257     if ( chdir( ".." ) == -1 ) { printf("chdir error in .. \n"); return; } // error
258     if( aHolder.close() < 0 )   return ; // error
259 
260     std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
261     std::sort( aDirVec.begin()  , aDirVec.end()  , Directory::lessDir  );
262 
263 }
264 
265 #endif
266 }
267