Adapt MCE INI parser for RX44
[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 enum DeviceType {
20         RX44,
21         RX51
22 }
23
24 static const DeviceType device = DeviceType.RX51;
25
26 List<LedPattern> mce_ini_parse () {
27         var list = new List<LedPattern> ();
28         var f = FileStream.open ("/etc/mce/mce.ini", "r");
29         string pattern_section = null;
30
31         switch (device) {
32         case DeviceType.RX44:
33                 pattern_section = "[LEDPatternNJoyRX44]";
34                 break;
35         case DeviceType.RX51:
36                 pattern_section = "[LEDPatternLystiRX51]";
37                 break;
38         }
39
40         var line = f.read_line ();
41         while (line != null) {
42                 if (line == pattern_section) {
43                         line = f.read_line ();
44                         while ((line != null) && (line[0] != '[')) {
45                                 if (line.has_prefix ("Pattern")) {
46                                         LedPattern pattern = null;
47                                         switch (device) {
48                                         case DeviceType.RX44:
49                                                 pattern = new LedPatternRX44 ();
50                                                 break;
51                                         case DeviceType.RX51:
52                                                 pattern = new LedPatternRX51 ();
53                                                 break;
54                                         }
55                                         pattern.parse (line);
56                                         list.append (pattern);
57                                 }
58                                 line = f.read_line ();
59                         }
60                 }
61                 line = f.read_line ();
62         }
63
64         return list;
65 }
66
67 void mce_ini_store (List<LedPattern> list) {
68         var f = FileStream.open ("/etc/mce/mce.ini", "r");
69         var g = FileStream.open ("/tmp/mce.ini", "w");
70         string pattern_section = null;
71
72         switch (device) {
73         case DeviceType.RX44:
74                 pattern_section = "[LEDPatternNJoyRX44]";
75                 break;
76         case DeviceType.RX51:
77                 pattern_section = "[LEDPatternLystiRX51]";
78                 break;
79         }
80
81         var line = f.read_line ();
82         while (line != null) {
83                 if (line == pattern_section) {
84                         g.printf ("%s\n", line);
85                         line = f.read_line ();
86                         while ((line != null) && (line[0] != '[')) {
87                                 if (line.has_prefix ("Pattern")) {
88                                         unowned List<LedPattern> node;
89                                         for (node = list.first (); node != null; node = node.next) {
90                                                 if (line.has_prefix (node.data.name + "=")) {
91                                                         g.printf ("%s\n", node.data.dump ());
92                                                         break;
93                                                 }
94                                         }
95                                         if (node == null) {
96                                                 g.printf ("%s\n", line);
97                                         }
98                                 } else {
99                                         g.printf ("%s\n", line);
100                                 }
101                                 line = f.read_line ();
102                         }
103                         if (line[0] == '[')
104                                 g.printf ("%s\n", line);
105                 } else {
106                         g.printf ("%s\n", line);
107                 }
108                 line = f.read_line ();
109         }
110 }