]> git.lyx.org Git - lyx.git/blobdiff - src/texstream.cpp
Cmake build: Prepare for hardening use of external programs
[lyx.git] / src / texstream.cpp
index 16f8dfbe68dee11acfa0a76fbf5e829d2f15ab6e..5b0e92759173f211ec2afeebc2d17d5f72b4ea37 100644 (file)
@@ -11,6 +11,9 @@
 #include <config.h>
 
 #include "texstream.h"
+
+#include "TexRow.h"
+
 #include "support/lstrings.h"
 #include "support/unicode.h"
 
@@ -30,10 +33,19 @@ using lyx::support::split;
 namespace lyx {
 
 
-void otexrowstream::append(docstring const & str, TexRow const & texrow)
+otexrowstream::otexrowstream(odocstream & os)
+       : os_(os), texrow_(make_unique<TexRow>())
+{}
+
+
+otexrowstream::~otexrowstream() = default;
+
+
+unique_ptr<TexRow> otexrowstream::releaseTexRow()
 {
-       os_ << str;
-       texrow_.append(texrow);
+       auto p = make_unique<TexRow>();
+       swap(texrow_, p);
+       return p;
 }
 
 
@@ -41,7 +53,7 @@ void otexrowstream::put(char_type const & c)
 {
        os_.put(c);
        if (c == '\n')
-               texrow_.newline();
+               texrow_->newline();
 }
 
 
@@ -57,6 +69,24 @@ void otexstream::put(char_type const & c)
 }
 
 
+size_t otexstringstream::length()
+{
+       auto pos = ods_.tellp();
+       return (pos >= 0) ? size_t(pos) : 0;
+}
+
+
+TexString otexstringstream::release()
+{
+       TexString ts(ods_.str(), move(texrow()));
+       // reset this
+       texrow() = TexRow();
+       ods_.clear();
+       ods_.str(docstring());
+       return ts;
+}
+
+
 BreakLine breakln;
 SafeBreakLine safebreakln;
 
@@ -93,6 +123,7 @@ otexrowstream & operator<<(otexrowstream & ots, odocstream_manip pf)
        return ots;
 }
 
+
 otexstream & operator<<(otexstream & ots, odocstream_manip pf)
 {
        otexrowstream & otrs = ots;
@@ -104,6 +135,38 @@ otexstream & operator<<(otexstream & ots, odocstream_manip pf)
 }
 
 
+otexrowstream & operator<<(otexrowstream & ots, TexString ts)
+{
+       ts.validate();
+       ots.os() << move(ts.str);
+       ots.texrow().append(move(ts.texrow));
+       return ots;
+}
+
+
+otexstream & operator<<(otexstream & ots, TexString ts)
+{
+       size_t const len = ts.str.length();
+       // Check whether there is something to output
+       if (len == 0)
+               return ots;
+
+       otexrowstream & otrs = ots;
+       if (ots.protectSpace()) {
+               if (!ots.canBreakLine() && ts.str[0] == ' ')
+                       otrs << "{}";
+               ots.protectSpace(false);
+       }
+
+       if (len > 1)
+               ots.canBreakLine(ts.str[len - 2] != '\n');
+       ots.lastChar(ts.str[len - 1]);
+
+       otrs << move(ts);
+       return ots;
+}
+
+
 otexrowstream & operator<<(otexrowstream & ots, docstring const & s)
 {
        ots.os() << s;