Remove the 4th option from the pivot table source selection dialog.
[LibreOffice.git] / l10ntools / source / directory.cxx
blob468bfbd678372144382933f79749156f091e4349
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #ifdef WNT
23 #include <windows.h>
24 #endif
26 #include <l10ntools/directory.hxx>
27 #include <rtl/ustring.hxx>
28 #include <iostream>
29 #include <vector>
30 #include <algorithm>
32 namespace transex
35 Directory::Directory( const rtl::OUString sFullPath , const rtl::OUString sEntry )
37 sFullName = sFullPath;
38 sDirectoryName = sEntry;
41 bool Directory::lessDir ( const Directory& rKey1, const Directory& rKey2 )
43 rtl::OUString sName1( ( static_cast< Directory >( rKey1 ) ).getDirectoryName() );
44 rtl::OUString sName2( ( static_cast< Directory >( rKey2 ) ).getDirectoryName() );
46 return sName1.compareTo( sName2 ) < 0 ;
50 void Directory::dump()
53 for( std::vector< transex::File >::iterator iter = aFileVec.begin() ; iter != aFileVec.end() ; ++iter )
55 std::cout << "FILE " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
58 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() ; ++iter )
60 std::cout << "DIR " << rtl::OUStringToOString( (*iter).getFullName().getStr() , RTL_TEXTENCODING_UTF8 , (*iter).getFullName().getLength() ).getStr() << "\n";
65 void Directory::scanSubDir( int nLevels )
67 readDirectory( sFullName );
68 dump();
69 if( nLevels > 0 ) {
70 for( std::vector< transex::Directory >::iterator iter = aDirVec.begin() ; iter != aDirVec.end() || nLevels > 0 ; ++iter , nLevels-- )
72 ( *iter ).scanSubDir();
77 #ifdef WNT
79 void Directory::readDirectory ( const rtl::OUString& sFullpath )
81 sal_Bool fFinished;
82 HANDLE hList;
83 TCHAR szDir[MAX_PATH+1];
84 TCHAR szSubDir[MAX_PATH+1];
85 WIN32_FIND_DATA FileData;
87 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 , sFullpath.getLength() );
88 const char *dirname = sFullpathext.getStr();
90 // Get the proper directory path
91 sprintf(szDir, "%s\\*", dirname);
93 // Get the first file
94 hList = FindFirstFile(szDir, &FileData);
95 if (hList == INVALID_HANDLE_VALUE)
97 //FindClose(hList);
98 //printf("No files found %s\n", szDir ); return;
100 else
102 fFinished = sal_False;
103 while (!fFinished)
106 sprintf(szSubDir, "%s\\%s", dirname, FileData.cFileName);
107 rtl::OString myfile( FileData.cFileName );
108 rtl::OString mydir( szSubDir );
110 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
112 if ( (strcmp(FileData.cFileName, ".") != 0 ) &&
113 (strcmp(FileData.cFileName, "..") != 0 ) )
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 );
121 else
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 );
127 if (!FindNextFile(hList, &FileData))
129 if (GetLastError() == ERROR_NO_MORE_FILES)
131 fFinished = sal_True;
137 FindClose(hList);
139 ::std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
140 ::std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
143 #else
145 class dirholder
147 private:
148 DIR *mpDir;
149 public:
150 dirholder(DIR *pDir) : mpDir(pDir) {}
151 int close() { int nRet = mpDir ? closedir(mpDir) : 0; mpDir = NULL; return nRet; }
152 ~dirholder() { close(); }
155 void Directory::readDirectory( const rtl::OUString& sFullpath )
157 struct stat statbuf;
158 struct stat statbuf2;
159 struct dirent *dirp;
160 DIR *dir;
162 if(sFullpath.isEmpty()) return;
164 rtl::OString sFullpathext = rtl::OUStringToOString( sFullpath , RTL_TEXTENCODING_UTF8 );
166 // stat
167 if( stat( sFullpathext.getStr(), &statbuf ) < 0 )
169 printf("warning: Cannot stat %s \n" , sFullpathext.getStr() );
170 return;
173 if( S_ISDIR(statbuf.st_mode ) == 0 )
174 return;
176 if( (dir = opendir( sFullpathext.getStr() ) ) == NULL )
178 printf("read error 2 in %s \n",sFullpathext.getStr());
179 return;
182 dirholder aHolder(dir);
184 const rtl::OString sDot ( "." ) ;
185 const rtl::OString sDDot( ".." );
187 if ( chdir( sFullpathext.getStr() ) == -1 )
189 printf("chdir error in %s \n",sFullpathext.getStr());
190 return;
193 sFullpathext += rtl::OString( "/" );
195 while( ( dirp = readdir( dir ) ) != NULL )
197 rtl::OString sEntryName( dirp->d_name );
199 if( sEntryName.equals( sDot ) || sEntryName.equals( sDDot ) )
200 continue;
202 // add dir entry
203 rtl::OString sEntity = sFullpathext;
204 sEntity += sEntryName;
206 // stat new entry
207 if( lstat( sEntity.getStr() , &statbuf2 ) < 0 )
209 printf("error on entry %s\n" , sEntity.getStr() ) ;
210 continue;
213 // add file / dir to vector
214 switch( statbuf2.st_mode & S_IFMT )
216 case S_IFREG:
218 transex::File aFile( rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
219 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
222 aFileVec.push_back( aFile ) ;
223 break;
225 case S_IFLNK:
226 case S_IFDIR:
228 transex::Directory aDir(
229 rtl::OStringToOUString( sEntity , RTL_TEXTENCODING_UTF8 , sEntity.getLength() ) ,
230 rtl::OStringToOUString( sEntryName , RTL_TEXTENCODING_UTF8 , sEntryName.getLength() )
232 aDirVec.push_back( aDir ) ;
233 break;
237 if ( chdir( ".." ) == -1 )
239 printf("chdir error in .. \n");
240 return;
243 if ( aHolder.close() < 0 )
244 return;
246 std::sort( aFileVec.begin() , aFileVec.end() , File::lessFile );
247 std::sort( aDirVec.begin() , aDirVec.end() , Directory::lessDir );
251 #endif
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */