Logo ROOT   6.18/05
Reference Guide
RStyleReader.cxx
Go to the documentation of this file.
1/// \file RDrawingOptsReader.cxx
2/// \ingroup Gpad ROOT7
3/// \author Axel Naumann <axel@cern.ch>
4/// \date 2017-09-26
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#include "RStyleReader.hxx" // in src/
17
18#include <ROOT/RColor.hxx>
19#include <ROOT/RLogger.hxx>
20
21#include <TROOT.h>
22#include <TSystem.h>
23
24#include <fstream>
25#include <string>
26#include <cctype>
27
28using namespace ROOT::Experimental;
29using namespace ROOT::Experimental::Internal;
30
31void RStyleReader::ReadDefaults()
32{
33 RStyleReader reader(fAttrs);
34 reader.AddFromStyleFile(std::string(TROOT::GetEtcDir().Data()) + "/system.rootstylerc");
35 reader.AddFromStyleFile(gSystem->GetHomeDirectory() + "/.rootstylerc");
36 reader.AddFromStyleFile(".rootstylerc");
37}
38
39namespace {
40 /// Possibly remove trailing comment starting with '#'
41 void RemoveComment(std::string& line)
42 {
43 auto posComment = line.find('#');
44 if (posComment != std::string::npos)
45 line.erase(posComment - 1);
46 }
47
48 /// Create a view on the key and one on the value of the line.
49 std::array<std::string_view, 2> SeparateKeyValue(const std::string& line)
50 {
51 std::string::size_type posColon = line.find(':');
52 if (posColon == std::string::npos)
53 return {};
54 std::array<std::string_view, 2> ret{{line, line}};
55 ret[0] = ret[0].substr(0, posColon);
56 ret[1] = ret[1].substr(posColon + 1);
57 return ret;
58 }
59
60 /// Remove leading an trailing whitespace from string_view.
61 std::string_view TrimWhitespace(std::string_view view)
62 {
63 std::string_view ret(view);
64 auto begin = ret.begin();
65 for (auto end = ret.end(); begin != end; ++begin) {
66 if (!std::isspace(*begin))
67 break;
68 }
69 ret.remove_prefix(begin - ret.begin());
70
71 auto rbegin = ret.rbegin();
72 for (auto rend = ret.rend(); rbegin != rend; ++rbegin) {
73 if (!std::isspace(*rbegin))
74 break;
75 }
76 ret.remove_suffix(rbegin - ret.rbegin());
77 return ret;
78 }
79}
80
81bool RStyleReader::AddFromStyleFile(const std::string &filename)
82{
83 std::ifstream in(filename);
84 if (!in)
85 return false;
86 std::string styleName;
87 std::string line;
88 long long lineNo = 0;
89 while (std::getline(in, line)) {
90 ++lineNo;
91 RemoveComment(line);
92 auto noSpaceLine = TrimWhitespace(line);
93 if (noSpaceLine.compare(0, 1, "[") == 0 && noSpaceLine.compare(noSpaceLine.length() - 1, 1, "]")) {
94 // A section introducing a style name.
95 noSpaceLine.remove_prefix(1); // [
96 noSpaceLine.remove_suffix(1); // ]
97 noSpaceLine = TrimWhitespace(noSpaceLine);
98 styleName = std::string(noSpaceLine);
99 continue;
100 }
101
102 auto keyValue = SeparateKeyValue(line);
103 for (auto& kv: keyValue)
104 kv = TrimWhitespace(kv);
105 if (keyValue[0].empty()) {
106 R__ERROR_HERE("GPrimitive") << "Syntax error in style file " << filename << ":" << lineNo << ": "
107 << "missing key in line \n" << line << "\nInoring line.";
108 continue;
109 }
110
111 fAttrs[styleName].GetAttribute(std::string(keyValue[0])) = std::string(keyValue[1]);
112 }
113 return true;
114}
#define R__ERROR_HERE(GROUP)
Definition: RLogger.hxx:183
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
AllStyles_t & fAttrs
Collection of attributes to read into.
bool AddFromStyleFile(const std::string &filename)
Adds attributes specified in filename to those already existing in fAttrs.
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
Definition: TROOT.cxx:3028
virtual std::string GetHomeDirectory(const char *userName=0) const
Return the user's home directory.
Definition: TSystem.cxx:902
TLine * line
basic_string_view< char > string_view