From 860e7af61d67876ba23d5a93ccfbfd67d7a94292 Mon Sep 17 00:00:00 2001 From: hasufell Date: Wed, 1 Oct 2014 23:08:58 +0200 Subject: [PATCH] Improve error handling Checks the file extension before attempting to read the file. Also allow empty argument list. --- Gtk.hs | 33 +++++++++++++++++++++------------ Main.hs | 6 ++++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Gtk.hs b/Gtk.hs index ee7b5d1..0955a6d 100644 --- a/Gtk.hs +++ b/Gtk.hs @@ -8,6 +8,7 @@ import Diagrams.Backend.Cairo.Internal import Graphics.UI.Gtk import Graphics.UI.Gtk.Windows.MessageDialog import System.Directory +import Util -- |Handle the whole GTK gui. @@ -126,24 +127,32 @@ showErrorDialog str = do -- |Draws a Diagram which is built from a given file to -- the gtk DrawingArea. +-- Prints an error dialog if no valid mesh file is found. drawDiag' :: (WidgetClass widget, RangeClass scale) => FilePath -> widget -> scale -> IO () -drawDiag' fp da scale' = do - mesh <- readFile fp - dw <- widgetGetDrawWindow da - adjustment <- rangeGetAdjustment scale' - scaleVal <- adjustmentGetValue adjustment - let (_, r) = renderDia Cairo - (CairoOptions "" (Width 600) SVG False) - (diagFromString (MkProp scaleVal) mesh) - renderWithDrawable dw r +drawDiag' fp da scale' = + case cmpExt "obj" fp of + True -> do + mesh <- readFile fp + dw <- widgetGetDrawWindow da + adjustment <- rangeGetAdjustment scale' + scaleVal <- adjustmentGetValue adjustment + let (_, r) = renderDia Cairo + (CairoOptions "" (Width 600) SVG False) + (diagFromString (MkProp scaleVal) mesh) + renderWithDrawable dw r + False -> showErrorDialog "No valid Mesh file!" -- |Saves a Diagram which is built from a given file as an SVG. +-- Prints an error dialog if no valid mesh file is found. saveDiag' :: FilePath -> IO () -saveDiag' fp = do - mesh <- readFile fp - renderCairo "out.svg" (Width 600) (diagFromString (MkProp 2) mesh) +saveDiag' fp = + case cmpExt "obj" fp of + True -> do + mesh <- readFile fp + renderCairo "out.svg" (Width 600) (diagFromString (MkProp 2) mesh) + False -> showErrorDialog "No valid Mesh file!" diff --git a/Main.hs b/Main.hs index b0ebfc8..0a0a010 100644 --- a/Main.hs +++ b/Main.hs @@ -4,5 +4,7 @@ import System.Environment main :: IO () main = do - [a] <- getArgs - startGUI a + a <- getArgs + case null a of + False -> startGUI (head a) + True -> startGUI ""