bes  Updated for version 3.20.6
CSV_Utils.cc
1 // CSV_Utils.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
8 // and Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // zednik Stephan Zednik <zednik@ucar.edu>
32 // pwest Patrick West <pwest@ucar.edu>
33 // jgarcia Jose Garcia <jgarcia@ucar.edu>
34 
35 #include <list>
36 
37 #include "CSV_Utils.h"
38 
39 #include <BESUtil.h>
40 
41 using std::vector;
42 using std::string;
43 using std::list;
44 
51 void
52 CSV_Utils::split( const string& str, char delimiter,
53  vector<string> &tokens )
54 {
55  /*
56  string::size_type lastPos = str.find_first_not_of( delimiters, 0 ) ;
57  string::size_type pos = str.find_first_not_of( delimiters, lastPos ) ;
58 
59  while( string::npos != pos || string::npos != lastPos )
60  {
61  if(lastPos != pos)
62  {
63  string token = str.substr( lastPos, pos - lastPos ) ;
64  tokens.push_back( token ) ;
65  }
66  lastPos = str.find_first_not_of( delimiters, pos ) ;
67  pos = str.find_first_of( delimiters, lastPos ) ;
68  }
69  */
70  if( !str.empty() )
71  {
72  list<string> tmplist ;
73  BESUtil::explode( delimiter, str, tmplist ) ;
74  list<string>::iterator i = tmplist.begin() ;
75  list<string>::iterator e = tmplist.end() ;
76  for( ; i != e; i++ )
77  {
78  tokens.push_back( (*i) ) ;
79  }
80  }
81 }
82 
90 void
91 CSV_Utils::slim( string& str )
92 {
93  if( *(--str.end()) == '\"' and *str.begin() == '\"' )
94  str = str.substr( 1, str.length() - 2 ) ;
95 }
96 
CSV_Utils::slim
static void slim(std::string &str)
Strips leading and trailing double quotes from string.
Definition: CSV_Utils.cc:91
CSV_Utils::split
static void split(const std::string &str, char delimiter, std::vector< std::string > &tokens)
Splits a string into separate strings based on the delimiter.
Definition: CSV_Utils.cc:52
BESUtil::explode
static void explode(char delim, const std::string &str, std::list< std::string > &values)
Definition: BESUtil.cc:561