add export visibility control to libpbd (works thus far on linux/gcc)
[ardour.git] / libs / pbd / pbd / tokenizer.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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 General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef PBD_TOKENIZER
21 #define PBD_TOKENIZER
22
23 #include <iterator>
24 #include <string>
25
26 #include "pbd/libpbd_visibility.h"
27 #include "pbd/whitespace.h"
28
29 namespace PBD {
30
31 /**
32     Tokenize string, this should work for standard
33     strings as well as Glib::ustring. This is a bit of a hack,
34     there are much better string tokenizing patterns out there.
35         If strip_whitespace is set to true, tokens will be checked to see
36         that they still have a length after stripping.  If no length, they
37         are discarded.
38 */
39 template<typename StringType, typename Iter>
40 LIBPBD_API unsigned int
41 tokenize(const StringType& str,        
42         const StringType& delims,
43         Iter it,
44                 bool strip_whitespace=false)
45 {
46     typename StringType::size_type start_pos = 0;
47     typename StringType::size_type end_pos = 0;
48     unsigned int token_count = 0;
49
50     do {
51         start_pos = str.find_first_not_of(delims, start_pos);
52         end_pos = str.find_first_of(delims, start_pos);
53         if (start_pos != end_pos) {
54             if (end_pos == str.npos) {
55                 end_pos = str.length();
56             }
57                 if (strip_whitespace) {
58                                 StringType stripped = str.substr(start_pos, end_pos - start_pos);
59                                 strip_whitespace_edges (stripped);
60                                 if (stripped.length()) {
61                                         *it++ = stripped;
62                                 }
63                         } else {
64                 *it++ = str.substr(start_pos, end_pos - start_pos);
65                         }
66             ++token_count;
67             start_pos = str.find_first_not_of(delims, end_pos + 1);
68         }
69     } while (start_pos != str.npos);
70
71     if (start_pos != str.npos) {
72         if (strip_whitespace) {
73                         StringType stripped = str.substr(start_pos, str.length() - start_pos);
74                         strip_whitespace_edges (stripped);
75                         if (stripped.length()) {
76                                 *it++ = stripped;
77                         }
78                 } else {
79                 *it++ = str.substr(start_pos, str.length() - start_pos);
80                 }
81         ++token_count;
82     }
83
84     return token_count;
85 }
86
87 } // namespace PBD
88
89 #endif // PBD_TOKENIZER
90
91