char_strings.hpp
Go to the documentation of this file.
00001 
00012 /*****************************************************************************
00013 ** Ifdefs
00014 *****************************************************************************/
00015 
00016 #ifndef ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_
00017 #define ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_
00018 
00019 /*****************************************************************************
00020 ** Includes
00021 *****************************************************************************/
00022 
00023 #include <cstdio> // vsnprintf
00024 #include <cstring> //strcat
00025 #include <new>
00026 #include "converter.hpp"
00027 #include <ecl/errors/handlers.hpp>
00028 #include <ecl/exceptions/standard_exception.hpp>
00029 #include <ecl/mpl/converters.hpp>
00030 
00031 #if _MSC_VER
00032 #define snprintf _snprintf
00033 #endif
00034 
00035 /*****************************************************************************
00036 ** Namespaces
00037 *****************************************************************************/
00038 
00039 namespace ecl {
00040 
00044 namespace converters {
00045 
00046 /*****************************************************************************
00047 ** Inheritable Buffer Interface
00048 *****************************************************************************/
00057 class CharStringBuffer : public ConverterBase {
00058     protected:
00059         /******************************************
00060         ** C&D's
00061         *******************************************/
00067         CharStringBuffer(char* begin, char* end) : buffer_begin(begin),buffer_end(end),delete_buffer(false) {}
00077         CharStringBuffer(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : delete_buffer(true) {
00078                 buffer_begin = new (std::nothrow) char[buffer_size];
00079                 buffer_end = buffer_begin + (buffer_size-1);
00080                 if ( buffer_begin == NULL ) {
00081                         ecl_debug_throw(ecl::StandardException(LOC,ecl::MemoryError,"Failed to allocate memory for the char string buffer."));
00082                         error_handler = ecl::MemoryError;
00083                 }
00084         }
00088         virtual ~CharStringBuffer() {
00089                 if(delete_buffer) {
00090                         delete[] buffer_begin;
00091                         delete_buffer = false;
00092                 }
00093         }
00094 
00095         char *buffer_begin;
00096         char *buffer_end;
00097         bool delete_buffer;
00098 };
00099 
00100 /*****************************************************************************
00101 ** Character String Converter Utilities
00102 *****************************************************************************/
00114 template <typename Number>
00115 char* convertUnsignedIntegral(Number number, char* buffer_begin, char* buffer_end)
00116 {
00117     *buffer_end = 0; // Set to the null terminator
00118     Number lsd;
00119     char* str_ptr = buffer_end;
00120 
00121     do {
00122         lsd = static_cast<Number>(number % 10);     // Determine the least significant digit.
00123         number = static_cast<Number>(number / 10);  // Deal with next most significant.
00124         --str_ptr;
00125         if ( str_ptr < buffer_begin ) { return NULL; }
00126         *str_ptr = '0'+lsd;
00127     } while (number != 0);
00128     return str_ptr;
00129 };
00130 
00139 template <typename Number>
00140 char* convertSignedIntegral(const Number &number, char* buffer_begin, char* buffer_end)
00141 {
00142     typedef typename Unsigned<Number>::type UnsignedNumber;
00143 
00144     char *s;
00145     if ( number >= 0 ) {
00146         s = convertUnsignedIntegral(static_cast<UnsignedNumber>(number),buffer_begin,buffer_end);
00147     } else {
00148         s = convertUnsignedIntegral(static_cast<UnsignedNumber>(-1*number),buffer_begin+1,buffer_end);
00149         --s;
00150         *s = '-';
00151     }
00152     return s;
00153 }
00154 
00155 
00156 }; // namespace converters
00157 
00162 /*****************************************************************************
00163 ** Char String Converters [char]
00164 *****************************************************************************/
00179 template<>
00180 class Converter<char*,char> : public virtual converters::CharStringBuffer {
00181     public:
00182         /******************************************
00183         ** C&D's
00184         *******************************************/
00190         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00196         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) :
00197                         converters::CharStringBuffer(buffer_size) {}
00198 
00199         virtual ~Converter() {}
00200 
00201         /******************************************
00202         ** Converters
00203         *******************************************/
00209         char* operator ()(const char &input) {
00210             return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end);
00211         }
00212 };
00213 /*****************************************************************************
00214 ** Char String Converters [unsigned char]
00215 *****************************************************************************/
00230 template<>
00231 class Converter<char*,unsigned char> : public virtual converters::CharStringBuffer {
00232     public:
00233         /******************************************
00234         ** C&D's
00235         *******************************************/
00241         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00247         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00248 
00249         virtual ~Converter() {}
00250 
00251         /******************************************
00252         ** Converters
00253         *******************************************/
00259         char* operator ()(const unsigned char &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
00260 };
00261 /*****************************************************************************
00262 ** Char String Converters [short]
00263 *****************************************************************************/
00278 template<>
00279 class Converter<char*,short> : public virtual converters::CharStringBuffer {
00280     public:
00281         /******************************************
00282         ** C&D's
00283         *******************************************/
00289         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00295         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00296 
00297         virtual ~Converter() {}
00298 
00299         /******************************************
00300         ** Converters
00301         *******************************************/
00307         char* operator ()(const short &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
00308 };
00309 /*****************************************************************************
00310 ** Char String Converters [unsigned short]
00311 *****************************************************************************/
00326 template<>
00327 class Converter<char*,unsigned short> : public virtual converters::CharStringBuffer {
00328     public:
00329         /******************************************
00330         ** C&D's
00331         *******************************************/
00337         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00343         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00344 
00345         virtual ~Converter() {}
00346 
00347         /******************************************
00348         ** Converters
00349         *******************************************/
00355         char* operator ()(const unsigned short &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
00356 };
00357 
00358 /*****************************************************************************
00359 ** Char String Converters [int]
00360 *****************************************************************************/
00375 template<>
00376 class Converter<char*,int> : public virtual converters::CharStringBuffer {
00377     public:
00383         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00389         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00390 
00391         virtual ~Converter() {}
00392 
00398         char* operator ()(const int &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
00399 };
00400 /*****************************************************************************
00401 ** Char String Converters [unsigned int]
00402 *****************************************************************************/
00417 template<>
00418 class Converter<char*,unsigned int> : public virtual converters::CharStringBuffer {
00419     public:
00420         /******************************************
00421         ** C&D's
00422         *******************************************/
00428         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00434         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00435 
00436         virtual ~Converter() {}
00437 
00438         /******************************************
00439         ** Converters
00440         *******************************************/
00446         char* operator ()(const unsigned int &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
00447 };
00448 
00449 /*****************************************************************************
00450 ** Char String Converters [long]
00451 *****************************************************************************/
00466 template<>
00467 class Converter<char*,long> : public virtual converters::CharStringBuffer {
00468     public:
00469         /******************************************
00470         ** C&D's
00471         *******************************************/
00477         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00483         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00484 
00485         virtual ~Converter() {}
00486 
00487         /******************************************
00488         ** Converters
00489         *******************************************/
00495         char* operator ()(const long &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
00496 };
00497 /*****************************************************************************
00498 ** Char String Converters [unsigned long]
00499 *****************************************************************************/
00514 template<>
00515 class Converter<char*,unsigned long> : public virtual converters::CharStringBuffer {
00516     public:
00522         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00528         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00529 
00530         virtual ~Converter() {}
00531 
00537         char* operator ()(const unsigned long &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
00538 };
00539 /*****************************************************************************
00540 ** Char String Converters [long long]
00541 *****************************************************************************/
00556 template<>
00557 class Converter<char*,long long> : public virtual converters::CharStringBuffer {
00558     public:
00564         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {}
00570         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {}
00571 
00572         virtual ~Converter() {}
00573 
00579         char* operator ()(const long long &input){ return converters::convertSignedIntegral(input,this->buffer_begin,this->buffer_end); }
00580 };
00581 /*****************************************************************************
00582 ** Char String Converters [unsigned long long]
00583 *****************************************************************************/
00598 template<>
00599 class Converter<char*,unsigned long long> : public virtual converters::CharStringBuffer {
00600     public:
00606         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
00607         }
00613         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
00614         }
00615 
00616         virtual ~Converter() {}
00617 
00623         char* operator ()(const unsigned long long &input){ return converters::convertUnsignedIntegral(input,this->buffer_begin,this->buffer_end); }
00624 };
00625 /*****************************************************************************
00626 ** Char String Converters [float]
00627 *****************************************************************************/
00637 template<>
00638 class Converter<char*,float> : public virtual converters::CharStringBuffer {
00639     public:
00640         /******************************************
00641         ** C&D's
00642         *******************************************/
00648         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
00649                 format_specifier[0] = '%';
00650                 format_specifier[1] = '.';
00651                 format_specifier[2] = 'x';
00652                 format_specifier[3] = 'f';
00653                 format_specifier[4] = 'f';
00654                 format_specifier[5] = '\0';
00655         }
00661         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
00662                 format_specifier[0] = '%';
00663                 format_specifier[1] = '.';
00664                 format_specifier[2] = 'x';
00665                 format_specifier[3] = 'f';
00666                 format_specifier[4] = 'f';
00667                 format_specifier[5] = '\0';
00668         }
00669 
00670         virtual ~Converter() {}
00671 
00672         /******************************************
00673         ** Converters
00674         *******************************************/
00686         char* operator ()(const float &input, const int& precision = -1){
00687                 if ( precision < 0 ) {
00688                 format_specifier[1] = 'f';
00689                 format_specifier[2] = '\0';
00690                 } else if ( precision < 10 ) {
00691                 format_specifier[1] = '.';
00692                 format_specifier[2] = '0'+precision;
00693                 format_specifier[3] = 'f';
00694                 format_specifier[4] = '\0';
00695             } else if ( precision < 20 )  {
00696                 format_specifier[1] = '.';
00697                 format_specifier[2] = '1';
00698                 format_specifier[3] = '0'+(precision - 10);
00699                 format_specifier[4] = 'f';
00700             } else {
00701                 format_specifier[1] = '.';
00702                 format_specifier[2] = '2';
00703                 format_specifier[3] = '0';
00704                 format_specifier[4] = 'f';
00705             }
00706 
00707             snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
00708             return this->buffer_begin;
00709 //            return converters::convertDecimal<32>(input,this->buffer_begin,this->buffer_end);
00710         }
00711     private:
00712         char format_specifier[6];
00713 };
00714 /*****************************************************************************
00715 ** Char String Converters [double]
00716 *****************************************************************************/
00726 template<>
00727 class Converter<char*,double> : public virtual converters::CharStringBuffer {
00728     public:
00729         /******************************************
00730         ** C&D's
00731         *******************************************/
00737         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
00738                 format_specifier[0] = '%';
00739                 format_specifier[1] = '.';
00740                 format_specifier[2] = 'x';
00741                 format_specifier[3] = 'f';
00742                 format_specifier[4] = 'f';
00743                 format_specifier[5] = '\0';
00744         }
00750         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
00751                 format_specifier[0] = '%';
00752                 format_specifier[1] = '.';
00753                 format_specifier[2] = 'x';
00754                 format_specifier[3] = 'f';
00755                 format_specifier[4] = 'f';
00756                 format_specifier[5] = '\0';
00757         }
00758 
00759         virtual ~Converter() {}
00760 
00761         /******************************************
00762         ** Converters
00763         *******************************************/
00775         char* operator ()(const double &input, const int& precision = -1){
00776                 if ( precision < 0 ) {
00777                 format_specifier[1] = 'f';
00778                 format_specifier[2] = '\0';
00779                 } else if ( precision < 10 ) {
00780                 format_specifier[1] = '.';
00781                 format_specifier[2] = '0'+precision;
00782                 format_specifier[3] = 'f';
00783                 format_specifier[4] = '\0';
00784             } else if ( precision < 20 )  {
00785                 format_specifier[1] = '.';
00786                 format_specifier[2] = '1';
00787                 format_specifier[3] = '0'+(precision - 10);
00788                 format_specifier[4] = 'f';
00789             } else {
00790                 format_specifier[1] = '.';
00791                 format_specifier[2] = '2';
00792                 format_specifier[3] = '0';
00793                 format_specifier[4] = 'f';
00794             }
00795             snprintf(this->buffer_begin, this->buffer_end-this->buffer_begin, format_specifier, input);
00796             return this->buffer_begin;
00797 //            return converters::convertDecimal<64>(input,this->buffer_begin,this->buffer_end);
00798         }
00799     private:
00800         char format_specifier[6];
00801 };
00802 /*****************************************************************************
00803 ** Char String Converters [bool]
00804 *****************************************************************************/
00818 template<>
00819 class Converter<char*,bool> : public virtual converters::CharStringBuffer {
00820     public:
00824         Converter() : converters::CharStringBuffer(6) {}
00825 
00826         virtual ~Converter() {}
00827 
00833         char* operator ()(const bool &input){
00834             *buffer_begin = '\0';
00835             input ? strcat(buffer_begin,"true") : strcat (buffer_begin,"false");
00836             return buffer_begin;
00837         }
00838 };
00839 
00840 /*****************************************************************************
00841  * Char String Converter Family
00842  ****************************************************************************/
00853 template <>
00854 class Converter<char*,void>  :
00855     public Converter<char*,char>,
00856     public Converter<char*,short>,
00857     public Converter<char*,int>,
00858     public Converter<char*,long>,
00859     public Converter<char*,unsigned char>,
00860     public Converter<char*,unsigned short>,
00861     public Converter<char*,unsigned int>,
00862     public Converter<char*,unsigned long>,
00863     public Converter<char*,float>,
00864     public Converter<char*,double>,
00865     public Converter<char*,long long>,
00866     public Converter<char*,unsigned long long>,
00867     public Converter<char*,bool>
00868 {
00869     public:
00875         Converter(char* begin, char* end) : converters::CharStringBuffer(begin,end) {
00876         }
00883         Converter(int buffer_size = 250) ecl_debug_throw_decl(StandardException) : converters::CharStringBuffer(buffer_size) {
00884         }
00885 
00886         virtual ~Converter() {}
00887 
00888         using Converter<char*,char>::operator();
00889         using Converter<char*,short>::operator();
00890         using Converter<char*,int>::operator();
00891         using Converter<char*,long>::operator();
00892         using Converter<char*,unsigned char>::operator();
00893         using Converter<char*,unsigned short>::operator();
00894         using Converter<char*,unsigned int>::operator();
00895         using Converter<char*,unsigned long>::operator();
00896         using Converter<char*,float>::operator();
00897         using Converter<char*,double>::operator();
00898         using Converter<char*,long long>::operator();
00899         using Converter<char*,unsigned long long>::operator();
00900         using Converter<char*,bool>::operator();
00901 };
00902 
00903 }; // Namespace ecl
00904 
00905 #endif /*ECL_CONVERTERS_CONVERT_CHAR_STRINGS_HPP_*/


ecl_converters
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:10