directory.cxx (3cd96b95) directory.cxx (46d9d397)
1/**************************************************************
1/**************************************************************
2 *
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
19 *
20 *************************************************************/
21
22
23
20 *************************************************************/
21
22
23
24
25// MARKER(update_precomp.py): autogen include statement, do not remove
26#include "precompiled_l10ntools.hxx"
27#include <l10ntools/directory.hxx>
28#include "tools/string.hxx"
29#include <iostream>
30#include <vector>
31#include <algorithm>
32
24// MARKER(update_precomp.py): autogen include statement, do not remove
25#include "precompiled_l10ntools.hxx"
26#include <l10ntools/directory.hxx>
27#include "tools/string.hxx"
28#include <iostream>
29#include <vector>
30#include <algorithm>
31
33namespace transex
32namespace transex
34{
35
36Directory::Directory( const rtl::OUString sFullpath ) : bSkipLinks( false )
37{
38 sFullName = sFullpath;
39}
40
41Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry ) : bSkipLinks( false )

--- 5 unchanged lines hidden (view full) ---

47
48Directory::Directory( const ByteString sFullPath ) : bSkipLinks( false )
49{
50 sDirectoryName = rtl::OUString( sFullPath.GetBuffer() , RTL_TEXTENCODING_UTF8 , sFullPath.Len() );
51}
52
53bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
54{
33{
34
35Directory::Directory( const rtl::OUString sFullpath ) : bSkipLinks( false )
36{
37 sFullName = sFullpath;
38}
39
40Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry ) : bSkipLinks( false )

--- 5 unchanged lines hidden (view full) ---

46
47Directory::Directory( const ByteString sFullPath ) : bSkipLinks( false )
48{
49 sDirectoryName = rtl::OUString( sFullPath.GetBuffer() , RTL_TEXTENCODING_UTF8 , sFullPath.Len() );
50}
51
52bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
53{
55 rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
56 rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
57
54 rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
55 rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
56
58 return sName1.compareTo( sName2 ) < 0 ;
59}
60
61
62void Directory::dump()
63{
57 return sName1.compareTo( sName2 ) < 0 ;
58}
59
60
61void Directory::dump()
62{
64
63
65 for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
66 {
67 std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
68 }
69
70 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
71 {
72 std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
73 }
64 for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
65 {
66 std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
67 }
68
69 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
70 {
71 std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
72 }
74
73
75}
76
77void Directory::scanSubDir( int nLevels )
78{
79 readDirectory( sFullName );
80 dump();
81 if( nLevels > 0 ) {
82 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )

--- 23 unchanged lines hidden (view full) ---

106 sal_Bool fFinished;
107 HANDLE hList;
108 TCHAR szDir[MAX_PATH+1];
109 TCHAR szSubDir[MAX_PATH+1];
110 WIN32_FIND_DATA FileData;
111
112 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
113 const char *dirname = sFullpathext.getStr();
74}
75
76void Directory::scanSubDir( int nLevels )
77{
78 readDirectory( sFullName );
79 dump();
80 if( nLevels > 0 ) {
81 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )

--- 23 unchanged lines hidden (view full) ---

105 sal_Bool fFinished;
106 HANDLE hList;
107 TCHAR szDir[MAX_PATH+1];
108 TCHAR szSubDir[MAX_PATH+1];
109 WIN32_FIND_DATA FileData;
110
111 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
112 const char *dirname = sFullpathext.getStr();
114
113
115 // Get the proper directory path
116 sprintf(szDir, "%s\\*", dirname);
114 // Get the proper directory path
115 sprintf(szDir, "%s\\*", dirname);
117
116
118 // Get the first file
119 hList = FindFirstFile(szDir, &FileData);
120 if (hList == INVALID_HANDLE_VALUE)
117 // Get the first file
118 hList = FindFirstFile(szDir, &FileData);
119 if (hList == INVALID_HANDLE_VALUE)
121 {
120 {
122 //FindClose(hList);
123 //printf("No files found %s\n", szDir ); return;
124 }
125 else
126 {
127 fFinished = sal_False;
128 while (!fFinished)
129 {
121 //FindClose(hList);
122 //printf("No files found %s\n", szDir ); return;
123 }
124 else
125 {
126 fFinished = sal_False;
127 while (!fFinished)
128 {
130
129
131 sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
132 rtl::OString myfile( FileData.cFileName );
133 rtl::OString mydir( szSubDir );
134
135 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
136 {
137 if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
138 (strcmp(FileData.cFileName, "..") != 0 ) )

--- 16 unchanged lines hidden (view full) ---

155 {
156 fFinished = sal_True;
157 }
158 }
159 }
160 }
161
162 FindClose(hList);
130 sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
131 rtl::OString myfile( FileData.cFileName );
132 rtl::OString mydir( szSubDir );
133
134 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
135 {
136 if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
137 (strcmp(FileData.cFileName, "..") != 0 ) )

--- 16 unchanged lines hidden (view full) ---

154 {
155 fFinished = sal_True;
156 }
157 }
158 }
159 }
160
161 FindClose(hList);
163
162
164 ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
165 ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
166}
167
168#else
169
170class dirholder
171{

--- 10 unchanged lines hidden (view full) ---

182 struct stat statbuf;
183 struct stat statbuf2;
184 struct dirent *dirp;
185 DIR *dir;
186
187 if( sFullpath.getLength() < 1 ) return;
188
189 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() ).getStr();
163 ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
164 ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
165}
166
167#else
168
169class dirholder
170{

--- 10 unchanged lines hidden (view full) ---

181 struct stat statbuf;
182 struct stat statbuf2;
183 struct dirent *dirp;
184 DIR *dir;
185
186 if( sFullpath.getLength() < 1 ) return;
187
188 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() ).getStr();
190
189
191 // stat
190 // stat
192 if( stat( sFullpathext.getStr() , &statbuf ) < 0 ){ printf("warning: Can not stat %s" , sFullpathext.getStr() ); return; }// error }
193
191 if( stat( sFullpathext.getStr() , &statbuf ) < 0 ){ printf("warning: Can not stat %s" , sFullpathext.getStr() ); return; } // error }
192
194 if( S_ISDIR(statbuf.st_mode ) == 0 ) { return; }// error } return; // not dir
195
196 if( (dir = opendir( sFullpathext.getStr() ) ) == NULL ) {printf("readerror 2 in %s \n",sFullpathext.getStr()); return; } // error } return; // error
197
198 dirholder aHolder(dir);
193 if( S_ISDIR(statbuf.st_mode ) == 0 ) { return; }// error } return; // not dir
194
195 if( (dir = opendir( sFullpathext.getStr() ) ) == NULL ) {printf("readerror 2 in %s \n",sFullpathext.getStr()); return; } // error } return; // error
196
197 dirholder aHolder(dir);
199
198
200 sFullpathext += rtl::OString( "/" );
201
202 const rtl::OString sDot ( "." ) ;
203 const rtl::OString sDDot( ".." );
199 sFullpathext += rtl::OString( "/" );
200
201 const rtl::OString sDot ( "." ) ;
202 const rtl::OString sDDot( ".." );
204
203
205 if ( chdir( sFullpathext.getStr() ) == -1 ) { printf("chdir error in %s \n",sFullpathext.getStr()); return; } // error
204 if ( chdir( sFullpathext.getStr() ) == -1 ) { printf("chdir error in %s \n",sFullpathext.getStr()); return; } // error
206
205
207 while( ( dirp = readdir( dir ) ) != NULL )
208 {
209 rtl::OString sEntryName( dirp->d_name );
210
211 if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
212 continue;
206 while( ( dirp = readdir( dir ) ) != NULL )
207 {
208 rtl::OString sEntryName( dirp->d_name );
209
210 if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
211 continue;
213
212
214 // add dir entry
215 rtl::OString sEntity = sFullpathext;
216 sEntity += sEntryName;
213 // add dir entry
214 rtl::OString sEntity = sFullpathext;
215 sEntity += sEntryName;
217
216
218 // stat new entry
219 if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
220 {
221 printf("error on entry %s\n" , sEntity.getStr() ) ; // error
222 continue;
223 }
224
225 // add file / dir to vector
226 switch( statbuf2.st_mode & S_IFMT )
227 {
228 case S_IFREG:
217 // stat new entry
218 if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
219 {
220 printf("error on entry %s\n" , sEntity.getStr() ) ; // error
221 continue;
222 }
223
224 // add file / dir to vector
225 switch( statbuf2.st_mode & S_IFMT )
226 {
227 case S_IFREG:
229 {
228 {
230 rtl::OString sFile = sFullpathext;
231 sFile += sEntryName ;
229 rtl::OString sFile = sFullpathext;
230 sFile += sEntryName ;
232 transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
233 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
231 transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
232 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
234 );
235
233 );
234
236 aFileVec.push_back( aFile ) ;
237 break;
238 }
235 aFileVec.push_back( aFile ) ;
236 break;
237 }
239 case S_IFLNK:
238 case S_IFLNK:
240 {
239 {
241 if( bSkipLinks ) break;
242 }
243 case S_IFDIR:
240 if( bSkipLinks ) break;
241 }
242 case S_IFDIR:
244 {
243 {
245 rtl::OString sDir = sFullpathext;
246 sDir += sEntryName ;
247
244 rtl::OString sDir = sFullpathext;
245 sDir += sEntryName ;
246
248 transex::Directory aDir(
247 transex::Directory aDir(
249 rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
248 rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
250 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
251 ) ;
252 aDirVec.push_back( aDir ) ;
253 break;
249 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
250 ) ;
251 aDirVec.push_back( aDir ) ;
252 break;
254 }
255 }
256 }
257 if ( chdir( ".." ) == -1 ) { printf("chdir error in .. \n"); return; } // error
258 if( aHolder.close() < 0 ) return ; // error
259
260 std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
261 std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
262
263}
264
265#endif
266}
253 }
254 }
255 }
256 if ( chdir( ".." ) == -1 ) { printf("chdir error in .. \n"); return; } // error
257 if( aHolder.close() < 0 ) return ; // error
258
259 std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
260 std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
261
262}
263
264#endif
265}
266