From 5038e68ad9eb66e007211c8f1b707612a0c01e29 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 12 Mar 2019 16:24:46 +0000 Subject: [PATCH] Remove sstream dependency. --- src/compose.hpp | 27 +++++++++++++-------------- src/raw_convert.h | 25 +++++++++++++++++-------- src/ssa_reader.cc | 8 +++----- src/ssa_reader.h | 2 +- src/stl_binary_writer.cc | 31 ++++++++++++++++++++++++------- src/subrip_reader.cc | 6 ++---- src/subrip_reader.h | 4 ++-- src/util.cc | 20 +++++++++++++------- src/util.h | 4 +--- src/wscript | 2 ++ test/test.cc | 16 +++++++--------- wscript | 5 +++++ 12 files changed, 90 insertions(+), 60 deletions(-) diff --git a/src/compose.hpp b/src/compose.hpp index ebabe67..bcddd43 100644 --- a/src/compose.hpp +++ b/src/compose.hpp @@ -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. @@ -33,10 +34,13 @@ #ifndef STRING_COMPOSE_H #define STRING_COMPOSE_H -#include +#include "locale_convert.h" +#include #include #include -#include // for multimap +#include +#include +#include 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 inline Composition &Composition::arg(const T &obj) { - os << obj; - - std::string rep = os.str(); + os += sub::locale_convert(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; } diff --git a/src/raw_convert.h b/src/raw_convert.h index 3afb8f7..c392500 100644 --- a/src/raw_convert.h +++ b/src/raw_convert.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2019 Carl Hetherington 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 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 diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc index a1672d8..f592b9e 100644 --- a/src/ssa_reader.cc +++ b/src/ssa_reader.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2016 Carl Hetherington + Copyright (C) 2016-2019 Carl Hetherington 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 #include #include #include @@ -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 */ diff --git a/src/ssa_reader.h b/src/ssa_reader.h index 07a0901..ee1b178 100644 --- a/src/ssa_reader.h +++ b/src/ssa_reader.h @@ -39,7 +39,7 @@ class SSAReader : public Reader { public: SSAReader (FILE* f); - SSAReader (std::string const & subs); + SSAReader (std::string subs); static std::list parse_line (RawSubtitle base, std::string line, int play_res_x, int play_res_y); diff --git a/src/stl_binary_writer.cc b/src/stl_binary_writer.cc index d4fe9ed..88e8ce5 100644 --- a/src/stl_binary_writer.cc +++ b/src/stl_binary_writer.cc @@ -28,6 +28,7 @@ #include "compose.hpp" #include "sub_assert.h" #include +#include #include #include #include @@ -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 diff --git a/src/subrip_reader.cc b/src/subrip_reader.cc index 5625679..16ba0a5 100644 --- a/src/subrip_reader.cc +++ b/src/subrip_reader.cc @@ -24,7 +24,6 @@ #include "subrip_reader.h" #include "exceptions.h" #include "util.h" -#include #include #include #include @@ -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 */ diff --git a/src/subrip_reader.h b/src/subrip_reader.h index 138d703..1ee14fb 100644 --- a/src/subrip_reader.h +++ b/src/subrip_reader.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2019 Carl Hetherington 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 */ diff --git a/src/util.cc b/src/util.cc index 57f5e1c..70cbf10 100644 --- a/src/util.cc +++ b/src/util.cc @@ -18,7 +18,6 @@ */ #include "util.h" -#include #include #include #include @@ -43,15 +42,22 @@ sub::empty_or_white_space (string s) } optional -sub::get_line_stringstream (locked_stringstream* str) +sub::get_line_string (string* s) { - if (!str->good ()) { - return optional (); + if (s->length() == 0) { + return optional(); + } + + 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 diff --git a/src/util.h b/src/util.h index 0512255..9819e3c 100644 --- a/src/util.h +++ b/src/util.h @@ -24,13 +24,11 @@ #include #include -class locked_stringstream; - namespace sub { extern bool empty_or_white_space (std::string s); extern void remove_unicode_bom (boost::optional& line); -extern boost::optional get_line_stringstream (locked_stringstream* str); extern boost::optional get_line_file (FILE* f); +extern boost::optional get_line_string (std::string* s); } diff --git a/src/wscript b/src/wscript index 655c5b5..4911117 100644 --- a/src/wscript +++ b/src/wscript @@ -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 diff --git a/test/test.cc b/test/test.cc index 3774a66..bc2acda 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2019 Carl Hetherington 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 @@ -19,13 +19,13 @@ #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE libsub_test -#include +#include "iso6937_tables.h" +#include "compose.hpp" #include #include #include #include #include -#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 8b5169b..392c31f 100644 --- 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: -- 2.30.2