Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
806e868860bba3f80825c4ed73fd86821e0cc500
[simgrid.git] / src / cxx / Environment.cxx
1 /*\r
2  * Environment.cxx\r
3  *\r
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           \r
5  * All right reserved. \r
6  *\r
7  * This program is free software; you can redistribute \r
8  * it and/or modify it under the terms of the license \r
9  *(GNU LGPL) which comes with this package. \r
10  *\r
11  */\r
12  \r
13  /* Environment member functions implementation.\r
14   */  \r
15   \r
16 #include <Environment.hpp>\r
17 \r
18 #include <NullPointerException.hpp>\r
19 #include <FileNotFoundException.hpp>\r
20 #include <InvalidArgumentException.hpp>\r
21 #include <LogicException.hpp>\r
22 #include <MsgException.hpp>\r
23 \r
24 #include <stdlib.h>\r
25 \r
26 #include <sys/types.h>\r
27 #include <sys/stat.h>\r
28 \r
29 #include <msg/msg.h>\r
30 \r
31 #ifndef S_ISREG\r
32         #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG)\r
33 #endif\r
34 \r
35 namespace SimGrid\r
36 {\r
37         namespace Msg\r
38         {\r
39                 Environment::Environment()\r
40                 {\r
41                         this->file = NULL;\r
42                         this->loaded = false;\r
43                 }\r
44                                 \r
45                 Environment::Environment(const Environment& rEnvironment)\r
46                 {\r
47                         this->file = rEnvironment.getFile();\r
48                         this->loaded = rEnvironment.isLoaded();\r
49                 }\r
50                 \r
51                 Environment::Environment(const char* file)\r
52                 throw(NullPointerException, InvalidArgumentException)\r
53                 {\r
54                         // check parameters\r
55                         \r
56                         if(!file)\r
57                                 throw NullPointerException("file (must not be NULL");\r
58                         \r
59                         struct stat statBuf = {0};\r
60                                 \r
61                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
62                                 throw InvalidArgumentException("file (file not found)");\r
63                                 \r
64                         this->file = file;\r
65                         this->loaded = false;\r
66                 }\r
67                 \r
68                 Environment::~Environment()\r
69                 {\r
70                         // NOTHING TODO\r
71                 }\r
72                 \r
73                 // Operations.\r
74                 \r
75                 void Environment::load(void)\r
76                 throw(LogicException)\r
77                 {\r
78                         // check logic\r
79                         \r
80                         if(this->loaded)\r
81                                 throw LogicException("environement already loaded");\r
82                         \r
83                         // check the parameters\r
84                         if(!this->file)\r
85                                 throw LogicException("you must specify the xml file which describe the environment to load\nuse Environment::setFile()"); \r
86                         \r
87                         MSG_create_environment(file);\r
88                         \r
89                         this->loaded = true;            \r
90                 }\r
91                 \r
92                 void Environment::load(const char* file)\r
93                 throw(NullPointerException, FileNotFoundException, LogicException)\r
94                 {\r
95                         // check logic\r
96                         \r
97                         if(this->loaded)\r
98                                 throw LogicException("environment already loaded");\r
99                         \r
100                         // check the parameters\r
101                                 \r
102                         if(!file)\r
103                                 throw NullPointerException("file");\r
104                         \r
105                         struct stat statBuf = {0};\r
106                                 \r
107                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
108                                 throw FileNotFoundException(file);\r
109                                         \r
110                         MSG_create_environment(file);\r
111                         \r
112                         this->file = file;\r
113                         this->loaded = true;    \r
114                 }\r
115                 \r
116                 bool Environment::isLoaded(void) const\r
117                 {\r
118                         return this->loaded;\r
119                 }\r
120                 \r
121                 // Getters/setters\r
122                 void Environment::setFile(const char* file)\r
123                 throw(NullPointerException, FileNotFoundException, LogicException)\r
124                 {\r
125                         // check logic\r
126                         \r
127                         if(this->loaded)\r
128                                 throw LogicException("your are trying to change the file of an already loaded environment");\r
129                                 \r
130                         // check parameters\r
131                         \r
132                         if(!file)\r
133                                 throw NullPointerException("file (must not be NULL");\r
134                         \r
135                         struct stat statBuf = {0};\r
136                                 \r
137                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
138                                 throw FileNotFoundException("file (file not found)");\r
139                                 \r
140                         this->file = file;\r
141                 }\r
142                 \r
143                 const char* Environment::getFile(void) const\r
144                 {\r
145                         return this->file;\r
146                 }\r
147         \r
148                 \r
149                 const Environment& Environment::operator = (const Environment& rEnvironment)\r
150                 throw(LogicException)\r
151                 {\r
152                         // check logic\r
153                         \r
154                         if(this->loaded)\r
155                                 throw LogicException("environment already loaded");\r
156                         \r
157                         this->file = rEnvironment.getFile();\r
158                         this->loaded = rEnvironment.isLoaded();\r
159                         \r
160                         return *this;\r
161                 }\r
162                                 \r
163         } // namespace Msg\r
164 } // namespace SimGrid