Improve error handling and reporting in the LED pattern 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 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                                         try {
56                                                 pattern.parse (line);
57                                                 list.append (pattern);
58                                         } catch (LedPatternError e) {
59                                                 Hildon.Banner.show_information (null, null, e.message);
60                                         }
61                                 }
62                                 line = f.read_line ();
63                         }
64                 }
65                 line = f.read_line ();
66         }
67
68         return list;
69 }
70
71 void mce_ini_store (List<LedPattern> list) {
72         var f = FileStream.open ("/etc/mce/mce.ini", "r");
73         var g = FileStream.open ("/tmp/mce.ini", "w");
74         string pattern_section = null;
75
76         switch (device) {
77         case DeviceType.RX44:
78                 pattern_section = "[LEDPatternNJoyRX44]";
79                 break;
80         case DeviceType.RX51:
81                 pattern_section = "[LEDPatternLystiRX51]";
82                 break;
83         }
84
85         var line = f.read_line ();
86         while (line != null) {
87                 if (line == pattern_section) {
88                         g.printf ("%s\n", line);
89                         line = f.read_line ();
90                         while ((line != null) && (line[0] != '[')) {
91                                 if (line.has_prefix ("Pattern")) {
92                                         unowned List<LedPattern> node;
93                                         for (node = list.first (); node != null; node = node.next) {
94                                                 if (line.has_prefix (node.data.name + "=")) {
95                                                         g.printf ("%s\n", node.data.dump ());
96                                                         break;
97                                                 }
98                                         }
99                                         if (node == null) {
100                                                 g.printf ("%s\n", line);
101                                         }
102                                 } else {
103                                         g.printf ("%s\n", line);
104                                 }
105                                 line = f.read_line ();
106                         }
107                         if (line[0] == '[')
108                                 g.printf ("%s\n", line);
109                 } else {
110                         g.printf ("%s\n", line);
111                 }
112                 line = f.read_line ();
113         }
114 }