1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
35 #include <l10ntools/directory.hxx>
36 #include <rtl/ustring.hxx>
44 Directory::Directory( const rtl::OUString sFullPath
, const rtl::OUString sEntry
)
46 sFullName
= sFullPath
;
47 sDirectoryName
= sEntry
;
50 bool Directory::lessDir ( const Directory
& rKey1
, const Directory
& rKey2
)
52 rtl::OUString
sName1( ( static_cast< Directory
>( rKey1
) ).getDirectoryName() );
53 rtl::OUString
sName2( ( static_cast< Directory
>( rKey2
) ).getDirectoryName() );
55 return sName1
.compareTo( sName2
) < 0 ;
59 void Directory::dump()
62 for( std::vector
< transex::File
>::iterator iter
= aFileVec
.begin() ; iter
!= aFileVec
.end() ; ++iter
)
64 std::cout
<< "FILE " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
67 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() ; ++iter
)
69 std::cout
<< "DIR " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
74 void Directory::scanSubDir( int nLevels
)
76 readDirectory( sFullName
);
79 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() || nLevels
> 0 ; ++iter
, nLevels
-- )
81 ( *iter
).scanSubDir();
88 void Directory::readDirectory ( const rtl::OUString
& sFullpath
)
92 TCHAR szDir
[MAX_PATH
+1];
93 TCHAR szSubDir
[MAX_PATH
+1];
94 WIN32_FIND_DATA FileData
;
96 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
, sFullpath
.getLength() );
97 const char *dirname
= sFullpathext
.getStr();
99 // Get the proper directory path
100 sprintf(szDir
, "%s\\*", dirname
);
102 // Get the first file
103 hList
= FindFirstFile(szDir
, &FileData
);
104 if (hList
== INVALID_HANDLE_VALUE
)
107 //printf("No files found %s\n", szDir ); return;
111 fFinished
= sal_False
;
115 sprintf(szSubDir
, "%s\\%s", dirname
, FileData
.cFileName
);
116 rtl::OString
myfile( FileData
.cFileName
);
117 rtl::OString
mydir( szSubDir
);
119 if (FileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
121 if ( (strcmp(FileData
.cFileName
, ".") != 0 ) &&
122 (strcmp(FileData
.cFileName
, "..") != 0 ) )
124 //sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
125 transex::Directory
aDir( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
126 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
127 aDirVec
.push_back( aDir
);
132 transex::File
aFile( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
133 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
134 aFileVec
.push_back( aFile
);
136 if (!FindNextFile(hList
, &FileData
))
138 if (GetLastError() == ERROR_NO_MORE_FILES
)
140 fFinished
= sal_True
;
148 ::std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
149 ::std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);
159 dirholder(DIR *pDir
) : mpDir(pDir
) {}
160 int close() { int nRet
= mpDir
? closedir(mpDir
) : 0; mpDir
= NULL
; return nRet
; }
161 ~dirholder() { close(); }
164 void Directory::readDirectory( const rtl::OUString
& sFullpath
)
167 struct stat statbuf2
;
171 if(sFullpath
.isEmpty()) return;
173 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
);
176 if( stat( sFullpathext
.getStr(), &statbuf
) < 0 )
178 printf("warning: Cannot stat %s \n" , sFullpathext
.getStr() );
182 if( S_ISDIR(statbuf
.st_mode
) == 0 )
185 if( (dir
= opendir( sFullpathext
.getStr() ) ) == NULL
)
187 printf("read error 2 in %s \n",sFullpathext
.getStr());
191 dirholder
aHolder(dir
);
193 const rtl::OString
sDot ( "." ) ;
194 const rtl::OString
sDDot( ".." );
196 if ( chdir( sFullpathext
.getStr() ) == -1 )
198 printf("chdir error in %s \n",sFullpathext
.getStr());
202 sFullpathext
+= rtl::OString( "/" );
204 while( ( dirp
= readdir( dir
) ) != NULL
)
206 rtl::OString
sEntryName( dirp
->d_name
);
208 if( sEntryName
.equals( sDot
) || sEntryName
.equals( sDDot
) )
212 rtl::OString sEntity
= sFullpathext
;
213 sEntity
+= sEntryName
;
216 if( lstat( sEntity
.getStr() , &statbuf2
) < 0 )
218 printf("error on entry %s\n" , sEntity
.getStr() ) ;
222 // add file / dir to vector
223 switch( statbuf2
.st_mode
& S_IFMT
)
227 transex::File
aFile( rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
228 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
231 aFileVec
.push_back( aFile
) ;
237 transex::Directory
aDir(
238 rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
239 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
241 aDirVec
.push_back( aDir
) ;
246 if ( chdir( ".." ) == -1 )
248 printf("chdir error in .. \n");
252 if ( aHolder
.close() < 0 )
255 std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
256 std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */