helpers.cpp: Add itoa() function

Origin commit data
------------------
Branch: ni/coolstream
Commit: 38470a8a80
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2016-09-03 (Sat, 03 Sep 2016)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
Michael Liebmann
2016-09-03 11:44:58 +02:00
parent 29984e3ff5
commit 0d3818862d
2 changed files with 34 additions and 0 deletions

View File

@@ -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");