OpenCPN Partial API docs
Loading...
Searching...
No Matches
cutil.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <stdio.h>
25#include <stdarg.h>
26#include <string.h>
27#include <math.h>
28#include <ctype.h>
29
30#include "model/cutil.h"
31
32double round_msvc(double x) { return (floor(x + 0.5)); }
33
34#ifdef __MSVC__
35#include <winsock2.h>
36#include <windows.h>
37#include <float.h> // for _clear87()
38
39long __stdcall MyUnhandledExceptionFilter(
40 struct _EXCEPTION_POINTERS *ExceptionInfo) {
41 // return EXCEPTION_EXECUTE_HANDLER ; // terminates the app
42
43 switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
44 case EXCEPTION_FLT_DENORMAL_OPERAND:
45 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
46 case EXCEPTION_FLT_INEXACT_RESULT:
47 case EXCEPTION_FLT_INVALID_OPERATION:
48 case EXCEPTION_FLT_OVERFLOW:
49 case EXCEPTION_FLT_STACK_CHECK:
50 case EXCEPTION_FLT_UNDERFLOW:
51 _clear87();
52 return EXCEPTION_CONTINUE_EXECUTION; // retry
53
54 default:
55 return EXCEPTION_CONTINUE_SEARCH; // standard fatal dialog box
56 }
57}
58#endif
59
60/* Replacement for __MSVC__ in absence of snprintf or _snprintf */
61#ifdef __MSVC__
62int mysnprintf(char *buffer, int count, const char *format, ...) {
63 int ret;
64
65 va_list arg;
66 va_start(arg, format);
67 ret = _vsnprintf(buffer, count, format, arg);
68
69 va_end(arg);
70 return ret;
71}
72#endif
73
74int NextPow2(int size) {
75 int n = size - 1; // compute dimensions needed as next larger power of 2
76 int shift = 1;
77 while ((n + 1) & n) {
78 n |= n >> shift;
79 shift <<= 1;
80 }
81
82 return n + 1;
83}
84#ifdef __WXMSW__
85extern "C" int clock_gettime_monotonic(struct timespec *tv) {
86 static LARGE_INTEGER ticksPerSec;
87 LARGE_INTEGER ticks;
88
89 if (!ticksPerSec.QuadPart) {
90 QueryPerformanceFrequency(&ticksPerSec);
91 if (!ticksPerSec.QuadPart) {
92 errno = ENOTSUP;
93 return -1;
94 }
95 }
96
97 QueryPerformanceCounter(&ticks);
98
99 tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
100 tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * 1e9) /
101 ticksPerSec.QuadPart);
102
103 return 0;
104}
105#endif
Extern C linked utilities.