Flow123d
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 
35 #include <mpi.h>
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 /* F_STACK_SHOW( stdout ); \ */
119 
120 /**
121  * @brief Replacement of new/delete operator in the spirit of xmalloc.
122  *
123  * Up to my knowledge overloading of original new/delete is the only clean.
124  * Possibly disadvantage is that all 'new' calls in system and other templates
125  * become also overloaded.
126  *
127  */
128 // @{
129 
130 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
131 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
132 void operator delete( void *p) throw();
133 void operator delete[]( void *p) throw();
134 // @}
135 
136 int xsystem(const char*);
137 
138 //! @brief Operations on files and directories
139 /// @{
140 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
141 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
142 int xremove( const char *s ); ///< Remove file or directory (function)
143 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
144 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
145 //#define tmpfile xtmpfile NOT_USED ///< Open a temporary file (function)
146 //! @}
147 
148 //! @brief auxiliary
149 /// @{
150 bool skip_to( istream &in, const string &pattern );
151 //! @}
152 
153 // string operations
154 char * xstrcpy(const char*);
155 char * xstrtok(char *s, int position = -1);
156 char * xstrtok(char*,const char* delim, int position = -1);
157 int xchomp( char * s );
158 #endif
159 //-----------------------------------------------------------------------------
160 // vim: set cindent: