-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
107 lines (80 loc) · 1.67 KB
/
common.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "common.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stdint.h>
#include <errno.h>
#if defined(__linux__)
/* Linux */
#include <unistd.h> /* access */
#elif defined (_MSC_VER)
#include <io.h>
#endif
long fileSize (const char *filename)
{
FILE *fp = fopen (filename, "rb");
if (!fp) return -1;
fseek (fp, 0, SEEK_END);
long ret = ftell (fp);
fclose (fp);
return ret;
}
bool is_big_endian(void)
{
union {
uint32_t i;
char c[4];
} bint = {0x01020304};
return (bint.c[0] == 1);
}
void cleanup (const char *outfile, int flags)
{
if (0 == (flags & KEEP_ON_ERROR))
{
if (file_exists (outfile))
remove (outfile);
}
}
bool file_exists (const char *filename)
{
#if defined(__linux__)
return (access(filename, F_OK) == 0);
#elif defined(_WIN32)
return (_access(filename, 0) == 0);
#endif
}
char *str_dup (const char *s) /* strdup replacement. */
{
size_t size = strlen (s) + 1;
char *ret = (char *)malloc (size);
if (!ret) return NULL;
strcpy (ret, s);
return ret;
}
#ifndef _MSC_VER // MS compatibility adapters for "secure" functions.
int tmpnam_s(char* temp_name, size_t sizeInChars)
{
char buffer [PATH_MAX] ;
if (NULL == tmpnam(buffer))
return -1;
if (strlen (buffer) >= sizeInChars)
{
return ERANGE;
}
if (temp_name == NULL)
{
return EINVAL;
}
strcpy (temp_name, buffer);
return 0;
}
int strcpy_s(char *dest, size_t dest_len, const char *src)
{
if (!src || !dest)
return EINVAL;
if (strlen (src) >= dest_len)
return ERANGE;
strcpy (dest, src);
return 0;
}
#endif // _MSC_VER