src/tool/string_tools.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Refresh des chaînes de caractère (string).
00020  *****************************************************************************/
00021 
00022 #include "../tool/string_tools.h"
00023 #include <sstream>
00024 
00025 bool str2long (const std::string &txt, long &valeur)
00026 {
00027   std::stringstream ss;
00028   ss << txt;
00029   ss >> valeur;
00030   return true;
00031 }
00032 
00033 bool str2int (const std::string &txt, int &valeur)
00034 {
00035   std::stringstream ss;
00036   ss << txt;
00037   ss >> valeur;
00038   return true;
00039 }
00040 
00041 bool str2double (const std::string &txt, double &valeur)
00042 {
00043   std::stringstream ss;
00044   ss << txt;
00045   ss >> valeur;
00046   return true;
00047 }
00048 
00049 bool str2bool(const std::string &str, bool &value)
00050 {
00051   // Try to convert str to a boolean value
00052   // return true and set the value on succeed
00053   // return if false its not a boolean
00054   if(str=="1" || str=="true" || str=="on")
00055   {
00056     value = true;
00057     return true;
00058   }
00059   if(str=="0" || str=="false" || str=="off")
00060   {
00061     value = false;
00062     return true;
00063   }
00064   return false;
00065 }
00066 
00067 std::string double2str (double x)
00068 {
00069   std::ostringstream ss;
00070   ss << x;
00071   return ss.str();
00072 }
00073 
00074 std::string long2str (long x)
00075 {
00076   std::ostringstream ss;
00077   ss << x;
00078   return ss.str();
00079 }
00080 
00081 std::string ulong2str (ulong x)
00082 {
00083   std::ostringstream ss;
00084   ss << x;
00085   return ss.str();
00086 }
00087 
00088 std::string bol2str (bool x)
00089 {
00090   std::ostringstream ss;
00091   if(x)
00092     ss << "true";
00093   else
00094     ss << "false";
00095   return ss.str();
00096 }
00097 

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7