Add mce.ini parser
[led-pattern-ed] / src / mce-ini-parse.vala
1 /* This file is part of LED Pattern Editor.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * LED Pattern Editor is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * LED Pattern Editor is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 List<LedPatternRX51> mce_ini_parse () {
20         var list = new List<LedPatternRX51> ();
21         var f = FileStream.open ("/etc/mce/mce.ini", "r");
22
23         var line = f.read_line ();
24         while (line != null) {
25                 if (line == "[LEDPatternLystiRX51]") {
26                         line = f.read_line ();
27                         while ((line != null) && (line[0] != '[')) {
28                                 if (line.has_prefix ("Pattern")) {
29                                         var pattern = new LedPatternRX51 ();
30                                         pattern.parse (line);
31                                         list.append (pattern);
32                                 }
33                                 line = f.read_line ();
34                         }
35                 }
36                 line = f.read_line ();
37         }
38
39         return list;
40 }
41
42 void mce_ini_store (List<LedPatternRX51> list) {
43         var f = FileStream.open ("/etc/mce/mce.ini", "r");
44         var g = FileStream.open ("/tmp/mce.ini", "w");
45
46         var line = f.read_line ();
47         while (line != null) {
48                 if (line == "[LEDPatternLystiRX51]") {
49                         g.printf ("%s\n", line);
50                         line = f.read_line ();
51                         while ((line != null) && (line[0] != '[')) {
52                                 if (line.has_prefix ("Pattern")) {
53                                         unowned List<LedPatternRX51> node;
54                                         for (node = list.first (); node != null; node = node.next) {
55                                                 if (line.has_prefix (node.data.name + "=")) {
56                                                         g.printf ("%s\n", node.data.dump ());
57                                                         break;
58                                                 }
59                                         }
60                                         if (node == null) {
61                                                 g.printf ("%s\n", line);
62                                         }
63                                 } else {
64                                         g.printf ("%s\n", line);
65                                 }
66                                 line = f.read_line ();
67                         }
68                         if (line[0] == '[')
69                                 g.printf ("%s\n", line);
70                 } else {
71                         g.printf ("%s\n", line);
72                 }
73                 line = f.read_line ();
74         }
75 }