Flow123d  jenkins-Flow123d-windows32-release-multijob-28
system.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file
26  * @brief ???
27  *
28  */
29 
30 #ifndef SYSTEM_H
31 #define SYSTEM_H
32 
33 
34 #include <mpi.h>
35 #include <iostream>
36 
37 //instead of #include "mpi.h"
38 //typedef int MPI_Comm;
39 
40 
41 // for a linux system we assume glibc library
42 // with support of ISOC99 functions
43 //#define _ISOC99_SOURCE
44 #ifndef _BSD_SOURCE
45  #define _BSD_SOURCE
46 #endif
47 
48 #ifdef WINDOWS
49  #include <direct.h>
50  #define GetCurrentDir _getcwd
51  #define PATH_MAX MAX_PATH
52 #else
53  #include <unistd.h>
54  #define GetCurrentDir getcwd
55  #include <limits.h> //PATH_MAX
56 #endif
57 
58 #define strcmpi strcasecmp
59 #define DIR_DELIMITER '/'
60 
61 #ifdef HAVE_CXX11_FULL
62  #define OPERATOR_NEW_THROW_EXCEPTION
63 #else
64  #define OPERATOR_NEW_THROW_EXCEPTION throw(std::bad_alloc)
65 #endif
66 
67 
68 using namespace std;
69 
70 
71 // **************************************************************
72 /*! @brief Identifiers for various output messages.
73  */
74 typedef enum MessageType {
76 } MessageType;
77 
78 
79 // **************************************************************
80 /*! @brief System structure for various global variables.
81  */
82 typedef struct SystemInfo {
83  int verbosity; // system printf verbosity
84  int pause_after_run; // to keep terminal open on Windows
85  string log_fname; // name of the master log file
86  FILE *log; // log file handle
87 
88  int n_proc; // number of processors
89  int my_proc; // self processor number
90 
92 
93 } SystemInfo;
94 
95 extern SystemInfo sys_info;
96 
97 
99 char * get_log_fname( void );
100 char * get_log_file( void );
101 void resume_log_file( void );
102 
103 
104 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
105 
106 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
107 void * xmalloc(size_t size);
108 void * xrealloc( void * ptr, size_t size );
109 
110 // TODO: implement as a templated function
111 #ifndef xfree
112  #define xfree(p) \
113  do { if (p) { free(p); (p)=NULL; } \
114  else {/*DBGMSG("Freeing NULL pointer?\n")*/; \
115  } \
116  } while (0) /// test & free memory
117 #endif
118 
119 /**
120  * @brief Replacement of new/delete operator in the spirit of xmalloc.
121  *
122  * Up to my knowledge overloading of original new/delete is the only clean.
123  * Possibly disadvantage is that all 'new' calls in system and other templates
124  * become also overloaded.
125  *
126  */
127 // @{
128 
129 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
130 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
131 void operator delete( void *p) throw();
132 void operator delete[]( void *p) throw();
133 // @}
134 
135 int xsystem(const char*);
136 
137 //! @brief Operations on files and directories
138 /// @{
139 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
140 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
141 int xremove( const char *s ); ///< Remove file or directory (function)
142 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
143 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
144 //#define tmpfile xtmpfile NOT_USED ///< Open a temporary file (function)
145 //! @}
146 
147 // string operations
148 char * xstrcpy(const char*);
149 char * xstrtok(char *s, int position = -1);
150 char * xstrtok(char*,const char* delim, int position = -1);
151 int xchomp( char * s );
152 #endif
153 //-----------------------------------------------------------------------------
154 // vim: set cindent:
int my_proc
Definition: system.hh:89
char * xstrtok(char *s, int position=-1)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
Definition: system.cc:340
Definition: system.hh:75
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
#define OPERATOR_NEW_THROW_EXCEPTION
Definition: system.hh:64
FILE * log
Definition: system.hh:86
int xremove(const char *s)
Remove file or directory (function)
Definition: system.cc:461
int xrename(const char *oldname, const char *newname)
Rename file (function)
Definition: xio.cc:515
int verbosity
Definition: system.hh:83
Definition: system.hh:75
char * get_log_file(void)
void resume_log_file(void)
System structure for various global variables.
Definition: system.hh:82
string log_fname
Definition: system.hh:85
Definition: system.hh:75
char * get_log_fname(void)
void * xrealloc(void *ptr, size_t size)
Reallocation of memory block with checking.
Definition: system.cc:253
void system_set_from_options()
Definition: system.cc:58
void * xmalloc(size_t size)
Memory allocation with checking.
Definition: system.cc:218
Definition: system.hh:75
SystemInfo sys_info
Definition: system.cc:55
Definition: system.hh:75
MessageType
Identifiers for various output messages.
Definition: system.hh:74
char * xstrcpy(const char *)
MAKE BRAND NEW COPY OF STRING.
Definition: system.cc:322
int xsystem(const char *)
SYSTEM with err handling.
Definition: system.cc:310
Definition: system.hh:75
int _xprintf(const char *const xprintf_file, const char *const xprintf_func, const int xprintf_line, MessageType type, const char *const fmt,...)
Multi-purpose printing routine: messages, warnings, errors.
Definition: system.cc:106
int xchdir(const char *s)
Change directory (GLIBC function, original in )
Definition: system.cc:448
int n_proc
Definition: system.hh:88
Definition: system.hh:75
char * xgetcwd(void)
Get current working directory (GLIBC function, original in )
Definition: system.cc:480
int xchomp(char *s)
Delete trailing whitespace characters (space,tab,CR,NL).
Definition: system.cc:392
int xmkdir(const char *s)
Operations on files and directories.
Definition: system.cc:419
MPI_Comm comm
Definition: system.hh:91
int pause_after_run
Definition: system.hh:84