1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
#include "../Common/Common.h"
#include <cstring>
#include "XMLUtil.h"
#include <sys/types.h>
#include <sys/stat.h>
// Track memory leaks on Windows to the line that new'd the memory
#ifdef _WIN32
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif
bool XMLUtil::IsWhiteSpace(char cLetter)
{
if ((cLetter == ' ') ||
(cLetter == '\n') ||
(cLetter == '\r') ||
(cLetter == '\t'))
return true;
return false;
}
// See if a character is 0 - 9
bool XMLUtil::IsDigit(char cLetter)
{
if ((cLetter >= '0') && (cLetter <= '9'))
return true;
return false;
}
// Strip the leading and trailing white space off a string.
string XMLUtil::StripWhiteSpace(const string& strText)
{
string strResult = "";
strResult.reserve(strText.length());
int iStart = 0;
while ((iStart < (int) strText.length()) && (IsWhiteSpace(strText[iStart])))
iStart++;
int iEnd = strText.length() - 1;
while ((iEnd > 0) && (IsWhiteSpace(strText[iEnd])))
iEnd--;
strResult = strText.substr(iStart, iEnd - iStart + 1);
return strResult;
}
// Return a string containing the contents of a file
string XMLUtil::LoadFile(const string& strFilename, unsigned int iSizeHint)
{
string strResult = "";
char szBuffer[XML_UTIL_READ_BUFFER_SIZE];
FILE* fp = NULL;
fp = fopen(strFilename.c_str(), "r");
if (fp != NULL)
{
#ifdef _WIN32
struct __stat64 buf;
int result;
result = _stat64(strFilename.c_str(), &buf);
strResult.reserve((unsigned long) buf.st_size + 256);
#else
// On unix, we default to 128,000 bytes or whatever the caller passed in as a hint
strResult.reserve(iSizeHint);
#endif
while (!feof(fp))
{
memset(szBuffer, 0, XML_UTIL_READ_BUFFER_SIZE);
fread(szBuffer, 1, XML_UTIL_READ_BUFFER_SIZE - 1, fp);
strResult += szBuffer;
}
fclose(fp);
fp = NULL;
}
return strResult;
}
// Returns what is between the given tag in the passed XML. We only return the first matching
// tag if there are multiple in the XML. Tags are case sensitive.
string XMLUtil::GetElementString(const string& strTag, const string& strXML, bool bStripWhiteSpace)
{
string strResult = "";
string strStart = "";
string strEnd = "";
strStart += "<";
strStart += strTag;
strStart += ">";
strEnd += "</";
strEnd += strTag;
strEnd += ">";
int iPosStart = strXML.find(strStart);
int iPosEnd = strXML.find(strEnd);
if ((iPosStart != -1) && (iPosEnd != -1))
{
strResult = strXML.substr(iPosStart + strStart.length(), iPosEnd - (iPosStart + strStart.length()));
}
if (bStripWhiteSpace)
strResult = StripWhiteSpace(strResult);
return strResult;
}
// Return the integer representing an element
int XMLUtil::GetElementInt(const string& strTag, const string& strXML, bool* pFound)
{
string strElement = GetElementString(strTag, strXML);
unsigned int i = 0;
for (i = 0; i < strElement.size(); i++)
{
if (i == 0)
{
if ((!IsDigit(strElement[i])) && ((strElement[i] != '-')))
break;
}
else
if (!IsDigit(strElement[i]))
break;
}
// Only try and convert something that is all digits
if (i == strElement.size())
{
if (pFound != NULL)
*pFound = true;
return atoi(strElement.c_str());
}
else
{
if (pFound != NULL)
*pFound = false;
return -1;
}
}
int64 XMLUtil::GetElementLongLong(const string& strTag, const string& strXML, bool* pFound)
{
string strElement = GetElementString(strTag, strXML);
unsigned int i = 0;
for (i = 0; i < strElement.size(); i++)
{
if (i == 0)
{
if ((!IsDigit(strElement[i])) && ((strElement[i] != '-')))
break;
}
else
if (!IsDigit(strElement[i]))
break;
}
// Only try and convert something that is all digits
if ((i > 0) && (i == strElement.size()))
{
if (pFound != NULL)
*pFound = true;
#ifdef _WIN32
return _atoi64(strElement.c_str());
#else
return atoll(strElement.c_str());
#endif
}
else
{
if (pFound != NULL)
*pFound = false;
return -1;
}
}
// Optionally can pass back a bool that tell us if the tag was found
float XMLUtil::GetElementFloat(const string& strTag, const string& strXML, bool* pFound)
{
string strElement = GetElementString(strTag, strXML);
bool bFoundDot = false;
unsigned int i = 0;
for (i = 0; i < strElement.size(); i++)
{
if (i == 0)
{
if ((!IsDigit(strElement[i])) && ((strElement[i] != '-')))
break;
}
else
{
if (!IsDigit(strElement[i]))
{
if ((strElement[i] == '.') && (!bFoundDot))
bFoundDot = true;
else
break;
}
}
}
// Only try and convert something that is all digits
if ((i > 0) && (i == strElement.size()))
{
if (pFound != NULL)
*pFound = true;
return (float) atof(strElement.c_str());
}
else
{
if (pFound != NULL)
*pFound = false;
return (float) 0.0;
}
}
// Return a vector containing all the text inside all tags matching the passed one
VECTOR_STRING XMLUtil::GetElementStrings(const string& strTag, const string& strXML, bool bStripWhiteSpace)
{
VECTOR_STRING vResult;
vResult.reserve(XML_UTIL_DEFAULT_VECTOR_SIZE);
string strStart = "";
string strEnd = "";
string strResult = "";
strStart += "<";
strStart += strTag;
strStart += ">";
strEnd += "</";
strEnd += strTag;
strEnd += ">";
unsigned int iPosStart = strXML.find(strStart);
unsigned int iPosEnd = strXML.find(strEnd);
while ((iPosStart != string::npos) && (iPosEnd != string::npos))
{
// We want to be able to handle having the same tag emedded in itself.
// So between the start tag and the first instance of the end tag,
// we'll count any other instances of the start tag. If we find some
// then we require that we continue until we get that number more of
// close tags.
unsigned int iCurrentStart = iPosStart + strStart.length();
unsigned int iEmbedCount = 0;
while ((iCurrentStart != string::npos) && (iCurrentStart < iPosEnd))
{
iCurrentStart = strXML.find(strStart, iCurrentStart);
if ((iCurrentStart != string::npos) && (iCurrentStart < iPosEnd))
{
iEmbedCount++;
iCurrentStart += strStart.length();
}
}
// Now look for end tag to balance the start tags
for (unsigned int i = 0; i < iEmbedCount; i++)
{
iPosEnd = strXML.find(strEnd, iPosEnd + strEnd.length());
// Check to make sure we're still matching tags
if (iPosEnd == string::npos)
break;
}
strResult = strXML.substr(iPosStart + strStart.length(), iPosEnd - (iPosStart + strStart.length()));
if (bStripWhiteSpace)
strResult = StripWhiteSpace(strResult);
iPosStart = strXML.find(strStart, iPosEnd + strEnd.length());
if (iPosStart != string::npos)
iPosEnd = strXML.find(strEnd, iPosStart);
vResult.push_back(strResult);
}
return vResult;
}
VECTOR_NAME_VALUE_PAIR XMLUtil::GetNameValuePairs(const string& strXML, bool bStripWhiteSpace)
{
VECTOR_NAME_VALUE_PAIR vResult;
vResult.reserve(XML_UTIL_DEFAULT_VECTOR_SIZE);
bool bInStartTag = false;
string strName = "";
string strValue = "";
unsigned int i = 0;
while (i < strXML.length())
{
if ((!bInStartTag) && (strXML[i] == '<'))
{
// Starting a new tag
bInStartTag = true;
}
else if (bInStartTag)
{
if (strXML[i] == '>')
{
// Hit the end of the start tag, get everything
// until we find the end tag.
string strFind = "</";
strFind += strName;
strFind += ">";
int iPos = -1;
iPos = strXML.find(strFind, i);
if (iPos != -1)
{
strValue = strXML.substr(i + 1, iPos - i - 1);
NameValuePair sPair;
sPair.strName = strName;
sPair.strValue = strValue;
vResult.push_back(sPair);
i = iPos + strFind.length();
}
else
break;
bInStartTag = false;
strName = "";
strValue = "";
}
else
strName += strXML[i];
}
i++;
}
return vResult;
}
|