coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dataset.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
6  * *
7  * Redistribution and use in source and binary forms, with or without *
8  * modification, are permitted provided that the following conditions *
9  * are met: *
10  * *
11  * Redistributions of source code must retain the above copyright *
12  * notice, this list of conditions and the following disclaimer. *
13  * *
14  * Redistributions in binary form must reproduce the above copyright *
15  * notice, this list of conditions and the following disclaimer in the *
16  * documentation and/or other materials provided with the distribution. *
17  * *
18  * Neither the name of Mike Kinghan nor the names of its contributors *
19  * may be used to endorse or promote products derived from this software *
20  * without specific prior written permission. *
21  * *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
32  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
33  * DAMAGE. *
34  * *
35  **************************************************************************/
36 
37 #include "dataset.h"
38 #include "if_control.h"
39 #include "chew.h"
40 #include "io.h"
41 #include "diagnostic.h"
42 #include "line_despatch.h"
43 #include <iostream>
44 #include <algorithm>
45 
50 using namespace std;
51 
60 static void
61 line_debug(int line)
62 {
63  if (line < 0) {
64  /* Call with negative value to write line numbers*/
65  cout << line_despatch::cur_line().num() << '\n';
66  } else if (line > 0 && (unsigned)line == line_despatch::cur_line().num()) {
67  /* Call with positive line number and put breakpoint here ...*/
68  cerr << "Here\n";
69  }
70 }
71 
75 
76 dataset::selector::selector(string const & extensions)
77  : _files(0)
78 {
79  if (extensions.length()) {
80  size_t start = 0;
81  size_t comma = extensions.find_first_of(',');
82  for ( ; comma != string::npos;
83  comma = extensions.find_first_of(',',start = comma + 1)) {
84  _filter_extensions.push_back(
85  extensions.substr(start,comma - start));
86  }
87  _filter_extensions.push_back(extensions.substr(start));
88  }
89 }
90 
91 bool dataset::selector::operator()(string const & filename)
92 {
93  bool verdict = false;
94  if (!_filter_extensions.size()) {
95  verdict = true;
96  } else {
97  size_t leaf_off = filename.find_last_of(PATH_DELIM);
98  if (leaf_off == string::npos) {
99  leaf_off = 0;
100  }
101  string::const_reverse_iterator start = filename.rbegin();
102  string::const_reverse_iterator end(filename.begin() + leaf_off);
103  string::const_reverse_iterator extn_pos = std::find(start,end,'.');
104  if (extn_pos != end) {
105  string extension(extn_pos.base(),filename.end());
106  verdict = find(_filter_extensions.begin(),
107  _filter_extensions.end(),extension) !=
108  _filter_extensions.end();
109  }
110  }
111  if (verdict) {
112  _files += verdict;
114  "To do (" << _files << ") \"" << filename << '\"' << emit();
115  }
116  return verdict;
117 }
118 
119 void dataset::driver::at_file(string const & filename)
120 {
121  unsigned error = 0;
122  line_type lineval;
123  if_control::top();
124  progress_processing_file() << "Processing file (" <<
125  ++_done_files << ") \"" << filename << '\"' << emit();
126  io::open(filename);
127  try {
128  while ((lineval = line_despatch::next()) != LT_EOF) {
129  line_debug(0);
130  if (lineval == LT_DIRECTIVE_DROP) {
132  } else {
133  if_control::transition(lineval);
134  }
135  }
136  } catch(unsigned ex) {
137  error = ex;
138  ++_error_files;
139 
140  }
141  io::close(error);
142 }
143 
144 void dataset::add(string const & path)
145 {
146  _ftree_.add(path,_selector_);
147 }
148 
149 /* EOF*/
static parsed_line & cur_line()
Get a reference to the current output line.
Definition: line_despatch.h:68
progress_msg< 3 > progress_added_file
Report added a file to input.
Definition: diagnostic.h:578
static selector _selector_
The selector for including files in the dataset
Definition: dataset.h:154
selector(std::string const &extensions=std::string())
Construct from a filter string.
Definition: dataset.cpp:76
static void add(std::string const &path)
Add files to the dataset.
Definition: dataset.cpp:144
static void line_debug(int line)
Definition: dataset.cpp:61
static line_type next()
Process the current input line and return its line type.
End of file.
Definition: line_type.h:72
static void close(unsigned error)
Finalise the current pairing of source input and processed output, if any.
Definition: io.cpp:179
unsigned num() const
Get the greatest source line number spanned by this line.
Definition: parsed_line.h:70
Encapsulates a set of directory/file trees.
Definition: file_tree.h:55
progress_msg< 1 > progress_processing_file
Report file being processed.
Definition: diagnostic.h:574
bool operator()(std::string const &filename)
Say whether a file is selected for processing.
Definition: dataset.cpp:91
static driver _driver_
The driver for traversing dataset
Definition: dataset.h:156
void add(std::string const &path, Filter &filter)
Add files from a path to the file_tree.
Definition: file_tree.h:357
#define PATH_DELIM
defined(_WIN32) && defined(_WIN64)
Definition: platform.h:78
A directive line of no more specific type than that is to be dropped.
Definition: line_type.h:74
Encapsulates the selection of files for processing.
Definition: dataset.h:57
The tag class is inserted in a diagnostic_base to tell it to emit itself.
Definition: diagnostic.h:77
std::vector< std::string > _filter_extensions
A vector of the file extensions that are to be selected.
Definition: dataset.h:79
static void drop()
Drop the current output line.
Definition: line_despatch.h:73
void at_file(std::string const &filename)
Process a file in the dataset.
Definition: dataset.cpp:119
static void top()
Reset the depth of #if-nesting to 0.
Definition: if_control.h:128
An error diagnostic.
struct driver encapsulates traversal of an input dataset to select and process files.
Definition: dataset.h:87
Encapsulates a filesystem path.
Definition: path.h:59
line_type
Enumeration of types of input lines.
Definition: line_type.h:52
static void open(std::string const &fname)
Open an input file and the appropriate output file.
Definition: io.cpp:202
static file_tree _ftree_
The tree of input files.
Definition: dataset.h:158
static void transition(line_type linetype)
Transition the if-control state given an evaluated line type.
Definition: if_control.cpp:310