From 52f46ed143719523a84603e680b7a380438231c8 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 7 May 2009 08:18:35 +0200 Subject: [PATCH] Created functions for formatting floating point numbers in a non-scientific way. --- src/common/string_formatting.cpp | 52 ++++++++++++++++++++++++++++++-- src/common/string_formatting.h | 7 ++++- src/extract/timecodes_v2.cpp | 21 ++----------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/common/string_formatting.cpp b/src/common/string_formatting.cpp index d4edb09fa..641738993 100644 --- a/src/common/string_formatting.cpp +++ b/src/common/string_formatting.cpp @@ -40,8 +40,56 @@ format_timecode(int64_t timecode, } std::string -to_string(int64_t i) { - return (boost::format("%1%") % i).str(); +to_string(int64_t value) { + return (boost::format("%1%") % value).str(); +} + +std::string +to_string(int value) { + return (boost::format("%1%") % value).str(); +} + +std::string +to_string(uint64_t value) { + return (boost::format("%1%") % value).str(); +} + +std::string +to_string(unsigned int value) { + return (boost::format("%1%") % value).str(); +} + +std::string +to_string(double value, + unsigned int precision) { + int64_t scale = 1; + for (int i = 0; i < precision; ++i) + scale *= 10; + + return to_string((int64_t)(value * scale), scale, precision); +} + +std::string +to_string(int64_t numerator, + int64_t denominator, + unsigned int precision) { + std::string output = (boost::format("%1%") % (numerator / denominator)).str(); + int64_t fractional_part = numerator % denominator; + + if (0 != fractional_part) { + std::string format = (boost::format(".%%0%1%d") % precision).str(); + output += (boost::format(format) % fractional_part).str(); + std::string::iterator end = output.end() - 1; + + while (*end == '0') + --end; + if (*end == '.') + --end; + + output.erase(end + 1, output.end()); + } + + return output; } void diff --git a/src/common/string_formatting.h b/src/common/string_formatting.h index f44a60032..637cb8600 100644 --- a/src/common/string_formatting.h +++ b/src/common/string_formatting.h @@ -36,6 +36,11 @@ std::string MTX_DLL_API format_timecode(int64_t timecode, unsigned int precision void MTX_DLL_API fix_format(const char *fmt, std::string &new_fmt); -std::string MTX_DLL_API to_string(int64_t i); +std::string MTX_DLL_API to_string(int value); +std::string MTX_DLL_API to_string(unsigned int value); +std::string MTX_DLL_API to_string(int64_t value); +std::string MTX_DLL_API to_string(uint64_t value); +std::string MTX_DLL_API to_string(double value, unsigned int precision); +std::string MTX_DLL_API to_string(int64_t numerator, int64_t denominator, unsigned int precision); #endif // __MTX_COMMON_STRING_FORMATTING_H diff --git a/src/extract/timecodes_v2.cpp b/src/extract/timecodes_v2.cpp index dd848e6ee..222ce0894 100644 --- a/src/extract/timecodes_v2.cpp +++ b/src/extract/timecodes_v2.cpp @@ -37,6 +37,7 @@ #include "common/ebml.h" #include "common/matroska.h" #include "common/mm_io.h" +#include "common/string_formatting.h" #include "extract/mkvextract.h" #include "extract/xtr_base.h" @@ -57,24 +58,6 @@ static vector timecode_extractors; // ------------------------------------------------------------------------ -static std::string -format_timecode_for_timecode_file(int64_t timecode) { - std::string output = (boost::format("%1%") % (int64_t)(timecode / 1000000)).str(); - int64_t fractional_part = timecode % 1000000; - - if (0 != fractional_part) { - output += (boost::format(".%06d") % fractional_part).str(); - std::string::iterator zeroes = output.end() - 1; - - while (*zeroes == '0') - --zeroes; - - output.erase(zeroes + 1, output.end()); - } - - return output; -} - static void close_timecode_files() { vector::iterator extractor; @@ -85,7 +68,7 @@ close_timecode_files() { sort(timecodes.begin(), timecodes.end()); mxforeach(timecode, timecodes) - extractor->m_file->puts(boost::format("%1%\n") % format_timecode_for_timecode_file(*timecode)); + extractor->m_file->puts(to_string(*timecode, 1000000, 6) + "\n"); delete extractor->m_file; } timecode_extractors.clear();