STK++ 1.0
STK_DManager_Util.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2010  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::DManager
00027  * created on: 12 oct. 2010
00028  * Purpose:  useful methods for processing strings and i/o streams..
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #include "../include/STK_DManager_Util.h"
00038 
00039 namespace STK
00040 {
00041 
00042 namespace DManager
00043 {
00044 
00045 /*  @ingroup DManager
00046  *  @brief check if a string represent a boolean.
00047  *
00048  *  A String is a boolean if it is written "TRUE" or "FALSE". There is no need
00049  *  to use upper case.
00050  *  @param str the string to check
00051  *  @return @c true if the String i a boolean, @c false otherwise.
00052  **/
00053 bool checkStringToBoolean( String const& str)
00054 {
00055   // is it TRUE ?
00056   if (toUpperString(str) == _T("TRUE")) { return true;}
00057   // is it FALSE ?
00058   if (toUpperString(str) == _T("FALSE")) { return true;}
00059   // not a bolean string
00060   return false;
00061 }
00062 
00063 /* @ingroup DManager
00064  *  @brief check if a string represent a boolean.
00065  *
00066  *  A String is a boolean if it is written "TRUE" or "FALSE". There is no need
00067  *  to use upper case.
00068  *  @param str the string to check
00069  *  @return @c true if the String is a boolean, @c false otherwise.
00070  **/
00071 bool StringToBoolean( String const& str)
00072 {
00073   // is it TRUE ?
00074   if (toUpperString(str) == _T("TRUE")) { return true;}
00075   // if it's not true, it's false
00076   return false;
00077 }
00078 
00079 
00080 /* remove all occurrences of the char @c c at the beginning and the end
00081  *  of the string.
00082  */
00083 void removeCharBeforeAndAfter( String & str, Char c )
00084 {
00085   // erase first whitespaces
00086   str.erase(0, str.find_first_not_of(c));
00087   // erase remaining whitespaces
00088   size_t found =str.find_last_not_of(c);
00089   if (found != str.npos)
00090     str.erase(found+1);
00091   else
00092     str.clear(); // str is all whitespace
00093 }
00094 
00095 /* Get the current field from the stream.
00096  *  @param is the stream to treat
00097  *  @param delimiter the delimiter of the current field
00098  **/
00099 String getField( istream& is, Char delimiter)
00100 {
00101   String strbuff;
00102   std::getline( is, strbuff, delimiter);
00103   removeCharBeforeAndAfter(strbuff, CHAR_BLANK);
00104   removeCharBeforeAndAfter(strbuff, CHAR_TAB);
00105   return strbuff;
00106 }
00107 
00108 
00109 } // namespace DManager
00110 
00111 } // namespace STK
00112 
00113