Logo ROOT  
Reference Guide
RPadLength.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2018, 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 "ROOT/RPadLength.hxx"
10
11#include "ROOT/RLogger.hxx"
12
13#include <string>
14
15///////////////////////////////////////////////////////////////////////
16/// Converts RPadLength to string like "0.1 + 25px"
17/// User coordinates not (yet) supported
18
20{
21 std::string res;
22
23 if (HasNormal()) {
24 double v = GetNormal();
25 if (v) res = std::to_string(v);
26 }
27
28 if (HasPixel()) {
29 double v = GetPixel();
30 if ((v > 0) && !res.empty())
31 res += " + ";
32
33 if ((v != 0) || res.empty()) {
34 res += std::to_string(v);
35 res += "px";
36 }
37 }
38
39 if (!Empty() && res.empty())
40 res = "0";
41
42 return res;
43}
44
45///////////////////////////////////////////////////////////////////////
46/// Parse string and fill RPadLength attributes
47/// String can be like "0.1 + 25px"
48/// User coordinates not (yet) supported
49
50bool ROOT::Experimental::RPadLength::ParseString(const std::string &value)
51{
52 Clear();
53
54 if (value.empty())
55 return true;
56
57 if (value == "0") {
58 SetNormal(0);
59 return true;
60 }
61
62 if (value == "0px") {
63 SetPixel(0);
64 return true;
65 }
66
67 std::size_t pos = 0;
68 std::string val = value;
69 int operand = 0;
70
71 while (!val.empty()) {
72 // skip empty spaces
73 while ((pos < val.length()) && ((val[pos] == ' ') || (val[pos] == '\t')))
74 ++pos;
75
76 if (pos >= val.length())
77 break;
78
79 if ((val[pos] == '-') || (val[pos] == '+')) {
80 if (operand) {
81 Clear();
82 R__ERROR_HERE("gpadv7") << "Fail to parse RPadLength " << value;
83 return false;
84 }
85 operand = (val[pos] == '-') ? -1 : 1;
86 pos++;
87 continue;
88 }
89
90 if (pos > 0) {
91 val.erase(0, pos);
92 pos = 0;
93 }
94
95 double v = 0;
96
97 try {
98 v = std::stod(val, &pos);
99 } catch (...) {
100 Clear();
101 return false;
102 }
103
104 val.erase(0, pos);
105 pos = 0;
106 if (!operand) operand = 1;
107
108 if ((val.length() > 0) && (val[0] == '%')) {
109 val.erase(0, 1);
110 SetNormal(GetNormal() + operand*0.01*v);
111 } else if ((val.length() > 1) && (val[0] == 'p') && (val[1] == 'x')) {
112 val.erase(0, 2);
113 SetPixel(GetPixel() + operand*v);
114 } else {
115 SetNormal(GetNormal() + operand*v);
116 }
117
118 operand = 0;
119 }
120
121 return true;
122}
#define R__ERROR_HERE(GROUP)
Definition: RLogger.hxx:183
static void GetPixel(int y, int width, Byte_t *scline)
Get pixels in line y and put in array scline.
Definition: TGWin32.cxx:4287
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
Definition: RPadLength.cxx:19
bool ParseString(const std::string &val)
Parse string and fill RPadLength attributes String can be like "0.1 + 25px" User coordinates not (yet...
Definition: RPadLength.cxx:50