1/*
2 * This module is used for tests dealing with FSRef "objects"
3 */
4#include "Python.h"
5#include "pyobjc-api.h"
6
7#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
8#pragma clang diagnostic ignored "-Wdeprecated-declarations"
9
10#import <CoreServices/CoreServices.h>
11
12#import <Foundation/Foundation.h>
13
14@interface OC_TestFSRefHelper : NSObject
15{
16}
17
18-(FSRef)fsrefForPath:(NSString*)path;
19-(NSString*)pathForFSRef:(in FSRef *)fsref;
20-(void)getFSRef:(out FSRef*)fsref forPath:(NSString*)path;
21-(NSString*)stringForFSRef:(FSRef)fsref;
22
23@end
24
25@implementation OC_TestFSRefHelper
26
27-(NSString*)stringForFSRef:(FSRef)fsref
28{
29	return [self pathForFSRef:&fsref];
30}
31
32-(FSRef)fsrefForPath:(NSString*)path
33{
34	FSRef fsref;
35	Boolean isDirectory;
36	OSStatus rc;
37
38	rc = FSPathMakeRef((UInt8*)[path UTF8String],
39		&fsref, &isDirectory);
40	if (rc != 0) {
41		[NSException raise:@"failure" format:@"status: %ld", (long)rc];
42	}
43
44	return fsref;
45}
46
47-(NSString*)pathForFSRef:(in FSRef *)fsref
48{
49	UInt8 buffer[256];
50	OSStatus rc;
51
52	rc = FSRefMakePath(fsref, buffer, sizeof(buffer));
53	if (rc != 0) {
54		[NSException raise:@"failure" format:@"status: %ld", (long)rc];
55	}
56
57	return [NSString stringWithUTF8String: (char*)buffer];
58}
59
60-(void)getFSRef:(out FSRef*)fsref forPath:(NSString*)path
61{
62	Boolean isDirectory;
63	OSStatus rc;
64
65	rc = FSPathMakeRef((UInt8*)[path UTF8String],
66		fsref, &isDirectory);
67	if (rc != 0) {
68		[NSException raise:@"failure" format:@"status: %ld", (long)rc];
69	}
70}
71
72@end
73
74static PyMethodDef mod_methods[] = {
75	{ 0, 0, 0, 0 }
76};
77
78#if PY_VERSION_HEX >= 0x03000000
79
80static struct PyModuleDef mod_module = {
81	PyModuleDef_HEAD_INIT,
82	"fsref",
83	NULL,
84	0,
85	mod_methods,
86	NULL,
87	NULL,
88	NULL,
89	NULL
90};
91
92#define INITERROR() return NULL
93#define INITDONE() return m
94
95PyObject* PyInit_fsref(void);
96
97PyObject*
98PyInit_fsref(void)
99
100#else
101
102#define INITERROR() return
103#define INITDONE() return
104
105void initfsref(void);
106
107void
108initfsref(void)
109#endif
110{
111	PyObject* m;
112
113#if PY_VERSION_HEX >= 0x03000000
114	m = PyModule_Create(&mod_module);
115#else
116	m = Py_InitModule4("fsref", mod_methods,
117		NULL, NULL, PYTHON_API_VERSION);
118#endif
119	if (!m) {
120		INITERROR();
121	}
122
123	if (PyObjC_ImportAPI(m) < 0) {
124		INITERROR();
125	}
126
127	if (PyModule_AddObject(m, "OC_TestFSRefHelper",
128			PyObjCClass_New([OC_TestFSRefHelper class])) < 0) {
129		INITERROR();
130	}
131
132	INITDONE();
133}
134