16 #ifndef ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_ 17 #define ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_ 27 #include <ecl/errors/handlers.hpp> 28 #include <ecl/exceptions/standard_exception.hpp> 29 #include <ecl/mpl/converters.hpp> 32 #define snprintf _snprintf 44 namespace converters {
57 class CharStringBuffer :
public ConverterBase {
67 CharStringBuffer(
char* begin,
char* end) : buffer_begin(begin),buffer_end(end),delete_buffer(false) {}
77 CharStringBuffer(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : delete_buffer(true) {
78 buffer_begin =
new (std::nothrow)
char[buffer_size];
79 buffer_end = buffer_begin + (buffer_size-1);
80 if ( buffer_begin == NULL ) {
88 virtual ~CharStringBuffer() {
90 delete[] buffer_begin;
91 delete_buffer =
false;
114 template <
typename Number>
115 char* convertUnsignedIntegral(Number number,
char* buffer_begin,
char* buffer_end)
119 char* str_ptr = buffer_end;
122 lsd =
static_cast<Number
>(number % 10);
123 number =
static_cast<Number
>(number / 10);
125 if ( str_ptr < buffer_begin ) {
return NULL; }
127 }
while (number != 0);
139 template <
typename Number>
140 char* convertSignedIntegral(
const Number &number,
char* buffer_begin,
char* buffer_end)
142 typedef typename Unsigned<Number>::type UnsignedNumber;
146 s = convertUnsignedIntegral(static_cast<UnsignedNumber>(number),buffer_begin,buffer_end);
148 s = convertUnsignedIntegral(static_cast<UnsignedNumber>(-1*number),buffer_begin+1,buffer_end);
180 class Converter<char*,
char> :
public virtual converters::CharStringBuffer {
190 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
197 converters::CharStringBuffer(buffer_size) {}
199 virtual ~Converter() {}
209 char* operator ()(
const char &input) {
210 return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end);
231 class Converter<char*,
unsigned char> :
public virtual converters::CharStringBuffer {
241 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
247 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
249 virtual ~Converter() {}
259 char* operator ()(
const unsigned char &input){
return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
279 class Converter<char*,
short> :
public virtual converters::CharStringBuffer {
289 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
295 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
297 virtual ~Converter() {}
307 char* operator ()(
const short &input){
return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
327 class Converter<char*,
unsigned short> :
public virtual converters::CharStringBuffer {
337 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
343 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
345 virtual ~Converter() {}
355 char* operator ()(
const unsigned short &input){
return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
376 class Converter<char*,
int> :
public virtual converters::CharStringBuffer {
383 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
389 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
391 virtual ~Converter() {}
398 char* operator ()(
const int &input){
return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
418 class Converter<char*,
unsigned int> :
public virtual converters::CharStringBuffer {
428 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
434 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
436 virtual ~Converter() {}
446 char* operator ()(
const unsigned int &input){
return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
467 class Converter<char*,
long> :
public virtual converters::CharStringBuffer {
477 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
483 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
485 virtual ~Converter() {}
495 char* operator ()(
const long &input){
return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
515 class Converter<char*,
unsigned long> :
public virtual converters::CharStringBuffer {
522 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
528 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
530 virtual ~Converter() {}
537 char* operator ()(
const unsigned long &input){
return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
557 class Converter<char*,
long long> :
public virtual converters::CharStringBuffer {
564 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {}
570 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
572 virtual ~Converter() {}
579 char* operator ()(
const long long &input){
return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
599 class Converter<char*,
unsigned long long> :
public virtual converters::CharStringBuffer {
606 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {
613 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
616 virtual ~Converter() {}
623 char* operator ()(
const unsigned long long &input){
return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
638 class Converter<char*,
float> :
public virtual converters::CharStringBuffer {
648 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {
649 format_specifier[0] =
'%';
650 format_specifier[1] =
'.';
651 format_specifier[2] =
'x';
652 format_specifier[3] =
'f';
653 format_specifier[4] =
'f';
654 format_specifier[5] =
'\0';
661 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
662 format_specifier[0] =
'%';
663 format_specifier[1] =
'.';
664 format_specifier[2] =
'x';
665 format_specifier[3] =
'f';
666 format_specifier[4] =
'f';
667 format_specifier[5] =
'\0';
670 virtual ~Converter() {}
686 char* operator ()(
const float &input,
const int& precision = -1){
687 if ( precision < 0 ) {
688 format_specifier[1] =
'f';
689 format_specifier[2] =
'\0';
690 }
else if ( precision < 10 ) {
691 format_specifier[1] =
'.';
692 format_specifier[2] =
'0'+precision;
693 format_specifier[3] =
'f';
694 format_specifier[4] =
'\0';
695 }
else if ( precision < 20 ) {
696 format_specifier[1] =
'.';
697 format_specifier[2] =
'1';
698 format_specifier[3] =
'0'+(precision - 10);
699 format_specifier[4] =
'f';
701 format_specifier[1] =
'.';
702 format_specifier[2] =
'2';
703 format_specifier[3] =
'0';
704 format_specifier[4] =
'f';
707 snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
708 return this->buffer_begin;
712 char format_specifier[6];
727 class Converter<char*,
double> :
public virtual converters::CharStringBuffer {
737 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {
738 format_specifier[0] =
'%';
739 format_specifier[1] =
'.';
740 format_specifier[2] =
'x';
741 format_specifier[3] =
'f';
742 format_specifier[4] =
'f';
743 format_specifier[5] =
'\0';
750 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
751 format_specifier[0] =
'%';
752 format_specifier[1] =
'.';
753 format_specifier[2] =
'x';
754 format_specifier[3] =
'f';
755 format_specifier[4] =
'f';
756 format_specifier[5] =
'\0';
759 virtual ~Converter() {}
775 char* operator ()(
const double &input,
const int& precision = -1){
776 if ( precision < 0 ) {
777 format_specifier[1] =
'f';
778 format_specifier[2] =
'\0';
779 }
else if ( precision < 10 ) {
780 format_specifier[1] =
'.';
781 format_specifier[2] =
'0'+precision;
782 format_specifier[3] =
'f';
783 format_specifier[4] =
'\0';
784 }
else if ( precision < 20 ) {
785 format_specifier[1] =
'.';
786 format_specifier[2] =
'1';
787 format_specifier[3] =
'0'+(precision - 10);
788 format_specifier[4] =
'f';
790 format_specifier[1] =
'.';
791 format_specifier[2] =
'2';
792 format_specifier[3] =
'0';
793 format_specifier[4] =
'f';
795 snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
796 return this->buffer_begin;
800 char format_specifier[6];
819 class Converter<char*,
bool> :
public virtual converters::CharStringBuffer {
824 Converter() : converters::CharStringBuffer(6) {}
826 virtual ~Converter() {}
833 char* operator ()(
const bool &input){
834 *buffer_begin =
'\0';
835 input ? strcat(buffer_begin,
"true") : strcat (buffer_begin,
"false");
854 class Converter<char*,
void> :
855 public Converter<char*,char>,
856 public Converter<char*,short>,
857 public Converter<char*,int>,
858 public Converter<char*,long>,
859 public Converter<char*,unsigned char>,
860 public Converter<char*,unsigned short>,
861 public Converter<char*,unsigned int>,
862 public Converter<char*,unsigned long>,
863 public Converter<char*,float>,
864 public Converter<char*,double>,
865 public Converter<char*,long long>,
866 public Converter<char*,unsigned long long>,
867 public Converter<char*,bool>
875 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {
883 Converter(
int buffer_size = 250)
ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
886 virtual ~Converter() {}
888 using Converter<char*,char>::operator();
889 using Converter<char*,short>::operator();
890 using Converter<char*,int>::operator();
891 using Converter<char*,long>::operator();
892 using Converter<char*,unsigned char>::operator();
893 using Converter<char*,unsigned short>::operator();
894 using Converter<char*,unsigned int>::operator();
895 using Converter<char*,unsigned long>::operator();
896 using Converter<char*,float>::operator();
897 using Converter<char*,double>::operator();
898 using Converter<char*,long long>::operator();
899 using Converter<char*,unsigned long long>::operator();
900 using Converter<char*,bool>::operator();
#define LOC
Stringify the line of code you are at.
Standard exception type, provides code location and error string.
#define ecl_debug_throw_decl(exception)
Debug mode throw exception declaration.
#define ecl_debug_throw(exception)
Debug mode exception throw.