Bump for 3.6-28
[LibreOffice.git] / l10ntools / source / directory.cxx
blobc2c73ea6a8630620ee4f301d01275c1e4ab0ee4e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
31 #ifdef WNT
32 #include <windows.h>
33 #endif
35 #include <l10ntools/directory.hxx>
36 #include <rtl/ustring.hxx>
37 #include <iostream>
38 #include <vector>
39 #include <algorithm>
41 namespace transex
44 Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry )
46 sFullName = sFullPath;
47 sDirectoryName = sEntry;
50 bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
52 rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
53 rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
55 return sName1.compareTo( sName2 ) < 0 ;
59 void Directory::dump()
62 for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
64 std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
67 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
69 std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
74 void Directory::scanSubDir( int nLevels )
76 readDirectory( sFullName );
77 dump();
78 if( nLevels > 0 ) {
79 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )
81 ( *iter ).scanSubDir();
86 #ifdef WNT
88 void Directory::readDirectory ( const rtl::OUString& sFullpath )
90 sal_Bool fFinished;
91 HANDLE hList;
92 TCHAR szDir[MAX_PATH+1];
93 TCHAR szSubDir[MAX_PATH+1];
94 WIN32_FIND_DATA FileData;
96 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
97 const char *dirname = sFullpathext.getStr();
99 // Get the proper directory path
100 sprintf(szDir, "%s\\*", dirname);
102 // Get the first file
103 hList = FindFirstFile(szDir, &FileData);
104 if (hList == INVALID_HANDLE_VALUE)
106 //FindClose(hList);
107 //printf("No files found %s\n", szDir ); return;
109 else
111 fFinished = sal_False;
112 while (!fFinished)
115 sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
116 rtl::OString myfile( FileData.cFileName );
117 rtl::OString mydir( szSubDir );
119 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
121 if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
122 (strcmp(FileData.cFileName, "..") != 0 ) )
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 );
130 else
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 );
136 if (!FindNextFile(hList, &FileData))
138 if (GetLastError() == ERROR_NO_MORE_FILES)
140 fFinished = sal_True;
146 FindClose(hList);
148 ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
149 ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
152 #else
154 class dirholder
156 private:
157 DIR *mpDir;
158 public:
159 dirholder(DIR *pDir) : mpDir(pDir) {}
160 int close() { int nRet = mpDir ? closedir(mpDir) : 0; mpDir = NULL; return nRet; }
161 ~dirholder() { close(); }
164 void Directory::readDirectory( const rtl::OUString& sFullpath )
166 struct stat statbuf;
167 struct stat statbuf2;
168 struct dirent *dirp;
169 DIR *dir;
171 if(sFullpath.isEmpty()) return;
173 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 );
175 // stat
176 if( stat( sFullpathext.getStr(), &statbuf ) < 0 )
178 printf("warning: Cannot stat %s \n" , sFullpathext.getStr() );
179 return;
182 if( S_ISDIR(statbuf.st_mode ) == 0 )
183 return;
185 if( (dir = opendir( sFullpathext.getStr() ) ) == NULL )
187 printf("read error 2 in %s \n",sFullpathext.getStr());
188 return;
191 dirholder aHolder(dir);
193 const rtl::OString sDot ( "." ) ;
194 const rtl::OString sDDot( ".." );
196 if ( chdir( sFullpathext.getStr() ) == -1 )
198 printf("chdir error in %s \n",sFullpathext.getStr());
199 return;
202 sFullpathext += rtl::OString( "/" );
204 while( ( dirp = readdir( dir ) ) != NULL )
206 rtl::OString sEntryName( dirp->d_name );
208 if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
209 continue;
211 // add dir entry
212 rtl::OString sEntity = sFullpathext;
213 sEntity += sEntryName;
215 // stat new entry
216 if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
218 printf("error on entry %s\n" , sEntity.getStr() ) ;
219 continue;
222 // add file / dir to vector
223 switch( statbuf2.st_mode & S_IFMT )
225 case S_IFREG:
227 transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
228 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
231 aFileVec.push_back( aFile ) ;
232 break;
234 case S_IFLNK:
235 case S_IFDIR:
237 transex::Directory aDir(
238 rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
239 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
241 aDirVec.push_back( aDir ) ;
242 break;
246 if ( chdir( ".." ) == -1 )
248 printf("chdir error in .. \n");
249 return;
252 if ( aHolder.close() < 0 )
253 return;
255 std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
256 std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
260 #endif
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */