Cat
Tools.cpp
Go to the documentation of this file.
1 //$Id: Tools.cpp,v 1.8 2006/05/05 15:45:51 fmachefe Exp $
2 //------------------------------------------------------------------------------
3 //
4 // Package : Tools
5 //
6 // Description:
7 //
8 // Author(s) : F. Machefert -
9 // Date : 16 January 2004
10 //
11 //------------------------------------------------------------------------------
12 
13 #ifndef __FCT_CPP_
14 #define __FCT_CPP_
15 
16 // Include
17 #include <stdlib.h>
18 #include <iostream>
19 #include <string>
20 #include <math.h>
21 
22 #ifdef _WIN32
23 #include <windows.h>
24 #endif
25 
26 //#include <ncurses.h>
27 #include <unistd.h>
28 
29 // CAT include files
30 #include "Application.h"
31 #include "Options.h"
32 
33 // local
34 #include "Tools.h"
35 
37 
39  gApp = cat;
40 }
41 
43  return gApp;
44 }
45 
46 std::string itos(int i){
47  char val[20];
48  sprintf(val,"%d",i);
49  std::string str(val);
50  return str;
51 }
52 
53 std::string ftos(float f){
54  char val[20];
55  sprintf(val,"%f",f);
56  std::string str(val);
57  return str;
58 }
59 
60 std::string dtos(double d){
61  char val[20];
62  sprintf(val,"%f",d);
63  std::string str(val);
64  return str;
65 }
66 
67 std::string btos(std::vector<bool> bits){
68  int i=bits.size()-1;
69  std::string output;
70  while(i>=0){
71  output+=itos(bits[i]);
72  i--;
73  }
74  return output;
75 }
76 
77 std::vector<bool> itob(int val,int nbit){
78  int i=nbit-1;
79  int power;
80  std::vector<bool> bits(nbit);
81  // int limit=1<<(nbit-1);
82  //int vsat=(val<limit)?val:int(limit-1); //saturation
83  int vsat=val;
84  while(i>=0){
85  power=1<<i;
86  bits[i]=(vsat/power==1);
87  vsat=vsat%power;
88  i--;
89  }
90  return bits;
91 }
92 
93 int stoi(std::string value){
94  const unsigned int codage=2;
95  int result=0;
96  char h[codage]={'0','1'};
97  const unsigned int size=value.size();
98  const char* word=value.c_str();
99  unsigned int pos=0;
100  while (pos<size){
101  bool isSeen=false;
102  for (unsigned int j=0; j<codage && !isSeen; j++){
103  if (h[j]==word[pos]){
104  result+=int(j*pow(2.,(int)(size-pos-1)));
105  isSeen=true;
106  }
107  }
108  pos++;
109  }
110  return result;
111 }
112 
113 double toDouble(std::string s){
114  // do not use a reference, since we're going to modify this string
115  // If you do not care about ',' or '.' in your string use a
116  // reference instead.
117  size_t found = s.find(",");
118  if(found != std::string::npos)
119  s[found]='.'; // Change ',' to '.'
120  return atof(s.c_str());
121 }
122 
123 //=============================================================================
124 //
125 //=============================================================================
126 StatusCode checkCmd( std::string format , commands instruction , vars &varlist) {
127  int size = instruction.size();
128  if ( size<=0 ){
129  return StatusCode::FAILURE;
130  }
131 
132  // line is empty
133  if ( format.empty() || std::string::npos==format.find_first_not_of(' ') ) {
134  return StatusCode::FAILURE;
135  }
136 
137  // split the format in a vector of strings
138  // decode the main command
139  std::string line = std::string (format,
140  format.find_first_not_of(' ') ,
141  format.size());
142  std::vector<std::string> cmd;
143  std::string separator(1,' ');
144  unsigned int ipos = line.find( separator );
145  cmd.push_back ( std::string( line, 0, ipos ) );
146  if ( 0!=std::string ( line , 0 , ipos ).compare(instruction[0]) ){
147  application()->info(std::string ( line , 0 , ipos )+" "+instruction[0],"");
148  return StatusCode::FAILURE;
149  }
150 
151  unsigned int pos ;
152 
153  std::string str = line;
154  while ( ipos!=std::string::npos ){
155  std::string tmp = std::string (str, ipos , str.size());
156  str = tmp;
157  pos=str.find_first_not_of( ' ' );
158  if ( pos == std::string::npos ) {
159  break;
160  }
161  std::string arg;
162  ipos = str.find(separator,pos);
163 
164  if (ipos==std::string::npos){
165  arg = std::string(str , pos );
166  }
167  else {
168  arg = std::string(str , pos, ipos - 1);
169  }
170  cmd.push_back(arg);
171  }
172 
173  // check parameter number
174  if ( instruction.size() != cmd.size() ){
175  application()->info("wrong number of parameter. a enelver !!!!","");
176  return StatusCode::FAILURE;
177  }
178  // no parameter
179  if ( 1==cmd.size() ){
180  return StatusCode::SUCCESS;
181  }
182  // decode the format
183  for (unsigned int index = 1; index < cmd.size() ; ++index){
184  // int ?
185  if ( 0==cmd[index].compare("%i") || 0==cmd[index].compare("%I") ){
186  if ( !isInt ( instruction[index].c_str() ) ) {
187  application()->warning("We expect an integer parameter ["+instruction[index]+"]","checkCmd");
188  return StatusCode::FAILURE;
189  }
190  varlist.push_back ( var( atoi(instruction[index].c_str()) ) );
191  }
192  // float ?
193  if ( 0==cmd[index].compare("%f") || 0==cmd[index].compare("%F") ){
194  if ( !isFloat ( instruction[index].c_str() ) ) {
195  application()->warning("We expect a float parameter ["+instruction[index]+"]","checkCmd");
196  return StatusCode::FAILURE;
197  }
198  varlist.push_back( var ( atof(instruction[index].c_str()) ) );
199  }
200  }
201  return StatusCode::SUCCESS;
202 }
203 //=============================================================================
204 //
205 //=============================================================================
206 bool isFloat( const char* chain ) {
207  const char *pChain = chain;
208  while (*pChain!='\0') {
209  if ( *pChain!='0' && *pChain!='1' && *pChain!='2' && *pChain!='3' &&
210  *pChain!='4' && *pChain!='5' && *pChain!='6' && *pChain!='7' &&
211  *pChain!='8' && *pChain!='9' && *pChain!='+' && *pChain!='-' &&
212  *pChain!='.' ){
213  return false;
214  }
215  pChain++;
216  }
217  return true;
218 }
219 
220 //=============================================================================
221 //
222 //=============================================================================
223 bool isInt( const char* chain ) {
224  const char *pChain = chain;
225  bool first = true;
226  while (*pChain!='\0') {
227  if ( ( *pChain<'0' || *pChain>'9' ) ){
228  application()->info(chain,"");
229  if ( !first || *pChain!='-' ){
230  return false;
231  }
232  }
233  first = false;
234  pChain++;
235  }
236  return true;
237 }
238 
239 //=============================================================================
240 //
241 //=============================================================================
242 bool askSvc( std::string msg ){
243  /*
244  AskPrinter asker = application() -> options() -> askPrinter ();
245  return asker ( msg);
246  */
247  return false;
248 }
249 
250 bool graphSvc( ){
251  // GraphPrinter graph = application() -> options() -> graphPrinter ();
252  // return graph ( );
253  return false;
254 }
255 
256 //=============================================================================
257 //
258 //=============================================================================
259 std::string inputSvc( std::string msg ){
260  /*
261  InputSvc inp = application() -> options() -> inputSvc ();
262  return inp ( msg );
263  */
264  return std::string("not implemented.");
265 }
266 
267 
268 //=========================================================================
269 //
270 //=========================================================================
271 void wait( int n )
272 {
273  application()->verbose("wait for "+itos(n)+" ms.","Tools::wait");
274 #ifdef WIN32
275  Sleep( n );
276 #else
277  usleep( 1000 * n );
278 #endif
279 }
280 
281 
282 #endif
283 
284 
void info(std::string mymsg)
Definition: Object.h:38
Definition: var.h:19
void verbose(std::string mymsg)
Definition: Object.h:36
f
Definition: cat.py:54
def size
Definition: cat.py:38
bool isInt(const char *chain)
Definition: Tools.cpp:223
cat
Definition: gui.py:104
void setApplication(Application *cat)
Definition: Tools.cpp:38
int stoi(std::string value)
Definition: Tools.cpp:93
Definition: cat.py:1
double toDouble(std::string s)
Definition: Tools.cpp:113
Application * gApp
Definition: Tools.cpp:36
list index
Definition: images.py:1021
bool isFloat(const char *chain)
Definition: Tools.cpp:206
Application * application()
Definition: Tools.cpp:42
std::string inputSvc(std::string msg)
Definition: Tools.cpp:259
std::string itos(int i)
Definition: Tools.cpp:46
StatusCode checkCmd(std::string format, commands instruction, vars &varlist)
Definition: Tools.cpp:126
std::vector< bool > itob(int val, int nbit)
Definition: Tools.cpp:77
void wait(int n)
Definition: Tools.cpp:271
std::string btos(std::vector< bool > bits)
Definition: Tools.cpp:67
std::string dtos(double d)
Definition: Tools.cpp:60
std::vector< var > vars
Definition: var.h:73
std::string ftos(float f)
Definition: Tools.cpp:53
std::vector< bool > bits
Definition: Tools.h:40
bool askSvc(std::string msg)
Definition: Tools.cpp:242
void warning(std::string mymsg)
Definition: Object.h:39
bool graphSvc()
Definition: Tools.cpp:250
std::vector< std::string > commands
Definition: Tools.h:42