Logo AND Algorithmique Numérique Distribuée

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