1 /**************************************************************
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 *
20 *************************************************************/
21
22 #include <precomp.h>
23 #include <tools/filecoll.hxx>
24
25 // NOT FULLY DEFINED SERVICES
26 #include <cosv/ploc_dir.hxx>
27
28 #include <stdio.h>
29
FileCollector(uintt i_nRoughNrOfFiles)30 FileCollector::FileCollector( uintt i_nRoughNrOfFiles )
31 // : aFoundFiles
32 {
33 if (i_nRoughNrOfFiles > 0)
34 aFoundFiles.reserve(i_nRoughNrOfFiles);
35 }
36
37 uintt
AddFilesFrom(const char * i_sRootDir,const char * i_sFilter,E_SearchMode i_eSearchMode)38 FileCollector::AddFilesFrom( const char * i_sRootDir,
39 const char * i_sFilter,
40 E_SearchMode i_eSearchMode )
41 {
42 uintt nSizeAtStart = aFoundFiles.size();
43
44 if (csv::no_str(i_sFilter) OR csv::no_str(i_sRootDir))
45 {
46 Cout() << "Warning: The filter contains no files." << Endl();
47 return 0;
48 }
49
50 csv::ploc::Directory aDir(i_sRootDir);
51 if (NOT aDir.Exists())
52 {
53 Cerr() << "Warning: The path for the files to be parsed could not be found:\n"
54 << i_sRootDir
55 << Endl();
56 return 0;
57 }
58
59 Cout() << "." << Flush();
60 aDir.GetContainedFiles(aFoundFiles, i_sFilter);
61
62 if (i_eSearchMode == recursive)
63 {
64 StreamStr aPath(1020);
65 aPath << i_sRootDir << csv::ploc::Delimiter();
66 uintt nSubDirStart = aPath.tellp();
67
68 StringVector aSubDirs;
69 aDir.GetContainedDirectories(aSubDirs);
70
71 for ( const_iterator iter = aSubDirs.begin();
72 iter != aSubDirs.end();
73 ++iter )
74 {
75 aPath.seekp(nSubDirStart);
76 aPath << (*iter);
77 AddFilesFrom( aPath.c_str(), i_sFilter, i_eSearchMode );
78 }
79 }
80
81 return aFoundFiles.size() - nSizeAtStart;
82 }
83
84 uintt
AddFile(const char * i_sFilePath)85 FileCollector::AddFile( const char * i_sFilePath )
86 {
87 FILE * pFile = fopen( i_sFilePath, "r" );
88 if ( pFile == 0 )
89 {
90 Cerr() << "Warning: The path for the file to be parsed could not be found:\n"
91 << i_sFilePath
92 << Endl();
93 return 0;
94 }
95
96 fclose(pFile);
97 aFoundFiles.push_back(i_sFilePath);
98 return 1;
99 }
100
101 void
EraseAll()102 FileCollector::EraseAll()
103 {
104 csv::erase_container(aFoundFiles);
105 }
106
107 FileCollector::const_iterator
Begin() const108 FileCollector::Begin() const
109 {
110 return aFoundFiles.begin();
111 }
112
113 FileCollector::const_iterator
End() const114 FileCollector::End() const
115 {
116 return aFoundFiles.end();
117 }
118
119 uintt
Size() const120 FileCollector::Size() const
121 {
122 return aFoundFiles.size();
123 }
124
125 /* vim: set noet sw=4 ts=4: */
126