summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2019-03-12 16:24:46 +0000
committerCarl Hetherington <cth@carlh.net>2019-03-12 16:24:46 +0000
commit5038e68ad9eb66e007211c8f1b707612a0c01e29 (patch)
tree70d5aa5d3f414defc3d3cd0b34de0725bf8760a9 /src
parenta8b629085c404d7a947ed242fbf64d8e0e91edcd (diff)
Remove sstream dependency.
Diffstat (limited to 'src')
-rw-r--r--src/compose.hpp27
-rw-r--r--src/raw_convert.h25
-rw-r--r--src/ssa_reader.cc8
-rw-r--r--src/ssa_reader.h2
-rw-r--r--src/stl_binary_writer.cc31
-rw-r--r--src/subrip_reader.cc6
-rw-r--r--src/subrip_reader.h4
-rw-r--r--src/util.cc20
-rw-r--r--src/util.h4
-rw-r--r--src/wscript2
10 files changed, 78 insertions, 51 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 <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;
}
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 <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
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 <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 */
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<RawSubtitle> 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 <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
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 <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 */
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 <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 */
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 <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>
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 <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);
}
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