Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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 #include "system/global_defs.h"
38 #include "system/exc_common.hh"
39 
40 
41 #ifndef _BSD_SOURCE
42  #define _BSD_SOURCE
43 #endif
44 
45 #ifdef WINDOWS
46  #include <direct.h>
47  #define GetCurrentDir _getcwd
48  #define PATH_MAX MAX_PATH
49 #else
50  #include <unistd.h>
51  #define GetCurrentDir getcwd
52  #include <limits.h> //PATH_MAX
53 #endif
54 
55 #define strcmpi strcasecmp
56 #define DIR_DELIMITER '/'
57 
58 #ifdef HAVE_CXX11_FULL
59  #define OPERATOR_NEW_THROW_EXCEPTION
60 #else
61  #define OPERATOR_NEW_THROW_EXCEPTION throw(std::bad_alloc)
62 #endif
63 
64 
65 using namespace std;
66 
67 
68 // **************************************************************
69 /*! @brief Identifiers for various output messages.
70  */
71 typedef enum MessageType {
73 } MessageType;
74 
75 
76 // **************************************************************
77 /*! @brief System structure for various global variables.
78  */
79 typedef struct SystemInfo {
80  int verbosity; // system printf verbosity
81  int pause_after_run; // to keep terminal open on Windows
82  string log_fname; // name of the master log file
83  FILE *log; // log file handle
84 
85  int n_proc; // number of processors
86  int my_proc; // self processor number
87 
89 
90 } SystemInfo;
91 
92 extern SystemInfo sys_info;
93 
94 
95 char * get_log_fname( void );
96 char * get_log_file( void );
97 void resume_log_file( void );
98 
99 
100 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
101 
102 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
103 void * xmalloc(size_t size);
104 void * xrealloc( void * ptr, size_t size );
105 
106 // TODO: implement as a templated function
107 #ifndef xfree
108  #define xfree(p) \
109  do { if (p) { free(p); (p)=NULL; } \
110  } while (0) /// test & free memory
111 #endif
112 
113 /**
114  * @brief Replacement of new/delete operator in the spirit of xmalloc.
115  *
116  * Up to my knowledge overloading of original new/delete is the only clean.
117  * Possibly disadvantage is that all 'new' calls in system and other templates
118  * become also overloaded.
119  *
120  */
121 // @{
122 
123 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
124 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
125 void operator delete( void *p) throw();
126 void operator delete[]( void *p) throw();
127 // @}
128 
129 int xsystem(const char*);
130 
131 //! @brief Operations on files and directories
132 /// @{
133 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
134 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
135 int xremove( const char *s ); ///< Remove file or directory (function)
136 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
137 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
138 //! @}
139 
140 // string operations
141 char * xstrcpy(const char*);
142 char * xstrtok(char *s, int position = -1);
143 char * xstrtok(char*,const char* delim, int position = -1);
144 int xchomp( char * s );
145 
146 
147 inline void chkerr(unsigned int ierr) {
148  do {
149  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
150  } while (0);
151 }
152 
153 inline void chkerr_assert(unsigned int ierr) {
154  if (debug_asserts_view) {
155  do {
156  if (ierr != 0) THROW( ExcChkErrAssert() << EI_ErrCode(ierr));
157  } while (0);
158  }
159 }
160 
161 #endif
162 //-----------------------------------------------------------------------------
163 // vim: set cindent:
int my_proc
Definition: system.hh:86
char * xstrtok(char *s, int position=-1)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
Definition: system.cc:330
Definition: system.hh:72
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
#define OPERATOR_NEW_THROW_EXCEPTION
Definition: system.hh:61
FILE * log
Definition: system.hh:83
int xremove(const char *s)
Remove file or directory (function)
Definition: system.cc:451
static const int debug_asserts_view
Definition: global_defs.h:209
int xrename(const char *oldname, const char *newname)
Changes the name of the file or directory specified by oldname to newname.
Definition: xio.cc:515
int verbosity
Definition: system.hh:80
void chkerr(unsigned int ierr)
Definition: system.hh:147
Definition: system.hh:72
char * get_log_file(void)
void resume_log_file(void)
System structure for various global variables.
Definition: system.hh:79
string log_fname
Definition: system.hh:82
Global macros to enhance readability and debugging, general constants.
Definition: system.hh:72
char * get_log_fname(void)
void * xrealloc(void *ptr, size_t size)
Reallocation of memory block with checking.
Definition: system.cc:243
void chkerr_assert(unsigned int ierr)
Definition: system.hh:153
void * xmalloc(size_t size)
Memory allocation with checking.
Definition: system.cc:209
Definition: system.hh:72
SystemInfo sys_info
Definition: system.cc:53
Definition: system.hh:72
MessageType
Identifiers for various output messages.
Definition: system.hh:71
char * xstrcpy(const char *)
MAKE BRAND NEW COPY OF STRING.
Definition: system.cc:312
int xsystem(const char *)
SYSTEM with err handling.
Definition: system.cc:300
Definition: system.hh:72
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:97
int xchdir(const char *s)
Change directory (GLIBC function, original in )
Definition: system.cc:438
int n_proc
Definition: system.hh:85
Definition: system.hh:72
char * xgetcwd(void)
Get current working directory (GLIBC function, original in )
Definition: system.cc:470
int xchomp(char *s)
Delete trailing whitespace characters (space,tab,CR,NL).
Definition: system.cc:382
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
int xmkdir(const char *s)
Operations on files and directories.
Definition: system.cc:409
MPI_Comm comm
Definition: system.hh:88
int pause_after_run
Definition: system.hh:81