1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
26 #include <l10ntools/directory.hxx>
27 #include <rtl/ustring.hxx>
35 Directory::Directory( const rtl::OUString sFullPath
, const rtl::OUString sEntry
)
37 sFullName
= sFullPath
;
38 sDirectoryName
= sEntry
;
41 bool Directory::lessDir ( const Directory
& rKey1
, const Directory
& rKey2
)
43 rtl::OUString
sName1( ( static_cast< Directory
>( rKey1
) ).getDirectoryName() );
44 rtl::OUString
sName2( ( static_cast< Directory
>( rKey2
) ).getDirectoryName() );
46 return sName1
.compareTo( sName2
) < 0 ;
50 void Directory::dump()
53 for( std::vector
< transex::File
>::iterator iter
= aFileVec
.begin() ; iter
!= aFileVec
.end() ; ++iter
)
55 std::cout
<< "FILE " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
58 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() ; ++iter
)
60 std::cout
<< "DIR " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
65 void Directory::scanSubDir( int nLevels
)
67 readDirectory( sFullName
);
70 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() || nLevels
> 0 ; ++iter
, nLevels
-- )
72 ( *iter
).scanSubDir();
79 void Directory::readDirectory ( const rtl::OUString
& sFullpath
)
83 TCHAR szDir
[MAX_PATH
+1];
84 TCHAR szSubDir
[MAX_PATH
+1];
85 WIN32_FIND_DATA FileData
;
87 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
, sFullpath
.getLength() );
88 const char *dirname
= sFullpathext
.getStr();
90 // Get the proper directory path
91 sprintf(szDir
, "%s\\*", dirname
);
94 hList
= FindFirstFile(szDir
, &FileData
);
95 if (hList
== INVALID_HANDLE_VALUE
)
98 //printf("No files found %s\n", szDir ); return;
102 fFinished
= sal_False
;
106 sprintf(szSubDir
, "%s\\%s", dirname
, FileData
.cFileName
);
107 rtl::OString
myfile( FileData
.cFileName
);
108 rtl::OString
mydir( szSubDir
);
110 if (FileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
112 if ( (strcmp(FileData
.cFileName
, ".") != 0 ) &&
113 (strcmp(FileData
.cFileName
, "..") != 0 ) )
115 //sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
116 transex::Directory
aDir( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
117 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
118 aDirVec
.push_back( aDir
);
123 transex::File
aFile( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
124 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
125 aFileVec
.push_back( aFile
);
127 if (!FindNextFile(hList
, &FileData
))
129 if (GetLastError() == ERROR_NO_MORE_FILES
)
131 fFinished
= sal_True
;
139 ::std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
140 ::std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);
150 dirholder(DIR *pDir
) : mpDir(pDir
) {}
151 int close() { int nRet
= mpDir
? closedir(mpDir
) : 0; mpDir
= NULL
; return nRet
; }
152 ~dirholder() { close(); }
155 void Directory::readDirectory( const rtl::OUString
& sFullpath
)
158 struct stat statbuf2
;
162 if(sFullpath
.isEmpty()) return;
164 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
);
167 if( stat( sFullpathext
.getStr(), &statbuf
) < 0 )
169 printf("warning: Cannot stat %s \n" , sFullpathext
.getStr() );
173 if( S_ISDIR(statbuf
.st_mode
) == 0 )
176 if( (dir
= opendir( sFullpathext
.getStr() ) ) == NULL
)
178 printf("read error 2 in %s \n",sFullpathext
.getStr());
182 dirholder
aHolder(dir
);
184 const rtl::OString
sDot ( "." ) ;
185 const rtl::OString
sDDot( ".." );
187 if ( chdir( sFullpathext
.getStr() ) == -1 )
189 printf("chdir error in %s \n",sFullpathext
.getStr());
193 sFullpathext
+= rtl::OString( "/" );
195 while( ( dirp
= readdir( dir
) ) != NULL
)
197 rtl::OString
sEntryName( dirp
->d_name
);
199 if( sEntryName
.equals( sDot
) || sEntryName
.equals( sDDot
) )
203 rtl::OString sEntity
= sFullpathext
;
204 sEntity
+= sEntryName
;
207 if( lstat( sEntity
.getStr() , &statbuf2
) < 0 )
209 printf("error on entry %s\n" , sEntity
.getStr() ) ;
213 // add file / dir to vector
214 switch( statbuf2
.st_mode
& S_IFMT
)
218 transex::File
aFile( rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
219 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
222 aFileVec
.push_back( aFile
) ;
228 transex::Directory
aDir(
229 rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
230 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
232 aDirVec
.push_back( aDir
) ;
237 if ( chdir( ".." ) == -1 )
239 printf("chdir error in .. \n");
243 if ( aHolder
.close() < 0 )
246 std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
247 std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */