summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2002-01-31 14:33:09 +0000
committerMatthew Newhook <matthew@zeroc.com>2002-01-31 14:33:09 +0000
commitbe05625de2f5e114fce865c39e9583df775e397e (patch)
treedf55de494238311208db5546e56ac97badea4ec7 /cpp
parentMove Slice/OutputUtil to IceUtil. Integrate IceXML/Output.cpp with (diff)
downloadice-be05625de2f5e114fce865c39e9583df775e397e.tar.bz2
ice-be05625de2f5e114fce865c39e9583df775e397e.tar.xz
ice-be05625de2f5e114fce865c39e9583df775e397e.zip
Move Slice/OutputUtil to IceUtil.
Diffstat (limited to 'cpp')
-rw-r--r--cpp/include/IceUtil/OutputUtil.h257
-rw-r--r--cpp/src/IceUtil/OutputUtil.cpp367
2 files changed, 624 insertions, 0 deletions
diff --git a/cpp/include/IceUtil/OutputUtil.h b/cpp/include/IceUtil/OutputUtil.h
new file mode 100644
index 00000000000..e6dfec7b585
--- /dev/null
+++ b/cpp/include/IceUtil/OutputUtil.h
@@ -0,0 +1,257 @@
+// **********************************************************************
+//
+// Copyright (c) 2001
+// MutableRealms, Inc.
+// Huntsville, AL, USA
+//
+// All Rights Reserved
+//
+// **********************************************************************
+
+#ifndef SLICE_OUTPUT_UTIL_H
+#define SLICE_OUTPUT_UTIL_H
+
+#include <IceUtil/Config.h>
+#include <fstream>
+#include <stack>
+
+namespace IceUtil
+{
+
+//
+// Common.
+//
+class ICE_UTIL_API NextLine { };
+class ICE_UTIL_API Separator { };
+
+extern ICE_UTIL_API NextLine nl;
+extern ICE_UTIL_API Separator sp;
+
+//
+// XMLOutput
+//
+class ICE_UTIL_API StartElement
+{
+public:
+
+ StartElement(const std::string& name)
+ : _name(name)
+ {
+ }
+
+ ~StartElement()
+ {
+ }
+
+ const std::string& getName() const { return _name; }
+
+private:
+
+ const std::string _name;
+};
+
+class ICE_UTIL_API EndElement { };
+
+typedef StartElement se;
+extern ICE_UTIL_API EndElement ee;
+
+//
+// For Output
+//
+class ICE_UTIL_API EndBlock { };
+class ICE_UTIL_API StartBlock { };
+
+extern ICE_UTIL_API StartBlock sb;
+extern ICE_UTIL_API EndBlock eb;
+
+// ----------------------------------------------------------------------
+// Indent
+// ----------------------------------------------------------------------
+
+//
+// Technically it's not necessary to have print() & nl() as virtual
+// since the opeator<< functions are specific to each OutputBase
+// derivative. However, since it's possible to call print() & nl()
+// manually I've decided to leave them as virtual.
+//
+
+class ICE_UTIL_API OutputBase : public ::IceUtil::noncopyable
+{
+public:
+
+ OutputBase();
+ OutputBase(std::ostream&);
+ OutputBase(const char*);
+
+ virtual ~OutputBase() { }
+
+ void setIndent(int); // what is the indent level?
+ void setUseTab(bool); // should we output tabs?
+
+ void open(const char*); // Open output stream
+
+ virtual void print(const char*); // Print a string
+
+ void inc(); // Increment indentation level
+ void dec(); // Decrement indentation level
+
+ void useCurrentPosAsIndent(); // Save the current position as indentation
+ void zeroIndent(); // Use zero identation
+ void restoreIndent(); // Restore indentation
+
+ virtual void nl(); // Print newline
+ void sp(); // Print separator
+
+ bool operator!() const; // Check whether the output state is ok
+
+protected:
+
+ std::ofstream _fout;
+ std::ostream& _out;
+ int _pos;
+ int _indent;
+ std::stack<int> _indentSave;
+ bool _separator;
+
+ bool _useTab;
+ int _indentSize;
+};
+
+class ICE_UTIL_API Output : public OutputBase
+{
+public:
+
+ Output();
+ Output(std::ostream&);
+ Output(const char*);
+
+ void setBeginBlock(const char *); // what do we use at block starts?
+ void setEndBlock(const char *); // what do we use the block end?
+
+ void sb(); // Start a block
+ void eb(); // End a block
+
+private:
+
+ std::string _blockStart;
+ std::string _blockEnd;
+};
+
+class ICE_UTIL_API XMLOutput : public OutputBase
+{
+public:
+
+ XMLOutput();
+ XMLOutput(std::ostream&);
+ XMLOutput(const char*);
+
+ void setSGML(bool);
+ virtual void print(const char*); // Print a string
+
+ virtual void nl(); // Print newline
+
+ void se(const std::string&); // Start an element
+ void ee(); // End an element
+
+private:
+
+ std::stack<std::string> _elementStack;
+ bool _printed;
+ bool _sgml;
+};
+
+//
+// Unfortunately, it's not possible to define operator<< for
+// OutputBase for nl, seperator, etc.
+//
+template<typename T>
+Output&
+operator<<(Output& out, const T& val)
+{
+ std::ostringstream s;
+ s << val;
+ out.print(s.str().c_str());
+ return out;
+}
+
+template<>
+inline Output&
+operator<<(Output& o, const NextLine&)
+{
+ o.nl();
+ return o;
+}
+
+template<>
+inline Output&
+operator<<(Output& o, const Separator&)
+{
+ o.sp();
+ return o;
+}
+
+template<>
+inline Output&
+operator<<(Output& o, const StartBlock&)
+{
+ o.sb();
+ return o;
+}
+
+template<>
+inline Output&
+operator<<(Output& o, const EndBlock&)
+{
+ o.eb();
+ return o;
+}
+
+ICE_UTIL_API Output& operator<<(Output&, std::ios_base& (*)(std::ios_base&));
+
+template<typename T>
+XMLOutput&
+operator<<(XMLOutput& out, const T& val)
+{
+ std::ostringstream s;
+ s << val;
+ out.print(s.str().c_str());
+ return out;
+}
+
+template<>
+inline XMLOutput&
+operator<<(XMLOutput& o, const NextLine&)
+{
+ o.nl();
+ return o;
+}
+
+template<>
+inline XMLOutput&
+operator<<(XMLOutput& o, const Separator&)
+{
+ o.sp();
+ return o;
+}
+
+template<>
+inline XMLOutput&
+operator<<(XMLOutput& o, const StartElement& e)
+{
+ o.se(e.getName());
+ return o;
+}
+
+template<>
+inline XMLOutput&
+operator<<(XMLOutput& o, const EndElement&)
+{
+ o.ee();
+ return o;
+}
+
+ICE_UTIL_API XMLOutput& operator<<(XMLOutput&, std::ios_base& (*)(std::ios_base&));
+
+}
+
+#endif
diff --git a/cpp/src/IceUtil/OutputUtil.cpp b/cpp/src/IceUtil/OutputUtil.cpp
new file mode 100644
index 00000000000..3e8f2146b5d
--- /dev/null
+++ b/cpp/src/IceUtil/OutputUtil.cpp
@@ -0,0 +1,367 @@
+// **********************************************************************
+//
+// Copyright (c) 2001
+// MutableRealms, Inc.
+// Huntsville, AL, USA
+//
+// All Rights Reserved
+//
+// **********************************************************************
+
+#include <IceUtil/OutputUtil.h>
+
+using namespace std;
+using namespace IceUtil;
+
+namespace IceUtil
+{
+
+NextLine nl;
+StartBlock sb;
+EndBlock eb;
+Separator sp;
+EndElement ee;
+
+}
+
+// ----------------------------------------------------------------------
+// Output
+// ----------------------------------------------------------------------
+
+IceUtil::OutputBase::OutputBase() :
+ _out(_fout),
+ _pos(0),
+ _indent(0),
+ _separator(true),
+ _useTab(true),
+ _indentSize(4)
+{
+}
+
+IceUtil::OutputBase::OutputBase(ostream& os) :
+ _out(os),
+ _pos(0),
+ _indent(0),
+ _separator(true),
+ _useTab(true),
+ _indentSize(4)
+{
+}
+
+
+IceUtil::OutputBase::OutputBase(const char* s) :
+ _out(_fout),
+ _pos(0),
+ _indent(0),
+ _separator(true),
+ _useTab(true),
+ _indentSize(4)
+{
+ open(s);
+}
+
+void
+IceUtil::OutputBase::open(const char* s)
+{
+ _fout.open(s);
+}
+
+void
+IceUtil::OutputBase::print(const char* s)
+{
+ for (unsigned int i = 0; i < strlen(s); ++i)
+ {
+ if (s[i] == '\n')
+ {
+ _pos = 0;
+ }
+ else
+ {
+ ++_pos;
+ }
+ }
+
+ _out << s;
+}
+
+void
+IceUtil::OutputBase::inc()
+{
+ _indent += _indentSize;
+}
+
+void
+IceUtil::OutputBase::dec()
+{
+ assert(_indent >= _indentSize);
+ _indent -= _indentSize;
+}
+
+void
+IceUtil::OutputBase::useCurrentPosAsIndent()
+{
+ _indentSave.push(_indent);
+ _indent = _pos;
+}
+
+void
+IceUtil::OutputBase::zeroIndent()
+{
+ _indentSave.push(_indent);
+ _indent = 0;
+}
+
+void
+IceUtil::OutputBase::restoreIndent()
+{
+ assert(!_indentSave.empty());
+ _indent = _indentSave.top();
+ _indentSave.pop();
+}
+
+void
+IceUtil::OutputBase::setIndent(int indentSize)
+{
+ _indentSize = indentSize;
+}
+
+void
+IceUtil::OutputBase::setUseTab(bool useTab)
+{
+ _useTab = useTab;
+}
+
+void
+IceUtil::OutputBase::nl()
+{
+ _out << '\n';
+ _pos = 0;
+ _separator = true;
+
+ int indent = _indent;
+
+ if (_useTab)
+ {
+ while (indent >= 8)
+ {
+ indent -= 8;
+ _out << '\t';
+ _pos += 8;
+ }
+ }
+ else
+ {
+ while (indent >= _indentSize)
+ {
+ indent -= _indentSize;
+ _out << " ";
+ _pos += _indentSize;
+ }
+ }
+
+ while (indent > 0)
+ {
+ --indent;
+ _out << ' ';
+ ++_pos;
+ }
+
+ _out.flush();
+}
+
+void
+IceUtil::OutputBase::sp()
+{
+ if (_separator)
+ {
+ _out << '\n';
+ }
+}
+
+bool
+IceUtil::OutputBase::operator!() const
+{
+ return !_out;
+}
+
+IceUtil::Output::Output() :
+ OutputBase(),
+ _blockStart("{"),
+ _blockEnd("}")
+{
+}
+
+IceUtil::Output::Output(ostream& os) :
+ OutputBase(os),
+ _blockStart("{"),
+ _blockEnd("}")
+{
+}
+
+IceUtil::Output::Output(const char* s) :
+ OutputBase(s),
+ _blockStart("{"),
+ _blockEnd("}")
+{
+}
+
+void
+IceUtil::Output::setBeginBlock(const char *bb)
+{
+ _blockStart = bb;
+}
+
+void
+IceUtil::Output::setEndBlock(const char *eb)
+{
+ _blockEnd = eb;
+}
+
+void
+IceUtil::Output::sb()
+{
+ if (_blockStart.length())
+ {
+ nl();
+ _out << _blockStart;
+ }
+ ++_pos;
+ inc();
+ _separator = false;
+}
+
+void
+IceUtil::Output::eb()
+{
+ dec();
+ if (_blockEnd.length())
+ {
+ nl();
+ _out << _blockEnd;
+ }
+ --_pos;
+}
+
+IceUtil::XMLOutput::XMLOutput() :
+ OutputBase(),
+ _printed(true),
+ _sgml(false)
+{
+}
+
+IceUtil::XMLOutput::XMLOutput(ostream& os) :
+ OutputBase(os),
+ _printed(true)
+{
+}
+
+IceUtil::XMLOutput::XMLOutput(const char* s) :
+ OutputBase(s),
+ _printed(true),
+ _sgml(false)
+{
+}
+
+void
+IceUtil::XMLOutput::setSGML(bool sgml)
+{
+ _sgml = true;
+}
+
+void
+IceUtil::XMLOutput::print(const char* s)
+{
+ if (!_printed)
+ {
+ _out << '>';
+ _printed = true;
+ }
+ OutputBase::print(s);
+}
+
+void
+IceUtil::XMLOutput::nl()
+{
+ if (!_printed)
+ {
+ _printed = true;
+ _out << '>';
+ }
+ OutputBase::nl();
+}
+
+void
+IceUtil::XMLOutput::se(const std::string& element)
+{
+ nl();
+
+ //
+ // If we're not in SGML mode the output of the '>' character is
+ // deferred until either the //end-element (in which case a /> is
+ // emitted) or until something //is displayed.
+ //
+ _out << '<' << element;
+ if (_sgml)
+ {
+ _out << '>';
+ }
+ else
+ {
+ _printed = false;
+ }
+
+
+ string::size_type pos = element.find_first_of(" \t");
+ if (pos == string::npos)
+ {
+ _elementStack.push(element);
+ }
+ else
+ {
+ _elementStack.push(element.substr(0, pos));
+ }
+
+ ++_pos; // TODO: ???
+ inc();
+ _separator = false;
+}
+
+void
+IceUtil::XMLOutput::ee()
+{
+ string element = _elementStack.top();
+ _elementStack.pop();
+
+ dec();
+ if (!_printed)
+ {
+ _out << "/>";
+ }
+ else
+ {
+ nl();
+ _out << "</" << element << '>';
+ }
+ --_pos; // TODO: ???
+ _printed = true;
+}
+
+
+Output&
+IceUtil::operator<<(Output& out, ios_base& (*val)(ios_base&))
+{
+ ostringstream s;
+ s << val;
+ out.print(s.str().c_str());
+ return out;
+}
+
+
+XMLOutput&
+IceUtil::operator<<(XMLOutput& out, ios_base& (*val)(ios_base&))
+{
+ ostringstream s;
+ s << val;
+ out.print(s.str().c_str());
+ return out;
+}