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.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_num2s
  5 #define tools_num2s
  6 
  7 // write numerics in a string as if done with a std::ostringstream::operator<<(<num>).
  8 
  9 #include "sprintf"
 10 #include "typedefs"
 11 
 12 #include <cstddef> //ptrdiff_t
 13 
 14 namespace tools {
 15 
 16 /*
 17 inline bool num2s(unsigned char a_value,std::string& a_s){
 18   return print2s(a_s,32,"%c",a_value);
 19 }
 20 
 21 inline bool num2s(char a_value,std::string& a_s){
 22   return print2s(a_s,32,"%c",a_value);
 23 }
 24 */
 25 
 26 inline bool num2s(unsigned short a_value,std::string& a_s) { //used in value
 27   return print2s(a_s,32,"%u",a_value);
 28 }
 29 
 30 inline bool num2s(short a_value,std::string& a_s){ //used in value
 31   return print2s(a_s,32,"%d",a_value);
 32 }
 33 
 34 inline bool num2s(unsigned int a_value,std::string& a_s) {
 35   return print2s(a_s,32,"%u",a_value);
 36 }
 37 
 38 /*
 39 inline bool num2sx(unsigned int a_value,std::string& a_s) {
 40   return print2s(a_s,32,"%x",a_value);
 41 }
 42 */
 43 
 44 inline bool num2s(int a_value,std::string& a_s){
 45   return print2s(a_s,32,"%d",a_value);
 46 }
 47 
 48 inline bool num2s(uint64 a_value,std::string& a_s) {
 49   return print2s(a_s,32,uint64_format(),a_value);
 50 }
 51 
 52 inline bool num2s(int64 a_value,std::string& a_s){
 53   return print2s(a_s,32,int64_format(),a_value);
 54 }
 55 
 56 inline bool num2s(float a_value,std::string& a_s){
 57   return print2s(a_s,32,"%g",a_value);
 58 }
 59 
 60 inline bool num2s(double a_value,std::string& a_s){
 61   return print2s(a_s,32,"%g",a_value);
 62 }
 63 
 64 inline bool size_t2s(size_t a_value,std::string& a_s) {
 65   if(sizeof(size_t)==8) {
 66     return num2s((uint64)a_value,a_s);
 67   } else { //assume 4 :
 68     return num2s((uint32)a_value,a_s);
 69   }
 70 }
 71 
 72 inline bool ptrdiff_t2s(ptrdiff_t a_value,std::string& a_s) {  //used in write_bsg.
 73   if(sizeof(ptrdiff_t)==8) {
 74     return num2s((int64)a_value,a_s);
 75   } else { //assume 4 :
 76     return num2s((int32)a_value,a_s);
 77   }
 78 }
 79 
 80 /*
 81 inline bool num2s(bool a_value,std::string& a_s){
 82   //a_s = a_value?"true":"false";
 83   a_s = a_value?"1":"0";
 84   return true;
 85 }
 86 */
 87 
 88 template <class T>
 89 inline bool numas(const T& a_value,std::string& a_s){
 90   std::string stmp;
 91   if(!num2s(a_value,stmp)) return false;
 92   a_s += stmp;
 93   return true;
 94 }
 95 
 96 template <class T>
 97 inline bool size_tas(const T& a_value,std::string& a_s){
 98   std::string stmp;
 99   if(!size_t2s(a_value,stmp)) return false;
100   a_s += stmp;
101   return true;
102 }
103 
104 /*
105 inline bool num2s(unsigned int a_linen,const char* a_lines[],std::string& a_s) {
106   a_s.clear();
107   for(unsigned int index=0;index<a_linen;index++) {
108     a_s += a_lines[index];
109     a_s += "\n";
110   }
111   return true;
112 }
113 */
114 
115 // for the below std::vector num2s in case T=std::string :
116 inline bool num2s(const std::string& a_value,std::string& a_s){a_s = a_value;return true;}
117 
118 template <class T>
119 class num_out : public std::string {
120   typedef std::string parent;
121 private:
122   //typedef typename std::enable_if<std::is_floating_point<T>::value, T>::type type_t;
123   //typedef typename std::enable_if<std::is_integral<T>::value, T>::type type_t;
124   //typedef typename std::enable_if<std::is_arithmetic<T>::value, T>::type type_t;
125 public:
126   num_out(const T& a_value) {
127     parent::operator+=("\"");
128     if(!numas(a_value,*this)) {} //throw
129     parent::operator+=("\"");
130   }
131 public:
132   num_out(const num_out& a_from):parent(a_from){}
133   num_out& operator=(const num_out& a_from){parent::operator=(a_from);return *this;}
134 };
135 
136 }
137 
138 #include <vector>
139 
140 namespace tools {
141 
142 template <class VEC>
143 inline bool nums2s(const VEC& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
144   a_s.clear();
145   typename VEC::size_type number = a_vals.size();
146   if(number<=0) return true; //it is ok.
147   number--;
148   std::string stmp;
149   bool status = true;
150   for(typename VEC::size_type index=0;index<number;index++) {
151     if(!num2s(a_vals[index],stmp)) status = false; //continue.
152     a_s += stmp;
153     a_s += a_sep;
154   }
155   if(!num2s(a_vals[number],stmp)) status = false;
156   a_s += stmp;
157   if(a_sep_at_end) a_s += a_sep;
158   return status;
159 }
160 
161 template <class T>
162 inline bool nums2s(const std::vector<T>& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
163   return nums2s< std::vector<T> >(a_vals,a_s,a_sep,a_sep_at_end);
164 }
165 
166 }
167 
168 #endif