16 #ifndef ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_ 17 #define ECL_CONVERTERS_CONVERT_CHAR_STRINGS_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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {
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';
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';
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';
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';
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 {
833 char* operator ()(
const bool &input){
834 *buffer_begin =
'\0';
835 input ? strcat(buffer_begin,
"true") : strcat (buffer_begin,
"false");
875 Converter(
char* begin,
char* end) : converters::CharStringBuffer(begin,end) {
Fast conversion to text format with character strings.
Converter(char *begin, char *end)
Converter(char *begin, char *end)
Converter(char *begin, char *end)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Fast conversion to text format with character strings.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(char *begin, char *end)
Primary templates for the family of converter classes.
Fast conversion to text format with character strings.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Fast conversion to text format with character strings.
Converter(char *begin, char *end)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(char *begin, char *end)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(char *begin, char *end)
Primary template and general fallback for converter classes.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Initialises with an internal buffer.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Fast conversion to text format with character strings.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Initialises with an internal buffer.
Converter(char *begin, char *end)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Fast conversion to text format with character strings.
Fast conversion to text format with character strings.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Fast conversion to text format with character strings.
Fast conversion to text format with character strings.
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(char *begin, char *end)
Converter(char *begin, char *end)
Initialises with an external buffer.
Converter(char *begin, char *end)
Fast conversion to text format with character strings.
#define ecl_debug_throw(exception)
Fast conversion to text format with character strings.
Fast conversion to text format with character strings.
Conversion to text format with character strings.
Converter(char *begin, char *end)
Initialises with an external buffer.
Converter(char *begin, char *end)