-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.c
82 lines (68 loc) · 1.39 KB
/
types.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* types.c
* By Ron
* Created September 1, 2008
*
* See LICENSE.txt
*
* Functions for converting between datatypes, etc.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "dynamic_callbacks.h"
void *safe_malloc(uint32_t size)
{
void *ret = malloc(size);
if(!ret){
//ret = memalloc(size);
//if(!ret){
MessageBoxA(0, "Malloc failed!", "FFFFUUUUCCCKKK!!!", 0);
//}
}else{
memset(ret, 0, size);
}
return ret;
}
void *safe_realloc(void *ptr, uint32_t size)
{
void *ret = realloc(ptr, size);
if(!ret)
DIE_MEM();
return ret;
}
char *unicode_alloc(const char *string)
{
size_t i;
char *unicode;
size_t unicode_length = (strlen(string) + 1) * 2;
if(unicode_length < strlen(string))
DIE("Overflow.");
unicode = malloc(unicode_length);
if(!unicode)
DIE_MEM();
memset(unicode, 0, unicode_length);
for(i = 0; i < strlen(string); i++)
{
unicode[(i * 2)] = string[i];
}
return unicode;
}
char *unicode_alloc_upper(const char *string)
{
size_t i;
char *unicode;
size_t unicode_length = (strlen(string) + 1) * 2;
if(unicode_length < strlen(string))
DIE("Overflow.");
unicode = malloc(unicode_length);
if(!unicode)
DIE_MEM();
memset(unicode, 0, unicode_length);
for(i = 0; i < strlen(string); i++)
{
unicode[(i * 2)] = toupper(string[i]);
}
return unicode;
}