Back to home page

EIC code displayed by LXR

 
 

    


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 print2s(std::string& a_string,int a_length,const char* a_format,...){
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   va_list args;
0065   va_start(args,a_format);
0066   int n = vsnpf(_s,a_length+1,a_format,args);
0067   va_end(args);
0068   if(n>a_length) { //a_string is compromised.
0069     a_string.clear(); //we cross fingers.
0070     return false;
0071   }
0072   if(_s[a_length]!='\0') { //a_string is compromised.
0073     a_string.clear(); //we cross fingers.
0074     return false;
0075   }
0076   a_string.resize(n);
0077   return true;
0078 }
0079 
0080 }
0081 
0082 #endif