add new sigc++2 directory
[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/whitespace.h>
27
28 namespace PBD {
29
30 /**
31     Tokenize string, this should work for standard
32     strings as well as Glib::ustring. This is a bit of a hack,
33     there are much better string tokenizing patterns out there.
34         If strip_whitespace is set to true, tokens will be checked to see
35         that they still have a length after stripping.  If no length, they
36         are discarded.
37 */
38 template<typename StringType, typename Iter>
39 unsigned int
40 tokenize(const StringType& str,        
41         const StringType& delims,
42         Iter it,
43                 bool strip_whitespace=false)
44 {
45     typename StringType::size_type start_pos = 0;
46     typename StringType::size_type end_pos = 0;
47     unsigned int token_count = 0;
48
49     do {
50         start_pos = str.find_first_not_of(delims, start_pos);
51         end_pos = str.find_first_of(delims, start_pos);
52         if (start_pos != end_pos) {
53             if (end_pos == str.npos) {
54                 end_pos = str.length();
55             }
56                 if (strip_whitespace) {
57                                 StringType stripped = str.substr(start_pos, end_pos - start_pos);
58                                 strip_whitespace_edges (stripped);
59                                 if (stripped.length()) {
60                                         *it++ = stripped;
61                                 }
62                         } else {
63                 *it++ = str.substr(start_pos, end_pos - start_pos);
64                         }
65             ++token_count;
66             start_pos = str.find_first_not_of(delims, end_pos + 1);
67         }
68     } while (start_pos != str.npos);
69
70     if (start_pos != str.npos) {
71         if (strip_whitespace) {
72                         StringType stripped = str.substr(start_pos, str.length() - start_pos);
73                         strip_whitespace_edges (stripped);
74                         if (stripped.length()) {
75                                 *it++ = stripped;
76                         }
77                 } else {
78                 *it++ = str.substr(start_pos, str.length() - start_pos);
79                 }
80         ++token_count;
81     }
82
83     return token_count;
84 }
85
86 } // namespace PBD
87
88 #endif // PBD_TOKENIZER
89
90