1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00

MSVC build fix & buildsystem overhaul

This commit is contained in:
2022-09-07 00:11:11 +10:00
parent aa52d09649
commit 8a5f397f26
28 changed files with 85 additions and 447 deletions

View File

@@ -0,0 +1,5 @@
add_library(base64
base64.cpp base64.h)
add_library(External::base64 ALIAS base64)
target_include_directories(base64
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

123
ext/base64/base64.cpp Normal file
View File

@@ -0,0 +1,123 @@
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include "base64.h"
#include <iostream>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}

9
ext/base64/base64.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef __BASE64_H__
#define __BASE64_H__
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif

4
ext/miniz/CMakeLists.txt Normal file
View File

@@ -0,0 +1,4 @@
add_library(miniz INTERFACE)
add_library(External::miniz ALIAS miniz)
target_include_directories(miniz
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

4916
ext/miniz/miniz.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
add_library(rapidxml INTERFACE)
add_library(External::rapidxml ALIAS rapidxml)
target_include_directories(rapidxml
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

52
ext/rapidxml/license.txt Normal file
View File

@@ -0,0 +1,52 @@
Use of this software is granted under one of the following two licenses,
to be chosen freely by the user.
1. Boost Software License - Version 1.0 - August 17th, 2003
===============================================================================
Copyright (c) 2006, 2007 Marcin Kalicinski
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
2. The MIT License
===============================================================================
Copyright (c) 2006, 2007 Marcin Kalicinski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

2596
ext/rapidxml/rapidxml.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,174 @@
#ifndef RAPIDXML_ITERATORS_HPP_INCLUDED
#define RAPIDXML_ITERATORS_HPP_INCLUDED
// Copyright (C) 2006, 2009 Marcin Kalicinski
// Version 1.13
// Revision $DateTime: 2009/05/13 01:46:17 $
//! \file rapidxml_iterators.hpp This file contains rapidxml iterators
#include "rapidxml.hpp"
namespace rapidxml
{
//! Iterator of child nodes of xml_node
template<class Ch>
class node_iterator
{
public:
typedef typename xml_node<Ch> value_type;
typedef typename xml_node<Ch> &reference;
typedef typename xml_node<Ch> *pointer;
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
node_iterator()
: m_node(0)
{
}
node_iterator(xml_node<Ch> *node)
: m_node(node->first_node())
{
}
reference operator *() const
{
assert(m_node);
return *m_node;
}
pointer operator->() const
{
assert(m_node);
return m_node;
}
node_iterator& operator++()
{
assert(m_node);
m_node = m_node->next_sibling();
return *this;
}
node_iterator operator++(int)
{
node_iterator tmp = *this;
++this;
return tmp;
}
node_iterator& operator--()
{
assert(m_node && m_node->previous_sibling());
m_node = m_node->previous_sibling();
return *this;
}
node_iterator operator--(int)
{
node_iterator tmp = *this;
++this;
return tmp;
}
bool operator ==(const node_iterator<Ch> &rhs)
{
return m_node == rhs.m_node;
}
bool operator !=(const node_iterator<Ch> &rhs)
{
return m_node != rhs.m_node;
}
private:
xml_node<Ch> *m_node;
};
//! Iterator of child attributes of xml_node
template<class Ch>
class attribute_iterator
{
public:
typedef typename xml_attribute<Ch> value_type;
typedef typename xml_attribute<Ch> &reference;
typedef typename xml_attribute<Ch> *pointer;
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
attribute_iterator()
: m_attribute(0)
{
}
attribute_iterator(xml_node<Ch> *node)
: m_attribute(node->first_attribute())
{
}
reference operator *() const
{
assert(m_attribute);
return *m_attribute;
}
pointer operator->() const
{
assert(m_attribute);
return m_attribute;
}
attribute_iterator& operator++()
{
assert(m_attribute);
m_attribute = m_attribute->next_attribute();
return *this;
}
attribute_iterator operator++(int)
{
attribute_iterator tmp = *this;
++this;
return tmp;
}
attribute_iterator& operator--()
{
assert(m_attribute && m_attribute->previous_attribute());
m_attribute = m_attribute->previous_attribute();
return *this;
}
attribute_iterator operator--(int)
{
attribute_iterator tmp = *this;
++this;
return tmp;
}
bool operator ==(const attribute_iterator<Ch> &rhs)
{
return m_attribute == rhs.m_attribute;
}
bool operator !=(const attribute_iterator<Ch> &rhs)
{
return m_attribute != rhs.m_attribute;
}
private:
xml_attribute<Ch> *m_attribute;
};
}
#endif

View File

@@ -0,0 +1,421 @@
#ifndef RAPIDXML_PRINT_HPP_INCLUDED
#define RAPIDXML_PRINT_HPP_INCLUDED
// Copyright (C) 2006, 2009 Marcin Kalicinski
// Version 1.13
// Revision $DateTime: 2009/05/13 01:46:17 $
//! \file rapidxml_print.hpp This file contains rapidxml printer implementation
#include "rapidxml.hpp"
// Only include streams if not disabled
#ifndef RAPIDXML_NO_STREAMS
#include <ostream>
#include <iterator>
#endif
namespace rapidxml
{
///////////////////////////////////////////////////////////////////////
// Printing flags
const int print_no_indenting = 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.
///////////////////////////////////////////////////////////////////////
// Internal
//! \cond internal
namespace internal
{
///////////////////////////////////////////////////////////////////////////
// Internal character operations
// Copy characters from given range to given output iterator
template<class OutIt, class Ch>
inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)
{
while (begin != end)
*out++ = *begin++;
return out;
}
// Copy characters from given range to given output iterator and expand
// characters into references (&lt; &gt; &apos; &quot; &amp;)
template<class OutIt, class Ch>
inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out)
{
while (begin != end)
{
if (*begin == noexpand)
{
*out++ = *begin; // No expansion, copy character
}
else
{
switch (*begin)
{
case Ch('<'):
*out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('>'):
*out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('\''):
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');
break;
case Ch('"'):
*out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');
break;
case Ch('&'):
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
break;
default:
*out++ = *begin; // No expansion, copy character
}
}
++begin; // Step to next character
}
return out;
}
// Fill given output iterator with repetitions of the same character
template<class OutIt, class Ch>
inline OutIt fill_chars(OutIt out, int n, Ch ch)
{
for (int i = 0; i < n; ++i)
*out++ = ch;
return out;
}
// Find character
template<class Ch, Ch ch>
inline bool find_char(const Ch *begin, const Ch *end)
{
while (begin != end)
if (*begin++ == ch)
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////
// Internal printing operations
// Print node
template<class OutIt, class Ch>
inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
// Print proper node type
switch (node->type())
{
// Document
case node_document:
out = print_children(out, node, flags, indent);
break;
// Element
case node_element:
out = print_element_node(out, node, flags, indent);
break;
// Data
case node_data:
out = print_data_node(out, node, flags, indent);
break;
// CDATA
case node_cdata:
out = print_cdata_node(out, node, flags, indent);
break;
// Declaration
case node_declaration:
out = print_declaration_node(out, node, flags, indent);
break;
// Comment
case node_comment:
out = print_comment_node(out, node, flags, indent);
break;
// Doctype
case node_doctype:
out = print_doctype_node(out, node, flags, indent);
break;
// Pi
case node_pi:
out = print_pi_node(out, node, flags, indent);
break;
// Unknown
default:
assert(0);
break;
}
// If indenting not disabled, add line break after node
if (!(flags & print_no_indenting))
*out = Ch('\n'), ++out;
// Return modified iterator
return out;
}
// Print children of the node
template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
out = print_node(out, child, flags, indent);
return out;
}
// Print attributes of the node
template<class OutIt, class Ch>
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags)
{
for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
{
if (attribute->name() && attribute->value())
{
// Print attribute name
*out = Ch(' '), ++out;
out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
*out = Ch('='), ++out;
// Print attribute value using appropriate quote type
if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))
{
*out = Ch('\''), ++out;
out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);
*out = Ch('\''), ++out;
}
else
{
*out = Ch('"'), ++out;
out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);
*out = Ch('"'), ++out;
}
}
}
return out;
}
// Print data node
template<class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_data);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
return out;
}
// Print data node
template<class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_cdata);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'); ++out;
*out = Ch('!'); ++out;
*out = Ch('['); ++out;
*out = Ch('C'); ++out;
*out = Ch('D'); ++out;
*out = Ch('A'); ++out;
*out = Ch('T'); ++out;
*out = Ch('A'); ++out;
*out = Ch('['); ++out;
out = copy_chars(node->value(), node->value() + node->value_size(), out);
*out = Ch(']'); ++out;
*out = Ch(']'); ++out;
*out = Ch('>'); ++out;
return out;
}
// Print element node
template<class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_element);
// Print element name and attributes, if any
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'), ++out;
out = copy_chars(node->name(), node->name() + node->name_size(), out);
out = print_attributes(out, node, flags);
// If node is childless
if (node->value_size() == 0 && !node->first_node())
{
// Print childless node tag ending
*out = Ch('/'), ++out;
*out = Ch('>'), ++out;
}
else
{
// Print normal node tag ending
*out = Ch('>'), ++out;
// Test if node contains a single data node only (and no other nodes)
xml_node<Ch> *child = node->first_node();
if (!child)
{
// If node has no children, only print its value without indenting
out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
}
else if (child->next_sibling() == 0 && child->type() == node_data)
{
// If node has a sole data child, only print its value without indenting
out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
}
else
{
// Print all children with full indenting
if (!(flags & print_no_indenting))
*out = Ch('\n'), ++out;
out = print_children(out, node, flags, indent + 1);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
}
// Print node end
*out = Ch('<'), ++out;
*out = Ch('/'), ++out;
out = copy_chars(node->name(), node->name() + node->name_size(), out);
*out = Ch('>'), ++out;
}
return out;
}
// Print declaration node
template<class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
// Print declaration start
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'), ++out;
*out = Ch('?'), ++out;
*out = Ch('x'), ++out;
*out = Ch('m'), ++out;
*out = Ch('l'), ++out;
// Print attributes
out = print_attributes(out, node, flags);
// Print declaration end
*out = Ch('?'), ++out;
*out = Ch('>'), ++out;
return out;
}
// Print comment node
template<class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_comment);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'), ++out;
*out = Ch('!'), ++out;
*out = Ch('-'), ++out;
*out = Ch('-'), ++out;
out = copy_chars(node->value(), node->value() + node->value_size(), out);
*out = Ch('-'), ++out;
*out = Ch('-'), ++out;
*out = Ch('>'), ++out;
return out;
}
// Print doctype node
template<class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_doctype);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'), ++out;
*out = Ch('!'), ++out;
*out = Ch('D'), ++out;
*out = Ch('O'), ++out;
*out = Ch('C'), ++out;
*out = Ch('T'), ++out;
*out = Ch('Y'), ++out;
*out = Ch('P'), ++out;
*out = Ch('E'), ++out;
*out = Ch(' '), ++out;
out = copy_chars(node->value(), node->value() + node->value_size(), out);
*out = Ch('>'), ++out;
return out;
}
// Print pi node
template<class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
{
assert(node->type() == node_pi);
if (!(flags & print_no_indenting))
out = fill_chars(out, indent, Ch('\t'));
*out = Ch('<'), ++out;
*out = Ch('?'), ++out;
out = copy_chars(node->name(), node->name() + node->name_size(), out);
*out = Ch(' '), ++out;
out = copy_chars(node->value(), node->value() + node->value_size(), out);
*out = Ch('?'), ++out;
*out = Ch('>'), ++out;
return out;
}
}
//! \endcond
///////////////////////////////////////////////////////////////////////////
// Printing
//! Prints XML to given output iterator.
//! \param out Output iterator to print to.
//! \param node Node to be printed. Pass xml_document to print entire document.
//! \param flags Flags controlling how XML is printed.
//! \return Output iterator pointing to position immediately after last character of printed text.
template<class OutIt, class Ch>
inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
{
return internal::print_node(out, &node, flags, 0);
}
#ifndef RAPIDXML_NO_STREAMS
//! Prints XML to given output stream.
//! \param out Output stream to print to.
//! \param node Node to be printed. Pass xml_document to print entire document.
//! \param flags Flags controlling how XML is printed.
//! \return Output stream.
template<class Ch>
inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)
{
print(std::ostream_iterator<Ch>(out), node, flags);
return out;
}
//! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.
//! \param out Output stream to print to.
//! \param node Node to be printed.
//! \return Output stream.
template<class Ch>
inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
{
return print(out, node);
}
#endif
}
#endif

View File

@@ -0,0 +1,122 @@
#ifndef RAPIDXML_UTILS_HPP_INCLUDED
#define RAPIDXML_UTILS_HPP_INCLUDED
// Copyright (C) 2006, 2009 Marcin Kalicinski
// Version 1.13
// Revision $DateTime: 2009/05/13 01:46:17 $
//! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful
//! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective.
#include "rapidxml.hpp"
#include <vector>
#include <string>
#include <fstream>
#include <stdexcept>
namespace rapidxml
{
//! Represents data loaded from a file
template<class Ch = char>
class file
{
public:
//! Loads file into the memory. Data will be automatically destroyed by the destructor.
//! \param filename Filename to load.
file(const char *filename)
{
using namespace std;
// Open stream
basic_ifstream<Ch> stream(filename, ios::binary);
if (!stream)
throw runtime_error(string("cannot open file ") + filename);
stream.unsetf(ios::skipws);
// Determine stream size
stream.seekg(0, ios::end);
size_t size = stream.tellg();
stream.seekg(0);
// Load data and add terminating 0
m_data.resize(size + 1);
stream.read(&m_data.front(), static_cast<streamsize>(size));
m_data[size] = 0;
}
//! Loads file into the memory. Data will be automatically destroyed by the destructor
//! \param stream Stream to load from
file(std::basic_istream<Ch> &stream)
{
using namespace std;
// Load data and add terminating 0
stream.unsetf(ios::skipws);
m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
if (stream.fail() || stream.bad())
throw runtime_error("error reading stream");
m_data.push_back(0);
}
//! Gets file data.
//! \return Pointer to data of file.
Ch *data()
{
return &m_data.front();
}
//! Gets file data.
//! \return Pointer to data of file.
const Ch *data() const
{
return &m_data.front();
}
//! Gets file data size.
//! \return Size of file data, in characters.
std::size_t size() const
{
return m_data.size();
}
private:
std::vector<Ch> m_data; // File data
};
//! Counts children of node. Time complexity is O(n).
//! \return Number of children of node
template<class Ch>
inline std::size_t count_children(xml_node<Ch> *node)
{
xml_node<Ch> *child = node->first_node();
std::size_t count = 0;
while (child)
{
++count;
child = child->next_sibling();
}
return count;
}
//! Counts attributes of node. Time complexity is O(n).
//! \return Number of attributes of node
template<class Ch>
inline std::size_t count_attributes(xml_node<Ch> *node)
{
xml_attribute<Ch> *attr = node->first_attribute();
std::size_t count = 0;
while (attr)
{
++count;
attr = attr->next_attribute();
}
return count;
}
}
#endif

View File

@@ -0,0 +1,22 @@
include(CheckIncludeFile)
include(CheckFunctionExists)
check_include_file(strings.h HAVE_STRINGS_H)
check_function_exists(strcasecmp HAVE_STRCASECMP)
check_function_exists(_stricmp HAVE__STRICMP)
check_function_exists(strncasecmp HAVE_STRNCASECMP)
check_function_exists(_strnicmp HAVE__STRNICMP)
add_library(ultragetopt
ultragetopt.c ultragetopt.h)
add_library(External::ultragetopt ALIAS ultragetopt)
target_include_directories(ultragetopt
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(ultragetopt PRIVATE
$<$<BOOL:${HAVE_STRINGS_H}>:HAVE_STRINGS_H=1>
$<$<BOOL:${HAVE_STRCASECMP}>:HAVE_STRCASECMP=1>
$<$<BOOL:${HAVE__STRICMP}>:HAVE__STRICMP=1>
$<$<BOOL:${HAVE_STRNCASECMP}>:HAVE_STRNCASECMP=1>
$<$<BOOL:${HAVE__STRNICMP}>:HAVE__STRNICMP=1>)

View File

@@ -0,0 +1,826 @@
/* Ultra-Getopt - A replacement for getopt() with support for many common
* extensions, MS-DOS formatted option strings, and much more.
*
* To use this library as a replacement for vendor-provided getopt() functions,
* define ULTRAGETOPT_REPLACE_GETOPT and include "ultragetopt.h" after the
* vendor-provided headers for getopt() functions.
*
* Copyright (C) 2007-2011, Kevin Locke <kevin@kevinlocke.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <ctype.h> /* islower() isupper() tolower() toupper() */
#include <stdarg.h>
#include <stdio.h> /* fprintf() */
#include <stdlib.h> /* getenv() */
#include <string.h> /* strcmp(), strncmp(), strchr() */
#if HAVE_STRINGS_H
# include <strings.h> /* strcasecmp(), strncasecmp() */
#endif
#undef ULTRAGETOPT_REPLACE_GETOPT /* Protect against project-wide defines */
#include "ultragetopt.h"
/* Define replacements for missing functions */
#if !HAVE_STRCASECMP && HAVE__STRICMP
# define strcasecmp _stricmp
#endif
#if !HAVE_STRNCASECMP && HAVE__STRNICMP
# define strncasecmp _strnicmp
#endif
#if !HAVE_STRCHR && HAVE_INDEX
# define strchr index
#endif
/* Supported defines:
* ULTRAGETOPT_LIKE_BSD Behave like BSD getopt()
* ULTRAGETOPT_LIKE_DARWIN Behave like Darwin (Mac OS) getopt()
* ULTRAGETOPT_LIKE_GNU Behave like GNU getopt()
* ULTRAGETOPT_LIKE_POSIX Behave like POSIX definition of getopt()
*
* ULTRAGETOPT_ASSIGNSPACE Parse "-o value" as "value" rather than " value"
* Note: Only applicable when argv[x] == "-o value"
* Not for argv[x] == "-o" [x+1] == "value"
* ULTRAGETOPT_DEFAULTOPTOPT Set optopt to this value by default on each
* call to getopt()
* ULTRAGETOPT_HYPHENARG Accept -option -arg as -option with argument
* "-arg" rather than -option missing argument
* Note: A single "-" is always accepted as an
* argument
* ULTRAGETOPT_LONGOPTADJACENT Accept adjacent arguments to long options
* (e.g. --optionarg) based on first longest-match
* ULTRAGETOPT_OPTIONPERMUTE Permute options, do not stop at first non-option
* Behaves like GNU getopt where leading '+' or
* $POSIXLY_CORRECT both stop this @ runtime
* ULTRAGETOPT_SHORTOPTASSIGN Support -o=file syntax for short options
* ULTRAGETOPT_SEPARATEDOPTIONAL Accept separated optional arguments
* Parse -o file as -o with argument file
* ULTRAGETOPT_DOS_DASH Support - and -- options in ultragetopt*_dos()
* ULTRAGETOPT_BSD_ERRORS Print error messages matching BSD getopt
* ULTRAGETOPT_DARWIN_ERRORS Print error messages matching Darwin getopt
* ULTRAGETOPT_GNU_ERRORS Print error messages matching GNU getopt
* ULTRAGETOPT_NO_EATDASHDASH Do not increment optind when argv[optind] is --
* ULTRAGETOPT_NO_OPTIONALARG Do not support GNU "::" optional argument
* Always supported in *_long*()
* ULTRAGETOPT_NO_OPTIONASSIGN Do not support --option=value syntax
*/
#ifdef ULTRAGETOPT_LIKE_POSIX
# define ULTRAGETOPT_NO_OPTIONALARG
# define ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_BSD_ERRORS
# undef ULTRAGETOPT_DARWIN_ERRORS
# undef ULTRAGETOPT_GNU_ERRORS
# undef ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_SHORTOPTASSIGN
#elif defined(ULTRAGETOPT_LIKE_GNU)
# define ULTRAGETOPT_GNU_ERRORS
# define ULTRAGETOPT_HYPHENARG
# define ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#elif defined(ULTRAGETOPT_LIKE_BSD)
# define ULTRAGETOPT_BSD_ERRORS
# define ULTRAGETOPT_OPTIONPERMUTE
# define ULTRAGETOPT_DEFAULTOPTOPT '?'
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#elif defined(ULTRAGETOPT_LIKE_DARWIN)
# define ULTRAGETOPT_DARWIN_ERRORS
# define ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#endif
#ifdef ULTRAGETOPT_NO_OPTIONASSIGN
static const char *const unixassigners = "";
static const char *const dosassigners = ":";
#elif defined(ULTRAGETOPT_OPTIONSPACE)
static const char *const unixassigners = "= ";
static const char *const dosassigners = ":= ";
#else
static const char *const unixassigners = "=";
static const char *const dosassigners = ":=";
#endif
#ifdef ULTRAGETOPT_DOS_DASH
static const char *const unixleaders = "-";
static const char *const dosleaders = "/-";
#else
static const char *const unixleaders = "-";
static const char *const dosleaders = "/";
#endif
/* Flags for all variants of ultragetopt*() */
static const int getoptflags = 0
#ifdef ULTRAGETOPT_SEPARATEDOPTIONAL
| UGO_SEPARATEDOPTIONAL
#endif
#ifdef ULTRAGETOPT_SHORTOPTASSIGN
| UGO_SHORTOPTASSIGN
#endif
#ifdef ULTRAGETOPT_NO_EATDASHDASH
| UGO_NOEATDASHDASH
#endif
#ifdef ULTRAGETOPT_HYPHENARG
| UGO_HYPHENARG
#endif
;
#ifdef ULTRAGETOPT_GNU_ERRORS
static const char *const errorarg =
"%s: option `%.*s' doesn't allow an argument\n";
static const char *const errornoarg =
"%s: option `%.*s' requires an argument \n";
static const char *const erroropt =
"%s: unrecognized option `%.*s'\n";
static const char *const errorargc =
"%s: option `-%c' does not take an argument\n";
static const char *const errornoargc =
"%s: option requires an argument -- `-%c'\n";
static const char *const erroroptc =
"%s: invalid option -- %c\n";
#elif defined(ULTRAGETOPT_BSD_ERRORS)
static const char *const errorarg =
"%s: option doesn't take an argument -- %.*s\n";
static const char *const errornoarg =
"%s: option requires an argument -- %.*s\n";
static const char *const erroropt =
"%s: unknown option -- %.*s\n";
static const char *const errorargc =
"%s: option doesn't take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: unknown option -- %c\n";
#elif defined(ULTRAGETOPT_DARWIN_ERRORS)
static const char *const errorarg =
"%s: option `%.*s' doesn't allow an argument\n"; /* with -- */
static const char *const errornoarg =
"%s: option `%.*s' requires an argument\n";
static const char *const erroropt =
"%s: unrecognized option `%.*s'\n"; /* with -- */
static const char *const errorargc =
"%s: option doesn't take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: invalid option -- %c\n";
#else /* POSIX-like */
static const char *const errorarg =
"%s: option does not take an argument -- %.*s\n";
static const char *const errornoarg =
"%s: option requires an argument -- %.*s\n";
static const char *const erroropt =
"%s: illegal option -- %.*s\n";
static const char *const errorargc =
"%s: option does not take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: illegal option -- %c\n";
#endif
/* Globals to match optarg, optind, opterr, optopt, optreset */
char *ultraoptarg = NULL;
int ultraoptind = 1;
int ultraopterr = 1;
int ultraoptreset = 0;
#ifdef ULTRAGETOPT_DEFAULTOPTOPT
int ultraoptopt = ULTRAGETOPT_DEFAULTOPTOPT -0;
#else
int ultraoptopt = 0;
#endif
static int ultraoptnum = 0; /* How many options of the current multi-option
argument have been processed? (e.g. -vvv) */
/* Add format error checking for gcc versions that support it */
#if defined(__GNUC__) && __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR > 6)
static void print_error(int flags, const char *template, ...)
__attribute__ ((format (printf, 2, 3)));
#endif
/* Print errors only if not suppressed */
static void print_error(int flags, const char *template, ...)
{
va_list ap;
va_start(ap, template);
if (ultraopterr != 0 && !(flags & UGO_NOPRINTERR))
vfprintf(stderr, template, ap);
va_end(ap);
}
/* Check if an argument string looks like an option string */
static inline int like_option(const char *arg, const char *optleaders)
{
return arg != NULL &&
arg[0] != '\0' && /* >= 2 characters long */
arg[1] != '\0' &&
strchr(optleaders, arg[0]) && /* Starts with optleader */
(arg[2] != '\0' || arg[0] != arg[1]); /* Not -- */
}
/* Check if an argument string looks like the option terminator string */
static inline int like_optterm(const char *arg, const char *optleaders)
{
return arg != NULL &&
arg[0] != '\0' &&
arg[1] != '\0' &&
arg[2] == '\0' &&
arg[0] == arg[1] &&
strchr(optleaders, arg[0]);
}
/* Check if an argument string looks like an option argument string */
static inline int like_optarg(const char *arg, const char *optleaders,
int allow_option)
{
return arg != NULL &&
(allow_option ||
(!like_option(arg, optleaders) && !like_optterm(arg, optleaders)));
}
/* If argv[curopt] matches a long option, return the index of that option
* Otherwise, return -1
* If it has an adjacent argument, return pointer to it in longarg, else NULL
*/
static int match_longopt(int curopt, char *const argv[],
const struct option *longopts, const char *assigners,
const char *optleaders, int flags, char **longarg)
{
size_t alen, optnamelen = 0;
char *optname;
char *temp;
int i;
int (*optncmp)(const char *s1, const char *s2, size_t n);
if (longarg == NULL)
longarg = &temp;
*longarg = NULL;
if (flags & UGO_CASEINSENSITIVE)
optncmp = strncasecmp;
else
optncmp = strncmp;
if (longopts == NULL)
return -1;
if (!like_option(argv[curopt], optleaders))
return -1;
if (flags & UGO_SINGLELEADERONLY) {
optname = argv[curopt]+1;
} else if (!strchr(optleaders, argv[curopt][1])) {
/* Possible short option */
if (flags & UGO_SINGLELEADERLONG)
optname = argv[curopt]+1;
else
return -1;
} else {
optname = argv[curopt]+2;
}
/* Do first longest-match search if requested */
if (flags & UGO_LONGOPTADJACENT) {
size_t matchlen = 0;
int matchind = -1;
for (i=0; longopts[i].name != NULL; i++) {
size_t longnamelen = strlen(longopts[i].name);
if (longnamelen > matchlen
&& optncmp(optname, longopts[i].name, longnamelen) == 0) {
matchind = i;
matchlen = longnamelen;
}
}
if (matchlen > 0) {
/* See if our match has an adjacent argument */
if (optname[matchlen] != '\0') {
/* Strip assigner character if present */
if (strchr(assigners, optname[matchlen]))
*longarg = optname+matchlen+1;
else
*longarg = optname+matchlen;
}
return matchind;
}
return -1;
}
/* Check for assigner in the option */
alen = strlen(assigners);
for (i=0; (unsigned)i < alen; i++) {
char *asn = strchr(optname, assigners[i]);
if (asn != NULL) {
optnamelen = asn - optname;
*longarg = asn+1;
break;
}
}
if (optnamelen == 0)
optnamelen = strlen(optname);
for (i=0; longopts[i].name != NULL; i++)
if (optncmp(optname, longopts[i].name, optnamelen) == 0
&& strlen(longopts[i].name) == optnamelen)
return i;
return -1;
}
/* Check if an option has a separate argument (in the following argv[] index) */
static int has_separate_argument(int curopt, int argc, char *const argv[],
const char *shortopts,
const struct option *longopts,
const char *assigners, const char *optleaders,
int flags)
{
int longind;
char *longarg;
assert(curopt < argc && like_option(argv[curopt], optleaders));
/* Check if we have a long option */
longind = match_longopt(ultraoptind, argv, longopts, assigners, optleaders,
flags, &longarg);
if (longind >= 0) {
if (longopts[longind].has_arg == no_argument
|| longarg != NULL
|| (longopts[longind].has_arg == optional_argument
&& !(flags & UGO_SEPARATEDOPTIONAL)))
return 0;
return like_optarg(argv[curopt+1], optleaders,
(flags & UGO_HYPHENARG) &&
longopts[longind].has_arg == required_argument);
} else if (!strchr(optleaders, argv[curopt][1])) {
/* Short option */
char *optpos;
optpos = strchr(shortopts, argv[curopt][1]);
if ((flags & UGO_CASEINSENSITIVE) && optpos == NULL) {
if (islower(argv[curopt][1]))
optpos = strchr(shortopts, toupper(argv[curopt][1]));
else
optpos = strchr(shortopts, tolower(argv[curopt][1]));
}
return optpos != NULL /* Option found */
&& optpos[1] == ':' /* Option takes argument */
&& (optpos[2] != ':' || (flags & UGO_SEPARATEDOPTIONAL))
&& argv[curopt][2] == '\0' /* Argument is not adjacent */
&& like_optarg(argv[curopt+1], /* Is an argument */
optleaders,
(flags & UGO_HYPHENARG) && optpos[2] != ':');
}
/* No match */
return 0;
}
/* Bring the next option, or terminator, up to ultraoptind if there is one
* Returns number of words shifted forward
*/
static int permute_options(int argc, char *argv[], const char *shortopts,
const struct option *longopts,
const char *assigners, const char *optleaders,
int flags)
{
int curopt = ultraoptind;
/* If we already have an option or no more possible, give up */
if (curopt >= argc || like_option(argv[curopt], optleaders))
return 0;
for ( ; curopt < argc && argv[curopt]; curopt++) {
int shiftarg = 0;
int i;
/* Permute options and the option terminator */
if (like_option(argv[curopt], optleaders)) {
/* Check if we need to shift argument too */
shiftarg = has_separate_argument(curopt, argc, argv, shortopts,
longopts, assigners, optleaders,
flags);
} else if (!like_optterm(argv[curopt], optleaders)) {
continue;
}
/* Shift option */
for (i=curopt; i>ultraoptind; i--) {
char *temp = argv[i];
argv[i] = argv[i-1];
argv[i-1] = temp;
if (shiftarg) {
temp = argv[i+1];
argv[i+1] = argv[i];
argv[i] = temp;
}
}
/* All done */
if (shiftarg)
return 2;
else
return 1;
}
/* Couldn't find an option, bummer */
return 0;
}
/* Handle a longopts[longind] matches argv[ultraoptind] actions */
static int handle_longopt(int longind, char *longarg, int noseparg,
char *const argv[],
const struct option *longopts, int *indexptr,
const char *optleaders, int flags)
{
/* Handle assignment arguments */
if (longarg && longopts[longind].has_arg == no_argument) {
print_error(flags, errorarg, argv[0],
longarg-argv[ultraoptind]-1, argv[ultraoptind]);
/* TODO: What is a good value to put in ultraoptopt? */
/* Looks like GNU getopt() uses val */
ultraoptopt = longopts[longind].val;
ultraoptind++;
return '?';
} else if (longarg) {
ultraoptind++;
ultraoptarg = longarg;
if (indexptr)
*indexptr = longind;
if (longopts[longind].flag) {
*(longopts[longind].flag) = longopts[longind].val;
return 0;
} else
return longopts[longind].val;
}
/* Handle missing required argument */
if (longopts[longind].has_arg == required_argument
&& (noseparg
|| !like_optarg(argv[ultraoptind+1],
optleaders,
flags & UGO_HYPHENARG))) {
print_error(flags, errornoarg, argv[0],
strlen(argv[ultraoptind]), argv[ultraoptind]);
ultraoptind++;
if (flags & UGO_MISSINGCOLON)
return ':';
else
return '?';
}
/* Handle available argument */
if ((longopts[longind].has_arg == required_argument
|| (longopts[longind].has_arg == optional_argument
&& (flags & UGO_SEPARATEDOPTIONAL)))
&& !noseparg
&& like_optarg(argv[ultraoptind+1],
optleaders,
(flags & UGO_HYPHENARG) &&
longopts[longind].has_arg == required_argument)) {
ultraoptarg = argv[ultraoptind+1];
ultraoptind += 2;
} else
ultraoptind++;
if (indexptr)
*indexptr = longind;
if (longopts[longind].flag) {
*(longopts[longind].flag) = longopts[longind].val;
return 0;
} else
return longopts[longind].val;
}
int ultragetopt_tunable(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr,
const char *assigners, const char *optleaders,
int flags)
{
char *opt; /* Option we are processing */
char *optpos; /* Pointer to opt in shortopts */
int noseparg = 0; /* Force option not to have a separate argument */
if (ultraoptreset) {
ultraoptind = 1;
ultraopterr = 1;
ultraoptnum = 0;
ultraoptreset = 0;
}
ultraoptarg = NULL;
#ifdef ULTRAGETOPT_DEFAULTOPTOPT
ultraoptopt = ULTRAGETOPT_DEFAULTOPTOPT -0;
#endif
/* Sanity check (These are specified verbatim in SUS) */
if (ultraoptind > argc
|| argv[ultraoptind] == NULL)
return -1;
/* No permuting when $POSIXLY_CORRECT is set (to match GNU getopt) */
if (getenv("POSIXLY_CORRECT"))
flags &= ~UGO_OPTIONPERMUTE;
/* Get flags from shortopts */
for ( ; shortopts && *shortopts; shortopts++) {
if (*shortopts == '+')
flags &= ~UGO_OPTIONPERMUTE;
else if (*shortopts == '-')
flags |= UGO_NONOPTARG;
else if (*shortopts == ':') {
flags |= UGO_NOPRINTERR;
flags |= UGO_MISSINGCOLON;
} else
break;
}
/* Found non-option */
if (!like_option(argv[ultraoptind], optleaders)) {
int shifted;
if (like_optterm(argv[ultraoptind], optleaders)) {
if (!(flags & UGO_NOEATDASHDASH))
ultraoptind++;
return -1;
}
if (flags & UGO_NONOPTARG) {
ultraoptarg = argv[ultraoptind];
ultraoptind++;
return 1;
}
if (!(flags & UGO_OPTIONPERMUTE))
return -1;
shifted = permute_options(argc, (char **)argv, shortopts, longopts,
assigners, optleaders, flags);
if (shifted == 0)
return -1;
else if (shifted == 1)
noseparg = 1;
if (like_optterm(argv[ultraoptind], optleaders)) {
if (!(flags & UGO_NOEATDASHDASH))
ultraoptind++;
return -1;
}
}
/* At this point we must have an option of some sort */
assert(like_option(argv[ultraoptind], optleaders));
/* Handle --* */
if (argv[ultraoptind][0] == argv[ultraoptind][1]) {
int longind;
char *longarg;
/* Handle long option */
longind = match_longopt(ultraoptind, argv, longopts, assigners,
optleaders, flags, &longarg);
if (longind < 0) {
if (longarg == NULL)
print_error(flags, erroropt, argv[0],
strlen(argv[ultraoptind]), argv[ultraoptind]);
else
print_error(flags, erroropt, argv[0],
longarg - argv[ultraoptind] - 1, argv[ultraoptind]);
/* TODO: What is a good value for optopt in this case? */
/* Looks like BSD uses 0 */
ultraoptopt = 0;
ultraoptind++;
return '?';
}
return handle_longopt(longind, longarg, noseparg, argv,
longopts, indexptr, optleaders, flags);
}
/* See if it matches a long-only option */
if (longopts != NULL &&
ultraoptnum == 0 &&
((flags & UGO_SINGLELEADERLONG) ||
(flags & UGO_SINGLELEADERONLY))) {
int longind;
char *longarg;
longind = match_longopt(ultraoptind, argv, longopts, assigners,
optleaders, flags, &longarg);
if (longind >= 0)
return handle_longopt(longind, longarg, noseparg, argv,
longopts, indexptr, optleaders, flags);
}
/* No long matches, process short option */
opt = argv[ultraoptind] + ultraoptnum + 1;
optpos = strchr(shortopts, opt[0]);
if (optpos == NULL && (flags & UGO_CASEINSENSITIVE)) {
if (islower(opt[0]))
optpos = strchr(shortopts, toupper(opt[0]));
else
optpos = strchr(shortopts, tolower(opt[0]));
}
/* This could indicate ultraoptnum not being reset properly */
assert(opt[0] != '\0');
/* Check for invalid or unrecognized option */
if (optpos == NULL || opt[0] == ':') {
print_error(flags, erroroptc, argv[0], opt[0]);
ultraoptopt = opt[0];
if (opt[1] != '\0')
ultraoptnum++;
else {
ultraoptnum = 0;
ultraoptind++;
}
return '?';
}
/* Handle arguments */
if (optpos[1] == ':') {
ultraoptnum = 0;
/* Handle adjacent arguments -ofile.txt */
if (opt[1] != '\0') {
/* Skip over assignment character */
if ((flags & UGO_SHORTOPTASSIGN) && strchr(assigners, opt[1]))
ultraoptarg = opt + 2;
else
ultraoptarg = opt + 1;
ultraoptind++;
return optpos[0];
}
/* Handle optional argument not present */
if ((flags & UGO_OPTIONALARG) /* accept optionals */
&& optpos[2] == ':' /* opt takes optional */
&& (argv[ultraoptind+1] == NULL /* optional doesn't exist */
|| !(flags & UGO_SEPARATEDOPTIONAL) /* separated not accepted */
|| like_option(argv[ultraoptind+1], optleaders))) {
ultraoptind++;
return optpos[0];
}
/* Handle separated argument missing */
if (ultraoptind+2 > argc
|| noseparg
|| !like_optarg(argv[ultraoptind+1],
optleaders,
(flags & UGO_HYPHENARG))) {
ultraoptind++;
print_error(flags, errornoargc, argv[0], opt[0]);
ultraoptopt = opt[0];
if (flags & UGO_MISSINGCOLON)
return ':';
else
return '?';
}
ultraoptind += 2;
ultraoptarg = argv[ultraoptind-1];
return optpos[0];
}
/* Handle argumentless option with assigned option */
if ((flags & UGO_SHORTOPTASSIGN)
&& opt[1] != '\0' && strchr(assigners, opt[1])) {
print_error(flags, errorargc, argv[0], opt[0]);
ultraoptnum = 0;
ultraoptopt = opt[0];
ultraoptind++;
return '?';
}
if (opt[1] != '\0') {
ultraoptnum++;
} else {
ultraoptnum = 0;
ultraoptind++;
}
return optpos[0];
}
/* POSIX-compliant getopt
*
* Handles optional argument '::' specifier as an extension for compatibility
* with glibc
*/
int ultragetopt(int argc, char * const argv[], const char *optstring)
{
int flags = getoptflags;
#ifdef ULTRAGETOPT_OPTIONPERMUTE
flags |= UGO_OPTIONPERMUTE;
#endif
#ifndef ULTRAGETOPT_NO_OPTIONALARG
flags |= UGO_OPTIONALARG;
#endif
return ultragetopt_tunable(argc, argv, optstring, NULL, NULL,
unixassigners, unixleaders, flags);
}
/* GNU getopt_long workalike
*
* Argument reordering not yet implemented
* Leading + and - under consideration (behavior violates POSIX...)
*/
int ultragetopt_long(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
unixassigners, unixleaders,
getoptflags | UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
/* GNU getopt_long_only workalike */
int ultragetopt_long_only(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
unixassigners, unixleaders,
getoptflags | UGO_SINGLELEADERLONG
| UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
int ultragetopt_dos(int argc, char * const argv[], const char *optstring)
{
return ultragetopt_tunable(argc, argv, optstring, NULL, NULL,
dosassigners, dosleaders,
getoptflags | UGO_CASEINSENSITIVE);
}
int ultragetopt_long_dos(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
dosassigners, dosleaders,
getoptflags | UGO_CASEINSENSITIVE
| UGO_SINGLELEADERLONG | UGO_SINGLELEADERONLY
| UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
/* vim:set sts=4 sw=4: */

View File

@@ -0,0 +1,105 @@
/* define ULTRAGETOPT_REPLACE_GETOPT for ultragetopt*() to replace getopt*() */
/* define ULTRAGETOPT_ONLY_DOS for ultragetopt*_dos() to replace ultragetopt*() */
#ifndef INCLUDED_GETOPT_H
#define INCLUDED_GETOPT_H 1
#ifdef __cplusplus
extern "C" {
#endif
#define ULTRAGETOPT_REPLACE_GETOPT
#define ULTRAGETOPT_LIKE_GNU
#define ULTRAGETOPT_LINKAGE extern
/* Flag values to pass to getopt_tunable() */
#define UGO_CASEINSENSITIVE 0x1
#define UGO_SINGLELEADERLONG 0x2
#define UGO_OPTIONPERMUTE 0x4
#define UGO_NONOPTARG 0x8
#define UGO_NOPRINTERR 0x10
#define UGO_OPTIONALARG 0x20
#define UGO_MISSINGCOLON 0x40
#define UGO_SEPARATEDOPTIONAL 0x80
#define UGO_SHORTOPTASSIGN 0x100
#define UGO_NOEATDASHDASH 0x200
#define UGO_LONGOPTADJACENT 0x400
#define UGO_HYPHENARG 0x800
#define UGO_SINGLELEADERONLY 0x1000
#ifndef required_argument
# define no_argument 0
# define required_argument 1
# define optional_argument 2
struct option {
const char *name; /* Name of the option */
int has_arg; /* Does the option take an argument? */
int *flag; /* Location to store val when option encountered */
int val; /* Value to return (or store in flag) for option */
};
#endif /* required_argument */
ULTRAGETOPT_LINKAGE char *ultraoptarg;
ULTRAGETOPT_LINKAGE int ultraoptind, ultraopterr, ultraoptopt, ultraoptreset;
ULTRAGETOPT_LINKAGE int ultragetopt(int argc, char *const argv[],
const char *optstring);
ULTRAGETOPT_LINKAGE int ultragetopt_long(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
ULTRAGETOPT_LINKAGE int ultragetopt_long_only(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
ULTRAGETOPT_LINKAGE int ultragetopt_dos(int argc, char * const argv[],
const char *optstring);
ULTRAGETOPT_LINKAGE int ultragetopt_long_dos(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
/* Getopt with modifiable (tunable) behavior - also the backend for all other
* getopt functions.
* assigners - string of characters accepted to assign to an option
* (e.g. --outfile=file.txt where '=' is the assigner)
* optleaders - string of characters that indicate an option
* (usually "-" on UNIX, "/" on DOS)
* flags - see README for list of accepted flags
*/
ULTRAGETOPT_LINKAGE int ultragetopt_tunable(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr,
const char *assigners, const char *optleaders, int flags);
#ifdef ULTRAGETOPT_REPLACE_GETOPT
# define optarg ultraoptarg
# define optind ultraoptind
# define opterr ultraopterr
# define optopt ultraoptopt
# define optreset ultraoptreset
# define getopt(argc, argv, optstring) \
ultragetopt(argc, argv, optstring)
# define getopt_long(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long(argc, argv, shortopts, longopts, indexptr)
# define getopt_long_only(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_only(argc, argv, shortopts, longopts, indexptr)
# define getopt_dos(argc, argv, optstring) \
ultragetopt_dos(argc, argv, optstring)
# define getopt_long_dos(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
#endif /* GETOPT_NO_EXTENSIONS */
#ifdef ULTRAGETOPT_DOS_ONLY
# define ultragetopt(argc, argv, optstring) \
ultragetopt_dos(argc, argv, optstring)
# define ultragetopt_long(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
# define ultragetopt_long_only(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
#endif /* ULTRAGETOPT_DOS_ONLY */
#ifdef __cplusplus
}
#endif
#endif /* INCLUDED_GETOPT_H */