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