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
23
24#include <stdio.h>
25#include <stdarg.h>
26#include <string.h>
27#include <math.h>
28#include <ctype.h>
29#include <time.h>
30
31#include "model/cutil.h"
32
33double round_msvc(double x) { return (floor(x + 0.5)); }
34
35#ifdef _MSC_VER
36#include <winsock2.h>
37#include <windows.h>
38#include <float.h> // for _clear87()
39
40long __stdcall MyUnhandledExceptionFilter(
41 struct _EXCEPTION_POINTERS *ExceptionInfo) {
42 // return EXCEPTION_EXECUTE_HANDLER ; // terminates the app
43
44 switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
45 case EXCEPTION_FLT_DENORMAL_OPERAND:
46 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
47 case EXCEPTION_FLT_INEXACT_RESULT:
48 case EXCEPTION_FLT_INVALID_OPERATION:
49 case EXCEPTION_FLT_OVERFLOW:
50 case EXCEPTION_FLT_STACK_CHECK:
51 case EXCEPTION_FLT_UNDERFLOW:
52 _clear87();
53 return EXCEPTION_CONTINUE_EXECUTION; // retry
54
55 default:
56 return EXCEPTION_CONTINUE_SEARCH; // standard fatal dialog box
57 }
58}
59#endif
60
61/* Replacement for __MSVC__ in absence of snprintf or _snprintf */
62#ifdef __MSVC__
63int mysnprintf(char *buffer, int count, const char *format, ...) {
64 int ret;
65
66 va_list arg;
67 va_start(arg, format);
68 ret = _vsnprintf(buffer, count, format, arg);
69
70 va_end(arg);
71 return ret;
72}
73#endif
74
75int NextPow2(int size) {
76 int n = size - 1; // compute dimensions needed as next larger power of 2
77 int shift = 1;
78 while ((n + 1) & n) {
79 n |= n >> shift;
80 shift <<= 1;
81 }
82
83 return n + 1;
84}
85#ifdef __WXMSW__
86extern "C" int clock_gettime_monotonic(struct timespec *tv) {
87 static LARGE_INTEGER ticksPerSec;
88 LARGE_INTEGER ticks;
89
90 if (!ticksPerSec.QuadPart) {
91 QueryPerformanceFrequency(&ticksPerSec);
92 if (!ticksPerSec.QuadPart) {
93 errno = ENOTSUP;
94 return -1;
95 }
96 }
97
98 QueryPerformanceCounter(&ticks);
99
100 tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
101 tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * 1e9) /
102 ticksPerSec.QuadPart);
103
104 return 0;
105}
106#endif
Extern C linked utilities.