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