Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Refactoring of code and documentation.
[simgrid.git] / src / cxx / Environment.cxx
1 #include <Environment.hpp>\r
2 \r
3 #include <sys/types.h>\r
4 #include <sys/stat.h>\r
5 \r
6 #ifndef S_ISREG\r
7         #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG)\r
8 #endif\r
9 \r
10 namespace SimGrid\r
11 {\r
12         namespace Msg\r
13         {\r
14                 Environment::Environment()\r
15                 {\r
16                         this->file = NULL;\r
17                         this->loaded = false;\r
18                 }\r
19                                 \r
20                 Environment::Environment(const Environment& rEnvironment);\r
21                 {\r
22                         this->file = rEnvironment.getFile();\r
23                         this->loaded = rEnvironment.isLoaded();\r
24                 }\r
25                 \r
26                 Environment::Environment(const char* file)\r
27                 throw(NullPointerException, InvalidArgumentException);\r
28                 {\r
29                         // check parameters\r
30                         \r
31                         if(!file)\r
32                                 throw NullPointerException("file (must not be NULL");\r
33                         \r
34                         struct stat statBuf = {0};\r
35                                 \r
36                         if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode))\r
37                                 throw InvalidParameterException("file (file not found)");\r
38                                 \r
39                         this->file = file;\r
40                         this->loaded = false;\r
41                 }\r
42                 \r
43                 Environment::~Environment()\r
44                 {\r
45                         // NOTHING TODO\r
46                 }\r
47                 \r
48                 // Operations.\r
49                 \r
50                 void Environment::load(void)\r
51                 throw(LogicException)\r
52                 {\r
53                         // check logic\r
54                         \r
55                         if(this->loaded)\r
56                                 throw LogicException("environement already loaded");\r
57                         \r
58                         // check the parameters\r
59                         if(!this->file)\r
60                                 throw LogicException("you must specify the xml file which describe the environment to load\nuse Environment::setFile()"); \r
61                         \r
62                         MSG_create_environment(file);\r
63                         \r
64                         this->loaded = true;            \r
65                 }\r
66                 \r
67                 void Environment::load(const char* file)\r
68                 throw(NullPointerException, FileNotFoundException, LogicException)\r
69                 {\r
70                         // check logic\r
71                         \r
72                         if(this->loaded)\r
73                                 throw LogicException("environment already loaded");\r
74                         \r
75                         // check the parameters\r
76                                 \r
77                         if(!file)\r
78                                 throw NullPointerException("file");\r
79                         \r
80                         struct stat statBuf = {0};\r
81                                 \r
82                         if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode))\r
83                                 throw FileNotFoundException(file);\r
84                                         \r
85                         MSG_create_environment(file);\r
86                         \r
87                         this->file = file;\r
88                         this->loaded = true;    \r
89                 }\r
90                 \r
91                 bool Environment::isLoaded(void) const\r
92                 {\r
93                         return this->loaded;\r
94                 }\r
95                 \r
96                 // Getters/setters\r
97                 void Environment::setFile(const char* file)\r
98                 throw(NullPointerException, FileNotFoundException, LogicException)\r
99                 {\r
100                         // check logic\r
101                         \r
102                         if(this->loaded)\r
103                                 throw LogicException("your are trying to change the file of an already loaded environment");\r
104                                 \r
105                         // check parameters\r
106                         \r
107                         if(!file)\r
108                                 throw NullPointerException("file (must not be NULL");\r
109                         \r
110                         struct stat statBuf = {0};\r
111                                 \r
112                         if(stat(statBuff, &info) < 0 || !S_ISREG(statBuff.st_mode))\r
113                                 throw FileNotFoundException("file (file not found)");\r
114                                 \r
115                         this->file = file;\r
116                 }\r
117                 \r
118                 const char* Environment::getFile(void) const\r
119                 {\r
120                         return this->file;\r
121                 }\r
122         \r
123                 \r
124                 const Environment& Environment::operator = (const Environment& rEnvironment)\r
125                 throw(LogicException)\r
126                 {\r
127                         // check logic\r
128                         \r
129                         if(this->loaded)\r
130                                 throw LogicException("environment already loaded");\r
131                         \r
132                         this->file = rEnvironment.getFile();\r
133                         this->loaded = rEnvironment.isLoaded();\r
134                         \r
135                         return *this;\r
136                 }\r
137                                 \r
138         } // namespace Msg\r
139 } // namespace SimGrid