/*************************************************************************** * Copyright (C) 2005 by Nicolas PASCAL * * nicolas.pascal@icare.univ-lille1.fr * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "tools.h" // bool is_big_endian() // { // short int word = 0x0001; // char *byte = (char *) &word; // return(byte[0] ? false : true); // } int MyTools::char2int(const char &c) { switch(c) { case '0': return 0; break; case '1': return 1; break; case '2': return 2; break; case '3': return 3; break; case '4': return 4; break; case '5': return 5; break; case '6': return 6; break; case '7': return 7; break; case '8': return 8; break; case '9': return 9; break; default: // should never arrive here return -1; break; } } int MyTools::string2int(const std::string &s) { // generate a flux with the string to be converted std::istringstream iss( s ); int n ; iss >> n; // put the flux into n return n; } float MyTools::string2float( const std::string & s ) { float f ; // generate a flux with the string to be converted std::istringstream iss( s ); iss >> f; // put the flux into f return f; } double MyTools::string2double( const std::string & s ) { // generate a flux with the string to be converted std::istringstream iss( s ); double f ; iss >> f; // put the flux into n return f; } std::string MyTools::int2string( const int & n, const int & nb_digit ) { std::ostringstream oss; if (nb_digit!=-1) { int nb_10_pow=0; int my_n=n; while ( my_n>=10 ) { nb_10_pow++; my_n/=10; } if (nb_digit>nb_10_pow) for (int i=nb_10_pow+1 ; i *"<< while (i!=my_s.end() && is_float) { is_float=is_float && ( isdigit(*i) || (*i)=='.' || (*i)=='-' ); ++i; } return is_float; } const int MyTools::count( const std::string & elt, const std::string & s ) { int nb=0; size_t pos = s.find_first_of(elt,0); while (pos!=std::string::npos) { nb++; pos = s.find_first_of(elt,pos+1); } return nb; } const int MyTools::count( const char elt, const std::string & s ) { std::string s_elt = &elt; return count(s_elt,s); } double MyTools::angle_2D(double* p1, double* p2) { double dtheta,theta1,theta2; theta1 = atan2(p1[1],p1[0]); theta2 = atan2(p2[1],p2[0]); dtheta = theta2 - theta1; while (dtheta > M_PI) dtheta -= M_2PI; while (dtheta < -M_PI) dtheta += M_2PI; return(dtheta); } std::string MyTools::char2string( const char & c ) { std::string s(""); s+=c; return s; } // double MyTools::deg_2_rad( const double & deg ) { // return (M_PI*deg/180.); // } std::string MyTools::to_bit( void* bit_field, const int size ) { std::ostringstream oss; char byte; int byte_idx=0; int bit=0; for (byte_idx=0;byte_idx>7); // 0x80 : select the more significant bit } return oss.str(); } vector MyTools::get_digits (unsigned long long n) { vector v; while (n > 0) { v.push_back (n % 10); n /= 10; } return v; };