char_strings.hpp
Go to the documentation of this file.
1 
12 /*****************************************************************************
13 ** Ifdefs
14 *****************************************************************************/
15 
16 #ifndef ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_
17 #define ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_
18 
19 /*****************************************************************************
20 ** Includes
21 *****************************************************************************/
22 
23 #include <cstdio> // vsnprintf
24 #include <cstring> //strcat
25 #include <new>
26 #include "converter.hpp"
27 #include <ecl/errors/handlers.hpp>
29 #include <ecl/mpl/converters.hpp>
30 
31 #if _MSC_VER
32 #define snprintf _snprintf
33 #endif
34 
35 /*****************************************************************************
36 ** Namespaces
37 *****************************************************************************/
38 
39 namespace ecl {
40 
44 namespace converters {
45 
46 /*****************************************************************************
47 ** Inheritable Buffer Interface
48 *****************************************************************************/
57 class CharStringBuffer : public ConverterBase {
58  protected:
59  /******************************************
60  ** C&D's
61  *******************************************/
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 ) {
81  ecl_debug_throw(ecl::StandardException(LOC,ecl::MemoryError,"Failed to allocate memory for the char string buffer."));
82  error_handler = ecl::MemoryError;
83  }
84  }
88  virtual ~CharStringBuffer() {
89  if(delete_buffer) {
90  delete[] buffer_begin;
91  delete_buffer = false;
92  }
93  }
94 
95  char *buffer_begin;
96  char *buffer_end;
97  bool delete_buffer;
98 };
99 
100 /*****************************************************************************
101 ** Character String Converter Utilities
102 *****************************************************************************/
114 template <typename Number>
115 char* convertUnsignedIntegral(Number number, char* buffer_begin, char* buffer_end)
116 {
117  *buffer_end = 0; // Set to the null terminator
118  Number lsd;
119  char* str_ptr = buffer_end;
120 
121  do {
122  lsd = static_cast<Number>(number % 10); // Determine the least significant digit.
123  number = static_cast<Number>(number / 10); // Deal with next most significant.
124  --str_ptr;
125  if ( str_ptr < buffer_begin ) { return NULL; }
126  *str_ptr = '0'+lsd;
127  } while (number != 0);
128  return str_ptr;
129 };
130 
139 template <typename Number>
140 char* convertSignedIntegral(const Number &number, char* buffer_begin, char* buffer_end)
141 {
142  typedef typename Unsigned<Number>::type UnsignedNumber;
143 
144  char *s;
145  if ( number >= 0 ) {
146  s = convertUnsignedIntegral(static_cast<UnsignedNumber>(number),buffer_begin,buffer_end);
147  } else {
148  s = convertUnsignedIntegral(static_cast<UnsignedNumber>(-1*number),buffer_begin+1,buffer_end);
149  --s;
150  *s = '-';
151  }
152  return s;
153 }
154 
155 
156 }; // namespace converters
157 
162 /*****************************************************************************
163 ** Char String Converters [char]
164 *****************************************************************************/
179 template<>
180 class Converter<char*,char> : public virtual converters::CharStringBuffer {
181  public:
182  /******************************************
183  ** C&D's
184  *******************************************/
190  Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
196  Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) :
197  converters::CharStringBuffer(buffer_size) {}
198 
199  virtual ~Converter() {}
200 
201  /******************************************
202  ** Converters
203  *******************************************/
209  char* operator ()(const char &input) {
210  return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end);
211  }
212 };
213 /*****************************************************************************
214 ** Char String Converters [unsigned char]
215 *****************************************************************************/
230 template<>
231 class Converter<char*,unsigned char> : public virtual converters::CharStringBuffer {
232  public:
233  /******************************************
234  ** C&D's
235  *******************************************/
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) {}
248 
249  virtual ~Converter() {}
250 
251  /******************************************
252  ** Converters
253  *******************************************/
259  char* operator ()(const unsigned char &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
260 };
261 /*****************************************************************************
262 ** Char String Converters [short]
263 *****************************************************************************/
278 template<>
279 class Converter<char*,short> : public virtual converters::CharStringBuffer {
280  public:
281  /******************************************
282  ** C&D's
283  *******************************************/
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) {}
296 
297  virtual ~Converter() {}
298 
299  /******************************************
300  ** Converters
301  *******************************************/
307  char* operator ()(const short &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
308 };
309 /*****************************************************************************
310 ** Char String Converters [unsigned short]
311 *****************************************************************************/
326 template<>
327 class Converter<char*,unsigned short> : public virtual converters::CharStringBuffer {
328  public:
329  /******************************************
330  ** C&D's
331  *******************************************/
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) {}
344 
345  virtual ~Converter() {}
346 
347  /******************************************
348  ** Converters
349  *******************************************/
355  char* operator ()(const unsigned short &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
356 };
357 
358 /*****************************************************************************
359 ** Char String Converters [int]
360 *****************************************************************************/
375 template<>
376 class Converter<char*,int> : public virtual converters::CharStringBuffer {
377  public:
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) {}
390 
391  virtual ~Converter() {}
392 
398  char* operator ()(const int &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
399 };
400 /*****************************************************************************
401 ** Char String Converters [unsigned int]
402 *****************************************************************************/
417 template<>
418 class Converter<char*,unsigned int> : public virtual converters::CharStringBuffer {
419  public:
420  /******************************************
421  ** C&D's
422  *******************************************/
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) {}
435 
436  virtual ~Converter() {}
437 
438  /******************************************
439  ** Converters
440  *******************************************/
446  char* operator ()(const unsigned int &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
447 };
448 
449 /*****************************************************************************
450 ** Char String Converters [long]
451 *****************************************************************************/
466 template<>
467 class Converter<char*,long> : public virtual converters::CharStringBuffer {
468  public:
469  /******************************************
470  ** C&D's
471  *******************************************/
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) {}
484 
485  virtual ~Converter() {}
486 
487  /******************************************
488  ** Converters
489  *******************************************/
495  char* operator ()(const long &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
496 };
497 /*****************************************************************************
498 ** Char String Converters [unsigned long]
499 *****************************************************************************/
514 template<>
515 class Converter<char*,unsigned long> : public virtual converters::CharStringBuffer {
516  public:
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) {}
529 
530  virtual ~Converter() {}
531 
537  char* operator ()(const unsigned long &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
538 };
539 /*****************************************************************************
540 ** Char String Converters [long long]
541 *****************************************************************************/
556 template<>
557 class Converter<char*,long long> : public virtual converters::CharStringBuffer {
558  public:
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) {}
571 
572  virtual ~Converter() {}
573 
579  char* operator ()(const long long &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
580 };
581 /*****************************************************************************
582 ** Char String Converters [unsigned long long]
583 *****************************************************************************/
598 template<>
599 class Converter<char*,unsigned long long> : public virtual converters::CharStringBuffer {
600  public:
606  Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
607  }
613  Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
614  }
615 
616  virtual ~Converter() {}
617 
623  char* operator ()(const unsigned long long &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
624 };
625 /*****************************************************************************
626 ** Char String Converters [float]
627 *****************************************************************************/
637 template<>
638 class Converter<char*,float> : public virtual converters::CharStringBuffer {
639  public:
640  /******************************************
641  ** C&D's
642  *******************************************/
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';
655  }
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';
668  }
669 
670  virtual ~Converter() {}
671 
672  /******************************************
673  ** Converters
674  *******************************************/
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';
700  } else {
701  format_specifier[1] = '.';
702  format_specifier[2] = '2';
703  format_specifier[3] = '0';
704  format_specifier[4] = 'f';
705  }
706 
707  snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
708  return this->buffer_begin;
709 // return converters::convertDecimal<32>(input,this->buffer_begin,this->buffer_end);
710  }
711  private:
712  char format_specifier[6];
713 };
714 /*****************************************************************************
715 ** Char String Converters [double]
716 *****************************************************************************/
726 template<>
727 class Converter<char*,double> : public virtual converters::CharStringBuffer {
728  public:
729  /******************************************
730  ** C&D's
731  *******************************************/
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';
744  }
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';
757  }
758 
759  virtual ~Converter() {}
760 
761  /******************************************
762  ** Converters
763  *******************************************/
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';
789  } else {
790  format_specifier[1] = '.';
791  format_specifier[2] = '2';
792  format_specifier[3] = '0';
793  format_specifier[4] = 'f';
794  }
795  snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
796  return this->buffer_begin;
797 // return converters::convertDecimal<64>(input,this->buffer_begin,this->buffer_end);
798  }
799  private:
800  char format_specifier[6];
801 };
802 /*****************************************************************************
803 ** Char String Converters [bool]
804 *****************************************************************************/
818 template<>
819 class Converter<char*,bool> : public virtual converters::CharStringBuffer {
820  public:
824  Converter() : converters::CharStringBuffer(6) {}
825 
826  virtual ~Converter() {}
827 
833  char* operator ()(const bool &input){
834  *buffer_begin = '\0';
835  input ? strcat(buffer_begin,"true") : strcat (buffer_begin,"false");
836  return buffer_begin;
837  }
838 };
839 
840 /*****************************************************************************
841  * Char String Converter Family
842  ****************************************************************************/
853 template <>
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>,
863  public Converter<char*,float>,
867  public Converter<char*,bool>
868 {
869  public:
875  Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
876  }
883  Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
884  }
885 
886  virtual ~Converter() {}
887 
901 };
902 
903 }; // Namespace ecl
904 
905 #endif /*ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_*/
Fast conversion to text format with character strings.
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)
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(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Converter(int buffer_size=250) ecl_debug_throw_decl(StandardException)
Primary template and general fallback for converter classes.
Definition: converter.hpp:56
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.
MemoryError
Converter(char *begin, char *end)
Initialises with an external buffer.
Converter(char *begin, char *end)


ecl_converters
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:21