netstat: Initial implementation.
[wine] / dlls / msvcp90 / misc.c
1 /*
2  * Copyright 2010 Piotr Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <limits.h>
23
24 #include "msvcp90.h"
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
30
31 /* ??0_Mutex@std@@QAE@XZ */
32 /* ??0_Mutex@std@@QEAA@XZ */
33 DEFINE_THISCALL_WRAPPER(mutex_ctor, 4)
34 mutex* __thiscall mutex_ctor(mutex *this)
35 {
36     CRITICAL_SECTION *cs = MSVCRT_operator_new(sizeof(*cs));
37     if(!cs) {
38         ERR("Out of memory\n");
39         throw_exception(EXCEPTION_BAD_ALLOC, NULL);
40     }
41
42     InitializeCriticalSection(cs);
43     cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
44     this->mutex = cs;
45     return this;
46 }
47
48 /* ??1_Mutex@std@@QAE@XZ */
49 /* ??1_Mutex@std@@QEAA@XZ */
50 DEFINE_THISCALL_WRAPPER(mutex_dtor, 4)
51 void __thiscall mutex_dtor(mutex *this)
52 {
53     ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
54     DeleteCriticalSection(this->mutex);
55     MSVCRT_operator_delete(this->mutex);
56 }
57
58 /* ?_Lock@_Mutex@std@@QAEXXZ */
59 /* ?_Lock@_Mutex@std@@QEAAXXZ */
60 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
61 void __thiscall mutex_lock(mutex *this)
62 {
63     EnterCriticalSection(this->mutex);
64 }
65
66 /* ?_Unlock@_Mutex@std@@QAEXXZ */
67 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
68 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
69 void __thiscall mutex_unlock(mutex *this)
70 {
71     LeaveCriticalSection(this->mutex);
72 }
73
74 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
75 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
76 void CDECL mutex_mutex_lock(mutex *m)
77 {
78     mutex_lock(m);
79 }
80
81 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
82 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
83 void CDECL mutex_mutex_unlock(mutex *m)
84 {
85     mutex_unlock(m);
86 }
87
88 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
89 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
90 void CDECL mutex_mutex_ctor(mutex *m)
91 {
92     mutex_ctor(m);
93 }
94
95 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
96 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
97 void CDECL mutex_mutex_dtor(mutex *m)
98 {
99     mutex_dtor(m);
100 }
101
102 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
103
104 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
105 void __cdecl _Lockit_init(int locktype) {
106     InitializeCriticalSection(&lockit_cs[locktype]);
107     lockit_cs[locktype].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
108 }
109
110 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
111 void __cdecl _Lockit_free(int locktype)
112 {
113     lockit_cs[locktype].DebugInfo->Spare[0] = 0;
114     DeleteCriticalSection(&lockit_cs[locktype]);
115 }
116
117 void init_lockit(void) {
118     int i;
119
120     for(i=0; i<_MAX_LOCK; i++)
121         _Lockit_init(i);
122 }
123
124 void free_lockit(void) {
125     int i;
126
127     for(i=0; i<_MAX_LOCK; i++)
128         _Lockit_free(i);
129 }
130
131 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
132 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
133 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
134 {
135     lockit->locktype = locktype;
136     EnterCriticalSection(&lockit_cs[locktype]);
137 }
138
139 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
140 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
141 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
142 {
143     _Lockit__Lockit_ctor_locktype(lockit, 0);
144 }
145
146 /* ??0_Lockit@std@@QAE@H@Z */
147 /* ??0_Lockit@std@@QEAA@H@Z */
148 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
149 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
150 {
151     _Lockit__Lockit_ctor_locktype(this, locktype);
152     return this;
153 }
154
155 /* ??0_Lockit@std@@QAE@XZ */
156 /* ??0_Lockit@std@@QEAA@XZ */
157 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
158 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
159 {
160     _Lockit__Lockit_ctor_locktype(this, 0);
161     return this;
162 }
163
164 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
165 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
166 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
167 {
168     LeaveCriticalSection(&lockit_cs[lockit->locktype]);
169 }
170
171 /* ??1_Lockit@std@@QAE@XZ */
172 /* ??1_Lockit@std@@QEAA@XZ */
173 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
174 void __thiscall _Lockit_dtor(_Lockit *this)
175 {
176     _Lockit__Lockit_dtor(this);
177 }
178
179 /* wctype */
180 unsigned short __cdecl wctype(const char *property)
181 {
182     static const struct {
183         const char *name;
184         unsigned short mask;
185     } properties[] = {
186         { "alnum", _DIGIT|_ALPHA },
187         { "alpha", _ALPHA },
188         { "cntrl", _CONTROL },
189         { "digit", _DIGIT },
190         { "graph", _DIGIT|_PUNCT|_ALPHA },
191         { "lower", _LOWER },
192         { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
193         { "punct", _PUNCT },
194         { "space", _SPACE },
195         { "upper", _UPPER },
196         { "xdigit", _HEX }
197     };
198     int i;
199
200     for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++)
201         if(!strcmp(property, properties[i].name))
202             return properties[i].mask;
203
204     return 0;
205 }
206
207 typedef void (__cdecl *MSVCP_new_handler_func)(void);
208 static MSVCP_new_handler_func MSVCP_new_handler;
209 static int __cdecl new_handler_wrapper(MSVCP_size_t unused)
210 {
211     MSVCP_new_handler();
212     return 1;
213 }
214
215 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
216 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
217 {
218     MSVCP_new_handler_func old_handler = MSVCP_new_handler;
219
220     TRACE("%p\n", new_handler);
221
222     MSVCP_new_handler = new_handler;
223     MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
224     return old_handler;
225 }
226
227 /* ?set_new_handler@std@@YAP6AXXZH@Z */
228 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
229 {
230     return set_new_handler(NULL);
231 }