Project

General

Profile

Statistics
| Branch: | Revision:

root / include / platdep / platmisc.h @ fbe00e73

History | View | Annotate | Download (4.09 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  PLATMISC.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//==========================================================================
8
9
/*--------------------------------------------------------------*
10
  Copyright (C) 1992-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#ifndef __PLATMISC_H
18
#define __PLATMISC_H
19
20
//
21
// Common Unix functionality
22
//
23
#ifdef _WIN32
24
25
#include <process.h>
26
#include <io.h>
27
#include <direct.h>
28
#include <stdlib.h> // _MAX_PATH
29
30
#define getpid _getpid
31
#define getcwd _getcwd
32
#define chdir  _chdir
33
#define mkdir(x,y) _mkdir(x)
34
#define gcvt _gcvt
35
36
// unistd.h contains usleep only on mingw 4.4 or later (minor version 16)
37
#if __MINGW32_MINOR_VERSION >= 16
38
#include <unistd.h>  // getpid(), getcwd(), etc
39
#else
40
#define usleep(x) _sleep((x)/1000)
41
#endif
42
43
#else
44
45
#include <sys/types.h>
46
#include <sys/stat.h>
47
#include <unistd.h>  // getpid(), getcwd(), etc
48
49
#if HAVE_DLOPEN
50
#include <dlfcn.h>
51
#endif
52
53
#endif
54
55
#include <exception>
56
#include <stdexcept>
57
58
#ifdef _MSC_VER
59
#define snprintf _snprintf
60
#define strcasecmp _stricmp
61
#define strncasecmp _strnicmp
62
#define unlink _unlink
63
#endif
64
65
//
66
// Include windows header -- needed for getting Windows error strings,
67
// and also by ccoroutine.
68
//
69
#ifdef _WIN32
70
#ifndef _WIN32_WINNT
71
#define _WIN32_WINNT 0x0400  // needed for the Fiber API
72
#endif
73
#define WIN32_LEAN_AND_MEAN
74
#include <windows.h>
75
#undef min
76
#undef max
77
#include <string>
78
79
// errorCode usually comes from GetLastError()
80
inline std::string opp_getWindowsError(DWORD errorCode)
81
{
82
     // Some nice microsoftism to produce an error msg...
83
     LPVOID lpMsgBuf;
84
     FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
85
                    FORMAT_MESSAGE_FROM_SYSTEM |
86
                    FORMAT_MESSAGE_IGNORE_INSERTS,
87
                    NULL,
88
                    errorCode,
89
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
90
                    (LPTSTR) &lpMsgBuf,
91
                    0,
92
                    NULL );
93
     int len = strlen((const char *)lpMsgBuf);
94
     std::string ret((const char *)lpMsgBuf, 0, len>3 ? len-3 : len); // chop ".\r\n"
95
     LocalFree( lpMsgBuf );
96
     return ret;
97
}
98
#endif
99
100
#ifdef _MSC_VER
101
#define va_copy(dst, src) ((void)((dst) = (src)))
102
#endif
103
104
105
//
106
// gcvt -- FreeBSD does not have it
107
//
108
#if defined __FreeBSD__
109
inline char *gcvt(double value, int ndigit, char *buf)
110
{
111
    sprintf(buf, "%.*g", ndigit, value);
112
    return buf;
113
}
114
#endif
115
116
117
//
118
// 64-bit file offsets
119
//
120
#include "intxtypes.h"
121
122
typedef int64 file_offset_t;  // off_t on Linux
123
124
/*
125
#if defined _MSC_VER
126
# define opp_ftell _ftelli64
127
# define opp_fseek _fseeki64
128
# if _MSC_VER < 1400
129
   // Kludge: in Visual C++ 7.1, 64-bit fseek/ftell is not part of the public
130
   // API, but the functions are there in the CRT. Declare them here.
131
   int __cdecl _fseeki64 (FILE *str, __int64 offset, int whence);
132
   __int64 __cdecl _ftelli64 (FILE *stream);
133
# endif
134
#else
135
# define opp_ftell ftello64
136
# define opp_fseek fseeko64
137
#endif
138
*/
139
140
//FIXME replace this with the above code!!! once it compiles with 7.1
141
#if defined _MSC_VER && (_MSC_VER >= 1400)
142
  #define opp_ftell _ftelli64
143
  #define opp_fseek _fseeki64
144
  #define opp_stat_t __stat64 // name of the struct
145
  #define opp_stat _stat64    // name of the function
146
  #define opp_fstat _fstati64
147
#elif defined _MSC_VER || __MINGW32__ // FIXME: no 64 bit version under mingw?
148
// for Visual C++ 7.1, fall back to 32-bit functions
149
  #define opp_ftell ftell
150
  #define opp_fseek fseek
151
  #define opp_stat_t stat
152
  #define opp_stat stat
153
  #define opp_fstat fstat
154
#elif defined __APPLE__ || defined __FreeBSD__
155
  #define opp_ftell ftello
156
  #define opp_fseek fseeko
157
  #define opp_stat_t stat
158
  #define opp_stat stat
159
  #define opp_fstat fstat
160
#else
161
  #define opp_ftell ftello64
162
  #define opp_fseek fseeko64
163
  #define opp_stat_t stat64
164
  #define opp_stat stat64
165
  #define opp_fstat fstat64
166
#endif
167
168
169
#endif