diff options
author | peavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-26 05:55:45 +0000 |
---|---|---|
committer | peavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-26 05:55:45 +0000 |
commit | 79c0364d54e9f0494c2e8da855cf09193e2d4704 (patch) | |
tree | 3f58706c5ead5d3d6e8b259d090cf6ce466a62fb /include | |
parent | 484229f5c65cfe3c88ed99af02502c62894f0494 (diff) |
Add Template itoa to convert integer to char*
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6119 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'include')
-rw-r--r-- | include/inspircd.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/include/inspircd.h b/include/inspircd.h index 3d9002f53..d3d424384 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -77,6 +77,25 @@ template<typename T> inline long ConvToInt(const T &in) return atoi(tmp.str().c_str()); } +/** Template function to convert integer to char, storing result in *res and + * also returning the pointer to res. Based on Stuart Lowe's C/C++ Pages. + */ +template<typename T, typename V, typename R> inline char* itoa(const T &in, V *res, R base) +{ + if (base < 2 || base > 16) { *res = 0; return res; } + char* out = res; + int quotient = in; + while (quotient) { + *out = "0123456789abcdef"[ std::abs( quotient % base ) ]; + ++out; + quotient /= base; + } + if ( in < 0 && base == 10) *out++ = '-'; + std::reverse( res, out ); + *out = 0; + return res; +} + /** This class contains various STATS counters * It is used by the InspIRCd class, which internally * has an instance of it. |