Remove sstream dependency.
authorCarl Hetherington <cth@carlh.net>
Tue, 12 Mar 2019 16:24:46 +0000 (16:24 +0000)
committerCarl Hetherington <cth@carlh.net>
Tue, 12 Mar 2019 16:24:46 +0000 (16:24 +0000)
12 files changed:
src/compose.hpp
src/raw_convert.h
src/ssa_reader.cc
src/ssa_reader.h
src/stl_binary_writer.cc
src/subrip_reader.cc
src/subrip_reader.h
src/util.cc
src/util.h
src/wscript
test/test.cc
wscript

index ebabe671a7f40545b0a546294a485375de192134..bcddd4348e5ed549fa5849767e7e874de870529e 100644 (file)
@@ -1,4 +1,5 @@
-/* Defines String::compose(fmt, arg...) for easy, i18n-friendly
+/* -*- c-basic-offset: 2 -*-
+ * Defines String::compose(fmt, arg...) for easy, i18n-friendly
  * composition of strings.
  *
  * Version 1.0.
 #ifndef STRING_COMPOSE_H
 #define STRING_COMPOSE_H
 
-#include <locked_sstream.h>
+#include "locale_convert.h"
+#include <boost/filesystem.hpp>
 #include <string>
 #include <list>
-#include <map>                 // for multimap
+#include <map>
+#include <inttypes.h>
+#include <cstdio>
 
 namespace StringPrivate
 {
@@ -56,7 +60,7 @@ namespace StringPrivate
     std::string str() const;
 
   private:
-    locked_stringstream os;
+    std::string os;
     int arg_no;
 
     // we store the output as a list - when the output string is requested, the
@@ -110,26 +114,21 @@ namespace StringPrivate
     }
   }
 
-
   // implementation of class Composition
   template <typename T>
   inline Composition &Composition::arg(const T &obj)
   {
-    os << obj;
-
-    std::string rep = os.str();
+    os += sub::locale_convert<std::string>(obj);
 
-    if (!rep.empty()) {                // manipulators don't produce output
-      for (specification_map::const_iterator i = specs.lower_bound(arg_no),
-            end = specs.upper_bound(arg_no); i != end; ++i) {
+    if (!os.empty()) {         // manipulators don't produce output
+      for (specification_map::const_iterator i = specs.lower_bound(arg_no), end = specs.upper_bound(arg_no); i != end; ++i) {
        output_list::iterator pos = i->second;
        ++pos;
 
-       output.insert(pos, rep);
+       output.insert(pos, os);
       }
 
-      os.str(std::string());
-      //os.clear();
+      os = "";
       ++arg_no;
     }
 
index 3afb8f787e704a24c7624b42085bd174cedbe269..c392500989baffc3646e21a429845ca778f9b001 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -32,15 +32,24 @@ template <typename P, typename Q>
 P
 raw_convert (Q v, int precision = 16)
 {
-       locked_stringstream s;
-       s.imbue (std::locale::classic ());
-       s << std::setprecision (precision);
-       s << v;
-       P r;
-       s >> r;
-       return r;
+       /* We can't write a generic version of raw_convert; all required
+          versions must be specialised.
+       */
+       BOOST_STATIC_ASSERT (sizeof (Q) == 0);
 }
 
+template <>
+int
+raw_convert (std::string v, int);
+
+template <>
+float
+raw_convert (std::string v, int);
+
+template <>
+std::string
+raw_convert (unsigned long v, int);
+
 };
 
 #endif
index a1672d84758c393786fceb6cec9e3bb9ba325879..f592b9e9371e86109bb374d60717014a45760982 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2016-2019 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -22,7 +22,6 @@
 #include "sub_assert.h"
 #include "raw_convert.h"
 #include "subtitle.h"
-#include <locked_sstream.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/bind.hpp>
 #include <boost/foreach.hpp>
@@ -40,10 +39,9 @@ using namespace boost::algorithm;
 using namespace sub;
 
 /** @param s Subtitle string encoded in UTF-8 */
-SSAReader::SSAReader (string const & s)
+SSAReader::SSAReader (string s)
 {
-       locked_stringstream str (s);
-       this->read (boost::bind (&get_line_stringstream, &str));
+       this->read (boost::bind(&get_line_string, &s));
 }
 
 /** @param f Subtitle file encoded in UTF-8 */
index 07a0901d41d5f573e8819f74d970162134f5469c..ee1b178fb80899147d2aef66850de3be476f0ef9 100644 (file)
@@ -39,7 +39,7 @@ class SSAReader : public Reader
 {
 public:
        SSAReader (FILE* f);
-       SSAReader (std::string const & subs);
+       SSAReader (std::string subs);
 
        static std::list<RawSubtitle> parse_line (RawSubtitle base, std::string line, int play_res_x, int play_res_y);
 
index d4fe9ed69c9185ee2101ff584d5c846c1ce88bde..88e8ce59a4789bd1913c596e5e478ea88d801781 100644 (file)
@@ -28,6 +28,7 @@
 #include "compose.hpp"
 #include "sub_assert.h"
 #include <boost/locale.hpp>
+#include <boost/algorithm/string.hpp>
 #include <list>
 #include <cmath>
 #include <fstream>
@@ -70,16 +71,32 @@ put_string (char* p, unsigned int n, string s)
        memset (p + s.length(), ' ', n - s.length ());
 }
 
+/** @param v Value
+ *  @param n Width to zero-pad v to.
+ */
 static void
 put_int_as_string (char* p, int v, unsigned int n)
 {
-       locked_stringstream s;
-       /* Be careful to ensure we get no thousands separators */
-       s.imbue (std::locale::classic ());
-       s << setw (n) << setfill ('0');
-       s << v;
-       SUB_ASSERT (s.str().length() == n);
-       put_string (p, s.str ());
+       char buffer[64];
+
+       switch (n) {
+       case 2:
+               snprintf (buffer, sizeof(buffer), "%02d", v);
+               break;
+       case 5:
+               snprintf (buffer, sizeof(buffer), "%05d", v);
+               break;
+       default:
+               SUB_ASSERT (false);
+       }
+
+       string s = buffer;
+
+       struct lconv* lc = localeconv ();
+       boost::algorithm::replace_all (s, lc->thousands_sep, "");
+       boost::algorithm::replace_all (s, lc->decimal_point, ".");
+
+       put_string (p, s);
 }
 
 static void
index 56256799bc5ae9628a84339dc01662adffb0eab5..16ba0a5beb6431083f1043e3dfcdb266072593aa 100644 (file)
@@ -24,7 +24,6 @@
 #include "subrip_reader.h"
 #include "exceptions.h"
 #include "util.h"
-#include <locked_sstream.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/regex.hpp>
@@ -46,10 +45,9 @@ using boost::algorithm::replace_all;
 using namespace sub;
 
 /** @param s Subtitle string encoded in UTF-8 */
-SubripReader::SubripReader (string const & s)
+SubripReader::SubripReader (string s)
 {
-       locked_stringstream str (s);
-       this->read (boost::bind (&get_line_stringstream, &str));
+       this->read (boost::bind(&get_line_string, &s));
 }
 
 /** @param f Subtitle file encoded in UTF-8 */
index 138d703c148b16dfec8d5f9fab9a760d14861504..1ee14fb9acf7c5ac53146ae2311cac127206e8f9 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -36,7 +36,7 @@ class SubripReader : public Reader
 {
 public:
        SubripReader (FILE* f);
-       SubripReader (std::string const & subs);
+       SubripReader (std::string subs);
 
 private:
        /* For tests */
index 57f5e1c91ef16d6367e983c67c29b054cf04fa29..70cbf1020be0e0aaa8b8976424d95f54b89a5a00 100644 (file)
@@ -18,7 +18,6 @@
 */
 
 #include "util.h"
-#include <locked_sstream.h>
 #include <string>
 #include <iostream>
 #include <cstdio>
@@ -43,15 +42,22 @@ sub::empty_or_white_space (string s)
 }
 
 optional<string>
-sub::get_line_stringstream (locked_stringstream* str)
+sub::get_line_string (string* s)
 {
-       if (!str->good ()) {
-               return optional<string> ();
+       if (s->length() == 0) {
+               return optional<string>();
+       }
+
+       size_t pos = s->find ("\n");
+       if (pos == string::npos) {
+               string const c = *s;
+               *s = "";
+               return c;
        }
 
-       string s;
-       getline (*str, s);
-       return s;
+       string const c = s->substr (0, pos);
+       s->erase (0, pos + 1);
+       return c;
 }
 
 optional<string>
index 0512255114605bed62b26f4c3666e5bdb5971a06..9819e3cc345108c044ae3a9eba25a1dad3f807c2 100644 (file)
 #include <boost/optional.hpp>
 #include <string>
 
-class locked_stringstream;
-
 namespace sub {
 
 extern bool empty_or_white_space (std::string s);
 extern void remove_unicode_bom (boost::optional<std::string>& line);
-extern boost::optional<std::string> get_line_stringstream (locked_stringstream* str);
 extern boost::optional<std::string> get_line_file (FILE* f);
+extern boost::optional<std::string> get_line_string (std::string* s);
 
 }
index 655c5b5452978f1c65132e4222490d36a60fa430..491111744d0e4c28d111afd7fe3fbf219d18ee15 100644 (file)
@@ -20,7 +20,9 @@ def build(bld):
                  horizontal_position.cc
                  iso6937.cc
                  iso6937_tables.cc
+                 locale_convert.cc
                  rational.cc
+                 raw_convert.cc
                  raw_subtitle.cc
                  reader.cc
                  reader_factory.cc
index 3774a66ec283179294231ae7dd29425c39689498..bc2acda81e4e8a786cc0be1db2b29c0f8117b8c5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 #define BOOST_TEST_DYN_LINK
 #define BOOST_TEST_MODULE libsub_test
-#include <locked_sstream.h>
+#include "iso6937_tables.h"
+#include "compose.hpp"
 #include <boost/test/unit_test.hpp>
 #include <boost/filesystem.hpp>
 #include <fstream>
 #include <string>
 #include <iostream>
-#include "iso6937_tables.h"
 
 using std::string;
 using std::cerr;
@@ -106,12 +106,10 @@ check_file (boost::filesystem::path ref, boost::filesystem::path check)
                BOOST_CHECK_EQUAL (r, this_time);
 
                for (uintmax_t i = 0; i < this_time; ++i) {
-                       locked_stringstream s;
-                       s << "Files differ at offset " << (offset + i)
-                         << "; reference is " << hex << ((int) ref_buffer[i])
-                         << ", check is " << hex << ((int) check_buffer[i]);
-
-                       BOOST_CHECK_MESSAGE (ref_buffer[i] == check_buffer[i], s.str ());
+                       string const s = String::compose (
+                               "Files differ at offset %1; reference is %2, check is %3", (offset + i), ((int) ref_buffer[i]), ((int) check_buffer[i])
+                               );
+                       BOOST_CHECK_MESSAGE (ref_buffer[i] == check_buffer[i], s);
                }
 
                offset += this_time;
diff --git a/wscript b/wscript
index 8b5169b1241c98fc5082f08e26d58481ba38bdab..392c31ff781dd13486b107b5b1f4d83ac3fb72b9 100644 (file)
--- a/wscript
+++ b/wscript
@@ -93,6 +93,11 @@ def configure(conf):
     conf.env.DISABLE_TESTS = conf.options.disable_tests
     conf.env.API_VERSION = API_VERSION
 
+    if conf.options.target_windows:
+        conf.env.append_value('CXXFLAGS', '-DLIBSUB_WINDOWS')
+    else:
+        conf.env.append_value('CXXFLAGS', '-DLIBSUB_POSIX')
+
     if conf.options.enable_debug:
         conf.env.append_value('CXXFLAGS', '-g')
     else: