Add mce.ini parser
authorPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 24 Feb 2010 16:57:57 +0000 (17:57 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 24 Feb 2010 17:55:50 +0000 (18:55 +0100)
Makefile
src/mce-ini-parse.vala [new file with mode: 0644]

index f57d8f9..18a8ae6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,8 @@ led_pattern_editor_SOURCES = $(patsubst %.vala,%.c,${led_pattern_editor_VALASOUR
 led_pattern_editor_VALASOURCES = \
        src/led-pattern-editor.vala \
        src/led-pattern.vala \
-       src/led-pattern-rx51.vala
+       src/led-pattern-rx51.vala \
+       src/mce-ini-parse.vala
 
 led_pattern_editor_VALAFLAGS = --pkg hildon-1 --pkg libosso
 
diff --git a/src/mce-ini-parse.vala b/src/mce-ini-parse.vala
new file mode 100644 (file)
index 0000000..d7a4438
--- /dev/null
@@ -0,0 +1,75 @@
+/* This file is part of LED Pattern Editor.
+ *
+ * Copyright (C) 2010 Philipp Zabel
+ *
+ * LED Pattern Editor is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LED Pattern Editor is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+List<LedPatternRX51> mce_ini_parse () {
+       var list = new List<LedPatternRX51> ();
+       var f = FileStream.open ("/etc/mce/mce.ini", "r");
+
+       var line = f.read_line ();
+       while (line != null) {
+               if (line == "[LEDPatternLystiRX51]") {
+                       line = f.read_line ();
+                       while ((line != null) && (line[0] != '[')) {
+                               if (line.has_prefix ("Pattern")) {
+                                       var pattern = new LedPatternRX51 ();
+                                       pattern.parse (line);
+                                       list.append (pattern);
+                               }
+                               line = f.read_line ();
+                       }
+               }
+               line = f.read_line ();
+       }
+
+       return list;
+}
+
+void mce_ini_store (List<LedPatternRX51> list) {
+       var f = FileStream.open ("/etc/mce/mce.ini", "r");
+       var g = FileStream.open ("/tmp/mce.ini", "w");
+
+       var line = f.read_line ();
+       while (line != null) {
+               if (line == "[LEDPatternLystiRX51]") {
+                       g.printf ("%s\n", line);
+                       line = f.read_line ();
+                       while ((line != null) && (line[0] != '[')) {
+                               if (line.has_prefix ("Pattern")) {
+                                       unowned List<LedPatternRX51> node;
+                                       for (node = list.first (); node != null; node = node.next) {
+                                               if (line.has_prefix (node.data.name + "=")) {
+                                                       g.printf ("%s\n", node.data.dump ());
+                                                       break;
+                                               }
+                                       }
+                                       if (node == null) {
+                                               g.printf ("%s\n", line);
+                                       }
+                               } else {
+                                       g.printf ("%s\n", line);
+                               }
+                               line = f.read_line ();
+                       }
+                       if (line[0] == '[')
+                               g.printf ("%s\n", line);
+               } else {
+                       g.printf ("%s\n", line);
+               }
+               line = f.read_line ();
+       }
+}