STK++ 1.0
STK_DManager_Util.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2011  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 juil. 2011
00028  * Purpose:  useful methods for processing dta, strings and i/o streams.
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00038 #ifndef STK_DMANAGER_UTIL_H
00039 #define STK_DMANAGER_UTIL_H
00040 
00041 #include <list>
00042 
00043 #include "../../STKernel/include/STK_Char.h"
00044 #include "../../STKernel/include/STK_String_Util.h"
00045 #include "../../STKernel/include/STK_Stream.h"
00046 
00047 namespace STK
00048 {
00049 
00054 static const String STRING_EMPTY   = String();
00055 
00058 static const String STRING_NL      = _T("\n");
00059 
00062 static const String STRING_BLANK  = _T(" ");
00063 
00066 static const String STRING_VAR  = _T("Var");
00067 
00072 static const Char CHAR_COMMENT = _T('#');
00073 
00076 static const Char CHAR_EQUAL = _T('=');
00077 
00080 static const Char CHAR_BLANK = _T(' ');
00081 
00084 static const Char CHAR_TAB = _T('\t');
00085 
00088 static const Char CHAR_SEP = _T(',');
00089 
00092 static const Char CHAR_OPENBRACKET = _T('[');
00093 
00096 static const Char CHAR_CLOSEBRACKET = _T(']');
00097 
00098 namespace DManager
00099 {
00108 bool checkStringToBoolean( String const& str);
00109 
00118 bool StringToBoolean( String const& str);
00119 
00126 void removeCharBeforeAndAfter( String & str, Char c );
00127 
00138 String getField( istream& is, Char delimiter);
00139 
00146 template<class TYPE>
00147 void readList(String const& strBuffer, std::list<TYPE>& lst, Char sep = CHAR_SEP)
00148 {
00149   // Declare an input string stream
00150   istringstream instream;
00151   // Use strBuffer as source of input.
00152   instream.str(strBuffer);
00153   // read the line
00154   do
00155   {
00156     // get field
00157     String strbuff = getField(instream, sep);
00158     // check if it is a blank field
00159     if (strbuff.empty()) {  break;}
00160     // append Data to the list
00161     TYPE value;
00162     if (stringToType(value, strbuff))
00163       lst.push_back(value);
00164     // TODO: else emit warning or Exception
00165   }
00166   while(1);
00167 }
00168 
00175 template<class TYPE>
00176 void writeList( ostream& os, std::list<TYPE> const& lst, Char sep = CHAR_SEP)
00177 {
00178   if (lst.empty()) return;
00179   typename std::list<TYPE>::const_iterator it = lst.begin();
00180   if (it == lst.end()) return;
00181   os << *it;
00182   it++;
00183   for ( ; it != lst.end(); it++)
00184   {
00185     os << sep << STRING_BLANK << *it;
00186   }
00187 }
00188 
00189 
00190 } // namespace DManager
00191 
00192 } // namespace STK
00193 
00194 #endif /* STK_DMANAGER_UTIL_H */