don't crash on unparseable booh-gui-rc file
authorGuillaume Cottenceau <gcottenc@gmail.com>
Thu, 17 Sep 2009 13:31:37 +0000 (15:31 +0200)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Thu, 17 Sep 2009 13:31:37 +0000 (15:31 +0200)
bin/booh

index 5323a3f..ce91b15 100644 (file)
--- a/bin/booh
+++ b/bin/booh
@@ -96,25 +96,32 @@ def read_config
     $config = {}
     $config_file = File.expand_path('~/.booh-gui-rc')
     if File.readable?($config_file)
-        xmldoc = REXML::Document.new(File.new($config_file))
-        xmldoc.root.elements.each { |element|
-            txt = element.get_text
-            if txt
-                if txt.value =~ /~~~/ || element.name == 'last-opens'
-                    $config[element.name] = txt.value.split(/~~~/)
+        begin
+            xmldoc = REXML::Document.new(File.new($config_file))
+        rescue
+            #- encoding unsupported anymore? file edited manually? ignore then
+            msg 1, "Ignoring #{$config_file}, failed to parse it: #{$!}"
+        end
+        if xmldoc
+            xmldoc.root.elements.each { |element|
+                txt = element.get_text
+                if txt
+                    if txt.value =~ /~~~/ || element.name == 'last-opens'
+                        $config[element.name] = txt.value.split(/~~~/)
+                    else
+                        $config[element.name] = txt.value
+                    end
+                elsif element.elements.size == 0
+                    $config[element.name] = ''
                 else
-                    $config[element.name] = txt.value
+                    $config[element.name] = {}
+                    element.each { |chld|
+                        txt = chld.get_text
+                        $config[element.name][chld.name] = txt ? txt.value : nil
+                    }
                 end
-            elsif element.elements.size == 0
-                $config[element.name] = ''
-            else
-                $config[element.name] = {}
-                element.each { |chld|
-                    txt = chld.get_text
-                    $config[element.name][chld.name] = txt ? txt.value : nil
-                }
-            end
-        }
+            }
+        end
     end
     $config['video-viewer'] ||= '/usr/bin/mplayer %f'
     $config['image-editor'] ||= '/usr/bin/gimp-remote %f || /usr/bin/gimp %f'