From 38470a8a80ed969eeb0e5f87ee353e71b0bb83a5 Mon Sep 17 00:00:00 2001 From: "M. Liebmann" Date: Sat, 3 Sep 2016 11:44:58 +0200 Subject: [PATCH] helpers.cpp: Add itoa() function --- src/system/helpers.cpp | 32 ++++++++++++++++++++++++++++++++ src/system/helpers.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 9a4b054f5..5a88ef0db 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -951,6 +951,38 @@ std::string to_string(unsigned long long i) return s.str(); } +/** + * C++ version 0.4 std::string style "itoa": + * Contributions from Stuart Lowe, Ray-Yuan Sheu, + + * Rodrigo de Salvo Braz, Luc Gallant, John Maloney + * and Brian Hunt + */ +std::string itoa(int value, int base) +{ + std::string buf; + + // check that the base if valid + if (base < 2 || base > 16) return buf; + + enum { kMaxDigits = 35 }; + buf.reserve( kMaxDigits ); // Pre-allocate enough space. + + int quotient = value; + + // Translating number to string with base: + do { + buf += "0123456789abcdef"[ std::abs( quotient % base ) ]; + quotient /= base; + } while ( quotient ); + + // Append the negative sign + if ( value < 0) buf += '-'; + + std::reverse( buf.begin(), buf.end() ); + return buf; +} + std::string getJFFS2MountPoint(int mtdPos) { FILE* fd = fopen("/proc/mounts", "r"); diff --git a/src/system/helpers.h b/src/system/helpers.h index 3b26a12a6..65146830e 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -105,6 +105,8 @@ std::string to_string(unsigned long); std::string to_string(long long); std::string to_string(unsigned long long); +std::string itoa(int value, int base); + inline int atoi(std::string &s) { return atoi(s.c_str()); } inline int atoi(const std::string &s) { return atoi(s.c_str()); } inline int access(std::string &s, int mode) { return access(s.c_str(), mode); }