1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: directory.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_transex3.hxx"
34 #include <transex3/directory.hxx>
35 #include "tools/string.hxx"
43 Directory::Directory( const rtl::OUString sFullpath
) : bSkipLinks( false )
45 sFullName
= sFullpath
;
48 Directory::Directory( const rtl::OUString sFullPath
, const rtl::OUString sEntry
) : bSkipLinks( false )
50 sFullName
= sFullPath
;
51 sDirectoryName
= sEntry
;
55 Directory::Directory( const ByteString sFullPath
) : bSkipLinks( false )
57 sDirectoryName
= rtl::OUString( sFullPath
.GetBuffer() , RTL_TEXTENCODING_UTF8
, sFullPath
.Len() );
60 bool Directory::lessDir ( const Directory
& rKey1
, const Directory
& rKey2
)
62 rtl::OUString
sName1( ( static_cast< Directory
>( rKey1
) ).getDirectoryName() );
63 rtl::OUString
sName2( ( static_cast< Directory
>( rKey2
) ).getDirectoryName() );
65 return sName1
.compareTo( sName2
) < 0 ;
69 void Directory::dump()
72 for( std::vector
< transex::File
>::iterator iter
= aFileVec
.begin() ; iter
!= aFileVec
.end() ; ++iter
)
74 std::cout
<< "FILE " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
77 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() ; ++iter
)
79 std::cout
<< "DIR " << rtl::OUStringToOString( (*iter
).getFullName().getStr() , RTL_TEXTENCODING_UTF8
, (*iter
).getFullName().getLength() ).getStr() << "\n";
84 void Directory::scanSubDir( int nLevels
)
86 readDirectory( sFullName
);
89 for( std::vector
< transex::Directory
>::iterator iter
= aDirVec
.begin() ; iter
!= aDirVec
.end() || nLevels
> 0 ; ++iter
, nLevels
-- )
91 ( *iter
).scanSubDir();
96 void Directory::setSkipLinks( bool is_skipped
)
98 bSkipLinks
= is_skipped
;
101 void Directory::readDirectory()
103 readDirectory( sFullName
);
107 #include <tools/prewin.h>
109 #include <tools/postwin.h>
111 void Directory::readDirectory ( const rtl::OUString
& sFullpath
)
115 TCHAR szDir
[MAX_PATH
+1];
116 TCHAR szSubDir
[MAX_PATH
+1];
117 WIN32_FIND_DATA FileData
;
119 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
, sFullpath
.getLength() );
120 const char *dirname
= sFullpathext
.getStr();
122 // Get the proper directory path
123 sprintf(szDir
, "%s\\*", dirname
);
125 // Get the first file
126 hList
= FindFirstFile(szDir
, &FileData
);
127 if (hList
== INVALID_HANDLE_VALUE
)
130 //printf("No files found %s\n", szDir ); return;
138 sprintf(szSubDir
, "%s\\%s", dirname
, FileData
.cFileName
);
139 rtl::OString
myfile( FileData
.cFileName
);
140 rtl::OString
mydir( szSubDir
);
142 if (FileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
144 if ( (strcmp(FileData
.cFileName
, ".") != 0 ) &&
145 (strcmp(FileData
.cFileName
, "..") != 0 ) )
147 //sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
148 transex::Directory
aDir( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
149 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
150 aDirVec
.push_back( aDir
);
155 transex::File
aFile( rtl::OStringToOUString( mydir
, RTL_TEXTENCODING_UTF8
, mydir
.getLength() ),
156 rtl::OStringToOUString( myfile
, RTL_TEXTENCODING_UTF8
, myfile
.getLength() ) );
157 aFileVec
.push_back( aFile
);
159 if (!FindNextFile(hList
, &FileData
))
161 if (GetLastError() == ERROR_NO_MORE_FILES
)
171 ::std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
172 ::std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);
177 void Directory::readDirectory( const rtl::OUString
& sFullpath
)
180 struct stat statbuf2
;
186 if( sFullpath
.getLength() < 1 ) return;
188 rtl::OString sFullpathext
= rtl::OUStringToOString( sFullpath
, RTL_TEXTENCODING_UTF8
, sFullpath
.getLength() ).getStr();
189 //printf("%s\n",sFullpathext.getStr());
190 const char* path
= sFullpathext
.getStr();
193 if( stat( path
, &statbuf
) < 0 ){ printf("warning: Can not stat %s" , path
); return; }// error }
195 if( S_ISDIR(statbuf
.st_mode
) == 0 ) { return; }// error } return; // not dir
197 if( (dir
= opendir( path
) ) == NULL
) {printf("readerror 2 in %s \n",path
); return; } // error } return; // error
199 sFullpathext
+= rtl::OString( "/" );
201 const rtl::OString
sDot ( "." ) ;
202 const rtl::OString
sDDot( ".." );
206 while( ( dirp
= readdir( dir
) ) != NULL
)
208 rtl::OString
sEntryName( dirp
->d_name
);
210 if( sEntryName
.equals( sDot
) || sEntryName
.equals( sDDot
) )
214 rtl::OString sEntity
= sFullpathext
;
215 sEntity
+= sEntryName
;
218 if( lstat( sEntity
.getStr() , &statbuf2
) < 0 )
220 printf("error on entry %s\n" , sEntity
.getStr() ) ; // error
224 // add file / dir to vector
225 switch( statbuf2
.st_mode
& S_IFMT
)
229 rtl::OString sFile
= sFullpathext
;
230 sFile
+= sEntryName
;
231 transex::File
aFile( rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
232 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
235 aFileVec
.push_back( aFile
) ;
240 if( bSkipLinks
) break;
244 rtl::OString sDir
= sFullpathext
;
247 transex::Directory
aDir(
248 rtl::OStringToOUString( sEntity
, RTL_TEXTENCODING_UTF8
, sEntity
.getLength() ) ,
249 rtl::OStringToOUString( sEntryName
, RTL_TEXTENCODING_UTF8
, sEntryName
.getLength() )
251 aDirVec
.push_back( aDir
) ;
257 if( closedir( dir
) < 0 ) return ; // error
259 std::sort( aFileVec
.begin() , aFileVec
.end() , File::lessFile
);
260 std::sort( aDirVec
.begin() , aDirVec
.end() , Directory::lessDir
);