Logo AND Algorithmique Numérique Distribuée

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