Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RStyleReader.cxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
3  * All rights reserved. *
4  * *
5  * For the licensing terms see $ROOTSYS/LICENSE. *
6  * For the list of contributors see $ROOTSYS/README/CREDITS. *
7  *************************************************************************/
8 
9 #include "RStyleReader.hxx" // in src/
10 
11 #include <ROOT/RColor.hxx>
12 #include <ROOT/RLogger.hxx>
13 
14 #include <TROOT.h>
15 #include <TSystem.h>
16 
17 #include <fstream>
18 #include <string>
19 #include <cctype>
20 
21 using namespace ROOT::Experimental;
22 using namespace ROOT::Experimental::Internal;
23 
24 /*
25 
26 void RStyleReader::ReadDefaults()
27 {
28  RStyleReader reader(fAttrs);
29  reader.AddFromStyleFile(std::string(TROOT::GetEtcDir().Data()) + "/system.rootstylerc");
30  reader.AddFromStyleFile(gSystem->GetHomeDirectory() + "/.rootstylerc");
31  reader.AddFromStyleFile(".rootstylerc");
32 }
33 
34 namespace {
35  /// Possibly remove trailing comment starting with '#'
36  void RemoveComment(std::string& line)
37  {
38  auto posComment = line.find('#');
39  if (posComment != std::string::npos)
40  line.erase(posComment - 1);
41  }
42 
43  /// Create a view on the key and one on the value of the line.
44  std::array<std::string_view, 2> SeparateKeyValue(const std::string& line)
45  {
46  std::string::size_type posColon = line.find(':');
47  if (posColon == std::string::npos)
48  return {};
49  std::array<std::string_view, 2> ret{{line, line}};
50  ret[0] = ret[0].substr(0, posColon);
51  ret[1] = ret[1].substr(posColon + 1);
52  return ret;
53  }
54 
55  /// Remove leading an trailing whitespace from string_view.
56  std::string_view TrimWhitespace(std::string_view view)
57  {
58  std::string_view ret(view);
59  auto begin = ret.begin();
60  for (auto end = ret.end(); begin != end; ++begin) {
61  if (!std::isspace(*begin))
62  break;
63  }
64  ret.remove_prefix(begin - ret.begin());
65 
66  auto rbegin = ret.rbegin();
67  for (auto rend = ret.rend(); rbegin != rend; ++rbegin) {
68  if (!std::isspace(*rbegin))
69  break;
70  }
71  ret.remove_suffix(rbegin - ret.rbegin());
72  return ret;
73  }
74 }
75 
76 bool RStyleReader::AddFromStyleFile(const std::string &filename)
77 {
78  std::ifstream in(filename);
79  if (!in)
80  return false;
81  std::string styleName;
82  std::string line;
83  long long lineNo = 0;
84  while (std::getline(in, line)) {
85  ++lineNo;
86  RemoveComment(line);
87  auto noSpaceLine = TrimWhitespace(line);
88  if (noSpaceLine.compare(0, 1, "[") == 0 && noSpaceLine.compare(noSpaceLine.length() - 1, 1, "]")) {
89  // A section introducing a style name.
90  noSpaceLine.remove_prefix(1); // [
91  noSpaceLine.remove_suffix(1); // ]
92  noSpaceLine = TrimWhitespace(noSpaceLine);
93  styleName = std::string(noSpaceLine);
94  continue;
95  }
96 
97  auto keyValue = SeparateKeyValue(line);
98  for (auto& kv: keyValue)
99  kv = TrimWhitespace(kv);
100  if (keyValue[0].empty()) {
101  R__ERROR_HERE("GPrimitive") << "Syntax error in style file " << filename << ":" << lineNo << ": "
102  << "missing key in line \n" << line << "\nInoring line.";
103  continue;
104  }
105 
106  fAttrs[styleName].GetAttribute(std::string(keyValue[0])) = std::string(keyValue[1]);
107  }
108  return true;
109 }
110 */