initial
git-svn-id: svn://svn.icculus.org/netradiant/trunk@1 61c419a2-8eb2-4b30-bcec-8cead039b335
This commit is contained in:
22
libs/stream/filestream.cpp
Normal file
22
libs/stream/filestream.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "filestream.h"
|
||||
194
libs/stream/filestream.h
Normal file
194
libs/stream/filestream.h
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_STREAM_FILESTREAM_H)
|
||||
#define INCLUDED_STREAM_FILESTREAM_H
|
||||
|
||||
#include "idatastream.h"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
namespace FileStreamDetail
|
||||
{
|
||||
inline int whence_for_seekdir(SeekableStream::seekdir direction)
|
||||
{
|
||||
switch(direction)
|
||||
{
|
||||
case SeekableStream::cur:
|
||||
return SEEK_CUR;
|
||||
case SeekableStream::end:
|
||||
return SEEK_END;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return SEEK_SET;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// \brief A wrapper around a file input stream opened for reading in binary mode. Similar to std::ifstream.
|
||||
///
|
||||
/// - Maintains a valid file handle associated with a name passed to the constructor.
|
||||
/// - Implements SeekableInputStream.
|
||||
class FileInputStream : public SeekableInputStream
|
||||
{
|
||||
std::FILE* m_file;
|
||||
public:
|
||||
FileInputStream(const char* name)
|
||||
{
|
||||
m_file = name[0] == '\0' ? 0 : fopen(name, "rb");
|
||||
}
|
||||
~FileInputStream()
|
||||
{
|
||||
if(!failed())
|
||||
fclose(m_file);
|
||||
}
|
||||
|
||||
bool failed() const
|
||||
{
|
||||
return m_file == 0;
|
||||
}
|
||||
|
||||
size_type read(byte_type* buffer, size_type length)
|
||||
{
|
||||
return fread(buffer, 1, length, m_file);
|
||||
}
|
||||
|
||||
size_type seek(size_type position)
|
||||
{
|
||||
return fseek(m_file, static_cast<long>(position), SEEK_SET);
|
||||
}
|
||||
size_type seek(offset_type offset, seekdir direction)
|
||||
{
|
||||
return fseek(m_file, offset, FileStreamDetail::whence_for_seekdir(direction));
|
||||
}
|
||||
size_type tell() const
|
||||
{
|
||||
return ftell(m_file);
|
||||
}
|
||||
|
||||
std::FILE* file()
|
||||
{
|
||||
return m_file;
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief A wrapper around a FileInputStream limiting access.
|
||||
///
|
||||
/// - Maintains an input stream.
|
||||
/// - Provides input starting at an offset in the file for a limited range.
|
||||
class SubFileInputStream : public InputStream
|
||||
{
|
||||
FileInputStream& m_istream;
|
||||
size_type m_remaining;
|
||||
public:
|
||||
typedef FileInputStream::position_type position_type;
|
||||
|
||||
SubFileInputStream(FileInputStream& istream, position_type offset, size_type size)
|
||||
: m_istream(istream), m_remaining(size)
|
||||
{
|
||||
m_istream.seek(offset);
|
||||
}
|
||||
|
||||
size_type read(byte_type* buffer, size_type length)
|
||||
{
|
||||
size_type result = m_istream.read(buffer, std::min(length, m_remaining));
|
||||
m_remaining -= result;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// \brief A wrapper around a stdc file stream opened for writing in binary mode. Similar to std::ofstream..
|
||||
///
|
||||
/// - Maintains a valid file handle associated with a name passed to the constructor.
|
||||
/// - Implements SeekableInputStream.
|
||||
class FileOutputStream : public SeekableOutputStream
|
||||
{
|
||||
std::FILE* m_file;
|
||||
public:
|
||||
FileOutputStream(const char* name)
|
||||
{
|
||||
m_file = name[0] == '\0' ? 0 : fopen(name, "wb");
|
||||
}
|
||||
~FileOutputStream()
|
||||
{
|
||||
if(!failed())
|
||||
fclose(m_file);
|
||||
}
|
||||
|
||||
bool failed() const
|
||||
{
|
||||
return m_file == 0;
|
||||
}
|
||||
|
||||
size_type write(const byte_type* buffer, size_type length)
|
||||
{
|
||||
return fwrite(buffer, 1, length, m_file);
|
||||
}
|
||||
|
||||
size_type seek(size_type position)
|
||||
{
|
||||
return fseek(m_file, static_cast<long>(position), SEEK_SET);
|
||||
}
|
||||
size_type seek(offset_type offset, seekdir direction)
|
||||
{
|
||||
return fseek(m_file, offset, FileStreamDetail::whence_for_seekdir(direction));
|
||||
}
|
||||
size_type tell() const
|
||||
{
|
||||
return ftell(m_file);
|
||||
}
|
||||
};
|
||||
|
||||
inline bool file_copy(const char* source, const char* target)
|
||||
{
|
||||
const std::size_t buffer_size = 1024;
|
||||
unsigned char buffer[buffer_size];
|
||||
|
||||
FileInputStream sourceFile(source);
|
||||
if(sourceFile.failed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
FileOutputStream targetFile(target);
|
||||
if(targetFile.failed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for(;;)
|
||||
{
|
||||
std::size_t size = sourceFile.read(buffer, buffer_size);
|
||||
if(size == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(targetFile.write(buffer, size) != size)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
22
libs/stream/memstream.cpp
Normal file
22
libs/stream/memstream.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "memstream.h"
|
||||
81
libs/stream/memstream.h
Normal file
81
libs/stream/memstream.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_STREAM_MEMSTREAM_H)
|
||||
#define INCLUDED_STREAM_MEMSTREAM_H
|
||||
|
||||
#include "itextstream.h"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
class BufferOutputStream : public TextOutputStream
|
||||
{
|
||||
std::vector<char> m_buffer;
|
||||
public:
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
m_buffer.insert(m_buffer.end(), buffer, buffer+length);
|
||||
return length;
|
||||
}
|
||||
const char* data() const
|
||||
{
|
||||
return &(*m_buffer.begin());
|
||||
}
|
||||
std::size_t size() const
|
||||
{
|
||||
return m_buffer.size();
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
std::vector<char> empty;
|
||||
std::swap(empty, m_buffer);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline BufferOutputStream& operator<<(BufferOutputStream& ostream, const T& t)
|
||||
{
|
||||
return ostream_write(ostream, t);
|
||||
}
|
||||
|
||||
|
||||
class BufferInputStream : public TextInputStream
|
||||
{
|
||||
const char* m_read;
|
||||
const char* m_end;
|
||||
public:
|
||||
BufferInputStream(const char* buffer, std::size_t length)
|
||||
: m_read(buffer), m_end(buffer + length)
|
||||
{
|
||||
}
|
||||
std::size_t read(char* buffer, std::size_t length)
|
||||
{
|
||||
std::size_t count = std::min(std::size_t(m_end - m_read), length);
|
||||
const char* end = m_read + count;
|
||||
while(m_read != end)
|
||||
{
|
||||
*buffer++ = *m_read++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
22
libs/stream/stringstream.cpp
Normal file
22
libs/stream/stringstream.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "stringstream.h"
|
||||
177
libs/stream/stringstream.h
Normal file
177
libs/stream/stringstream.h
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_STREAM_STRINGSTREAM_H)
|
||||
#define INCLUDED_STREAM_STRINGSTREAM_H
|
||||
|
||||
#include "itextstream.h"
|
||||
#include "string/string.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
/// \brief A wrapper around a STL vector of char.
|
||||
/// Maintains a null-terminated array of char.
|
||||
/// Provides a limited STL-style interface to push and pop characters at the end of the string.
|
||||
class StringBuffer
|
||||
{
|
||||
std::vector<char> m_string;
|
||||
public:
|
||||
StringBuffer()
|
||||
{
|
||||
m_string.push_back('\0');
|
||||
}
|
||||
explicit StringBuffer(std::size_t capacity)
|
||||
{
|
||||
m_string.reserve(capacity);
|
||||
m_string.push_back('\0');
|
||||
}
|
||||
explicit StringBuffer(const char* string) : m_string(string, string + string_length(string) + 1)
|
||||
{
|
||||
}
|
||||
|
||||
typedef std::vector<char>::iterator iterator;
|
||||
typedef std::vector<char>::const_iterator const_iterator;
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return m_string.begin();
|
||||
}
|
||||
const_iterator begin() const
|
||||
{
|
||||
return m_string.begin();
|
||||
}
|
||||
iterator end()
|
||||
{
|
||||
return m_string.end() - 1;
|
||||
}
|
||||
const_iterator end() const
|
||||
{
|
||||
return m_string.end() - 1;
|
||||
}
|
||||
|
||||
void push_back(char c)
|
||||
{
|
||||
m_string.insert(end(), c);
|
||||
}
|
||||
void pop_back()
|
||||
{
|
||||
m_string.erase(end() - 1);
|
||||
}
|
||||
void push_range(const char* first, const char* last)
|
||||
{
|
||||
m_string.insert(end(), first, last);
|
||||
}
|
||||
void push_string(const char* string)
|
||||
{
|
||||
push_range(string, string + string_length(string));
|
||||
}
|
||||
char* c_str()
|
||||
{
|
||||
return &(*m_string.begin());
|
||||
}
|
||||
const char* c_str() const
|
||||
{
|
||||
return &(*m_string.begin());
|
||||
}
|
||||
|
||||
char& back()
|
||||
{
|
||||
return *(end() - 1);
|
||||
}
|
||||
const char& back() const
|
||||
{
|
||||
return *(end() - 1);
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
return m_string.size() == 1;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
m_string.clear();
|
||||
m_string.push_back('\0');
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief A TextOutputStream which writes to a StringBuffer.
|
||||
/// Similar to std::stringstream.
|
||||
class StringOutputStream : public TextOutputStream
|
||||
{
|
||||
StringBuffer m_string;
|
||||
public:
|
||||
typedef StringBuffer::iterator iterator;
|
||||
typedef StringBuffer::const_iterator const_iterator;
|
||||
|
||||
StringOutputStream()
|
||||
{
|
||||
}
|
||||
StringOutputStream(std::size_t capacity) : m_string(capacity)
|
||||
{
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
m_string.push_range(buffer, buffer + length);
|
||||
return length;
|
||||
}
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return m_string.begin();
|
||||
}
|
||||
const_iterator begin() const
|
||||
{
|
||||
return m_string.begin();
|
||||
}
|
||||
iterator end()
|
||||
{
|
||||
return m_string.end();
|
||||
}
|
||||
const_iterator end() const
|
||||
{
|
||||
return m_string.end();
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return m_string.empty();
|
||||
}
|
||||
char* c_str()
|
||||
{
|
||||
return m_string.c_str();
|
||||
}
|
||||
const char* c_str() const
|
||||
{
|
||||
return m_string.c_str();
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
m_string.clear();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline StringOutputStream& operator<<(StringOutputStream& ostream, const T& t)
|
||||
{
|
||||
return ostream_write(ostream, t);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
22
libs/stream/textfilestream.cpp
Normal file
22
libs/stream/textfilestream.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "textfilestream.h"
|
||||
87
libs/stream/textfilestream.h
Normal file
87
libs/stream/textfilestream.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_STREAM_TEXTFILESTREAM_H)
|
||||
#define INCLUDED_STREAM_TEXTFILESTREAM_H
|
||||
|
||||
#include "itextstream.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/// \brief A wrapper around a file input stream opened for reading in text mode. Similar to std::ifstream.
|
||||
class TextFileInputStream : public TextInputStream
|
||||
{
|
||||
FILE* m_file;
|
||||
public:
|
||||
TextFileInputStream(const char* name)
|
||||
{
|
||||
m_file = name[0] == '\0' ? 0 : fopen(name, "rt");
|
||||
}
|
||||
~TextFileInputStream()
|
||||
{
|
||||
if(!failed())
|
||||
fclose(m_file);
|
||||
}
|
||||
|
||||
bool failed() const
|
||||
{
|
||||
return m_file == 0;
|
||||
}
|
||||
|
||||
std::size_t read(char* buffer, std::size_t length)
|
||||
{
|
||||
return fread(buffer, 1, length, m_file);
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief A wrapper around a file input stream opened for writing in text mode. Similar to std::ofstream.
|
||||
class TextFileOutputStream : public TextOutputStream
|
||||
{
|
||||
FILE* m_file;
|
||||
public:
|
||||
TextFileOutputStream(const char* name)
|
||||
{
|
||||
m_file = name[0] == '\0' ? 0 : fopen(name, "wt");
|
||||
}
|
||||
~TextFileOutputStream()
|
||||
{
|
||||
if(!failed())
|
||||
fclose(m_file);
|
||||
}
|
||||
|
||||
bool failed() const
|
||||
{
|
||||
return m_file == 0;
|
||||
}
|
||||
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
return fwrite(buffer, 1, length, m_file);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline TextFileOutputStream& operator<<(TextFileOutputStream& ostream, const T& t)
|
||||
{
|
||||
return ostream_write(ostream, t);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
22
libs/stream/textstream.cpp
Normal file
22
libs/stream/textstream.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "textstream.h"
|
||||
533
libs/stream/textstream.h
Normal file
533
libs/stream/textstream.h
Normal file
@@ -0,0 +1,533 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
GtkRadiant is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_STREAM_TEXTSTREAM_H)
|
||||
#define INCLUDED_STREAM_TEXTSTREAM_H
|
||||
|
||||
/// \file
|
||||
/// \brief Text-output-formatting.
|
||||
|
||||
#include "itextstream.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "generic/arrayrange.h"
|
||||
|
||||
namespace TextOutputDetail
|
||||
{
|
||||
inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal)
|
||||
{
|
||||
for (; decimal != 0; decimal /= 10)
|
||||
{
|
||||
*--ptr = char('0' + int(decimal % 10));
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#if defined (_WIN64) || defined (__LP64__)
|
||||
inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal)
|
||||
{
|
||||
for (; decimal != 0; decimal /= 10)
|
||||
{
|
||||
*--ptr = char('0' + (size_t)(decimal % 10));
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline char* write_signed_nonzero_decimal_backward(char* ptr, int decimal, bool show_positive)
|
||||
{
|
||||
const bool negative = decimal < 0 ;
|
||||
ptr = write_unsigned_nonzero_decimal_backward(ptr, negative ? -decimal : decimal);
|
||||
if(negative)
|
||||
{
|
||||
*--ptr = '-';
|
||||
}
|
||||
else if(show_positive)
|
||||
{
|
||||
*--ptr = '+';
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline char* write_unsigned_nonzero_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
|
||||
{
|
||||
ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal);
|
||||
if(show_positive)
|
||||
{
|
||||
*--ptr = '+';
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#if defined (_WIN64) || defined (__LP64__)
|
||||
inline char* write_size_t_nonzero_decimal_backward(char* ptr, size_t decimal, bool show_positive)
|
||||
{
|
||||
ptr = write_size_t_nonzero_decimal_backward(ptr, decimal);
|
||||
if(show_positive)
|
||||
{
|
||||
*--ptr = '+';
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline char* write_signed_decimal_backward(char* ptr, int decimal, bool show_positive)
|
||||
{
|
||||
if(decimal == 0)
|
||||
{
|
||||
*--ptr = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = write_signed_nonzero_decimal_backward(ptr, decimal, show_positive);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline char* write_unsigned_decimal_backward(char* ptr, unsigned int decimal, bool show_positive)
|
||||
{
|
||||
if(decimal == 0)
|
||||
{
|
||||
*--ptr = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = write_unsigned_nonzero_decimal_backward(ptr, decimal, show_positive);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#if defined (_WIN64) || defined (__LP64__)
|
||||
inline char* write_size_t_decimal_backward(char* ptr, size_t decimal, bool show_positive)
|
||||
{
|
||||
if(decimal == 0)
|
||||
{
|
||||
*--ptr = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = write_size_t_nonzero_decimal_backward(ptr, decimal, show_positive);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
/// \brief Writes a single character \p c to \p ostream.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, char c)
|
||||
{
|
||||
ostream.write(&c, 1);
|
||||
return ostream;
|
||||
}
|
||||
|
||||
/// \brief Writes a double-precision floating point value \p d to \p ostream.
|
||||
/// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const double d)
|
||||
{
|
||||
const std::size_t bufferSize = 16;
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%g", d));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
/// \brief Writes a single-precision floating point value \p f to \p ostream.
|
||||
/// The value will be formatted either as decimal with trailing zeros removed, or with scientific 'e' notation, whichever is shorter.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const float f)
|
||||
{
|
||||
return ostream_write(ostream, static_cast<double>(f));
|
||||
}
|
||||
|
||||
/// \brief Writes a signed integer \p i to \p ostream in decimal form.
|
||||
/// A '-' sign will be added if the value is negative.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const int i)
|
||||
{
|
||||
const std::size_t bufferSize = 16;
|
||||
#if 1
|
||||
char buf[bufferSize];
|
||||
char* begin = TextOutputDetail::write_signed_decimal_backward(buf + bufferSize, i, false);
|
||||
ostream.write(begin, (buf + bufferSize) - begin);
|
||||
#else
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%i", i));
|
||||
#endif
|
||||
return ostream;
|
||||
}
|
||||
|
||||
typedef unsigned int Unsigned;
|
||||
|
||||
/// \brief Writes an unsigned integer \p i to \p ostream in decimal form.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Unsigned i)
|
||||
{
|
||||
const std::size_t bufferSize = 16;
|
||||
#if 1
|
||||
char buf[bufferSize];
|
||||
char* begin = TextOutputDetail::write_unsigned_decimal_backward(buf + bufferSize, i, false);
|
||||
ostream.write(begin, (buf + bufferSize) - begin);
|
||||
#else
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
|
||||
#endif
|
||||
return ostream;
|
||||
}
|
||||
|
||||
#if defined (_WIN64) || defined (__LP64__)
|
||||
|
||||
/// \brief Writes a size_t \p i to \p ostream in decimal form.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const size_t i)
|
||||
{
|
||||
// max is 18446744073709551615, buffer of 32 chars should always be enough
|
||||
const std::size_t bufferSize = 32;
|
||||
#if 1
|
||||
char buf[bufferSize];
|
||||
char* begin = TextOutputDetail::write_size_t_decimal_backward(buf + bufferSize, i, false);
|
||||
ostream.write(begin, (buf + bufferSize) - begin);
|
||||
#else
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%u", i));
|
||||
#endif
|
||||
return ostream;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// \brief Writes a null-terminated \p string to \p ostream.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const char* string)
|
||||
{
|
||||
ostream.write(string, strlen(string));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
class HexChar
|
||||
{
|
||||
public:
|
||||
char m_value;
|
||||
HexChar(char value) : m_value(value)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief Writes a single character \p c to \p ostream in hexadecimal form.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const HexChar& c)
|
||||
{
|
||||
const std::size_t bufferSize = 16;
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%X", c.m_value & 0xFF));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class LeftJustified
|
||||
{
|
||||
public:
|
||||
const T& m_t;
|
||||
std::size_t m_size;
|
||||
LeftJustified(const T& t, std::size_t size) : m_t(t), m_size(size)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
LeftJustified<T> makeLeftJustified(const T& t, std::size_t size)
|
||||
{
|
||||
return LeftJustified<T>(t, size);
|
||||
}
|
||||
|
||||
template<typename TextOutputStreamType>
|
||||
class CountingOutputStream
|
||||
{
|
||||
TextOutputStreamType& m_ostream;
|
||||
public:
|
||||
std::size_t m_count;
|
||||
CountingOutputStream(TextOutputStreamType& ostream) : m_ostream(ostream)
|
||||
{
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
m_count += length;
|
||||
return m_ostream.write(buffer, length);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TextOutputStreamType, typename T>
|
||||
inline CountingOutputStream<TextOutputStreamType>& operator<<(CountingOutputStream<TextOutputStreamType>& ostream, const T& t)
|
||||
{
|
||||
return ostream_write(ostream, t);
|
||||
}
|
||||
|
||||
|
||||
/// \brief Writes any type to \p ostream padded with spaces afterwards.
|
||||
template<typename TextOutputStreamType, typename T>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LeftJustified<T>& justified)
|
||||
{
|
||||
CountingOutputStream<TextOutputStreamType> count(ostream);
|
||||
count << justified.m_t;
|
||||
while(justified.m_size > count.m_count)
|
||||
{
|
||||
count << ' ';
|
||||
}
|
||||
return ostream;
|
||||
}
|
||||
|
||||
class FloatFormat
|
||||
{
|
||||
public:
|
||||
double m_f;
|
||||
int m_width;
|
||||
int m_precision;
|
||||
FloatFormat(double f, int width, int precision)
|
||||
: m_f(f), m_width(width), m_precision(precision)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief Writes a floating point value to \p ostream with a specific width and precision.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FloatFormat& formatted)
|
||||
{
|
||||
const std::size_t bufferSize = 32;
|
||||
char buf[bufferSize];
|
||||
ostream.write(buf, snprintf(buf, bufferSize, "%*.*lf", formatted.m_width, formatted.m_precision, formatted.m_f));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
// never displays exponent, prints up to 10 decimal places
|
||||
class Decimal
|
||||
{
|
||||
public:
|
||||
double m_f;
|
||||
Decimal(double f) : m_f(f)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Decimal& decimal)
|
||||
{
|
||||
const int bufferSize = 22;
|
||||
char buf[bufferSize];
|
||||
std::size_t length = snprintf(buf, bufferSize, "%10.10lf", decimal.m_f);
|
||||
const char* first = buf;
|
||||
for(; *first == ' '; ++first)
|
||||
{
|
||||
}
|
||||
const char* last = buf + length - 1;
|
||||
for(; *last == '0'; --last)
|
||||
{
|
||||
}
|
||||
if(*last == '.')
|
||||
{
|
||||
--last;
|
||||
}
|
||||
ostream.write(first, last - first + 1);
|
||||
return ostream;
|
||||
}
|
||||
|
||||
|
||||
/// \brief Writes a \p range of characters to \p ostream.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const StringRange& range)
|
||||
{
|
||||
ostream.write(range.first, range.last - range.first);
|
||||
return ostream;
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
class Quoted
|
||||
{
|
||||
public:
|
||||
const Type& m_type;
|
||||
Quoted(const Type& type)
|
||||
: m_type(type)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
inline Quoted<Type> makeQuoted(const Type& type)
|
||||
{
|
||||
return Quoted<Type>(type);
|
||||
}
|
||||
|
||||
/// \brief Writes any type to \p ostream with a quotation mark character before and after it.
|
||||
template<typename TextOutputStreamType, typename Type>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Quoted<Type>& quoted)
|
||||
{
|
||||
return ostream << '"' << quoted.m_type << '"';
|
||||
}
|
||||
|
||||
|
||||
class LowerCase
|
||||
{
|
||||
public:
|
||||
const char* m_string;
|
||||
LowerCase(const char* string) : m_string(string)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief Writes a string to \p ostream converted to lower-case.
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const LowerCase& lower)
|
||||
{
|
||||
for(const char* p = lower.m_string; *p != '\0'; ++p)
|
||||
{
|
||||
ostream << static_cast<char>(std::tolower(*p));
|
||||
}
|
||||
return ostream;
|
||||
}
|
||||
|
||||
|
||||
/// \brief A wrapper for a TextInputStream optimised for reading a single character at a time.
|
||||
template<typename TextInputStreamType, int SIZE = 1024>
|
||||
class SingleCharacterInputStream
|
||||
{
|
||||
TextInputStreamType& m_inputStream;
|
||||
char m_buffer[SIZE];
|
||||
char* m_cur;
|
||||
char* m_end;
|
||||
|
||||
bool fillBuffer()
|
||||
{
|
||||
m_end = m_buffer + m_inputStream.read(m_buffer, SIZE);
|
||||
m_cur = m_buffer;
|
||||
return m_cur != m_end;
|
||||
}
|
||||
public:
|
||||
|
||||
SingleCharacterInputStream(TextInputStreamType& inputStream) : m_inputStream(inputStream), m_cur(m_buffer), m_end(m_buffer)
|
||||
{
|
||||
}
|
||||
bool readChar(char& c)
|
||||
{
|
||||
if(m_cur == m_end && !fillBuffer())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
c = *m_cur++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
|
||||
class SingleCharacterOutputStream : public TextOutputStream
|
||||
{
|
||||
enum unnamed0 { m_bufsize = 1024 };
|
||||
TextOutputStream& m_ostream;
|
||||
char m_buffer[m_bufsize];
|
||||
char* m_pos;
|
||||
const char* m_end;
|
||||
|
||||
const char* end() const
|
||||
{
|
||||
return m_end;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
m_pos = m_buffer;
|
||||
}
|
||||
void flush()
|
||||
{
|
||||
m_ostream.write(m_buffer, m_pos - m_buffer);
|
||||
reset();
|
||||
}
|
||||
public:
|
||||
SingleCharacterOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
|
||||
{
|
||||
}
|
||||
~SingleCharacterOutputStream()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
void write(const char c)
|
||||
{
|
||||
if(m_pos == end())
|
||||
{
|
||||
flush();
|
||||
}
|
||||
*m_pos++ = c;
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
const char*const end = buffer + length;
|
||||
for(const char* p = buffer; p != end; ++p)
|
||||
{
|
||||
write(*p);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
/// \brief A wrapper for a TextOutputStream, optimised for writing a few characters at a time.
|
||||
template<typename TextOutputStreamType, int SIZE = 1024>
|
||||
class BufferedTextOutputStream : public TextOutputStream
|
||||
{
|
||||
TextOutputStreamType outputStream;
|
||||
char m_buffer[SIZE];
|
||||
char* m_cur;
|
||||
|
||||
public:
|
||||
BufferedTextOutputStream(TextOutputStreamType& outputStream) : outputStream(outputStream), m_cur(m_buffer)
|
||||
{
|
||||
}
|
||||
~BufferedTextOutputStream()
|
||||
{
|
||||
outputStream.write(m_buffer, m_cur - m_buffer);
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
std::size_t remaining = length;
|
||||
for(;;)
|
||||
{
|
||||
std::size_t n = std::min(remaining, std::size_t((m_buffer + SIZE) - m_cur));
|
||||
m_cur = std::copy(buffer, buffer + n, m_cur);
|
||||
remaining -= n;
|
||||
if(remaining == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
outputStream.write(m_buffer, SIZE);
|
||||
m_cur = m_buffer;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user