Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
not needed now
[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 <stdlib.h>\r
19 \r
20 #include <sys/types.h>\r
21 #include <sys/stat.h>\r
22 \r
23 #include <msg/msg.h>\r
24 \r
25 #ifndef S_ISREG\r
26         #define S_ISREG(__mode) (((__mode) & S_IFMT) == S_IFREG)\r
27 #endif\r
28 \r
29 namespace SimGrid\r
30 {\r
31         namespace Msg\r
32         {\r
33                 Environment::Environment()\r
34                 {\r
35                         this->file = NULL;\r
36                         this->loaded = false;\r
37                 }\r
38                                 \r
39                 Environment::Environment(const Environment& rEnvironment)\r
40                 {\r
41                         this->file = rEnvironment.getFile();\r
42                         this->loaded = rEnvironment.isLoaded();\r
43                 }\r
44                 \r
45                 Environment::Environment(const char* file)\r
46                 throw(NullPointerException, FileNotFoundException)\r
47                 {\r
48                         // check parameters\r
49                         \r
50                         if(!file)\r
51                                 throw NullPointerException("file (must not be NULL");\r
52                         \r
53                         struct stat statBuf = {0};\r
54                                 \r
55                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
56                                 throw FileNotFoundException("file (file not found)");\r
57                                 \r
58                         this->file = file;\r
59                         this->loaded = false;\r
60                 }\r
61                 \r
62                 Environment::~Environment()\r
63                 {\r
64                         // NOTHING TODO\r
65                 }\r
66                 \r
67                 // Operations.\r
68                 \r
69                 void Environment::load(void)\r
70                 throw(LogicException)\r
71                 {\r
72                         // check logic\r
73                         \r
74                         if(this->loaded)\r
75                                 throw LogicException("environement already loaded");\r
76                         \r
77                         // check the parameters\r
78                         if(!this->file)\r
79                                 throw LogicException("you must specify the xml file which describe the environment to load\nuse Environment::setFile()"); \r
80                         \r
81                         MSG_create_environment(file);\r
82                         \r
83                         this->loaded = true;            \r
84                 }\r
85                 \r
86                 void Environment::load(const char* file)\r
87                 throw(NullPointerException, FileNotFoundException, LogicException)\r
88                 {\r
89                         // check logic\r
90                         \r
91                         if(this->loaded)\r
92                                 throw LogicException("environment already loaded");\r
93                         \r
94                         // check the parameters\r
95                                 \r
96                         if(!file)\r
97                                 throw NullPointerException("file");\r
98                         \r
99                         struct stat statBuf = {0};\r
100                                 \r
101                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
102                                 throw FileNotFoundException(file);\r
103                                         \r
104                         MSG_create_environment(file);\r
105                         \r
106                         this->file = file;\r
107                         this->loaded = true;    \r
108                 }\r
109                 \r
110                 bool Environment::isLoaded(void) const\r
111                 {\r
112                         return this->loaded;\r
113                 }\r
114                 \r
115                 // Getters/setters\r
116                 void Environment::setFile(const char* file)\r
117                 throw(NullPointerException, FileNotFoundException, LogicException)\r
118                 {\r
119                         // check logic\r
120                         \r
121                         if(this->loaded)\r
122                                 throw LogicException("your are trying to change the file of an already loaded environment");\r
123                                 \r
124                         // check parameters\r
125                         \r
126                         if(!file)\r
127                                 throw NullPointerException("file (must not be NULL");\r
128                         \r
129                         struct stat statBuf = {0};\r
130                                 \r
131                         if(stat(file, &statBuf) < 0 || !S_ISREG(statBuf.st_mode))\r
132                                 throw FileNotFoundException("file (file not found)");\r
133                                 \r
134                         this->file = file;\r
135                 }\r
136                 \r
137                 const char* Environment::getFile(void) const\r
138                 {\r
139                         return this->file;\r
140                 }\r
141         \r
142                 \r
143                 const Environment& Environment::operator = (const Environment& rEnvironment)\r
144                 throw(LogicException)\r
145                 {\r
146                         // check logic\r
147                         \r
148                         if(this->loaded)\r
149                                 throw LogicException("environment already loaded");\r
150                         \r
151                         this->file = rEnvironment.getFile();\r
152                         this->loaded = rEnvironment.isLoaded();\r
153                         \r
154                         return *this;\r
155                 }\r
156                                 \r
157         } // namespace Msg\r
158 } // namespace SimGrid\r
159 \r