OpenCPN Partial API docs
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 
35 
36 double round_msvc(double x) { return (floor(x + 0.5)); }
37 
38 #ifdef __MSVC__
39 #include <windows.h>
40 #include <float.h> // for _clear87()
41 
42 long __stdcall MyUnhandledExceptionFilter(
43  struct _EXCEPTION_POINTERS *ExceptionInfo) {
44  // return EXCEPTION_EXECUTE_HANDLER ; // terminates the app
45 
46  switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
47  case EXCEPTION_FLT_DENORMAL_OPERAND:
48  case EXCEPTION_FLT_DIVIDE_BY_ZERO:
49  case EXCEPTION_FLT_INEXACT_RESULT:
50  case EXCEPTION_FLT_INVALID_OPERATION:
51  case EXCEPTION_FLT_OVERFLOW:
52  case EXCEPTION_FLT_STACK_CHECK:
53  case EXCEPTION_FLT_UNDERFLOW:
54  _clear87();
55  return EXCEPTION_CONTINUE_EXECUTION; // retry
56 
57  default:
58  return EXCEPTION_CONTINUE_SEARCH; // standard fatal dialog box
59  }
60 }
61 #endif
62 
63 /* Replacement for __MSVC__ in absence of snprintf or _snprintf */
64 #ifdef __MSVC__
65 int mysnprintf(char *buffer, int count, const char *format, ...) {
66  int ret;
67 
68  va_list arg;
69  va_start(arg, format);
70  ret = _vsnprintf(buffer, count, format, arg);
71 
72  va_end(arg);
73  return ret;
74 }
75 #endif
76 
77 int NextPow2(int size) {
78  int n = size - 1; // compute dimensions needed as next larger power of 2
79  int shift = 1;
80  while ((n + 1) & n) {
81  n |= n >> shift;
82  shift <<= 1;
83  }
84 
85  return n + 1;
86 }