FrontISTR 5.2.0
Large-scale structural analysis program with finit element method
Loading...
Searching...
No Matches
hecmw_util.c
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright (c) 2019 FrontISTR Commons
3 * This software is released under the MIT License, see LICENSE.txt
4 *****************************************************************************/
5
6#include <stdio.h>
7#include <string.h>
8#include <stdarg.h>
9#include <time.h>
10#include <ctype.h>
11#include <errno.h>
12#include "hecmw_config.h"
13#include "hecmw_util.h"
14
15void HECMW_fprintf(FILE *fp, char *fmt, ...) {
16 va_list ap;
17 va_start(ap, fmt);
18 vfprintf(fp, fmt, ap);
19 va_end(ap);
20}
21
22void HECMW_printerr(char *fmt, ...) {
23 va_list ap;
24 va_start(ap, fmt);
25 vfprintf(stderr, fmt, ap);
26 va_end(ap);
27}
28
29char *HECMW_get_date(void) {
30 int rc;
31 time_t now;
32 static char static_buf[100];
33
34 if (time(&now) == (time_t)-1) return NULL;
35 rc = strftime(static_buf, sizeof(static_buf), "%b %d %H:%M:%S",
36 localtime(&now));
37 return rc ? static_buf : NULL;
38}
39
40/* thread-save version of HECMW_get_date */
41char *HECMW_get_date_r(char *buf, int len) {
42 int rc;
43 time_t now;
44 struct tm result;
45
46 if (time(&now) == (time_t)-1) return NULL;
47
48#if defined(__WIN32__) || defined(__WIN64__)
49 /* localtime_r is not available on Windows */
50 #pragma omp critical
51 {
52 rc = strftime(buf, len, "%b %d %H:%M:%S", localtime(&now));
53 }
54#else
55 if (localtime_r(&now, &result) == NULL) return NULL;
56 rc = strftime(buf, len, "%b %d %H:%M:%S", &result);
57#endif
58
59 return rc ? buf : NULL;
60}
61
62void HECMW_assert_(int cond, char *cond_str, char *file, int line) {
63 if (!cond) {
64 fprintf(stderr, "%s:%d: Assersion `%s' failed.\n", file, line, cond_str);
65#ifdef HECMW_SERIAL
66 abort();
67#else
68 MPI_Abort(MPI_COMM_WORLD, HECMW_EXIT_ERROR);
69#endif
70 }
71}
72
73int HECMW_check_condition_(int cond, char *cond_str, int isabort, char *file,
74 int line) {
75 if (cond) return 0;
76
77 if (isabort) {
78 fprintf(stderr, "%s:%d: Assertion `%s' falied.\n", file, line, cond_str);
79#ifdef HECMW_SERIAL
80 abort();
81#else
82 MPI_Abort(MPI_COMM_WORLD, HECMW_EXIT_ERROR);
83#endif
84 }
85 return 1;
86}
87
89#ifdef HECMW_SERIAL
90 abort();
91#else
92 /* HECMW_comm_is_initialized() ? MPI_Abort(comm, HECMW_EXIT_ERROR) :
93 * abort(); */
95 MPI_Abort(comm, HECMW_EXIT_ERROR);
96 } else {
97 abort();
98 }
99#endif
100}
101
102char *HECMW_toupper(char *s) {
103 char *p;
104
105 if (s == NULL) return NULL;
106
107 for (p = s; *p; p++) {
108 *p = (char)toupper(*p);
109 }
110 return s;
111}
112
113char *HECMW_tolower(char *s) {
114 char *p;
115
116 if (s == NULL) return NULL;
117
118 for (p = s; *p; p++) {
119 *p = (char)tolower(*p);
120 }
121 return s;
122}
123
125
126void HECMW_print_vmsg(int loglv, int msgno, const char *fmt, va_list ap) {
127 char msg[HECMW_MSG_LEN + 1];
128 char vmsg[HECMW_MSG_LEN + 1];
129
130 HECMW_snprintf(msg, sizeof(msg), "%s", HECMW_strmsg(msgno));
131 HECMW_vsnprintf(vmsg, sizeof(vmsg), fmt, ap);
132 if (strlen(vmsg) > 0) {
133 HECMW_snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), " (%s)", vmsg);
134 }
135 HECMW_log(loglv, msg);
136}
137
138void HECMW_print_msg(int loglv, int msgno, const char *fmt, ...) {
139 va_list ap;
140 va_start(ap, fmt);
141 HECMW_print_vmsg(loglv, msgno, fmt, ap);
142 va_end(ap);
143}
144
145int HECMW_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
146#ifdef WIN32_MSVC
147 return _vsnprintf(str, size, format, ap);
148#else
149 return vsnprintf(str, size, format, ap);
150#endif
151}
152
153int HECMW_snprintf(char *str, size_t size, const char *format, ...) {
154 va_list ap;
155 int rtc;
156
157 va_start(ap, format);
158#ifdef WIN32_MSVC
159 rtc = _vsnprintf(str, size, format, ap);
160#else
161 rtc = vsnprintf(str, size, format, ap);
162#endif
163 va_end(ap);
164
165 return rtc;
166}
int HECMW_comm_is_initialized(void)
Definition: hecmw_comm.c:697
#define HECMW_EXIT_ERROR
Definition: hecmw_config.h:62
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
#define HECMW_MSG_LEN
Definition: hecmw_config.h:74
char * HECMW_get_errmsg(void)
Definition: hecmw_error.c:58
#define NULL
int HECMW_log(int loglv, const char *fmt,...)
Definition: hecmw_log.c:260
#define HECMW_LOG_ERROR
Definition: hecmw_log.h:15
char * HECMW_strmsg(int msgno)
Definition: hecmw_msg.c:31
void HECMW_abort(HECMW_Comm comm)
Definition: hecmw_util.c:88
void HECMW_fprintf(FILE *fp, char *fmt,...)
Definition: hecmw_util.c:15
char * HECMW_get_date_r(char *buf, int len)
Definition: hecmw_util.c:41
void HECMW_print_vmsg(int loglv, int msgno, const char *fmt, va_list ap)
Definition: hecmw_util.c:126
char * HECMW_tolower(char *s)
Definition: hecmw_util.c:113
void HECMW_printerr(char *fmt,...)
Definition: hecmw_util.c:22
void HECMW_print_error(void)
Definition: hecmw_util.c:124
int HECMW_check_condition_(int cond, char *cond_str, int isabort, char *file, int line)
Definition: hecmw_util.c:73
char * HECMW_get_date(void)
Definition: hecmw_util.c:29
void HECMW_assert_(int cond, char *cond_str, char *file, int line)
Definition: hecmw_util.c:62
void HECMW_print_msg(int loglv, int msgno, const char *fmt,...)
Definition: hecmw_util.c:138
int HECMW_snprintf(char *str, size_t size, const char *format,...)
Definition: hecmw_util.c:153
char * HECMW_toupper(char *s)
Definition: hecmw_util.c:102
int HECMW_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Definition: hecmw_util.c:145