| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsXMLReader.cxx |
| 5 |
|
| 6 |
Copyright (c) Kitware, Inc. All rights reserved. |
| 7 |
See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
| 8 |
|
| 9 |
This software is distributed WITHOUT ANY WARRANTY; without even |
| 10 |
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the above copyright notices for more information. |
| 12 |
|
| 13 |
=========================================================================*/ |
| 14 |
#include "kwsXMLReader.h" |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
namespace kws{ |
| 18 |
|
| 19 |
XMLReader::XMLReader() |
| 20 |
{ |
| 21 |
m_CurrentPos = 0; |
| 22 |
} |
| 23 |
|
| 24 |
XMLReader::~XMLReader() = default; |
| 25 |
|
| 26 |
bool XMLReader::Open(const char* filename) |
| 27 |
{ |
| 28 |
// Open file for reading |
| 29 |
m_File.open(filename,std::ifstream::binary); |
| 30 |
if ( m_File.fail() ) |
| 31 |
{ |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
m_File.seekg(0,std::ios::end); |
| 36 |
unsigned long fileSize = static_cast<unsigned long>(m_File.tellg()); |
| 37 |
m_File.seekg(0,std::ios::beg); |
| 38 |
|
| 39 |
char* buf = new char[fileSize+1]; |
| 40 |
m_File.read(buf,fileSize); |
| 41 |
buf[fileSize] = 0; |
| 42 |
m_Buffer = buf; |
| 43 |
m_Buffer.resize(fileSize); |
| 44 |
delete [] buf; |
| 45 |
|
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
std::string XMLReader::GetValue() |
| 50 |
{ |
| 51 |
return m_Value; |
| 52 |
} |
| 53 |
|
| 54 |
std::string XMLReader::GetTag() |
| 55 |
{ |
| 56 |
auto begin_tag_start = |
| 57 |
static_cast<long int>(m_Buffer.find("<", m_CurrentPos)); |
| 58 |
if(begin_tag_start == -1) |
| 59 |
{ |
| 60 |
m_CurrentPos++; |
| 61 |
m_Value = ""; |
| 62 |
return ""; |
| 63 |
} |
| 64 |
|
| 65 |
auto comment_start = |
| 66 |
static_cast<long int>(m_Buffer.find("<?", m_CurrentPos)); |
| 67 |
if (begin_tag_start == comment_start) { |
| 68 |
m_CurrentPos += 2; |
| 69 |
return "NA"; |
| 70 |
} |
| 71 |
comment_start = static_cast<long int>(m_Buffer.find("</",m_CurrentPos)); |
| 72 |
if(begin_tag_start == comment_start) |
| 73 |
{ |
| 74 |
m_CurrentPos+=2; |
| 75 |
return "NA"; |
| 76 |
} |
| 77 |
|
| 78 |
comment_start = static_cast<long int>(m_Buffer.find("<!--",m_CurrentPos)); |
| 79 |
if(begin_tag_start == comment_start) |
| 80 |
{ |
| 81 |
auto comment_end = m_Buffer.find("-->", comment_start); |
| 82 |
if(comment_end != std::string::npos) |
| 83 |
{ |
| 84 |
m_CurrentPos=comment_end+3; |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
std::cout << "ERROR: Cannot find closing comments in configuration file" << std::endl; |
| 89 |
m_CurrentPos+=2; |
| 90 |
} |
| 91 |
return "NA"; |
| 92 |
} |
| 93 |
|
| 94 |
auto begin_tag_end = |
| 95 |
static_cast<long int>(m_Buffer.find(">", begin_tag_start + 1)); |
| 96 |
m_Tag = m_Buffer.substr(begin_tag_start + 1, |
| 97 |
begin_tag_end - begin_tag_start - 1); |
| 98 |
|
| 99 |
if (!strcmp(m_Tag.c_str(), "Description")) { |
| 100 |
m_CurrentPos++; |
| 101 |
return "Description"; |
| 102 |
} |
| 103 |
|
| 104 |
std::string m_EndTag = "</"; |
| 105 |
m_EndTag += m_Tag; |
| 106 |
auto end_tag_begin = m_Buffer.find(m_EndTag, begin_tag_end + 1); |
| 107 |
if(end_tag_begin == std::string::npos) |
| 108 |
{ |
| 109 |
std::cout << "XML parsing error, cannot find close tag for " |
| 110 |
<< m_Tag.c_str() << std::endl; |
| 111 |
return ""; |
| 112 |
} |
| 113 |
|
| 114 |
m_CurrentPos = end_tag_begin+1; |
| 115 |
|
| 116 |
std::string value = m_Buffer.substr(begin_tag_end+1,end_tag_begin-begin_tag_end-1); |
| 117 |
m_Value = value; |
| 118 |
|
| 119 |
// If we have multiple tags inside the actually tag we return the values separated |
| 120 |
// by comas. This assume that the order of the tags is correct and present and |
| 121 |
// that we only have 1 level in the XML tree. |
| 122 |
unsigned int i=0; |
| 123 |
auto pos1 = value.find("<", 0); |
| 124 |
while(pos1 != std::string::npos) |
| 125 |
{ |
| 126 |
if(i>0) |
| 127 |
{ |
| 128 |
m_Value += ","; |
| 129 |
} |
| 130 |
auto pos2 = value.find(">", pos1); |
| 131 |
auto pos3 = value.find("<", pos2); |
| 132 |
|
| 133 |
if (i == 0) |
| 134 |
{ |
| 135 |
m_Value = value.substr(pos2 + 1, pos3 - pos2 - 1); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
m_Value += value.substr(pos2 + 1, pos3 - pos2 - 1); |
| 140 |
} |
| 141 |
|
| 142 |
pos1 = value.find("<",pos3 + 1); |
| 143 |
i++; |
| 144 |
} |
| 145 |
return m_Tag; |
| 146 |
} |
| 147 |
|
| 148 |
/** Check the file to see if a current tag is defined */ |
| 149 |
std::string XMLReader::GetValue(std::string tag) |
| 150 |
{ |
| 151 |
m_CurrentPos = 0; |
| 152 |
std::string tagt = this->GetTag(); |
| 153 |
while((tagt.length()>0) && (strcmp(tagt.c_str(),tag.c_str()))) |
| 154 |
{ |
| 155 |
tagt = this->GetTag(); |
| 156 |
} |
| 157 |
|
| 158 |
return m_Value; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
std::string XMLReader::GetCurrentTag() |
| 163 |
{ |
| 164 |
return m_Tag; |
| 165 |
} |
| 166 |
|
| 167 |
void XMLReader::Close() |
| 168 |
{ |
| 169 |
m_File.close(); |
| 170 |
} |
| 171 |
|
| 172 |
} // end namespace |