1
2 /* Web Polygraph http://www.web-polygraph.org/
3 * Copyright 2003-2011 The Measurement Factory
4 * Licensed under the Apache License, Version 2.0 */
5
6 #include "base/polygraph.h"
7
8 #include "xstd/h/string.h"
9 #include "xstd/h/sstream.h"
10
11 #include "xstd/gadgets.h"
12 #include "xml/XmlAttr.h"
13
14
Int(const String & name,int val)15 XmlAttr XmlAttr::Int(const String &name, int val) {
16 char buf[64];
17 ofixedstream os(buf, sizeof(buf));
18 os << val << ends;
19 return XmlAttr(name, buf);
20 }
21
22
Double(const String & name,double val)23 XmlAttr XmlAttr::Double(const String &name, double val) {
24 char buf[64];
25 ofixedstream os(buf, sizeof(buf));
26 configureStream(os, 2);
27 os << val << ends;
28 return XmlAttr(name, buf);
29 }
30
XmlAttr()31 XmlAttr::XmlAttr(): theNode(0) {
32 }
33
XmlAttr(const XmlAttr & a)34 XmlAttr::XmlAttr(const XmlAttr &a): theNode(0), theName(a.theName), theValue(a.theValue) {
35 }
36
XmlAttr(const String & aName,const String & aVal)37 XmlAttr::XmlAttr(const String &aName, const String &aVal):
38 theNode(0), theName(aName), theValue(aVal) {
39 }
40
~XmlAttr()41 XmlAttr::~XmlAttr() {
42 Assert(!theNode);
43 }
44
node(XmlNode * aNode)45 void XmlAttr::node(XmlNode *aNode) {
46 Assert(!theNode || !aNode);
47 theNode = aNode;
48 }
49
name(const String & aName)50 void XmlAttr::name(const String &aName) {
51 theName = aName;
52 }
53
value(const String & aVal)54 void XmlAttr::value(const String &aVal) {
55 theValue = aVal;
56 }
57
print(ostream & os,const String & pfx) const58 ostream &XmlAttr::print(ostream &os, const String &pfx) const {
59 os << pfx << theName << '=';
60 os << '"';
61
62 // escape double quotes
63 const char *p = theValue.cstr();
64 while (const char *next = strchr(p, '"')) {
65 if (next > p)
66 os.write(p, next - p);
67 os << """;
68 p = next + 1;
69 }
70 if (*p)
71 os << p;
72
73 os << '"';
74 return os;
75 }
76