Cat
Macros | Functions | Variables
Tools.cpp File Reference
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <unistd.h>
#include "Application.h"
#include "Options.h"
#include "Tools.h"

Go to the source code of this file.

Macros

#define __FCT_CPP_
 

Functions

void setApplication (Application *cat)
 
Applicationapplication ()
 
std::string itos (int i)
 
std::string ftos (float f)
 
std::string dtos (double d)
 
std::string btos (std::vector< bool > bits)
 
std::vector< bool > itob (int val, int nbit)
 
int stoi (std::string value)
 
double toDouble (std::string s)
 
StatusCode checkCmd (std::string format, commands instruction, vars &varlist)
 
bool isFloat (const char *chain)
 
bool isInt (const char *chain)
 
bool askSvc (std::string msg)
 
bool graphSvc ()
 
std::string inputSvc (std::string msg)
 
void wait (int n)
 

Variables

ApplicationgApp
 

Macro Definition Documentation

◆ __FCT_CPP_

#define __FCT_CPP_

Definition at line 14 of file Tools.cpp.

Function Documentation

◆ application()

Application* application ( )

◆ askSvc()

bool askSvc ( std::string  msg)

Definition at line 242 of file Tools.cpp.

242  {
243  /*
244  AskPrinter asker = application() -> options() -> askPrinter ();
245  return asker ( msg);
246  */
247  return false;
248 }

◆ btos()

std::string btos ( std::vector< bool >  )

Conversion from binary (std::vector<std::string>) to std::string

Definition at line 67 of file Tools.cpp.

References itos().

Referenced by IOdata::dump().

67  {
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 }
std::string itos(int i)
Definition: Tools.cpp:46
std::vector< bool > bits
Definition: Tools.h:40

◆ checkCmd()

StatusCode checkCmd ( std::string  ,
commands  ,
vars  
)

check the format of an instruction

Definition at line 126 of file Tools.cpp.

References application(), StatusCode::FAILURE, images::index, Object::info(), isFloat(), isInt(), cat::size, StatusCode::SUCCESS, and Object::warning().

126  {
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 }
void info(std::string mymsg)
Definition: Object.h:38
Definition: var.h:19
def size
Definition: cat.py:38
bool isInt(const char *chain)
Definition: Tools.cpp:223
list index
Definition: images.py:1021
bool isFloat(const char *chain)
Definition: Tools.cpp:206
Application * application()
Definition: Tools.cpp:42
void warning(std::string mymsg)
Definition: Object.h:39

◆ dtos()

std::string dtos ( double  d)

Definition at line 60 of file Tools.cpp.

60  {
61  char val[20];
62  sprintf(val,"%f",d);
63  std::string str(val);
64  return str;
65 }

◆ ftos()

std::string ftos ( float  )

Conversion from float to std::string

Definition at line 53 of file Tools.cpp.

53  {
54  char val[20];
55  sprintf(val,"%f",f);
56  std::string str(val);
57  return str;
58 }
f
Definition: cat.py:54

◆ graphSvc()

bool graphSvc ( )

Definition at line 250 of file Tools.cpp.

250  {
251  // GraphPrinter graph = application() -> options() -> graphPrinter ();
252  // return graph ( );
253  return false;
254 }

◆ inputSvc()

std::string inputSvc ( std::string  msg)

Definition at line 259 of file Tools.cpp.

259  {
260  /*
261  InputSvc inp = application() -> options() -> inputSvc ();
262  return inp ( msg );
263  */
264  return std::string("not implemented.");
265 }

◆ isFloat()

bool isFloat ( const char *  )

check if string is compatible with float

Definition at line 206 of file Tools.cpp.

Referenced by checkCmd().

206  {
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 }

◆ isInt()

bool isInt ( const char *  )

check if string is compatible with int

Definition at line 223 of file Tools.cpp.

References application(), and Object::info().

Referenced by checkCmd().

223  {
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 }
void info(std::string mymsg)
Definition: Object.h:38
Application * application()
Definition: Tools.cpp:42

◆ itob()

std::vector<bool> itob ( int  ,
int   
)

Conversion from integer to binary (std::string)

Definition at line 77 of file Tools.cpp.

Referenced by IOdata::dump().

77  {
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 }
std::vector< bool > bits
Definition: Tools.h:40

◆ itos()

std::string itos ( int  )

Conversion from int (long) to std::string

Definition at line 46 of file Tools.cpp.

Referenced by btos(), and wait().

46  {
47  char val[20];
48  sprintf(val,"%d",i);
49  std::string str(val);
50  return str;
51 }

◆ setApplication()

void setApplication ( Application )

Define entry point for the application

Definition at line 38 of file Tools.cpp.

References gui::cat.

Referenced by Application::Application().

38  {
39  gApp = cat;
40 }
cat
Definition: gui.py:104
Application * gApp
Definition: Tools.cpp:36

◆ stoi()

int stoi ( std::string  )

Conversion from binary (std::string) to integer

Definition at line 93 of file Tools.cpp.

References cat::size.

93  {
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 }
def size
Definition: cat.py:38

◆ toDouble()

double toDouble ( std::string  )

Conversion from string (potentially with a comma) to a double

Definition at line 113 of file Tools.cpp.

113  {
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 }

◆ wait()

void wait ( int  nbr_ms)

system wait for a few seconds

Definition at line 271 of file Tools.cpp.

References application(), itos(), and Object::verbose().

Referenced by ADCMeasurement::execute(), CurrentMeasurement::execute(), UsbFTInterface::init(), and UsbFTMLInterface::init().

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 }
void verbose(std::string mymsg)
Definition: Object.h:36
Application * application()
Definition: Tools.cpp:42
std::string itos(int i)
Definition: Tools.cpp:46

Variable Documentation

◆ gApp

Application* gApp

Definition at line 36 of file Tools.cpp.

Referenced by application().