comparison extern/vera/src/plugins/Profiles.cpp @ 548:a7c0eb100760

CMake: import vera++ 1.3.0, closes #729
author David Demelier <markand@malikania.fr>
date Wed, 22 Nov 2017 20:10:03 +0100
parents
children
comparison
equal deleted inserted replaced
547:a95954e53589 548:a7c0eb100760
1 //
2 // Copyright (C) 2006-2007 Maciej Sobczak
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7
8 #include "Profiles.h"
9 #include "RootDirectory.h"
10 #include "tcl/cpptcl-1.1.4/cpptcl.h"
11 #include <set>
12 #include <map>
13 #include <fstream>
14 #include <sstream>
15 #include <algorithm>
16 #include <cstring>
17 #include <cerrno>
18
19
20 namespace Vera
21 {
22 namespace Plugins
23 {
24
25
26 Profiles::RuleNameCollection Profiles::getListOfScriptNamesTcl(
27 const Vera::Plugins::Profiles::ProfileName & profile)
28 {
29 RuleNameCollection allRules;
30
31 // name of the profile is also the name of the profile file
32
33 const Vera::Plugins::RootDirectory::DirectoryName veraRoot =
34 Vera::Plugins::RootDirectory::getRootDirectory();
35
36 std::string fileName(veraRoot + "/profiles/");
37 fileName += profile;
38
39 std::ifstream profileFile(fileName.c_str());
40 if (profileFile.is_open() == false)
41 {
42 std::ostringstream ss;
43 ss << "Cannot open profile description for profile '" << profile
44 << "': "<< strerror(errno);
45 throw Vera::Plugins::ProfileError(ss.str());
46 }
47
48 Tcl::interpreter interp;
49 interp.eval(profileFile);
50 if (profileFile.bad())
51 {
52 throw std::runtime_error(
53 "Cannot read from " + fileName + ": " + strerror(errno));
54 }
55
56 const Tcl::object ruleList = interp.eval("set rules");
57
58 const size_t ruleListLength = ruleList.length(interp);
59 for (size_t i = 0; i != ruleListLength; ++i)
60 {
61 const Vera::Plugins::Rules::RuleName rName = ruleList.at(interp, i).get();
62 allRules.push_back(rName);
63 }
64
65 return allRules;
66 }
67
68 Profiles::RuleNameCollection Profiles::getListOfScriptNamesKeys(
69 const Vera::Plugins::Profiles::ProfileName & profile)
70 {
71 RuleNameCollection allRules;
72
73 // name of the profile is also the name of the profile file
74
75 const Vera::Plugins::RootDirectory::DirectoryName veraRoot =
76 Vera::Plugins::RootDirectory::getRootDirectory();
77
78 std::string fileName(veraRoot + "/profiles/");
79 fileName += profile;
80
81 std::ifstream profileFile(fileName.c_str());
82 if (profileFile.is_open() == false)
83 {
84 std::ostringstream ss;
85 ss << "Cannot open profile description for profile '" << profile
86 << "': "<< strerror(errno);
87 throw Vera::Plugins::ProfileError(ss.str());
88 }
89
90 std::string line;
91 while (getline(profileFile, line))
92 {
93 if (line.empty() == false && line[0] != '#')
94 {
95 std::string::size_type pos = line.find("=");
96 if (pos != std::string::npos)
97 {
98 std::string name = line.substr(0, pos);
99 std::string value = line.substr(pos + 1);
100
101 if (name == "rule")
102 {
103 allRules.push_back(value);
104 }
105 }
106 }
107 }
108
109 if (profileFile.bad())
110 {
111 throw std::runtime_error(
112 "Cannot read from " + fileName + ": " + strerror(errno));
113 }
114 profileFile.close();
115
116 return allRules;
117 }
118
119 Profiles::RuleNameCollection Profiles::getListOfScriptNames(
120 const Vera::Plugins::Profiles::ProfileName & profile)
121 {
122 try
123 {
124 return getListOfScriptNamesTcl(profile);
125 }
126 catch (...)
127 {
128 return getListOfScriptNamesKeys(profile);
129 }
130 }
131
132 void Profiles::executeProfile(const ProfileName & profile)
133 {
134 const RuleNameCollection scripts = getListOfScriptNames(profile);
135
136 typedef RuleNameCollection::const_iterator iterator;
137 const iterator end = scripts.end();
138 for (iterator it = scripts.begin(); it != end; ++it)
139 {
140 Rules::executeRule(*it);
141 }
142 }
143
144 }
145 }