Warning, /include/Geant4/tools/sprintf is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sprintf
0005 #define tools_sprintf
0006
0007 #include <string>
0008 #include "snpf"
0009
0010 namespace tools {
0011
0012 inline bool vsprintf(std::string& a_string,int a_length,const char* a_format,va_list a_args){
0013 a_string.clear();
0014 if(a_length<0) return false;
0015 if(!a_format) return false;
0016 char* _s = new char[a_length+1];
0017 if(!_s) return false;
0018 _s[a_length] = '\0';
0019 int n = vsnpf(_s,a_length+1,a_format,a_args);
0020 if(n>a_length) {
0021 delete [] _s;
0022 return false;
0023 }
0024 if(_s[a_length]!='\0') {
0025 delete [] _s;
0026 return false;
0027 }
0028 a_string = _s;
0029 delete [] _s;
0030 return true;
0031 }
0032
0033
0034 inline bool sprintf(std::string& a_string,int a_length,const char* a_format,...){
0035 a_string.clear();
0036 if(a_length<0) return false;
0037 if(!a_format) return false;
0038 char* _s = new char[a_length+1];
0039 if(!_s) return false;
0040 _s[a_length] = '\0';
0041 va_list args;
0042 va_start(args,a_format);
0043 int n = vsnpf(_s,a_length+1,a_format,args);
0044 va_end(args);
0045 if(n>a_length) {
0046 delete [] _s;
0047 return false;
0048 }
0049 if(_s[a_length]!='\0') {
0050 delete [] _s;
0051 return false;
0052 }
0053 a_string = _s;
0054 delete [] _s;
0055 return true;
0056 }
0057
0058 inline bool print2sv(std::string& a_string,int a_length,const char* a_format,va_list a_args){
0059 if(a_length<0) {a_string.clear();return false;}
0060 if(!a_format) {a_string.clear();return false;}
0061 a_string.assign(a_length,' '); //data = a_length+1
0062 char* _s = const_cast<char*>(a_string.c_str());
0063 //_s[a_length] shoulg be '\0'.
0064 int n = vsnpf(_s,a_length+1,a_format,a_args);
0065 if(n>a_length) { //a_string is compromised.
0066 a_string.clear(); //we cross fingers.
0067 return false;
0068 }
0069 if(_s[a_length]!='\0') { //a_string is compromised.
0070 a_string.clear(); //we cross fingers.
0071 return false;
0072 }
0073 a_string.resize(n);
0074 return true;
0075 }
0076
0077 inline bool print2s(std::string& a_string,int a_length,const char* a_format,...){
0078 if(a_length<0) {a_string.clear();return false;}
0079 if(!a_format) {a_string.clear();return false;}
0080 a_string.assign(a_length,' '); //data = a_length+1
0081 char* _s = const_cast<char*>(a_string.c_str());
0082 //_s[a_length] shoulg be '\0'.
0083 va_list args;
0084 va_start(args,a_format);
0085 int n = vsnpf(_s,a_length+1,a_format,args);
0086 va_end(args);
0087 if(n>a_length) { //a_string is compromised.
0088 a_string.clear(); //we cross fingers.
0089 return false;
0090 }
0091 if(_s[a_length]!='\0') { //a_string is compromised.
0092 a_string.clear(); //we cross fingers.
0093 return false;
0094 }
0095 a_string.resize(n);
0096 return true;
0097 }
0098
0099 template <class MATRIX> //for example : MATRIX = mat<symbol,4>
0100 inline void set_matrix(MATRIX& a_matrix,const std::string& a_fmt) {
0101 unsigned int DR = a_matrix.rows();
0102 unsigned int DC = a_matrix.cols();
0103 std::string ss;
0104 for(unsigned int i=0;i<DR;i++) {
0105 for(unsigned int j=0;j<DC;j++) {
0106 tools::sprintf(ss,128,a_fmt.c_str(),i,j);
0107 a_matrix.set_value(i,j,ss);
0108 }
0109 }
0110 }
0111
0112 }
0113
0114 #endif