merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / directory.cxx
blobc4138d3a07dd1a7ff08dd4d5722ba85225120fb6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: directory.cxx,v $
10 * $Revision: 1.6 $
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"
36 #include <iostream>
37 #include <vector>
38 #include <algorithm>
40 namespace transex
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 );
87 dump();
88 if( nLevels > 0 ) {
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 );
106 #ifdef WNT
107 #include <tools/prewin.h>
108 #include <windows.h>
109 #include <tools/postwin.h>
111 void Directory::readDirectory ( const rtl::OUString& sFullpath )
113 BOOL fFinished;
114 HANDLE hList;
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)
129 //FindClose(hList);
130 //printf("No files found %s\n", szDir ); return;
132 else
134 fFinished = FALSE;
135 while (!fFinished)
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 );
153 else
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)
163 fFinished = TRUE;
169 FindClose(hList);
171 ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
172 ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
175 #else
177 void Directory::readDirectory( const rtl::OUString& sFullpath )
179 struct stat statbuf;
180 struct stat statbuf2;
181 struct dirent *dirp;
182 DIR *dir;
183 //int ret;
184 //char *ptr;
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();
192 // stat
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( ".." );
204 chdir( path );
206 while( ( dirp = readdir( dir ) ) != NULL )
208 rtl::OString sEntryName( dirp->d_name );
210 if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
211 continue;
213 // add dir entry
214 rtl::OString sEntity = sFullpathext;
215 sEntity += sEntryName;
217 // stat new entry
218 if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
220 printf("error on entry %s\n" , sEntity.getStr() ) ; // error
221 continue;
224 // add file / dir to vector
225 switch( statbuf2.st_mode & S_IFMT )
227 case S_IFREG:
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 ) ;
236 break;
238 case S_IFLNK:
240 if( bSkipLinks ) break;
242 case S_IFDIR:
244 rtl::OString sDir = sFullpathext;
245 sDir += sEntryName ;
247 transex::Directory aDir(
248 rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
249 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
250 ) ;
251 aDirVec.push_back( aDir ) ;
252 break;
256 chdir( ".." );
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 );
264 #endif