Skip to content

Commit

Permalink
xml.lite changes from coda-oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 9, 2021
1 parent 169df75 commit e208dd4
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <vector>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <numeric> // std::accumulate
#include <memory>

Expand Down
7 changes: 1 addition & 6 deletions externals/coda-oss/modules/c++/sys/include/sys/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ struct File
* \param accessFlags File access flags
* \param creationFlags File creation flags
*/
File(const sys::Filesystem::path& str, int accessFlags = READ_ONLY,
int creationFlags = EXISTING)
{
create(str, accessFlags, creationFlags);
}
explicit File(const std::string& str, int accessFlags = READ_ONLY,
int creationFlags = EXISTING)
{
Expand Down Expand Up @@ -166,7 +161,7 @@ struct File
* \param accessFlags File access flags
* \param creationFlags File creation flags
*/
void create(const sys::Filesystem::path& str, int accessFlags,
void create(const std::string& str, int accessFlags,
int creationFlags);

/*!
Expand Down
4 changes: 1 addition & 3 deletions externals/coda-oss/modules/c++/sys/source/FileUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
#include <string.h>
#include <errno.h>

void sys::File::create(const sys::Filesystem::path& str_, int accessFlags,
void sys::File::create(const std::string& str, int accessFlags,
int creationFlags)
{
const auto str = str_.string();

if (accessFlags & sys::File::WRITE_ONLY)
creationFlags |= sys::File::TRUNCATE;
mHandle = open(str.c_str(), accessFlags | creationFlags, _SYS_DEFAULT_PERM);
Expand Down
31 changes: 18 additions & 13 deletions externals/coda-oss/modules/c++/sys/source/FileWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
#include <cmath>
#include "sys/File.h"

void sys::File::create(const sys::Filesystem::path& str_,
void sys::File::create(const std::string& str,
int accessFlags,
int creationFlags)
{
const auto str = str_.string();

// If the truncate bit is on AND the file does exist,
// we need to set the mode to TRUNCATE_EXISTING
if ((creationFlags & sys::File::TRUNCATE) && sys::OS().exists(str) )
Expand All @@ -43,13 +41,16 @@ void sys::File::create(const sys::Filesystem::path& str_,
creationFlags = ~sys::File::TRUNCATE & creationFlags;
}

const DWORD dwDesiredAccess = accessFlags;
const DWORD dwCreationDisposition = creationFlags;
mHandle = CreateFile(str.c_str(),
accessFlags,
FILE_SHARE_READ, NULL,
creationFlags,
FILE_ATTRIBUTE_NORMAL, NULL);

if (mHandle == SYS_INVALID_HANDLE)
dwDesiredAccess,
FILE_SHARE_READ,
nullptr /*lpSecurityAttributes*/,
dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL,
static_cast<HANDLE>(0) /*hTemplateFile*/);
if (mHandle == INVALID_HANDLE_VALUE)
{
throw sys::SystemException(Ctxt(FmtX("Error opening file: [%s]", str.c_str())));
}
Expand Down Expand Up @@ -128,12 +129,16 @@ sys::Off_T sys::File::seekTo(sys::Off_T offset, int whence)
{
/* Ahhh!!! */
LARGE_INTEGER largeInt;
LARGE_INTEGER toWhere;
largeInt.QuadPart = offset;
if (!SetFilePointerEx(mHandle, largeInt, &toWhere, whence))
throw sys::SystemException(Ctxt("SetFilePointer failed"));
LARGE_INTEGER newFilePointer;
const DWORD dwMoveMethod = whence;
if (SetFilePointerEx(mHandle, largeInt, &newFilePointer, dwMoveMethod) == 0)
{
const auto dwLastError = GetLastError();
throw sys::SystemException(Ctxt("SetFilePointer failed: GetLastError() = " + str::toString(dwLastError)));
}

return (sys::Off_T) toWhere.QuadPart;
return static_cast<sys::Off_T>(newFilePointer.QuadPart);
}

sys::Off_T sys::File::length()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* itself.
*/

#include <assert.h>

#include "xml/lite/Element.h"

namespace xml
Expand Down Expand Up @@ -89,7 +91,7 @@ class Document
std::string characterData = "");
virtual Element *createElement(const std::string & qname,
const std::string & uri,
const std::string& characterData, string_encoding);
const std::string& characterData, StringEncoding);
virtual Element* createElement(const std::string& qname,
const std::string& uri,
const sys::U8string& characterData);
Expand Down Expand Up @@ -157,6 +159,20 @@ class Document
Element *mRootNode;
bool mOwnRoot;
};

inline Element& getRootElement(Document& doc)
{
auto retval = doc.getRootElement();
assert(retval != nullptr);
return *retval;
}
inline const Element& getRootElement(const Document& doc)
{
auto retval = doc.getRootElement();
assert(retval != nullptr);
return *retval;
}

}
}

Expand Down
52 changes: 26 additions & 26 deletions externals/coda-oss/modules/c++/xml.lite/include/xml/lite/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace xml
namespace lite
{
/*!
* \class string_encoding
* \class StringEncoding
* \brief Specifies how std::string is encoded by MinidomParser.
*
* This is needed because our use of Xerces generates different
Expand All @@ -64,10 +64,10 @@ namespace lite
* this allows Western European languages to be displayed. On *ix,
* UTF-8 is the norm ...
*/
enum class string_encoding
enum class StringEncoding
{
windows_1252 // more-or-less ISO5589-1, https://en.wikipedia.org/wiki/Windows-1252
, utf_8
Windows1252 // more-or-less ISO5589-1, https://en.wikipedia.org/wiki/Windows-1252
, Utf8
};
// Could do the same for std::wstring, but there isn't any code needing it right now.

Expand Down Expand Up @@ -106,7 +106,7 @@ class Element
setCharacterData(characterData);
}
Element(const std::string& qname, const std::string& uri,
const std::string& characterData, string_encoding encoding) :
const std::string& characterData, StringEncoding encoding) :
Element(qname, uri, nullptr)
{
setCharacterData(characterData, encoding);
Expand All @@ -118,7 +118,7 @@ class Element
setCharacterData(characterData);
}

// string_encoding is assumed based on the platform: Windows-1252 or UTF-8.
// StringEncoding is assumed based on the platform: Windows-1252 or UTF-8.
static std::unique_ptr<Element> create(const std::string& qname, const std::string& uri = "", const std::string& characterData = "");
// Encoding of "characterData" is assumed based on the platform: Windows-1252 or UTF-8
static std::unique_ptr<Element> createU8(const std::string& qname, const std::string& uri = "", const std::string& characterData = "");
Expand All @@ -132,18 +132,18 @@ class Element
//! Destroys any child elements.
void destroyChildren();

/*!
* Copy constructor
* \param element Takes an element
*/
Element(const Element& element);
// use clone() to duplicate an Element
#if !(defined(SWIG) || defined(SWIG_PYTHON_SILENT_MEMLEAK))
private: // SWIG needs these
#endif
Element(const Element&);
Element& operator=(const Element&);
#if !(defined(SWIG) || defined(SWIG_PYTHON_SILENT_MEMLEAK))
public:
#endif

/*!
* Assignment operator
* \param element Takes an element
* \return a reference to *this
*/
Element& operator=(const Element& element);
Element(Element&&) = default;
Element& operator=(Element&&) = default;

/*!
* Clone function performs deep copy
Expand Down Expand Up @@ -289,13 +289,13 @@ class Element
// print() routine (above) can write documents with a Windows-1252 encoding
// as the string is just copied to the output.
//
// The only valid setting for string_encoding is utf_8; but defaulting that
// The only valid setting for StringEncoding is Utf8; but defaulting that
// could change behavior on Windows.
void print(io::OutputStream& stream, string_encoding /*=utf_8*/) const;
void print(io::OutputStream& stream, StringEncoding /*=Utf8*/) const;

void prettyPrint(io::OutputStream& stream,
const std::string& formatter = " ") const;
void prettyPrint(io::OutputStream& stream, string_encoding /*=utf_8*/,
void prettyPrint(io::OutputStream& stream, StringEncoding /*=Utf8*/,
const std::string& formatter = " ") const;

/*!
Expand All @@ -321,11 +321,11 @@ class Element
{
return mCharacterData;
}
const sys::Optional<string_encoding>& getEncoding() const
const sys::Optional<StringEncoding>& getEncoding() const
{
return mEncoding;
}
const sys::Optional<string_encoding>& getCharacterData(std::string& result) const
const sys::Optional<StringEncoding>& getCharacterData(std::string& result) const
{
result = getCharacterData();
return getEncoding();
Expand All @@ -336,9 +336,9 @@ class Element
* Sets the character data for this element.
* \param characters The data to add to this element
*/
void setCharacterData_(const std::string& characters, const string_encoding*);
void setCharacterData_(const std::string& characters, const StringEncoding*);
void setCharacterData(const std::string& characters);
void setCharacterData(const std::string& characters, string_encoding);
void setCharacterData(const std::string& characters, StringEncoding);
void setCharacterData(const sys::U8string& characters);

/*!
Expand Down Expand Up @@ -458,7 +458,7 @@ class Element

void depthPrint(io::OutputStream& stream, int depth,
const std::string& formatter) const;
void depthPrint(io::OutputStream& stream, string_encoding, int depth,
void depthPrint(io::OutputStream& stream, StringEncoding, int depth,
const std::string& formatter) const;

Element* mParent;
Expand All @@ -472,7 +472,7 @@ class Element

private:
// ... and how that data is encoded
sys::Optional<string_encoding> mEncoding;
sys::Optional<StringEncoding> mEncoding;
void depthPrint(io::OutputStream& stream, bool utf8, int depth,
const std::string& formatter) const;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ class MinidomHandler : public ContentHandler
* the non-ASCII data.
*/
virtual void storeEncoding(bool value);
bool storeEncoding() const;

protected:
void characters(const char* value, int length, const string_encoding*);
bool storeEncoding() const;
void characters(const char* value, int length, const StringEncoding*);

std::string currentCharacterData;
std::shared_ptr<const string_encoding> mpEncoding;
std::shared_ptr<const StringEncoding> mpEncoding;
std::stack<int> bytesForElement;
std::stack<Element *> nodeStack;
Document *mDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct MinidomParser
* \param size This is the size of the stream to feed the parser
*/
virtual void parse(io::InputStream& is, int size = io::InputStream::IS_END);

virtual void parse(io::InputStream& is, StringEncoding, int size = io::InputStream::IS_END);

/*!
* This clears the MinidomHandler, killing its underlying Document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <typeinfo>
#include "XMLException.h"
#include "Element.h" // StringEncoding

namespace xml
{
Expand All @@ -39,6 +40,9 @@ class XMLReaderInterface : public io::OutputStream
{
}

XMLReaderInterface(const XMLReaderInterface&) = delete;
XMLReaderInterface& operator=(const XMLReaderInterface&) = delete;

//! Destructor
virtual ~XMLReaderInterface()
{
Expand Down Expand Up @@ -70,14 +74,6 @@ class XMLReaderInterface : public io::OutputStream
virtual void destroy() = 0;

virtual std::string getDriverName() const = 0;

private:
//! Private copy constructor
XMLReaderInterface(const XMLReaderInterface &);

//! Private overloaded assignment operator
XMLReaderInterface & operator=(const XMLReaderInterface &);

};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
*/

#ifndef __XML_LITE_XERCES_XML_READER_H__
#define __XML_LITE_XERCES_XML_READER_H__
#ifndef CODA_OSS_xml_lite_XMLReaderXerces_h_INCLUDED_
#define CODA_OSS_xml_lite_XMLReaderXerces_h_INCLUDED_

#include "xml/lite/xml_lite_config.h"

Expand Down Expand Up @@ -99,6 +99,9 @@ class XMLReaderXerces : public XMLReaderInterface
}

void parse(io::InputStream& is, int size = io::InputStream::IS_END);

void parse(bool storeEncoding, io::InputStream& is, int size = io::InputStream::IS_END);
void parse(bool storeEncoding, io::InputStream& is, StringEncoding, int size = io::InputStream::IS_END);

//! Method to create an xml reader
void create();
Expand All @@ -109,6 +112,9 @@ class XMLReaderXerces : public XMLReaderInterface
std::string getDriverName() const { return "xerces"; }

private:
void parse(bool storeEncoding, io::InputStream& is, const StringEncoding*, int size);
void parse(bool storeEncoding, const std::vector<sys::byte>&, const StringEncoding* pEncoding);

virtual void write(const void*, size_t)
{
throw xml::lite::XMLException(Ctxt("I'm not sure how you got here!"));
Expand All @@ -119,4 +125,4 @@ class XMLReaderXerces : public XMLReaderInterface
}

#endif
#endif
#endif // CODA_OSS_xml_lite_XMLReaderXerces_h_INCLUDED_
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ xml::lite::Element* xml::lite::Document::createElement(const std::string& qname,
}
xml::lite::Element* xml::lite::Document::createElement(const std::string& qname,
const std::string& uri,
const std::string& characterData, string_encoding encoding)
const std::string& characterData, StringEncoding encoding)
{
auto elem = newElement(qname, uri);
elem->setCharacterData(characterData, encoding);
Expand Down
Loading

0 comments on commit e208dd4

Please sign in to comment.