Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : :
31 : : #ifdef WNT
32 : : #include <windows.h>
33 : : #endif
34 : :
35 : : #include <l10ntools/directory.hxx>
36 : : #include <rtl/ustring.hxx>
37 : : #include <iostream>
38 : : #include <vector>
39 : : #include <algorithm>
40 : :
41 : : namespace transex
42 : : {
43 : :
44 : 0 : Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry )
45 : : {
46 : 0 : sFullName = sFullPath;
47 : 0 : sDirectoryName = sEntry;
48 : 0 : }
49 : :
50 : 0 : bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
51 : : {
52 : 0 : rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
53 : 0 : rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
54 : :
55 : 0 : return sName1.compareTo( sName2 ) < 0 ;
56 : : }
57 : :
58 : :
59 : 0 : void Directory::dump()
60 : : {
61 : :
62 : 0 : for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
63 : : {
64 : 0 : std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
65 : : }
66 : :
67 : 0 : for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
68 : : {
69 : 0 : std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
70 : : }
71 : :
72 : 0 : }
73 : :
74 : 0 : void Directory::scanSubDir( int nLevels )
75 : : {
76 : 0 : readDirectory( sFullName );
77 : 0 : dump();
78 : 0 : if( nLevels > 0 ) {
79 : 0 : for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )
80 : : {
81 : 0 : ( *iter ).scanSubDir();
82 : : }
83 : : }
84 : 0 : }
85 : :
86 : : #ifdef WNT
87 : :
88 : : void Directory::readDirectory ( const rtl::OUString& sFullpath )
89 : : {
90 : : sal_Bool fFinished;
91 : : HANDLE hList;
92 : : TCHAR szDir[MAX_PATH+1];
93 : : TCHAR szSubDir[MAX_PATH+1];
94 : : WIN32_FIND_DATA FileData;
95 : :
96 : : rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
97 : : const char *dirname = sFullpathext.getStr();
98 : :
99 : : // Get the proper directory path
100 : : sprintf(szDir, "%s\\*", dirname);
101 : :
102 : : // Get the first file
103 : : hList = FindFirstFile(szDir, &FileData);
104 : : if (hList == INVALID_HANDLE_VALUE)
105 : : {
106 : : //FindClose(hList);
107 : : //printf("No files found %s\n", szDir ); return;
108 : : }
109 : : else
110 : : {
111 : : fFinished = sal_False;
112 : : while (!fFinished)
113 : : {
114 : :
115 : : sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
116 : : rtl::OString myfile( FileData.cFileName );
117 : : rtl::OString mydir( szSubDir );
118 : :
119 : : if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
120 : : {
121 : : if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
122 : : (strcmp(FileData.cFileName, "..") != 0 ) )
123 : : {
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 );
128 : : }
129 : : }
130 : : else
131 : : {
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 );
135 : : }
136 : : if (!FindNextFile(hList, &FileData))
137 : : {
138 : : if (GetLastError() == ERROR_NO_MORE_FILES)
139 : : {
140 : : fFinished = sal_True;
141 : : }
142 : : }
143 : : }
144 : : }
145 : :
146 : : FindClose(hList);
147 : :
148 : : ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
149 : : ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
150 : : }
151 : :
152 : : #else
153 : :
154 : : class dirholder
155 : : {
156 : : private:
157 : : DIR *mpDir;
158 : : public:
159 : 0 : dirholder(DIR *pDir) : mpDir(pDir) {}
160 : 0 : int close() { int nRet = mpDir ? closedir(mpDir) : 0; mpDir = NULL; return nRet; }
161 : 0 : ~dirholder() { close(); }
162 : : };
163 : :
164 : 0 : void Directory::readDirectory( const rtl::OUString& sFullpath )
165 : : {
166 : : struct stat statbuf;
167 : : struct stat statbuf2;
168 : : struct dirent *dirp;
169 : : DIR *dir;
170 : :
171 : 0 : if(sFullpath.isEmpty()) return;
172 : :
173 : 0 : rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 );
174 : :
175 : : // stat
176 : 0 : if( stat( sFullpathext.getStr(), &statbuf ) < 0 )
177 : : {
178 : 0 : printf("warning: Cannot stat %s \n" , sFullpathext.getStr() );
179 : : return;
180 : : }
181 : :
182 : 0 : if( S_ISDIR(statbuf.st_mode ) == 0 )
183 : : return;
184 : :
185 : 0 : if( (dir = opendir( sFullpathext.getStr() ) ) == NULL )
186 : : {
187 : 0 : printf("read error 2 in %s \n",sFullpathext.getStr());
188 : : return;
189 : : }
190 : :
191 : 0 : dirholder aHolder(dir);
192 : :
193 : 0 : const rtl::OString sDot ( "." ) ;
194 : 0 : const rtl::OString sDDot( ".." );
195 : :
196 : 0 : if ( chdir( sFullpathext.getStr() ) == -1 )
197 : : {
198 : 0 : printf("chdir error in %s \n",sFullpathext.getStr());
199 : : return;
200 : : }
201 : :
202 : 0 : sFullpathext += rtl::OString( "/" );
203 : :
204 : 0 : while( ( dirp = readdir( dir ) ) != NULL )
205 : : {
206 : 0 : rtl::OString sEntryName( dirp->d_name );
207 : :
208 : 0 : if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
209 : 0 : continue;
210 : :
211 : : // add dir entry
212 : 0 : rtl::OString sEntity = sFullpathext;
213 : 0 : sEntity += sEntryName;
214 : :
215 : : // stat new entry
216 : 0 : if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
217 : : {
218 : 0 : printf("error on entry %s\n" , sEntity.getStr() ) ;
219 : 0 : continue;
220 : : }
221 : :
222 : : // add file / dir to vector
223 : 0 : switch( statbuf2.st_mode & S_IFMT )
224 : : {
225 : : case S_IFREG:
226 : : {
227 : 0 : transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
228 : 0 : rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
229 : 0 : );
230 : :
231 : 0 : aFileVec.push_back( aFile ) ;
232 : 0 : break;
233 : : }
234 : : case S_IFLNK:
235 : : case S_IFDIR:
236 : : {
237 : : transex::Directory aDir(
238 : 0 : rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
239 : 0 : rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
240 : 0 : ) ;
241 : 0 : aDirVec.push_back( aDir ) ;
242 : 0 : break;
243 : : }
244 : : }
245 : 0 : }
246 : 0 : if ( chdir( ".." ) == -1 )
247 : : {
248 : 0 : printf("chdir error in .. \n");
249 : : return;
250 : : }
251 : :
252 : 0 : if ( aHolder.close() < 0 )
253 : : return;
254 : :
255 : 0 : std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
256 : 0 : std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
257 : :
258 : : }
259 : :
260 : : #endif
261 : 0 : }
262 : :
263 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|