add keybinding editor
[ardour.git] / libs / pbd / strsplit.cc
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 #include <pbd/strsplit.h>
21
22 using namespace std;
23 using namespace Glib;
24
25 void
26 split (string str, vector<string>& result, char splitchar)
27 {       
28         string::size_type pos;
29         string remaining;
30         string::size_type len = str.length();
31         int cnt;
32
33         cnt = 0;
34
35         if (str.empty()) {
36                 return;
37         }
38
39         for (string::size_type n = 0; n < len; ++n) {
40                 if (str[n] == splitchar) {
41                         cnt++;
42                 }
43         }
44
45         if (cnt == 0) {
46                 result.push_back (str);
47                 return;
48         }
49
50         remaining = str;
51
52         while ((pos = remaining.find_first_of (splitchar)) != string::npos) {
53                 result.push_back (remaining.substr (0, pos));
54                 remaining = remaining.substr (pos+1);
55         }
56
57         if (remaining.length()) {
58
59                 result.push_back (remaining);
60         }
61 }
62
63 void
64 split (ustring str, vector<ustring>& result, char splitchar)
65 {       
66         ustring::size_type pos;
67         ustring remaining;
68         ustring::size_type len = str.length();
69         int cnt;
70
71         cnt = 0;
72
73         if (str.empty()) {
74                 return;
75         }
76
77         for (ustring::size_type n = 0; n < len; ++n) {
78                 if (str[n] == gunichar(splitchar)) {
79                         cnt++;
80                 }
81         }
82
83         if (cnt == 0) {
84                 result.push_back (str);
85                 return;
86         }
87
88         remaining = str;
89
90         while ((pos = remaining.find_first_of (splitchar)) != ustring::npos) {
91                 result.push_back (remaining.substr (0, pos));
92                 remaining = remaining.substr (pos+1);
93         }
94
95         if (remaining.length()) {
96
97                 result.push_back (remaining);
98         }
99 }