Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
dotattributes.h
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2022 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#ifndef DOTATTRIBUTES_H
17
#define DOTATTRIBUTES_H
18
19
#include <map>
20
#include <string>
21
22
#include "
regex.h
"
23
#include "
dstring.h
"
24
25
//! Class representing an attribute list of a dot graph object.
26
class
DotAttributes
27
{
28
public
:
29
//! Creates an instance of a DotAttribute list given its initial string representation
30
DotAttributes
(
const
DString
&input) :
m_input
(input) {}
31
32
//! Return the string representation of the attribute list
33
DString
str
()
const
{
return
m_input
; }
34
35
//! update a given attribute with a new value.
36
//! If the attribute is not found a new attribute will be appended.
37
void
updateValue
(
const
DString
&key,
const
DString
&inpValue)
38
{
39
// look for key\s*=
40
const
std::string regStr = key.
str
()+R
"(\s*=)";
41
const
reg::Ex
re { regStr };
42
reg::Match
match;
43
std::string s =
m_input
.
str
();
44
if
(
reg::search
(s,match,re))
// replace existing attribute
45
{
46
size_t
len = s.length();
47
size_t
startPos = match.position()+match.length();
// position after =
48
size_t
pos = startPos;
49
while
(pos<len &&
disspace
(s[pos])) pos++;
50
if
(pos<len && s[pos]==
'"'
)
// quoted value, search for end quote, ignoring escaped quotes
51
{
52
char
pc=s[pos];
53
pos++;
// skip over start quote
54
while
(pos<len && (s[pos]!=
'"'
|| (s[pos]==
'"'
&& pc==
'\\'
))) pc=s[pos++];
55
if
(pos<len) pos++;
// skip over end quote
56
}
57
else
// unquoted value, search for attribute separator (space,comma, or semicolon)
58
{
59
while
(pos<len && s[pos]!=
','
&& s[pos]!=
';'
&& !
disspace
(s[pos])) pos++;
60
}
61
DString
value;
62
if
(inpValue.
empty
())
63
{
64
value =
m_input
.
mid
(startPos,pos-startPos);
65
}
66
else
67
{
68
value = inpValue;
69
}
70
// pos is now the position after the value, so replace the part between [start..pos) with the new value
71
m_input
=
m_input
.
left
(startPos)+value.
quoted
()+
m_input
.
mid
(pos);
72
}
73
else
// append new attribute
74
{
75
if
(!inpValue.
empty
())
76
{
77
if
(!
m_input
.
empty
())
m_input
+=
","
;
78
m_input
+=key+
"="
+inpValue.
quoted
();
79
}
80
}
81
}
82
83
private
:
84
DString
m_input
;
85
};
86
87
#endif
// DOTATTRIBUTES_H
DString
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition
dstring.h:89
DString::mid
DString mid(size_t index, size_t len=npos) const
Definition
dstring.h:323
DString::empty
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition
dstring.h:153
DString::quoted
DString quoted() const
Definition
dstring.h:357
DString::left
DString left(size_t len) const
Definition
dstring.h:311
DString::str
const std::string & str() const
Definition
dstring.h:634
DotAttributes::m_input
DString m_input
Definition
dotattributes.h:84
DotAttributes::DotAttributes
DotAttributes(const DString &input)
Creates an instance of a DotAttribute list given its initial string representation.
Definition
dotattributes.h:30
DotAttributes::updateValue
void updateValue(const DString &key, const DString &inpValue)
Definition
dotattributes.h:37
DotAttributes::str
DString str() const
Return the string representation of the attribute list.
Definition
dotattributes.h:33
reg::Ex
Class representing a regular expression.
Definition
regex.h:39
reg::Match
Object representing the matching results.
Definition
regex.h:154
dstring.h
disspace
bool disspace(char c)
Definition
dstring.h:67
reg::search
bool search(std::string_view str, Match &match, const Ex &re, size_t pos)
Search in a given string str starting at position pos for a match against regular expression re.
Definition
regex.cpp:847
regex.h
src
dotattributes.h
Generated by
1.19.0