nidas v1.2.3
string_token.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2010, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#include <string>
28#include <vector>
29
30std::string &
31trim (std::string &s)
32{
33 std::string::size_type i = 0;
34 while (i < s.length() && isspace (s[i]))
35 ++i;
36 s.erase (0, i);
37 i = s.length() - 1;
38 while (i > 0 && isspace (s[i]))
39 --i;
40 s.erase (i + 1, std::string::npos);
41 return s;
42}
43
47void
48string_token(std::vector<std::string>& substrings, const std::string& text,
49 const std::string& delims = ",")
50{
51 std::string::size_type len = text.length();
52 std::string::size_type pos = 0;
53 std::string::size_type next;
54 do
55 {
56 next = std::string::npos;
57 if (pos < len)
58 next = text.find_first_of(delims, pos);
59 std::string::size_type sublen =
60 (next == std::string::npos) ? len - pos : next - pos;
61 std::string ss = text.substr(pos, sublen);
62 substrings.push_back(trim(ss));
63 pos += sublen + 1;
64 }
65 while (next != std::string::npos);
66}
67
68
int len
Definition sing.cc:948
std::string & trim(std::string &s)
Definition string_token.h:31
void string_token(std::vector< std::string > &substrings, const std::string &text, const std::string &delims=",")
Split a string up into a vector of substrings delimited by commas.
Definition string_token.h:48