Flow123d  release_1.8.2-1603-g0109a2b
system.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file system.hh
15  * @brief
16  */
17 
18 #ifndef SYSTEM_H
19 #define SYSTEM_H
20 
21 
22 #include <mpi.h>
23 #include <iostream>
24 
25 #include "system/global_defs.h"
26 #include "system/exc_common.hh"
27 
28 
29 #ifndef _BSD_SOURCE
30  #define _BSD_SOURCE
31 #endif
32 
33 #ifdef WINDOWS
34  #include <direct.h>
35  #define GetCurrentDir _getcwd
36  #define PATH_MAX MAX_PATH
37 #else
38  #include <unistd.h>
39  #define GetCurrentDir getcwd
40  #include <limits.h> //PATH_MAX
41 #endif
42 
43 #define strcmpi strcasecmp
44 #define DIR_DELIMITER '/'
45 
46 
47 // Assuming all compilers supports CXX11 features
48 #define OPERATOR_NEW_THROW_EXCEPTION
49 
50 
51 
52 using namespace std;
53 
54 
55 // **************************************************************
56 /*! @brief Identifiers for various output messages.
57  */
58 typedef enum MessageType {
60 } MessageType;
61 
62 
63 // **************************************************************
64 /*! @brief System structure for various global variables.
65  */
66 typedef struct SystemInfo {
67  int verbosity; // system printf verbosity
68  int pause_after_run; // to keep terminal open on Windows
69  string log_fname; // name of the master log file
70  FILE *log; // log file handle
71 
72  int n_proc; // number of processors
73  int my_proc; // self processor number
74 
76 
77 } SystemInfo;
78 
79 extern SystemInfo sys_info;
80 
81 
82 char * get_log_fname( void );
83 char * get_log_file( void );
84 void resume_log_file( void );
85 
86 
87 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
88 
89 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
90 void * xmalloc(size_t size);
91 void * xrealloc( void * ptr, size_t size );
92 
93 // TODO: implement as a templated function
94 #ifndef xfree
95  #define xfree(p) \
96  do { if (p) { free(p); (p)=NULL; } \
97  } while (0) /// test & free memory
98 #endif
99 
100 /**
101  * @brief Replacement of new/delete operator in the spirit of xmalloc.
102  *
103  * Up to my knowledge overloading of original new/delete is the only clean.
104  * Possibly disadvantage is that all 'new' calls in system and other templates
105  * become also overloaded.
106  *
107  */
108 // @{
109 
110 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
111 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
112 void operator delete( void *p) throw();
113 void operator delete[]( void *p) throw();
114 // @}
115 
116 int xsystem(const char*);
117 
118 //! @brief Operations on files and directories
119 /// @{
120 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
121 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
122 int xremove( const char *s ); ///< Remove file or directory (function)
123 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
124 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
125 //! @}
126 
127 // string operations
128 char * xstrcpy(const char*);
129 char * xstrtok(char *s, int position = -1);
130 char * xstrtok(char*,const char* delim, int position = -1);
131 int xchomp( char * s );
132 
133 /**
134  * Wrapper to check return codes of C functions. In particular PETSC calls.
135  */
136 inline void chkerr(unsigned int ierr) {
137  do {
138  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
139  } while (0);
140 }
141 
142 /**
143  * Wrapper to check return codes of C functions. In particular PETSC calls.
144  * This version do the check just as an debugging assert. So the code is empty
145  * in release version.
146  */
147 inline void chkerr_assert(unsigned int ierr) {
148 #ifdef FLOW123D_DEBUG_ASSERTS
149  chkerr(ierr);
150 #endif
151 }
152 
153 #endif
154 //-----------------------------------------------------------------------------
155 // vim: set cindent:
int my_proc
Definition: system.hh:73
char * xstrtok(char *s, int position=-1)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
Definition: system.cc:298
Definition: system.hh:59
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
#define OPERATOR_NEW_THROW_EXCEPTION
Definition: system.hh:48
FILE * log
Definition: system.hh:70
int xremove(const char *s)
Remove file or directory (function)
Definition: system.cc:419
int xrename(const char *oldname, const char *newname)
Changes the name of the file or directory specified by oldname to newname.
Definition: xio.cc:498
int verbosity
Definition: system.hh:67
void chkerr(unsigned int ierr)
Definition: system.hh:136
Definition: system.hh:59
char * get_log_file(void)
void resume_log_file(void)
System structure for various global variables.
Definition: system.hh:66
string log_fname
Definition: system.hh:69
Global macros to enhance readability and debugging, general constants.
Definition: system.hh:59
char * get_log_fname(void)
void * xrealloc(void *ptr, size_t size)
Reallocation of memory block with checking.
Definition: system.cc:232
void chkerr_assert(unsigned int ierr)
Definition: system.hh:147
void * xmalloc(size_t size)
Memory allocation with checking.
Definition: system.cc:198
Definition: system.hh:59
SystemInfo sys_info
Definition: system.cc:42
Definition: system.hh:59
MessageType
Identifiers for various output messages.
Definition: system.hh:58
char * xstrcpy(const char *)
MAKE BRAND NEW COPY OF STRING.
Definition: system.cc:280
int xsystem(const char *)
SYSTEM with err handling.
Definition: system.cc:268
Definition: system.hh:59
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:86
int xchdir(const char *s)
Change directory (GLIBC function, original in <unistd.h>)
Definition: system.cc:406
int n_proc
Definition: system.hh:72
Definition: system.hh:59
char * xgetcwd(void)
Get current working directory (GLIBC function, original in <unistd.h>)
Definition: system.cc:438
int xchomp(char *s)
Delete trailing whitespace characters (space,tab,CR,NL).
Definition: system.cc:350
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:44
int xmkdir(const char *s)
Operations on files and directories.
Definition: system.cc:377
MPI_Comm comm
Definition: system.hh:75
int pause_after_run
Definition: system.hh:68