aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2013-06-02 13:47:19 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-02-15 15:21:54 +0100
commit8046e11445ba71b767d4fde08ce1e43e0dcd0359 (patch)
treed6991bb858a91336e76831b8dbd39c28345f21a9 /src
parent78ad52e40992d6e68238dc1574d4ae6c6f922d27 (diff)
downloadsciteco-8046e11445ba71b767d4fde08ce1e43e0dcd0359.tar.gz
use GLib's GError information to yield errors
* results in better error messages, e.g. when opening files * the case that a file to be opened (EB) exists but is not readably is handled for the first time
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp7
-rw-r--r--src/parser.cpp5
-rw-r--r--src/parser.h6
-rw-r--r--src/qregisters.cpp14
-rw-r--r--src/qregisters.h2
-rw-r--r--src/ring.cpp10
-rw-r--r--src/ring.h2
7 files changed, 27 insertions, 19 deletions
diff --git a/src/main.cpp b/src/main.cpp
index ce2cdb4..974aed2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -185,6 +185,8 @@ process_options(int &argc, char **&argv)
{NULL}
};
+ GError *gerror = NULL;
+
GOptionContext *options;
GOptionGroup *interface_group = interface.get_options();
@@ -194,8 +196,9 @@ process_options(int &argc, char **&argv)
if (interface_group)
g_option_context_add_group(options, interface_group);
- if (!g_option_context_parse(options, &argc, &argv, NULL)) {
- g_printf("Option parsing failed!\n");
+ if (!g_option_context_parse(options, &argc, &argv, &gerror)) {
+ g_fprintf(stderr, "Option parsing failed: %s\n",
+ gerror->message);
exit(EXIT_FAILURE);
}
diff --git a/src/parser.cpp b/src/parser.cpp
index 4dbec88..aed9348 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -177,10 +177,11 @@ Execute::macro(const gchar *macro, bool locals)
void
Execute::file(const gchar *filename, bool locals)
{
+ GError *gerror = NULL;
gchar *macro_str, *p;
- if (!g_file_get_contents(filename, &macro_str, NULL, NULL))
- throw State::Error("Error reading file \"%s\"", filename);
+ if (!g_file_get_contents(filename, &macro_str, NULL, &gerror))
+ throw State::GError(gerror);
/* only when executing files, ignore Hash-Bang line */
if (*macro_str == '#')
p = MAX(strchr(macro_str, '\r'), strchr(macro_str, '\n'))+1;
diff --git a/src/parser.h b/src/parser.h
index f7c9bed..8e73d64 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -156,6 +156,12 @@ public:
void display_full(void);
};
+ class GError : public Error {
+ public:
+ GError(const ::GError *gerror)
+ : Error("%s", gerror->message) {}
+ };
+
class SyntaxError : public Error {
public:
SyntaxError(gchar chr)
diff --git a/src/qregisters.cpp b/src/qregisters.cpp
index 9cf1461..550b103 100644
--- a/src/qregisters.cpp
+++ b/src/qregisters.cpp
@@ -198,15 +198,17 @@ QRegister::execute(bool locals)
g_free(str);
}
-bool
+void
QRegister::load(const gchar *filename)
{
gchar *contents;
gsize size;
+ GError *gerror = NULL;
+
/* FIXME: prevent excessive allocations by reading file into buffer */
- if (!g_file_get_contents(filename, &contents, &size, NULL))
- return false;
+ if (!g_file_get_contents(filename, &contents, &size, &gerror))
+ throw State::GError(gerror);
edit();
string.reset();
@@ -219,8 +221,6 @@ QRegister::load(const gchar *filename)
g_free(contents);
current_edit();
-
- return true;
}
tecoInt
@@ -531,9 +531,7 @@ StateLoadQReg::done(const gchar *str)
if (*str) {
register_argument->undo_load();
- if (!register_argument->load(str))
- throw Error("Cannot load \"%s\" into Q-Register \"%s\"",
- str, register_argument->name);
+ register_argument->load(str);
} else {
if (ring.current)
ring.undo_edit();
diff --git a/src/qregisters.h b/src/qregisters.h
index 452abe7..80e6532 100644
--- a/src/qregisters.h
+++ b/src/qregisters.h
@@ -112,7 +112,7 @@ public:
void execute(bool locals = true);
- bool load(const gchar *filename);
+ void load(const gchar *filename);
inline void
undo_load(void)
{
diff --git a/src/ring.cpp b/src/ring.cpp
index 134bf63..a649a0d 100644
--- a/src/ring.cpp
+++ b/src/ring.cpp
@@ -125,14 +125,16 @@ Buffer::UndoTokenClose::run(void)
* 2.) On other platforms read into and copy from a statically sized buffer
* (perhaps page-sized)
*/
-bool
+void
Buffer::load(const gchar *filename)
{
gchar *contents;
gsize size;
- if (!g_file_get_contents(filename, &contents, &size, NULL))
- return false;
+ GError *gerror = NULL;
+
+ if (!g_file_get_contents(filename, &contents, &size, &gerror))
+ throw State::GError(gerror);
edit();
@@ -151,8 +153,6 @@ Buffer::load(const gchar *filename)
#endif
set_filename(filename);
-
- return true;
}
void
diff --git a/src/ring.h b/src/ring.h
index 70b7738..92d32f0 100644
--- a/src/ring.h
+++ b/src/ring.h
@@ -114,7 +114,7 @@ public:
TECODocument::undo_edit();
}
- bool load(const gchar *filename);
+ void load(const gchar *filename);
inline void
undo_close(void)