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10.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 ]

Diff markup

Differences between /externals/g4tools/include/tools/sprintf (Version 11.3.0) and /externals/g4tools/include/tools/sprintf (Version 11.1.1)


  1 // Copyright (C) 2010, Guy Barrand. All rights      1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.            2 // See the file tools.license for terms.
  3                                                     3 
  4 #ifndef tools_sprintf                               4 #ifndef tools_sprintf
  5 #define tools_sprintf                               5 #define tools_sprintf
  6                                                     6 
  7 #include <string>                                   7 #include <string>
  8 #include "snpf"                                     8 #include "snpf"
  9                                                     9 
 10 namespace tools {                                  10 namespace tools {
 11                                                    11 
 12 inline bool vsprintf(std::string& a_string,int     12 inline bool vsprintf(std::string& a_string,int a_length,const char* a_format,va_list a_args){
 13   a_string.clear();                                13   a_string.clear();
 14   if(a_length<0) return false;                     14   if(a_length<0) return false;
 15   if(!a_format) return false;                      15   if(!a_format) return false;
 16   char* _s = new char[a_length+1];                 16   char* _s = new char[a_length+1];
 17   if(!_s) return false;                            17   if(!_s) return false;
 18   _s[a_length] = '\0';                             18   _s[a_length] = '\0';
 19   int n = vsnpf(_s,a_length+1,a_format,a_args)     19   int n = vsnpf(_s,a_length+1,a_format,a_args);
 20   if(n>a_length) {                                 20   if(n>a_length) {
 21     delete [] _s;                                  21     delete [] _s;
 22     return false;                                  22     return false;
 23   }                                                23   }
 24   if(_s[a_length]!='\0') {                         24   if(_s[a_length]!='\0') {
 25     delete [] _s;                                  25     delete [] _s;
 26     return false;                                  26     return false;
 27   }                                                27   }
 28   a_string = _s;                                   28   a_string = _s;
 29   delete [] _s;                                    29   delete [] _s;
 30   return true;                                     30   return true;
 31 }                                                  31 }
 32                                                    32 
 33                                                    33 
 34 inline bool sprintf(std::string& a_string,int      34 inline bool sprintf(std::string& a_string,int a_length,const char* a_format,...){
 35   a_string.clear();                                35   a_string.clear();
 36   if(a_length<0) return false;                     36   if(a_length<0) return false;
 37   if(!a_format) return false;                      37   if(!a_format) return false;
 38   char* _s = new char[a_length+1];                 38   char* _s = new char[a_length+1];
 39   if(!_s) return false;                            39   if(!_s) return false;
 40   _s[a_length] = '\0';                             40   _s[a_length] = '\0';
 41   va_list args;                                    41   va_list args;
 42   va_start(args,a_format);                         42   va_start(args,a_format);
 43   int n = vsnpf(_s,a_length+1,a_format,args);      43   int n = vsnpf(_s,a_length+1,a_format,args);
 44   va_end(args);                                    44   va_end(args);
 45   if(n>a_length) {                                 45   if(n>a_length) {
 46     delete [] _s;                                  46     delete [] _s;
 47     return false;                                  47     return false;
 48   }                                                48   }
 49   if(_s[a_length]!='\0') {                         49   if(_s[a_length]!='\0') {
 50     delete [] _s;                                  50     delete [] _s;
 51     return false;                                  51     return false;
 52   }                                                52   }
 53   a_string = _s;                                   53   a_string = _s;
 54   delete [] _s;                                    54   delete [] _s;
 55   return true;                                     55   return true;
 56 }                                                  56 }
 57                                                    57 
 58 inline bool print2sv(std::string& a_string,int     58 inline bool print2sv(std::string& a_string,int a_length,const char* a_format,va_list a_args){
 59   if(a_length<0) {a_string.clear();return fals     59   if(a_length<0) {a_string.clear();return false;}
 60   if(!a_format) {a_string.clear();return false     60   if(!a_format) {a_string.clear();return false;}
 61   a_string.assign(a_length,' '); //data = a_le     61   a_string.assign(a_length,' '); //data = a_length+1
 62   char* _s = const_cast<char*>(a_string.c_str(     62   char* _s = const_cast<char*>(a_string.c_str());
 63   //_s[a_length] shoulg be '\0'.                   63   //_s[a_length] shoulg be '\0'.
 64   int n = vsnpf(_s,a_length+1,a_format,a_args)     64   int n = vsnpf(_s,a_length+1,a_format,a_args);
 65   if(n>a_length) { //a_string is compromised.      65   if(n>a_length) { //a_string is compromised.
 66     a_string.clear(); //we cross fingers.          66     a_string.clear(); //we cross fingers.
 67     return false;                                  67     return false;
 68   }                                                68   }
 69   if(_s[a_length]!='\0') { //a_string is compr     69   if(_s[a_length]!='\0') { //a_string is compromised.
 70     a_string.clear(); //we cross fingers.          70     a_string.clear(); //we cross fingers.
 71     return false;                                  71     return false;
 72   }                                                72   }
 73   a_string.resize(n);                              73   a_string.resize(n);
 74   return true;                                     74   return true;
 75 }                                                  75 }
 76                                                    76 
 77 inline bool print2s(std::string& a_string,int      77 inline bool print2s(std::string& a_string,int a_length,const char* a_format,...){
 78   if(a_length<0) {a_string.clear();return fals     78   if(a_length<0) {a_string.clear();return false;}
 79   if(!a_format) {a_string.clear();return false     79   if(!a_format) {a_string.clear();return false;}
 80   a_string.assign(a_length,' '); //data = a_le     80   a_string.assign(a_length,' '); //data = a_length+1
 81   char* _s = const_cast<char*>(a_string.c_str(     81   char* _s = const_cast<char*>(a_string.c_str());
 82   //_s[a_length] shoulg be '\0'.                   82   //_s[a_length] shoulg be '\0'.
 83   va_list args;                                    83   va_list args;
 84   va_start(args,a_format);                         84   va_start(args,a_format);
 85   int n = vsnpf(_s,a_length+1,a_format,args);      85   int n = vsnpf(_s,a_length+1,a_format,args);
 86   va_end(args);                                    86   va_end(args);
 87   if(n>a_length) { //a_string is compromised.      87   if(n>a_length) { //a_string is compromised.
 88     a_string.clear(); //we cross fingers.          88     a_string.clear(); //we cross fingers.
 89     return false;                                  89     return false;
 90   }                                                90   }
 91   if(_s[a_length]!='\0') { //a_string is compr     91   if(_s[a_length]!='\0') { //a_string is compromised.
 92     a_string.clear(); //we cross fingers.          92     a_string.clear(); //we cross fingers.
 93     return false;                                  93     return false;
 94   }                                                94   }
 95   a_string.resize(n);                              95   a_string.resize(n);
 96   return true;                                     96   return true;
 97 }                                                  97 }
 98                                                    98 
 99 template <class MATRIX> //for example : MATRIX     99 template <class MATRIX> //for example : MATRIX = mat<symbol,4>
100 inline void set_matrix(MATRIX& a_matrix,const     100 inline void set_matrix(MATRIX& a_matrix,const std::string& a_fmt) {
101   unsigned int DR = a_matrix.rows();              101   unsigned int DR = a_matrix.rows();
102   unsigned int DC = a_matrix.cols();              102   unsigned int DC = a_matrix.cols();
103   std::string ss;                                 103   std::string ss;
104   for(unsigned int i=0;i<DR;i++) {                104   for(unsigned int i=0;i<DR;i++) {
105     for(unsigned int j=0;j<DC;j++) {              105     for(unsigned int j=0;j<DC;j++) {
106       tools::sprintf(ss,128,a_fmt.c_str(),i,j)    106       tools::sprintf(ss,128,a_fmt.c_str(),i,j);
107       a_matrix.set_value(i,j,ss);                 107       a_matrix.set_value(i,j,ss);
108     }                                             108     }
109   }                                               109   }
110 }                                                 110 }
111                                                   111 
112 }                                                 112 }
113                                                   113 
114 #endif                                            114 #endif