Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
MsppManager.cpp
1 
12 #include "stdafx.h"
13 #include "MsppManager.h"
14 
15 #include <time.h>
16 #include <boost/bind.hpp>
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/operations.hpp>
19 
20 
21 using namespace kome::core;
22 
23 
24 #include <crtdbg.h>
25 #ifdef _DEBUG
26  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
27  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
28 #endif // _DEBUG
29 
30 
31 
32 #define APP_NAME "Mass++"
33 #define REG_NAME "Mass++2"
34 #define MSPP_VERSION "2.7.5"
35 
36 #define TO_STR( s ) TO_STR_( s )
37 #define TO_STR_( s ) #s
38 
39 
40 // constructor
42  // initialize
43  m_ini = NULL;
44  m_params = NULL;
45 
46  // set path
47  setPath();
48 
49  // set mspp plugin dir
50  setPluginDir();
51 
52  // delete tmp files
54 
55  // version
56  m_app = APP_NAME;
57  m_reg = REG_NAME;
58  m_version = MSPP_VERSION;
59 
60 #if defined(_WIN64) || defined(__amd64__)
61  m_platform = "64-bit";
62 #else
63  m_platform = "32-bit";
64 #endif // _WIN64 || __amd64__
65 
67  m_pid = getprocessid();
68 
69  m_bInitializedMATLAB = false; // @Date:2013/08/08 <Add> A.Ozaki
70 }
71 
72 // destructor
74 }
75 
76 // get the application name
77 const char* MsppManager::getAppName() {
78  return m_app.c_str();
79 }
80 
81 // get the platform name
83  return m_platform.c_str();
84 }
85 
86 // get the registory name
87 const char* MsppManager::getRegName() {
88  return m_reg.c_str();
89 }
90 
91 // get version
92 const char* MsppManager::getVersion() {
93  return m_version.c_str();
94 }
95 
96 // set file path
98  // Mass++ dir
99 #ifdef MSPP_INSTALL_DIR
100  m_msppDir = TO_STR( MSPP_INSTALL_DIR );
101 #else
102  std::string moduleDir = getmoduledir();
103  m_msppDir = getabsolutepath( moduleDir.c_str(), ".." );
104 #endif // MSPP_INSTALL_DIR
105 
106  // conf dir
107  std::string homeDir = gethomedir();
108  m_confDir = getabsolutepath( homeDir.c_str(), ".mspp" );
109  if( !fileexists( m_confDir.c_str() ) ) {
110  makedirectory( m_confDir.c_str() );
111  }
112 
113  // tmp dir
114  const char* envariables[] = {
115  "TMP", "TEMP", "tmp", "temp"
116  };
117  unsigned int num = sizeof( envariables ) / sizeof( const char* );
118 
119  std::string tmpDir;
120  for( unsigned int i = 0; i < num && tmpDir.empty(); i++ ) {
121  tmpDir = getenvariable( envariables[ i ] );
122  }
123 
124  // cut by path separator
125  std::string::size_type pos = tmpDir.find( PATH_SEPARATOR );
126  if( pos != tmpDir.npos ) {
127  tmpDir = tmpDir.substr( 0, pos );
128  }
129 
130  // default directory
131  if( tmpDir.empty() ) {
132 #ifdef __unix
133  tmpDir = "/tmp";
134 #else
135  tmpDir = "C:\\TEMP";
136 #endif // __unix
137  }
138 
139  m_tmpDir = getabsolutepath( tmpDir.c_str(), "MassPlusPlus_Tmp" );
140 
141  // make directory
142  if( !fileexists( m_tmpDir.c_str() ) ) {
143  makedirectory( m_tmpDir.c_str() );
144  }
145 }
146 
147 // delete tmp files
149  // check directory
150  if( m_tmpDir.empty() ) {
151  return;
152  }
153 
154  // delete files
155  boost::filesystem::path p( m_tmpDir.c_str(), boost::filesystem::native );
156  boost::filesystem::directory_iterator end;
157 
158  for( boost::filesystem::directory_iterator it( p ); it != end; it++ ) {
159  boost::filesystem::path child = boost::filesystem::absolute( *it );
160 
161  std::string fileName = getfilename( child.string().c_str() );
162  int pid = toint( fileName.c_str(), 16, m_pid );
163 
164  if( pid == m_pid || !processexists( pid ) ) {
165  removedirs( child.string().c_str() );
166  }
167  }
168 }
169 
170 // get Mass++ directory
171 const char* MsppManager::getMsppDir() {
172  return m_msppDir.c_str();
173 }
174 
175 // get config directory
176 const char* MsppManager::getConfDir() {
177  return m_confDir.c_str();
178 }
179 
180 // get temporary file directory
181 const char* MsppManager::getTmpDir() {
182  // check the member
183  if( m_pidTmpDir.empty() ) {
184  std::string pidStr = FMT( "%04x", m_pid );
185  m_pidTmpDir = getpath( m_tmpDir.c_str(), pidStr.c_str() );
186 
187  // make directory
188  if( !fileexists( m_pidTmpDir.c_str() ) ) {
189  makedirs( m_pidTmpDir.c_str() );
190  }
191  }
192 
193  return m_pidTmpDir.c_str();
194 }
195 
196 // get temporary file name
197 std::string MsppManager::getTmpFileName( const char* prefix, const char* suffix, const char* dir ) {
198  // file path
199  std::string path;
200  std::string fileName;
201  std::string d = NVL( dir, "" );
202 
203  // check directory
204  std::string tmpDir = getTmpDir();
205  if( tmpDir.empty() ) {
206  return path;
207  }
208 
209  // check directory
210  if( d.empty() ) {
211  d = tmpDir;
212  }
213  else if( !isabsolutepath( d.c_str() ) ) {
214  d = getpath( tmpDir.c_str(), d.c_str() );
215  makedirs( d.c_str() );
216  }
217 
218  // date
219  struct tm date;
220  time_t now;
221 
222  time( &now );
223 
224 #ifdef _MSC_VER
225  localtime_s( &date, &now );
226 #else
227  date = *( localtime( &now ) );
228 #endif
229 
230  // tempolary file path
231  long count = 0;
232  while( path.empty() && count >= 0 ) {
233  // file
234  fileName = FMT(
235  "%s%02d%02d%02d%04x%s",
236  prefix,
237  date.tm_mon + 1,
238  date.tm_mday,
239  date.tm_year % 100,
240  count,
241  suffix
242  );
243  for( unsigned int i = 0; i < fileName.length(); i++ ) {
244  char c = fileName[ i ];
245  if( c == '/' || c == '\\' || c == ':' || c == '?'
246  || c == '>' || c == '<' || c == '|' || c == ' '
247  || c == '\r' || c == '\t' || c == '\n' || c == ',' ) {
248  fileName[ i ] = '_';
249  }
250  }
251  std::string p = getabsolutepath( d.c_str(), fileName.c_str() );
252 
253  // check file
254  if( !fileexists( p.c_str() ) ) {
255  path = p;
256  }
257 
258  count++;
259  }
260 
261  LOG_TRACE( FMT( "Temporary File [%s]", path.c_str() ) );
262 
263  return fileName;
264 }
265 
266 // set ini file
268  m_ini = ini;
269 }
270 
271 // get ini file
273  return m_ini;
274 }
275 
276 // set parameters file
278  m_params = params;
279 }
280 
281 // get parameters file
283  return m_params;
284 }
285 
286 // get proceess ID
287 unsigned int MsppManager::getPid() {
288  return m_pid;
289 }
290 
291 // get instance
293  // create object (This is the only object.)
294  static MsppManager mgr;
295 
296  return mgr;
297 }
298 
299 // @date 2011.10.21 <Add> M.Izumi ->
300 //set mspp plugin dir
302  std::string str = getplugindir();
303  stringtoken( str.c_str(), PATH_SEPARATOR, m_pluginPaths );
304 }
305 
306 // get mspp plugin dir
307 std::vector< std::string > MsppManager::getPluginDir(){
308  return m_pluginPaths;
309 }
310 //<-
311 
312 // >>>>>> @Date:2013/08/08 <Add> A.Ozaki
313 //
314 // get initialized flag of MATLAB
316 {
317  return m_bInitializedMATLAB;
318 }
319 
320 // set initialized flag of MATLAB
322 {
323  m_bInitializedMATLAB = true;
324  return;
325 }
326 
327 // reset initialized flag of MATLAB
329 {
330  m_bInitializedMATLAB = false;
331  return;
332 }
333 
334 //
335 // <<<<<< @Date:2013/08/08 <Add> A.Ozaki
std::string getfilename(const char *path)
get file name
std::string getenvariable(const char *name)
get environment variable
static MsppManager & getInstance()
gets MsppManager object (This is the only object.)
std::vector< std::string > getPluginDir()
get mspp plugin dir
bool isabsolutepath(const char *file)
judges whether the specified file path is absolute file path or not.
bool removedirs(const char *path)
remove directories
const char * getConfDir()
gets config directory
std::string getmoduledir()
gets module file directory
const char * getRegName()
gets the registory name
Definition: MsppManager.cpp:87
std::string m_platform
Definition: MsppManager.h:77
Mass++ manager class.
Definition: MsppManager.h:28
IniFile * getIniFile()
gets ini file
void setInitializedMATLAB(void)
sets initialized flag value
#define PATH_SEPARATOR
Definition: CoreMacros.h:83
bool makedirs(const char *path)
make directories
const char * getPlatformName()
gets the platform name
Definition: MsppManager.cpp:82
IniFile * getParameters()
gets parameters
int toint(const char *s, const int radix, const int dfval)
convert string into integer
MsppManager()
constructor
Definition: MsppManager.cpp:41
bool fileexists(const char *path)
judge whether file exists
std::string getabsolutepath(const char *dir, const char *file)
get absolute path
void resetInitializedMATLAB(void)
resets initialized flag value
void setParameters(IniFile *params)
sets parameters file
unsigned int stringtoken(const char *s, const char *delim, std::vector< std::string > &tokens)
get tokens from string
const char * getTmpDir()
gets temporary file directory name
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
void setIniFile(IniFile *ini)
sets ini file
#define NULL
Definition: CoreMacros.h:18
bool makedirectory(const char *path)
creates directory
implements of MsppManager class
void deleteTmpFiles()
deletes temporary files
const char * getVersion()
gets Mass++ version
Definition: MsppManager.cpp:92
const char * getMsppDir()
gets Mass++ directory
const char * getAppName()
gets the application name
Definition: MsppManager.cpp:77
bool processexists(const unsigned int pid)
judges whether specified process is exists or not.
std::string getplugindir()
gets mspp plugin paths
unsigned long m_pid
Definition: MsppManager.h:71
unsigned int getprocessid()
gets procsess ID
ini file management class
Definition: IniFile.h:30
unsigned int getPid()
gets the process ID
void setPath()
sets file path
Definition: MsppManager.cpp:97
std::string m_pidTmpDir
Definition: MsppManager.h:62
std::string getTmpFileName(const char *prefix, const char *suffix, const char *dir=NULL)
gets temporary file name
std::string gethomedir()
gets current user's home directory
virtual ~MsppManager()
destructor
Definition: MsppManager.cpp:73
void setPluginDir()
set mspp plugin dir
std::vector< std::string > m_pluginPaths
Definition: MsppManager.h:74
bool isInitializedMATLAB(void)
gets MATLAB's initialized flag value
std::string getpath(const char *dir, const char *file)
get file path