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