Skip to content

Commit

Permalink
Address leaks in XMLWriterClass
Browse files Browse the repository at this point in the history
Use std::shared_ptr to allow for deep copy constructors
  • Loading branch information
williamfgc committed Sep 30, 2021
1 parent 3ba75e9 commit 216d9dd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 83 deletions.
139 changes: 61 additions & 78 deletions src/QMCTools/ppconvert/src/XMLWriterClass2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
//
// File created by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
//////////////////////////////////////////////////////////////////////////////////////





#include "XMLWriterClass2.h"
Expand All @@ -21,27 +18,26 @@

XMLAttribute::XMLAttribute(std::string name, std::string content)
{
Name = name;
Name = name;
Content = content;
}

XMLAttribute::XMLAttribute (const XMLAttribute &attr)
{
Name = attr.Name;
Content = attr.Content;
XMLAttribute::XMLAttribute(const XMLAttribute& attr)
{
Name = attr.Name;
Content = attr.Content;
}


void
XMLAttribute::Write(std::string &out)
void XMLAttribute::Write(std::string& out)
{
std::stringstream str;
str << Name << "=\"" << Content << "\"";
out = str.str();
}


XMLElement::XMLElement(const XMLElement &elem)
XMLElement::XMLElement(const XMLElement& elem)
{
Attributes = elem.Attributes;
Children = elem.Children;
Expand All @@ -50,183 +46,170 @@ XMLElement::XMLElement(const XMLElement &elem)
Level = elem.Level;
}

void
XMLElement::Indent(std::ostream &out)
void XMLElement::Indent(std::ostream& out)
{
for (int i=0; i<Level; i++)
for (int i = 0; i < Level; i++)
out << " ";
}


void
XMLElement::Write(std::ostream &out)
void XMLElement::Write(std::ostream& out)
{
Indent(out);
out << "<" << Name;
int lineCount = Name.size()+1;
for (int i=0; i<Attributes.size(); i++) {
int lineCount = Name.size() + 1;
for (int i = 0; i < Attributes.size(); i++)
{
out << " ";
std::string str;
Attributes[i]->Write(str);
if (lineCount + str.size() + 2*Level > 73) {
if (lineCount + str.size() + 2 * Level > 73)
{
out << "\n";
Indent (out);
Indent(out);
out << " ";
lineCount = 1;
}
lineCount += str.size();
out << str;
}
int numLines = 0;
for (int i=0; i<Content.size(); i++)
for (int i = 0; i < Content.size(); i++)
numLines += (Content[i] == '\n');

if (Content == "" && Children.size()==0)
if (Content == "" && Children.size() == 0)
out << "/>\n";
else {
else
{
out << ">";
if (numLines > 0 || Children.size() > 0)
out << "\n";
out << Content;
for (int i=0; i<Children.size(); i++)
for (int i = 0; i < Children.size(); i++)
Children[i]->Write(out);
if (numLines > 0 || Children.size()>0)
if (numLines > 0 || Children.size() > 0)
Indent(out);
out << "</" << Name << ">\n";
}
}


void
XMLElement::AddContent(std::vector<double> &data)

void XMLElement::AddContent(std::vector<double>& data)
{
std::stringstream str;
str.setf(std::ios_base::scientific, std::ios_base::floatfield);
str << std::setprecision(14);
for (int i=0; i<data.size(); i++) {
if ((i%3 == 0))
for (int i = 0; i < data.size(); i++)
{
if ((i % 3 == 0))
Indent(str);
str.width(22);
str << data[i];
if ((i%3) == 2)
if ((i % 3) == 2)
str << "\n";
}
if ((data.size()%3) != 0)
str << "\n";
if ((data.size() % 3) != 0)
str << "\n";
Content += str.str();
}



bool
XMLWriterClass::StartDocument(std::string fname, std::string version,
std::string encoding, std::string standalone)
bool XMLWriterClass::StartDocument(std::string fname, std::string version, std::string encoding, std::string standalone)
{
Out.open (fname.c_str(),std::ofstream::out | std::ofstream::trunc);
Out.open(fname.c_str(), std::ofstream::out | std::ofstream::trunc);
if (!Out.is_open())
return false;
if (version == "")
version = "1.0";
if (encoding == "")
encoding = "UTF-8";

Out << "<?xml version=\"" << version
<< "\" encoding=\"" << encoding << "\"?>\n";

Out << "<?xml version=\"" << version << "\" encoding=\"" << encoding << "\"?>\n";

return true;
}

bool
XMLWriterClass::EndDocument()
bool XMLWriterClass::EndDocument()
{
if (Elements.size() <=0)
if (Elements.size() <= 0)
return false;
Elements[0]->Write(Out);
return true;
}

bool
XMLWriterClass::StartElement(std::string name)
bool XMLWriterClass::StartElement(std::string name)
{
int level = Elements.size();
XMLElement* elem = new XMLElement(name, level);

auto elem = std::make_shared<XMLElement>(name, level);
if (level > 0)
Elements.back()->AddElement (elem);
Elements.back()->AddElement(elem);
Elements.push_back(elem);
return true;
}

bool
XMLWriterClass::EndElement()
bool XMLWriterClass::EndElement()
{
if (Elements.size() > 1)
Elements.pop_back();
return true;
}

bool
XMLWriterClass::FullEndElement()
bool XMLWriterClass::FullEndElement()
{
Elements.pop_back();
return true;
}

bool
XMLWriterClass::WriteAttribute (std::string name, std::string content)
bool XMLWriterClass::WriteAttribute(std::string name, std::string content)
{
XMLAttribute *attr = new XMLAttribute (name, content);
Elements.back()->AddAttribute (attr);
auto attr = std::make_shared<XMLAttribute>(name, content);
Elements.back()->AddAttribute(attr);
return true;
}

bool
XMLWriterClass::WriteAttribute (std::string name, double val, bool scientific)
bool XMLWriterClass::WriteAttribute(std::string name, double val, bool scientific)
{
std::stringstream content;
if (scientific) {
if (scientific)
{
content.setf(std::ios_base::scientific, std::ios_base::floatfield);
content << std::setprecision(14);
}
content << val;
XMLAttribute *attr = new XMLAttribute (name, content.str());
Elements.back()->AddAttribute (attr);
auto attr = std::make_shared<XMLAttribute>(name, content.str());
Elements.back()->AddAttribute(attr);
return true;
}

bool
XMLWriterClass::WriteAttribute (std::string name, int val)
bool XMLWriterClass::WriteAttribute(std::string name, int val)
{
std::stringstream content;
content << val;
XMLAttribute *attr = new XMLAttribute (name, content.str());
Elements.back()->AddAttribute (attr);
auto attr = std::make_shared<XMLAttribute>(name, content.str());
Elements.back()->AddAttribute(attr);
return true;
}

bool
XMLWriterClass::WriteData(std::vector<double> data)
bool XMLWriterClass::WriteData(std::vector<double> data)
{
Elements.back()->AddContent (data);
Elements.back()->AddContent(data);
return true;
}

bool
XMLWriterClass::WriteData(std::string data)
bool XMLWriterClass::WriteData(std::string data)
{
Elements.back()->AddContent (data);
Elements.back()->AddContent(data);
return true;
}


bool
XMLWriterClass::WriteElement (std::string name, std::vector<double> data)
bool XMLWriterClass::WriteElement(std::string name, std::vector<double> data)
{
int level = Elements.size();
XMLElement *elem = new XMLElement (name, level);
elem->AddContent (data);
auto elem = std::make_shared<XMLElement>(name, level);
elem->AddContent(data);
if (level > 0)
Elements.back()->AddElement (elem);
Elements.back()->AddElement(elem);
return true;
}

11 changes: 6 additions & 5 deletions src/QMCTools/ppconvert/src/XMLWriterClass2.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory> // std::shared_ptr


class XMLAttribute
Expand All @@ -38,8 +39,8 @@ class XMLAttribute

class XMLElement
{
std::vector<XMLAttribute*> Attributes;
std::vector<XMLElement*> Children;
std::vector<std::shared_ptr<XMLAttribute>> Attributes;
std::vector<std::shared_ptr<XMLElement>> Children;
std::string Name, Content;
int Level;
void Indent(std::ostream& out);
Expand All @@ -49,9 +50,9 @@ class XMLElement

inline int GetLevel() { return Level; }

void AddElement(XMLElement* elem) { Children.push_back(elem); }
void AddElement(std::shared_ptr<XMLElement>& elem) { Children.push_back(elem); }

void AddAttribute(XMLAttribute* attr) { Attributes.push_back(attr); }
void AddAttribute(std::shared_ptr<XMLAttribute>& attr) { Attributes.push_back(attr); }

void AddContent(std::string content) { Content += content; }

Expand All @@ -70,7 +71,7 @@ class XMLElement
class XMLWriterClass
{
private:
std::vector<XMLElement*> Elements;
std::vector<std::shared_ptr<XMLElement>> Elements;
void Write();
std::ofstream Out;

Expand Down

0 comments on commit 216d9dd

Please sign in to comment.