1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_l10ntools.hxx"
#include <l10ntools/directory.hxx>
#include "tools/string.hxx"
#include <iostream>
#include <vector>
#include <algorithm>
namespace transex
{
Directory::Directory( const rtl::OUString sFullpath ) : bSkipLinks( false )
{
sFullName = sFullpath;
}
Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry ) : bSkipLinks( false )
{
sFullName = sFullPath;
sDirectoryName = sEntry;
}
Directory::Directory( const ByteString sFullPath ) : bSkipLinks( false )
{
sDirectoryName = rtl::OUString( sFullPath.GetBuffer() , RTL_TEXTENCODING_UTF8 , sFullPath.Len() );
}
bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
{
rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
return sName1.compareTo( sName2 ) < 0 ;
}
void Directory::dump()
{
for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
{
std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
}
for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
{
std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
}
}
void Directory::scanSubDir( int nLevels )
{
readDirectory( sFullName );
dump();
if( nLevels > 0 ) {
for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )
{
( *iter ).scanSubDir();
}
}
}
void Directory::setSkipLinks( bool is_skipped )
{
bSkipLinks = is_skipped;
}
void Directory::readDirectory()
{
readDirectory( sFullName );
}
#ifdef WNT
#include <tools/prewin.h>
#include <windows.h>
#include <tools/postwin.h>
void Directory::readDirectory ( const rtl::OUString& sFullpath )
{
sal_Bool fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
TCHAR szSubDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
const char *dirname = sFullpathext.getStr();
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
//FindClose(hList);
//printf("No files found %s\n", szDir ); return;
}
else
{
fFinished = sal_False;
while (!fFinished)
{
sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
rtl::OString myfile( FileData.cFileName );
rtl::OString mydir( szSubDir );
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
(strcmp(FileData.cFileName, "..") != 0 ) )
{
//sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
transex::Directory aDir( rtl::OStringToOUString( mydir , RTL_TEXTENCODING_UTF8 , mydir.getLength() ),
rtl::OStringToOUString( myfile , RTL_TEXTENCODING_UTF8 , myfile.getLength() ) );
aDirVec.push_back( aDir );
}
}
else
{
transex::File aFile( rtl::OStringToOUString( mydir , RTL_TEXTENCODING_UTF8 , mydir.getLength() ),
rtl::OStringToOUString( myfile , RTL_TEXTENCODING_UTF8 , myfile.getLength() ) );
aFileVec.push_back( aFile );
}
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = sal_True;
}
}
}
}
FindClose(hList);
::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
}
#else
class dirholder
{
private:
DIR *mpDir;
public:
dirholder(DIR *pDir) : mpDir(pDir) {}
int close() { int nRet = mpDir ? closedir(mpDir) : 0; mpDir = NULL; return nRet; }
~dirholder() { close(); }
};
void Directory::readDirectory( const rtl::OUString& sFullpath )
{
struct stat statbuf;
struct stat statbuf2;
struct dirent *dirp;
DIR *dir;
if( sFullpath.getLength() < 1 ) return;
rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() ).getStr();
// stat
if( stat( sFullpathext.getStr() , &statbuf ) < 0 ){ printf("warning: Can not stat %s" , sFullpathext.getStr() ); return; }// error }
if( S_ISDIR(statbuf.st_mode ) == 0 ) { return; }// error } return; // not dir
if( (dir = opendir( sFullpathext.getStr() ) ) == NULL ) {printf("readerror 2 in %s \n",sFullpathext.getStr()); return; } // error } return; // error
dirholder aHolder(dir);
sFullpathext += rtl::OString( "/" );
const rtl::OString sDot ( "." ) ;
const rtl::OString sDDot( ".." );
if ( chdir( sFullpathext.getStr() ) == -1 ) { printf("chdir error in %s \n",sFullpathext.getStr()); return; } // error
while( ( dirp = readdir( dir ) ) != NULL )
{
rtl::OString sEntryName( dirp->d_name );
if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
continue;
// add dir entry
rtl::OString sEntity = sFullpathext;
sEntity += sEntryName;
// stat new entry
if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
{
printf("error on entry %s\n" , sEntity.getStr() ) ; // error
continue;
}
// add file / dir to vector
switch( statbuf2.st_mode & S_IFMT )
{
case S_IFREG:
{
rtl::OString sFile = sFullpathext;
sFile += sEntryName ;
transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
);
aFileVec.push_back( aFile ) ;
break;
}
case S_IFLNK:
{
if( bSkipLinks ) break;
}
case S_IFDIR:
{
rtl::OString sDir = sFullpathext;
sDir += sEntryName ;
transex::Directory aDir(
rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
) ;
aDirVec.push_back( aDir ) ;
break;
}
}
}
if ( chdir( ".." ) == -1 ) { printf("chdir error in .. \n"); return; } // error
if( aHolder.close() < 0 ) return ; // error
std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
}
#endif
}
|