Reworked track settings.
[demorecorder] / src / CdlParser.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 namespace IdWorks {
19   
20   public static class CdlParser : Object {
21     
22     const unichar CHAR_DLM = ',';
23     const unichar CHAR_ESC = '\"';
24     const unichar CHAR_EOL = '\n';
25     
26     public static string[] ParseLine(string line) {
27       string[] values = {};
28       
29       bool in_esc = false;
30       
31       int pos = 0;
32       StringBuilder buffer = new StringBuilder();
33       unichar c;
34       
35       while ((c = line[pos]) != CHAR_EOL && !in_esc && pos < line.length) {
36         switch(c) {
37           case CHAR_ESC:
38             // if we are already escaped add to buffer
39             if (in_esc) {
40               buffer.append_unichar(c);
41               in_esc = false;
42             }
43             // else enter escape mode
44             else {
45               in_esc = true;
46             }
47             break;
48           case CHAR_DLM:
49             // add the buffer to list and clear the buffer
50             values += buffer.str;
51             buffer.truncate(0);
52             break;
53           default:
54             // normal char to add to buffer
55             buffer.append_unichar(c);
56             break;
57         }
58         ++pos;
59       }
60       // check for data left floating over
61       if (0 < buffer.len) values += buffer.str;
62       buffer.truncate(0);
63       buffer = null;
64       
65       return values;
66     }
67     
68   }
69   
70 }