STK++ 0.9.13
STK_DManager_Util.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2010 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::DManager
27 * created on: 12 oct. 2010
28 * Purpose: useful methods for processing strings and i/o streams..
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
38#include <algorithm>
39#include "../include/STK_DManager_Util.h"
40
41namespace STK
42{
43
44namespace DManager
45{
46/* @ingroup DManager
47 * convert a String to a TypeDataFile.
48 * @param type the String we want to convert
49 * @return the TypeDataFile represented by the String @c type. if the string
50 * does not match any known name, the @c unknown_ type is returned.
51 **/
53{
54 if (toUpperString(type) == toUpperString(_T("csv"))) return csv_;
55 return unknown_;
56}
57
58/* @ingroup DManager
59 * convert a TypeDataFile to a String.
60 * @param type the type of data file we want to convert
61 * @return the string associated to this type.
62 **/
64{
65 if (type == csv_) return String(_T("csv"));
66 return String(_T("unknown"));
67}
68
69
70/* @ingroup DManager
71 * @brief check if a string represent a boolean.
72 *
73 * A String is a boolean if it is written "TRUE" or "FALSE". There is no need
74 * to use upper case.
75 * @param str the string to check
76 * @return @c true if the String i a boolean, @c false otherwise.
77 **/
79{
80 // is it TRUE ?
81 if (toUpperString(str) == _T("TRUE")) { return true;}
82 // is it FALSE ?
83 if (toUpperString(str) == _T("FALSE")) { return true;}
84 // not a bolean string
85 return false;
86}
87
88/* @ingroup DManager
89 * @brief check if a string represent a boolean.
90 *
91 * A String is a boolean if it is written "TRUE" or "FALSE". There is no need
92 * to use upper case.
93 * @param str the string to check
94 * @return @c true if the String is a boolean, @c false otherwise.
95 **/
97{
98 // is it TRUE ?
99 if (toUpperString(str) == _T("TRUE")) { return true;}
100 // if it's not true, it's false
101 return false;
102}
103
104
105/* remove all occurrences of the char @c c at the beginning and the end
106 * of the string.
107 */
109{
110 // erase first whitespaces
111 str.erase(0, str.find_first_not_of(c));
112 // erase remaining whitespaces
113 size_t found =str.find_last_not_of(c);
114 if (found != str.npos)
115 str.erase(found+1);
116 else
117 str.clear(); // str is all whitespace
118}
119
120/* Get the current field from the stream.
121 * @param is the stream to treat
122 * @param delimiter the delimiter of the current field
123 **/
125{
126 std::getline( is, value, delimiter);
127 removeCharBeforeAndAfter(value, CHAR_BLANK);
128 removeCharBeforeAndAfter(value, CHAR_TAB);
129 return is;
130}
131
132/* @ingroup DManager
133 * @return the number of line in an istream.
134 * @param is the stream to parse.
135 **/
137{
138 char last = CHAR_BLANK;
140 int nbLine = std::count_if( std::istreambuf_iterator<Char>( is )
141 , std::istreambuf_iterator<Char>(),
142 test
143 );
144 if (last != CHAR_NL) nbLine++;
145#ifdef STK_DEBUG
146 stk_cout << _T("In nbEndOfLine, nbLine = ") << nbLine << _T("\n");
147#endif
148 return nbLine;
149}
150
151
152
153} // namespace DManager
154
155} // namespace STK
156
157
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
bool StringToBoolean(String const &str)
convert a string to a boolean.
TypeDataFile stringToTypeDataFile(String const &type)
convert a String to a TypeDataFile.
TypeDataFile
type of the data file we handle.
bool checkStringToBoolean(String const &str)
check if a string represent a boolean.
int nbEndOfLine(istream &is)
String TypeDataFileToString(TypeDataFile const &type)
convert a TypeDataFile to a String.
void removeCharBeforeAndAfter(String &str, Char c)
remove all occurrences of the char c at the beginning and the end of the string str.
istream & getField(istream &is, String &value, Char delimiter)
Get the current field from the input stream.
@ unknown_
unknown reduction
char Char
STK fundamental type of a Char.
std::basic_string< Char > String
STK fundamental type of a String.
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.
Template functor testing if a Char is an end of line.