Fixed i18n system.
[ardour.git] / libs / pbd / pbd / tokenizer.h
1 #ifndef PBD_TOKENIZER
2 #define PBD_TOKENIZER
3
4 #include <iterator>
5 #include <string>
6
7 namespace PBD {
8
9 /**
10     Tokenize string, this should work for standard
11     strings aswell as Glib::ustring. This is a bit of a hack,
12     there are much better string tokenizing patterns out there.
13 */
14 template<typename StringType, typename Iter>
15 unsigned int
16 tokenize(const StringType& str,        
17         const StringType& delims,
18         Iter it)
19 {
20     typename StringType::size_type start_pos = 0;
21     typename StringType::size_type end_pos = 0;
22     unsigned int token_count = 0;
23
24     do {
25         start_pos = str.find_first_not_of(delims, start_pos);
26         end_pos = str.find_first_of(delims, start_pos);
27         if (start_pos != end_pos) {
28             if (end_pos == str.npos) {
29                 end_pos = str.length();
30             }
31             *it++ = str.substr(start_pos, end_pos - start_pos);
32             ++token_count;
33             start_pos = str.find_first_not_of(delims, end_pos + 1);
34         }
35     } while (start_pos != str.npos);
36
37     if (start_pos != str.npos) {
38         *it++ = str.substr(start_pos, str.length() - start_pos);
39         ++token_count;
40     }
41
42     return token_count;
43 }
44
45 } // namespace PBD
46
47 #endif // PBD_TOKENIZER
48
49