diff options
author | Karl Berry <karl@freefriends.org> | 2018-01-17 22:50:15 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2018-01-17 22:50:15 +0000 |
commit | e7dfe5ab98e759081fc73990ed6c1ac6910a105d (patch) | |
tree | 1635f79c86856a6aa57a45b8b952b651ef9fcba9 /Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp | |
parent | ae542350b1e3add10d4ee3d7b68f610132f31a50 (diff) |
dvisvgm 2.3.1 (patched)
git-svn-id: svn://tug.org/texlive/trunk@46352 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp')
-rw-r--r-- | Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp | 71 |
1 files changed, 68 insertions, 3 deletions
diff --git a/Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp b/Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp index c6b9ea63fe9..9517a6a1013 100644 --- a/Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp +++ b/Build/source/texk/dvisvgm/dvisvgm-src/src/utility.cpp @@ -2,7 +2,7 @@ ** utility.cpp ** ** ** ** This file is part of dvisvgm -- a fast DVI to SVG converter ** -** Copyright (C) 2005-2017 Martin Gieseking <martin.gieseking@uos.de> ** +** Copyright (C) 2005-2018 Martin Gieseking <martin.gieseking@uos.de> ** ** ** ** This program is free software; you can redistribute it and/or ** ** modify it under the terms of the GNU General Public License as ** @@ -20,7 +20,9 @@ #include <algorithm> #include <cctype> +#include <fstream> #include <functional> +#include <iterator> #include "utility.hpp" using namespace std; @@ -56,12 +58,57 @@ string util::normalize_space (string str, const char *ws) { } -string& util::tolower (string &str) { - transform(str.begin(), str.end(), str.begin(), ::tolower); +/** Replaces all occurences of a substring with another string. + * @param[in] str string to search through + * @param[in] find string to look for + * @param[in] repl replacement for "find" + * @return the resulting string */ +string util::replace (string str, const string &find, const string &repl) { + if (!find.empty() && !repl.empty()) { + size_t first = str.find(find); + while (first != string::npos) { + str.replace(first, find.length(), repl); + first = str.find(find, first+repl.length()); + } + } return str; } +/** Splits a string at all occurences of a given separator string and + * returns the substrings. + * @param[in] str string to split + * @param[in] sep separator to look for + * @return the substrings between the separators */ +vector<string> util::split (const string &str, const string &sep) { + vector<string> parts; + if (str.empty() || sep.empty()) + parts.emplace_back(str); + else { + size_t left=0; + while (left <= str.length()) { + size_t right = str.find(sep, left); + if (right == string::npos) { + parts.emplace_back(str.substr(left)); + left = string::npos; + } + else { + parts.emplace_back(str.substr(left, right-left)); + left = right+sep.length(); + } + } + } + return parts; +} + + +string util::tolower (const string &str) { + string ret=str; + transform(str.begin(), str.end(), ret.begin(), ::tolower); + return ret; +} + + /** Returns the integer part of log10 of a given integer \f$n>0\f$. * If \f$n<0\f$, the result is 0. */ int util::ilog10 (int n) { @@ -72,3 +119,21 @@ int util::ilog10 (int n) { } return result; } + + +/** Returns the contents of a file. + * @param[in] fname name/path of the file */ +string util::read_file_contents (const string &fname) { + ifstream ifs(fname.c_str(), ios::binary); + return string(istreambuf_iterator<char>(ifs.rdbuf()), istreambuf_iterator<char>()); +} + + +/** Writes a sequence of bytes given as a string to a file. + * @param[in] name/path of the file to write + * @param[in] start iterator pointing to the begin of the byte sequence + * @param[in] end iterator pointing to the first byte after the byte sequence to write */ +void util::write_file_contents (const string &fname, string::iterator start, string::iterator end) { + ofstream ofs(fname.c_str(), ios::binary); + copy(start, end, ostream_iterator<char>(ofs)); +} |