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