Remove dynamic exceptions
[anna.git] / source / io / AbstractReader.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <stdio.h>
10
11 #include <anna/config/defines.hpp>
12
13 #include <anna/io/AbstractReader.hpp>
14 #include <anna/io/internal/sccs.hpp>
15
16 using namespace std;
17 using namespace anna;
18
19 io::AbstractReader::AbstractReader() :
20   a_file(NULL),
21   a_ex(NULL) {
22   sccs::activate();
23 }
24
25 io::AbstractReader::AbstractReader(const char* filename) :
26   a_filename(filename),
27   a_ex(NULL) {
28   if((a_file = fopen(filename, "r")) == NULL)
29     a_ex = new RuntimeException(filename, errno, ANNA_FILE_LOCATION);
30
31   sccs::activate();
32 }
33
34 io::AbstractReader::AbstractReader(const std::string& filename) :
35   a_filename(filename),
36   a_ex(NULL) {
37   if((a_file = fopen(filename.c_str(), "r")) == NULL)
38     a_ex = new RuntimeException(filename, errno, ANNA_FILE_LOCATION);
39
40   sccs::activate();
41 }
42
43 /* virtual */
44 io::AbstractReader::~AbstractReader() {
45   close();
46 }
47
48 void io::AbstractReader::close()
49 {
50   delete a_ex;
51   a_ex = NULL;
52
53   if(a_file != NULL) {
54     fclose(a_file);
55     a_file = NULL;
56   }
57 }
58
59 void io::AbstractReader::open(const std::string& filename)
60 noexcept(false) {
61   close();
62
63   if((a_file = fopen(filename.c_str(), "r")) == NULL)
64     throw RuntimeException(filename, errno, ANNA_FILE_LOCATION);
65 }
66
67 void io::AbstractReader::verify()
68 noexcept(false) {
69   if(a_ex != NULL)
70     throw *a_ex;
71
72   if(a_file == NULL) {
73     string msg("io::AbstractReader | File: ");
74     msg += a_filename;
75     msg += " | Cannot open";
76     throw RuntimeException(msg, ANNA_FILE_LOCATION);
77   }
78 }