-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.c
155 lines (120 loc) · 3.15 KB
/
util.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* pyrsistence - A Python extension for External Memory Data Structures (EMDs)
* huku <[email protected]>
*
* util.c - Home of various helper functions that don't fit anywhere else.
*/
#include "util.h"
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef DEBUG
/* Prints a debugging message if `DEBUG' is defined. */
void msgf(const char *fmt, ...)
{
char ftime[BUFSIZ], new_fmt[BUFSIZ];
time_t now;
va_list va;
now = time(NULL);
strftime(ftime, sizeof(ftime), "%Y-%m-%d %H:%M:%S", localtime(&now));
snprintf(new_fmt, sizeof(new_fmt), "(%s) [*] %s\n", ftime, fmt);
va_start(va, fmt);
vfprintf(stderr, new_fmt, va);
va_end(va);
}
#endif
/* A version of `perror()' that works on Microsoft Windows and Unixoids too. */
void serror(const char *prefix)
{
#ifdef _WIN32
char buf[BUFSIZ];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, BUFSIZ, NULL);
fprintf(stderr, "%s: %s", prefix, buf);
#else
perror(prefix);
#endif
}
/* Concatenate `dirname' and `filename' into a buffer of size `PATH_MAX'. */
char *path_combine(const char *dirname, const char *filename)
{
static char combined[PATH_MAX];
char *ret = NULL;
if(snprintf(combined, PATH_MAX, "%s%c%s", dirname, PATH_SEP, filename) >= PATH_MAX)
{
serror("path_combine: snprintf");
goto _err;
}
ret = &combined[0];
_err:
return ret;
}
/* Platform independent function for creating a directory. */
int mk_dir(const char *dirname)
{
int ret = -1;
#ifdef _WIN32
if(CreateDirectoryA(dirname, NULL) == 0)
{
serror("makedir: CreateDirectoryA");
goto _err;
}
#else
if(mkdir(dirname, 0700) != 0)
{
serror("makedir: mkdir");
goto _err;
}
#endif
ret = 0;
_err:
return ret;
}
/* Platform independent function for removing a directory. */
void rm_dir(const char *dirname)
{
#ifdef _WIN32
RemoveDirectoryA(dirname);
#else
rmdir(dirname);
#endif
}
/* Compare Python objects `obj1' and `obj2' and return true if equal. */
int equal_objects(PyObject *obj1, PyObject *obj2)
{
#if PY_MAJOR_VERSION >= 3
return (obj1 != NULL && obj2 != NULL &&
PyObject_RichCompareBool(obj1, obj2, Py_EQ) == 1);
#else
int result;
return (obj1 != NULL && obj2 != NULL &&
PyObject_Cmp(obj1, obj2, &result) != -1 && result == 0);
#endif
}
/* Make sure `obj' has a `pickle()' method and return a reference to the latter. */
int valid_pickler(PyObject *obj, PyObject **picklep)
{
PyObject *pickle;
int ret = -1;
if((pickle = PyObject_GetAttrString(obj, "pickle")) == NULL)
goto _ret;
if(PyCallable_Check(pickle) != 1)
goto _ret;
*picklep = pickle;
ret = 0;
_ret:
return ret;
}
/* Make sure `obj' has a `unpickle()' method and return a reference to the latter. */
int valid_unpickler(PyObject *obj, PyObject **unpicklep)
{
PyObject *unpickle;
int ret = -1;
if((unpickle = PyObject_GetAttrString(obj, "unpickle")) == NULL)
goto _ret;
if(PyCallable_Check(unpickle) != 1)
goto _ret;
*unpicklep = unpickle;
ret = 0;
_ret:
return ret;
}