/Mac/Modules/file/_Filemodule.c

http://unladen-swallow.googlecode.com/ · C · 3410 lines · 3040 code · 322 blank · 48 comment · 381 complexity · cf7ba1a10e4ee6fdbeaf4f9220fe8bfd MD5 · raw file

Large files are truncated click here to view the full file

  1. /* ========================== Module _File ========================== */
  2. #include "Python.h"
  3. #include "pymactoolbox.h"
  4. #ifndef HAVE_OSX105_SDK
  5. typedef SInt16 FSIORefNum;
  6. #endif
  7. /* Macro to test whether a weak-loaded CFM function exists */
  8. #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
  9. PyErr_SetString(PyExc_NotImplementedError, \
  10. "Not available in this shared library/OS version"); \
  11. return NULL; \
  12. }} while(0)
  13. #include <Carbon/Carbon.h>
  14. #ifdef USE_TOOLBOX_OBJECT_GLUE
  15. #ifndef __LP64__
  16. extern int _PyMac_GetFSSpec(PyObject *v, FSSpec *spec);
  17. extern PyObject *_PyMac_BuildFSSpec(FSSpec *spec);
  18. #define PyMac_BuildFSSpec _PyMac_BuildFSSpec
  19. #endif /* __LP64__*/
  20. extern int _PyMac_GetFSRef(PyObject *v, FSRef *fsr);
  21. extern PyObject *_PyMac_BuildFSRef(FSRef *spec);
  22. #define PyMac_BuildFSRef _PyMac_BuildFSRef
  23. #define PyMac_GetFSSpec _PyMac_GetFSSpec
  24. #define PyMac_GetFSRef _PyMac_GetFSRef
  25. #else /* !USE_TOOLBOX_OBJECT_GLUE */
  26. #ifndef __LP64__
  27. extern int PyMac_GetFSSpec(PyObject *v, FSSpec *spec);
  28. extern PyObject *PyMac_BuildFSSpec(FSSpec *spec);
  29. #endif /* !__LP64__*/
  30. extern int PyMac_GetFSRef(PyObject *v, FSRef *fsr);
  31. extern PyObject *PyMac_BuildFSRef(FSRef *spec);
  32. #endif /* !USE_TOOLBOX_OBJECT_GLUE */
  33. /* Forward declarations */
  34. static PyObject *FSRef_New(FSRef *itself);
  35. #ifndef __LP64__
  36. static PyObject *FInfo_New(FInfo *itself);
  37. static PyObject *FSSpec_New(FSSpec *itself);
  38. #define FSSpec_Convert PyMac_GetFSSpec
  39. #endif /* !__LP64__ */
  40. static PyObject *Alias_New(AliasHandle itself);
  41. #ifndef __LP64__
  42. static int FInfo_Convert(PyObject *v, FInfo *p_itself);
  43. #endif /* !__LP64__ */
  44. #define FSRef_Convert PyMac_GetFSRef
  45. static int Alias_Convert(PyObject *v, AliasHandle *p_itself);
  46. /*
  47. ** UTCDateTime records
  48. */
  49. static int
  50. UTCDateTime_Convert(PyObject *v, UTCDateTime *ptr)
  51. {
  52. return PyArg_Parse(v, "(HlH)", &ptr->highSeconds, &ptr->lowSeconds, &ptr->fraction);
  53. }
  54. static PyObject *
  55. UTCDateTime_New(UTCDateTime *ptr)
  56. {
  57. return Py_BuildValue("(HlH)", ptr->highSeconds, ptr->lowSeconds, ptr->fraction);
  58. }
  59. /*
  60. ** Optional fsspec and fsref pointers. None will pass NULL
  61. */
  62. #ifndef __LP64__
  63. static int
  64. myPyMac_GetOptFSSpecPtr(PyObject *v, FSSpec **spec)
  65. {
  66. if (v == Py_None) {
  67. *spec = NULL;
  68. return 1;
  69. }
  70. return PyMac_GetFSSpec(v, *spec);
  71. }
  72. #endif /* !__LP64__ */
  73. static int
  74. myPyMac_GetOptFSRefPtr(PyObject *v, FSRef **ref)
  75. {
  76. if (v == Py_None) {
  77. *ref = NULL;
  78. return 1;
  79. }
  80. return PyMac_GetFSRef(v, *ref);
  81. }
  82. /*
  83. ** Parse/generate objsect
  84. */
  85. static PyObject *
  86. PyMac_BuildHFSUniStr255(HFSUniStr255 *itself)
  87. {
  88. return Py_BuildValue("u#", itself->unicode, itself->length);
  89. }
  90. #ifndef __LP64__
  91. static OSErr
  92. _PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
  93. {
  94. FSRef fsr;
  95. OSErr err;
  96. *path = '\0';
  97. err = FSpMakeFSRef(fss, &fsr);
  98. if (err == fnfErr) {
  99. /* FSSpecs can point to non-existing files, fsrefs can't. */
  100. FSSpec fss2;
  101. int tocopy;
  102. err = FSMakeFSSpec(fss->vRefNum, fss->parID,
  103. (unsigned char*)"", &fss2);
  104. if (err)
  105. return err;
  106. err = FSpMakeFSRef(&fss2, &fsr);
  107. if (err)
  108. return err;
  109. err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len-1);
  110. if (err)
  111. return err;
  112. /* This part is not 100% safe: we append the filename part, but
  113. ** I'm not sure that we don't run afoul of the various 8bit
  114. ** encodings here. Will have to look this up at some point...
  115. */
  116. strcat(path, "/");
  117. tocopy = fss->name[0];
  118. if ((strlen(path) + tocopy) >= len)
  119. tocopy = len - strlen(path) - 1;
  120. if (tocopy > 0)
  121. strncat(path, (char*)fss->name+1, tocopy);
  122. }
  123. else {
  124. if (err)
  125. return err;
  126. err = (OSErr)FSRefMakePath(&fsr, (unsigned char*)path, len);
  127. if (err)
  128. return err;
  129. }
  130. return 0;
  131. }
  132. #endif /* !__LP64__ */
  133. static PyObject *File_Error;
  134. /* ------------------- Object type FSCatalogInfo -------------------- */
  135. static PyTypeObject FSCatalogInfo_Type;
  136. #define FSCatalogInfo_Check(x) ((x)->ob_type == &FSCatalogInfo_Type || PyObject_TypeCheck((x), &FSCatalogInfo_Type))
  137. typedef struct FSCatalogInfoObject {
  138. PyObject_HEAD
  139. FSCatalogInfo ob_itself;
  140. } FSCatalogInfoObject;
  141. static PyObject *FSCatalogInfo_New(FSCatalogInfo *itself)
  142. {
  143. FSCatalogInfoObject *it;
  144. if (itself == NULL) { Py_INCREF(Py_None); return Py_None; }
  145. it = PyObject_NEW(FSCatalogInfoObject, &FSCatalogInfo_Type);
  146. if (it == NULL) return NULL;
  147. it->ob_itself = *itself;
  148. return (PyObject *)it;
  149. }
  150. static int FSCatalogInfo_Convert(PyObject *v, FSCatalogInfo *p_itself)
  151. {
  152. if (!FSCatalogInfo_Check(v))
  153. {
  154. PyErr_SetString(PyExc_TypeError, "FSCatalogInfo required");
  155. return 0;
  156. }
  157. *p_itself = ((FSCatalogInfoObject *)v)->ob_itself;
  158. return 1;
  159. }
  160. static void FSCatalogInfo_dealloc(FSCatalogInfoObject *self)
  161. {
  162. /* Cleanup of self->ob_itself goes here */
  163. self->ob_type->tp_free((PyObject *)self);
  164. }
  165. static PyMethodDef FSCatalogInfo_methods[] = {
  166. {NULL, NULL, 0}
  167. };
  168. static PyObject *FSCatalogInfo_get_nodeFlags(FSCatalogInfoObject *self, void *closure)
  169. {
  170. return Py_BuildValue("H", self->ob_itself.nodeFlags);
  171. }
  172. static int FSCatalogInfo_set_nodeFlags(FSCatalogInfoObject *self, PyObject *v, void *closure)
  173. {
  174. return PyArg_Parse(v, "H", &self->ob_itself.nodeFlags)-1;
  175. return 0;
  176. }
  177. static PyObject *FSCatalogInfo_get_volume(FSCatalogInfoObject *self, void *closure)
  178. {
  179. return Py_BuildValue("h", self->ob_itself.volume);
  180. }
  181. static int FSCatalogInfo_set_volume(FSCatalogInfoObject *self, PyObject *v, void *closure)
  182. {
  183. return PyArg_Parse(v, "h", &self->ob_itself.volume)-1;
  184. return 0;
  185. }
  186. static PyObject *FSCatalogInfo_get_parentDirID(FSCatalogInfoObject *self, void *closure)
  187. {
  188. return Py_BuildValue("l", self->ob_itself.parentDirID);
  189. }
  190. static int FSCatalogInfo_set_parentDirID(FSCatalogInfoObject *self, PyObject *v, void *closure)
  191. {
  192. return PyArg_Parse(v, "l", &self->ob_itself.parentDirID)-1;
  193. return 0;
  194. }
  195. static PyObject *FSCatalogInfo_get_nodeID(FSCatalogInfoObject *self, void *closure)
  196. {
  197. return Py_BuildValue("l", self->ob_itself.nodeID);
  198. }
  199. static int FSCatalogInfo_set_nodeID(FSCatalogInfoObject *self, PyObject *v, void *closure)
  200. {
  201. return PyArg_Parse(v, "l", &self->ob_itself.nodeID)-1;
  202. return 0;
  203. }
  204. static PyObject *FSCatalogInfo_get_createDate(FSCatalogInfoObject *self, void *closure)
  205. {
  206. return Py_BuildValue("O&", UTCDateTime_New, &self->ob_itself.createDate);
  207. }
  208. static int FSCatalogInfo_set_createDate(FSCatalogInfoObject *self, PyObject *v, void *closure)
  209. {
  210. return PyArg_Parse(v, "O&", UTCDateTime_Convert, &self->ob_itself.createDate)-1;
  211. return 0;
  212. }
  213. static PyObject *FSCatalogInfo_get_contentModDate(FSCatalogInfoObject *self, void *closure)
  214. {
  215. return Py_BuildValue("O&", UTCDateTime_New, &self->ob_itself.contentModDate);
  216. }
  217. static int FSCatalogInfo_set_contentModDate(FSCatalogInfoObject *self, PyObject *v, void *closure)
  218. {
  219. return PyArg_Parse(v, "O&", UTCDateTime_Convert, &self->ob_itself.contentModDate)-1;
  220. return 0;
  221. }
  222. static PyObject *FSCatalogInfo_get_attributeModDate(FSCatalogInfoObject *self, void *closure)
  223. {
  224. return Py_BuildValue("O&", UTCDateTime_New, &self->ob_itself.attributeModDate);
  225. }
  226. static int FSCatalogInfo_set_attributeModDate(FSCatalogInfoObject *self, PyObject *v, void *closure)
  227. {
  228. return PyArg_Parse(v, "O&", UTCDateTime_Convert, &self->ob_itself.attributeModDate)-1;
  229. return 0;
  230. }
  231. static PyObject *FSCatalogInfo_get_accessDate(FSCatalogInfoObject *self, void *closure)
  232. {
  233. return Py_BuildValue("O&", UTCDateTime_New, &self->ob_itself.accessDate);
  234. }
  235. static int FSCatalogInfo_set_accessDate(FSCatalogInfoObject *self, PyObject *v, void *closure)
  236. {
  237. return PyArg_Parse(v, "O&", UTCDateTime_Convert, &self->ob_itself.accessDate)-1;
  238. return 0;
  239. }
  240. static PyObject *FSCatalogInfo_get_backupDate(FSCatalogInfoObject *self, void *closure)
  241. {
  242. return Py_BuildValue("O&", UTCDateTime_New, &self->ob_itself.backupDate);
  243. }
  244. static int FSCatalogInfo_set_backupDate(FSCatalogInfoObject *self, PyObject *v, void *closure)
  245. {
  246. return PyArg_Parse(v, "O&", UTCDateTime_Convert, &self->ob_itself.backupDate)-1;
  247. return 0;
  248. }
  249. static PyObject *FSCatalogInfo_get_permissions(FSCatalogInfoObject *self, void *closure)
  250. {
  251. FSPermissionInfo* info = (FSPermissionInfo*)&(self->ob_itself.permissions);
  252. return Py_BuildValue("(llll)", info->userID, info->groupID, info->userAccess, info->mode);
  253. }
  254. static int FSCatalogInfo_set_permissions(FSCatalogInfoObject *self, PyObject *v, void *closure)
  255. {
  256. long userID;
  257. long groupID;
  258. long userAccess;
  259. long mode;
  260. int r;
  261. FSPermissionInfo* info = (FSPermissionInfo*)&(self->ob_itself.permissions);
  262. r = PyArg_Parse(v, "(llll)", &userID, &groupID, &userAccess, &mode);
  263. if (!r) {
  264. return -1;
  265. }
  266. info->userID = userID;
  267. info->groupID = groupID;
  268. info->userAccess = userAccess;
  269. info->mode = mode;
  270. return 0;
  271. }
  272. static PyObject *FSCatalogInfo_get_valence(FSCatalogInfoObject *self, void *closure)
  273. {
  274. return Py_BuildValue("l", self->ob_itself.valence);
  275. }
  276. static int FSCatalogInfo_set_valence(FSCatalogInfoObject *self, PyObject *v, void *closure)
  277. {
  278. return PyArg_Parse(v, "l", &self->ob_itself.valence)-1;
  279. return 0;
  280. }
  281. static PyObject *FSCatalogInfo_get_dataLogicalSize(FSCatalogInfoObject *self, void *closure)
  282. {
  283. return Py_BuildValue("l", self->ob_itself.dataLogicalSize);
  284. }
  285. static int FSCatalogInfo_set_dataLogicalSize(FSCatalogInfoObject *self, PyObject *v, void *closure)
  286. {
  287. return PyArg_Parse(v, "l", &self->ob_itself.dataLogicalSize)-1;
  288. return 0;
  289. }
  290. static PyObject *FSCatalogInfo_get_dataPhysicalSize(FSCatalogInfoObject *self, void *closure)
  291. {
  292. return Py_BuildValue("l", self->ob_itself.dataPhysicalSize);
  293. }
  294. static int FSCatalogInfo_set_dataPhysicalSize(FSCatalogInfoObject *self, PyObject *v, void *closure)
  295. {
  296. return PyArg_Parse(v, "l", &self->ob_itself.dataPhysicalSize)-1;
  297. return 0;
  298. }
  299. static PyObject *FSCatalogInfo_get_rsrcLogicalSize(FSCatalogInfoObject *self, void *closure)
  300. {
  301. return Py_BuildValue("l", self->ob_itself.rsrcLogicalSize);
  302. }
  303. static int FSCatalogInfo_set_rsrcLogicalSize(FSCatalogInfoObject *self, PyObject *v, void *closure)
  304. {
  305. return PyArg_Parse(v, "l", &self->ob_itself.rsrcLogicalSize)-1;
  306. return 0;
  307. }
  308. static PyObject *FSCatalogInfo_get_rsrcPhysicalSize(FSCatalogInfoObject *self, void *closure)
  309. {
  310. return Py_BuildValue("l", self->ob_itself.rsrcPhysicalSize);
  311. }
  312. static int FSCatalogInfo_set_rsrcPhysicalSize(FSCatalogInfoObject *self, PyObject *v, void *closure)
  313. {
  314. return PyArg_Parse(v, "l", &self->ob_itself.rsrcPhysicalSize)-1;
  315. return 0;
  316. }
  317. static PyObject *FSCatalogInfo_get_sharingFlags(FSCatalogInfoObject *self, void *closure)
  318. {
  319. return Py_BuildValue("l", self->ob_itself.sharingFlags);
  320. }
  321. static int FSCatalogInfo_set_sharingFlags(FSCatalogInfoObject *self, PyObject *v, void *closure)
  322. {
  323. return PyArg_Parse(v, "l", &self->ob_itself.sharingFlags)-1;
  324. return 0;
  325. }
  326. static PyObject *FSCatalogInfo_get_userPrivileges(FSCatalogInfoObject *self, void *closure)
  327. {
  328. return Py_BuildValue("b", self->ob_itself.userPrivileges);
  329. }
  330. static int FSCatalogInfo_set_userPrivileges(FSCatalogInfoObject *self, PyObject *v, void *closure)
  331. {
  332. return PyArg_Parse(v, "b", &self->ob_itself.userPrivileges)-1;
  333. return 0;
  334. }
  335. static PyGetSetDef FSCatalogInfo_getsetlist[] = {
  336. {"nodeFlags", (getter)FSCatalogInfo_get_nodeFlags, (setter)FSCatalogInfo_set_nodeFlags, NULL},
  337. {"volume", (getter)FSCatalogInfo_get_volume, (setter)FSCatalogInfo_set_volume, NULL},
  338. {"parentDirID", (getter)FSCatalogInfo_get_parentDirID, (setter)FSCatalogInfo_set_parentDirID, NULL},
  339. {"nodeID", (getter)FSCatalogInfo_get_nodeID, (setter)FSCatalogInfo_set_nodeID, NULL},
  340. {"createDate", (getter)FSCatalogInfo_get_createDate, (setter)FSCatalogInfo_set_createDate, NULL},
  341. {"contentModDate", (getter)FSCatalogInfo_get_contentModDate, (setter)FSCatalogInfo_set_contentModDate, NULL},
  342. {"attributeModDate", (getter)FSCatalogInfo_get_attributeModDate, (setter)FSCatalogInfo_set_attributeModDate, NULL},
  343. {"accessDate", (getter)FSCatalogInfo_get_accessDate, (setter)FSCatalogInfo_set_accessDate, NULL},
  344. {"backupDate", (getter)FSCatalogInfo_get_backupDate, (setter)FSCatalogInfo_set_backupDate, NULL},
  345. {"permissions", (getter)FSCatalogInfo_get_permissions, (setter)FSCatalogInfo_set_permissions, NULL},
  346. {"valence", (getter)FSCatalogInfo_get_valence, (setter)FSCatalogInfo_set_valence, NULL},
  347. {"dataLogicalSize", (getter)FSCatalogInfo_get_dataLogicalSize, (setter)FSCatalogInfo_set_dataLogicalSize, NULL},
  348. {"dataPhysicalSize", (getter)FSCatalogInfo_get_dataPhysicalSize, (setter)FSCatalogInfo_set_dataPhysicalSize, NULL},
  349. {"rsrcLogicalSize", (getter)FSCatalogInfo_get_rsrcLogicalSize, (setter)FSCatalogInfo_set_rsrcLogicalSize, NULL},
  350. {"rsrcPhysicalSize", (getter)FSCatalogInfo_get_rsrcPhysicalSize, (setter)FSCatalogInfo_set_rsrcPhysicalSize, NULL},
  351. {"sharingFlags", (getter)FSCatalogInfo_get_sharingFlags, (setter)FSCatalogInfo_set_sharingFlags, NULL},
  352. {"userPrivileges", (getter)FSCatalogInfo_get_userPrivileges, (setter)FSCatalogInfo_set_userPrivileges, NULL},
  353. {NULL, NULL, NULL, NULL},
  354. };
  355. #define FSCatalogInfo_compare NULL
  356. #define FSCatalogInfo_repr NULL
  357. #define FSCatalogInfo_hash NULL
  358. static int FSCatalogInfo_tp_init(PyObject *_self, PyObject *_args, PyObject *_kwds)
  359. {
  360. static char *kw[] = {
  361. "nodeFlags",
  362. "volume",
  363. "parentDirID",
  364. "nodeID",
  365. "createDate",
  366. "contentModDate",
  367. "atributeModDate",
  368. "accessDate",
  369. "backupDate",
  370. "valence",
  371. "dataLogicalSize",
  372. "dataPhysicalSize",
  373. "rsrcLogicalSize",
  374. "rsrcPhysicalSize",
  375. "sharingFlags",
  376. "userPrivileges"
  377. , 0};
  378. if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "|HhllO&O&O&O&O&llllllb", kw, &((FSCatalogInfoObject *)_self)->ob_itself.nodeFlags,
  379. &((FSCatalogInfoObject *)_self)->ob_itself.volume,
  380. &((FSCatalogInfoObject *)_self)->ob_itself.parentDirID,
  381. &((FSCatalogInfoObject *)_self)->ob_itself.nodeID,
  382. UTCDateTime_Convert, &((FSCatalogInfoObject *)_self)->ob_itself.createDate,
  383. UTCDateTime_Convert, &((FSCatalogInfoObject *)_self)->ob_itself.contentModDate,
  384. UTCDateTime_Convert, &((FSCatalogInfoObject *)_self)->ob_itself.attributeModDate,
  385. UTCDateTime_Convert, &((FSCatalogInfoObject *)_self)->ob_itself.accessDate,
  386. UTCDateTime_Convert, &((FSCatalogInfoObject *)_self)->ob_itself.backupDate,
  387. &((FSCatalogInfoObject *)_self)->ob_itself.valence,
  388. &((FSCatalogInfoObject *)_self)->ob_itself.dataLogicalSize,
  389. &((FSCatalogInfoObject *)_self)->ob_itself.dataPhysicalSize,
  390. &((FSCatalogInfoObject *)_self)->ob_itself.rsrcLogicalSize,
  391. &((FSCatalogInfoObject *)_self)->ob_itself.rsrcPhysicalSize,
  392. &((FSCatalogInfoObject *)_self)->ob_itself.sharingFlags,
  393. &((FSCatalogInfoObject *)_self)->ob_itself.userPrivileges))
  394. {
  395. return -1;
  396. }
  397. return 0;
  398. }
  399. #define FSCatalogInfo_tp_alloc PyType_GenericAlloc
  400. static PyObject *FSCatalogInfo_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
  401. {
  402. PyObject *self;
  403. if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;
  404. memset(&((FSCatalogInfoObject *)self)->ob_itself, 0, sizeof(FSCatalogInfo));
  405. return self;
  406. }
  407. #define FSCatalogInfo_tp_free PyObject_Del
  408. static PyTypeObject FSCatalogInfo_Type = {
  409. PyObject_HEAD_INIT(NULL)
  410. 0, /*ob_size*/
  411. "Carbon.File.FSCatalogInfo", /*tp_name*/
  412. sizeof(FSCatalogInfoObject), /*tp_basicsize*/
  413. 0, /*tp_itemsize*/
  414. /* methods */
  415. (destructor) FSCatalogInfo_dealloc, /*tp_dealloc*/
  416. 0, /*tp_print*/
  417. (getattrfunc)0, /*tp_getattr*/
  418. (setattrfunc)0, /*tp_setattr*/
  419. (cmpfunc) FSCatalogInfo_compare, /*tp_compare*/
  420. (reprfunc) FSCatalogInfo_repr, /*tp_repr*/
  421. (PyNumberMethods *)0, /* tp_as_number */
  422. (PySequenceMethods *)0, /* tp_as_sequence */
  423. (PyMappingMethods *)0, /* tp_as_mapping */
  424. (hashfunc) FSCatalogInfo_hash, /*tp_hash*/
  425. 0, /*tp_call*/
  426. 0, /*tp_str*/
  427. PyObject_GenericGetAttr, /*tp_getattro*/
  428. PyObject_GenericSetAttr, /*tp_setattro */
  429. 0, /*tp_as_buffer*/
  430. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
  431. 0, /*tp_doc*/
  432. 0, /*tp_traverse*/
  433. 0, /*tp_clear*/
  434. 0, /*tp_richcompare*/
  435. 0, /*tp_weaklistoffset*/
  436. 0, /*tp_iter*/
  437. 0, /*tp_iternext*/
  438. FSCatalogInfo_methods, /* tp_methods */
  439. 0, /*tp_members*/
  440. FSCatalogInfo_getsetlist, /*tp_getset*/
  441. 0, /*tp_base*/
  442. 0, /*tp_dict*/
  443. 0, /*tp_descr_get*/
  444. 0, /*tp_descr_set*/
  445. 0, /*tp_dictoffset*/
  446. FSCatalogInfo_tp_init, /* tp_init */
  447. FSCatalogInfo_tp_alloc, /* tp_alloc */
  448. FSCatalogInfo_tp_new, /* tp_new */
  449. FSCatalogInfo_tp_free, /* tp_free */
  450. };
  451. /* ----------------- End object type FSCatalogInfo ------------------ */
  452. /* ----------------------- Object type FInfo ------------------------ */
  453. #ifndef __LP64__
  454. static PyTypeObject FInfo_Type;
  455. #define FInfo_Check(x) ((x)->ob_type == &FInfo_Type || PyObject_TypeCheck((x), &FInfo_Type))
  456. typedef struct FInfoObject {
  457. PyObject_HEAD
  458. FInfo ob_itself;
  459. } FInfoObject;
  460. static PyObject *FInfo_New(FInfo *itself)
  461. {
  462. FInfoObject *it;
  463. if (itself == NULL) return PyMac_Error(resNotFound);
  464. it = PyObject_NEW(FInfoObject, &FInfo_Type);
  465. if (it == NULL) return NULL;
  466. it->ob_itself = *itself;
  467. return (PyObject *)it;
  468. }
  469. static int FInfo_Convert(PyObject *v, FInfo *p_itself)
  470. {
  471. if (!FInfo_Check(v))
  472. {
  473. PyErr_SetString(PyExc_TypeError, "FInfo required");
  474. return 0;
  475. }
  476. *p_itself = ((FInfoObject *)v)->ob_itself;
  477. return 1;
  478. }
  479. static void FInfo_dealloc(FInfoObject *self)
  480. {
  481. /* Cleanup of self->ob_itself goes here */
  482. self->ob_type->tp_free((PyObject *)self);
  483. }
  484. static PyMethodDef FInfo_methods[] = {
  485. {NULL, NULL, 0}
  486. };
  487. static PyObject *FInfo_get_Type(FInfoObject *self, void *closure)
  488. {
  489. return Py_BuildValue("O&", PyMac_BuildOSType, self->ob_itself.fdType);
  490. }
  491. static int FInfo_set_Type(FInfoObject *self, PyObject *v, void *closure)
  492. {
  493. return PyArg_Parse(v, "O&", PyMac_GetOSType, &self->ob_itself.fdType)-1;
  494. return 0;
  495. }
  496. static PyObject *FInfo_get_Creator(FInfoObject *self, void *closure)
  497. {
  498. return Py_BuildValue("O&", PyMac_BuildOSType, self->ob_itself.fdCreator);
  499. }
  500. static int FInfo_set_Creator(FInfoObject *self, PyObject *v, void *closure)
  501. {
  502. return PyArg_Parse(v, "O&", PyMac_GetOSType, &self->ob_itself.fdCreator)-1;
  503. return 0;
  504. }
  505. static PyObject *FInfo_get_Flags(FInfoObject *self, void *closure)
  506. {
  507. return Py_BuildValue("H", self->ob_itself.fdFlags);
  508. }
  509. static int FInfo_set_Flags(FInfoObject *self, PyObject *v, void *closure)
  510. {
  511. return PyArg_Parse(v, "H", &self->ob_itself.fdFlags)-1;
  512. return 0;
  513. }
  514. static PyObject *FInfo_get_Location(FInfoObject *self, void *closure)
  515. {
  516. return Py_BuildValue("O&", PyMac_BuildPoint, self->ob_itself.fdLocation);
  517. }
  518. static int FInfo_set_Location(FInfoObject *self, PyObject *v, void *closure)
  519. {
  520. return PyArg_Parse(v, "O&", PyMac_GetPoint, &self->ob_itself.fdLocation)-1;
  521. return 0;
  522. }
  523. static PyObject *FInfo_get_Fldr(FInfoObject *self, void *closure)
  524. {
  525. return Py_BuildValue("h", self->ob_itself.fdFldr);
  526. }
  527. static int FInfo_set_Fldr(FInfoObject *self, PyObject *v, void *closure)
  528. {
  529. return PyArg_Parse(v, "h", &self->ob_itself.fdFldr)-1;
  530. return 0;
  531. }
  532. static PyGetSetDef FInfo_getsetlist[] = {
  533. {"Type", (getter)FInfo_get_Type, (setter)FInfo_set_Type, "4-char file type"},
  534. {"Creator", (getter)FInfo_get_Creator, (setter)FInfo_set_Creator, "4-char file creator"},
  535. {"Flags", (getter)FInfo_get_Flags, (setter)FInfo_set_Flags, "Finder flag bits"},
  536. {"Location", (getter)FInfo_get_Location, (setter)FInfo_set_Location, "(x, y) location of the file's icon in its parent finder window"},
  537. {"Fldr", (getter)FInfo_get_Fldr, (setter)FInfo_set_Fldr, "Original folder, for 'put away'"},
  538. {NULL, NULL, NULL, NULL},
  539. };
  540. #define FInfo_compare NULL
  541. #define FInfo_repr NULL
  542. #define FInfo_hash NULL
  543. static int FInfo_tp_init(PyObject *_self, PyObject *_args, PyObject *_kwds)
  544. {
  545. FInfo *itself = NULL;
  546. static char *kw[] = {"itself", 0};
  547. if (PyArg_ParseTupleAndKeywords(_args, _kwds, "|O&", kw, FInfo_Convert, &itself))
  548. {
  549. if (itself) memcpy(&((FInfoObject *)_self)->ob_itself, itself, sizeof(FInfo));
  550. return 0;
  551. }
  552. return -1;
  553. }
  554. #define FInfo_tp_alloc PyType_GenericAlloc
  555. static PyObject *FInfo_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
  556. {
  557. PyObject *self;
  558. if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;
  559. memset(&((FInfoObject *)self)->ob_itself, 0, sizeof(FInfo));
  560. return self;
  561. }
  562. #define FInfo_tp_free PyObject_Del
  563. static PyTypeObject FInfo_Type = {
  564. PyObject_HEAD_INIT(NULL)
  565. 0, /*ob_size*/
  566. "Carbon.File.FInfo", /*tp_name*/
  567. sizeof(FInfoObject), /*tp_basicsize*/
  568. 0, /*tp_itemsize*/
  569. /* methods */
  570. (destructor) FInfo_dealloc, /*tp_dealloc*/
  571. 0, /*tp_print*/
  572. (getattrfunc)0, /*tp_getattr*/
  573. (setattrfunc)0, /*tp_setattr*/
  574. (cmpfunc) FInfo_compare, /*tp_compare*/
  575. (reprfunc) FInfo_repr, /*tp_repr*/
  576. (PyNumberMethods *)0, /* tp_as_number */
  577. (PySequenceMethods *)0, /* tp_as_sequence */
  578. (PyMappingMethods *)0, /* tp_as_mapping */
  579. (hashfunc) FInfo_hash, /*tp_hash*/
  580. 0, /*tp_call*/
  581. 0, /*tp_str*/
  582. PyObject_GenericGetAttr, /*tp_getattro*/
  583. PyObject_GenericSetAttr, /*tp_setattro */
  584. 0, /*tp_as_buffer*/
  585. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
  586. 0, /*tp_doc*/
  587. 0, /*tp_traverse*/
  588. 0, /*tp_clear*/
  589. 0, /*tp_richcompare*/
  590. 0, /*tp_weaklistoffset*/
  591. 0, /*tp_iter*/
  592. 0, /*tp_iternext*/
  593. FInfo_methods, /* tp_methods */
  594. 0, /*tp_members*/
  595. FInfo_getsetlist, /*tp_getset*/
  596. 0, /*tp_base*/
  597. 0, /*tp_dict*/
  598. 0, /*tp_descr_get*/
  599. 0, /*tp_descr_set*/
  600. 0, /*tp_dictoffset*/
  601. FInfo_tp_init, /* tp_init */
  602. FInfo_tp_alloc, /* tp_alloc */
  603. FInfo_tp_new, /* tp_new */
  604. FInfo_tp_free, /* tp_free */
  605. };
  606. #endif /* !__LP64__ */
  607. /* --------------------- End object type FInfo ---------------------- */
  608. /* ----------------------- Object type Alias ------------------------ */
  609. static PyTypeObject Alias_Type;
  610. #define Alias_Check(x) ((x)->ob_type == &Alias_Type || PyObject_TypeCheck((x), &Alias_Type))
  611. typedef struct AliasObject {
  612. PyObject_HEAD
  613. AliasHandle ob_itself;
  614. void (*ob_freeit)(AliasHandle ptr);
  615. } AliasObject;
  616. static PyObject *Alias_New(AliasHandle itself)
  617. {
  618. AliasObject *it;
  619. if (itself == NULL) return PyMac_Error(resNotFound);
  620. it = PyObject_NEW(AliasObject, &Alias_Type);
  621. if (it == NULL) return NULL;
  622. it->ob_itself = itself;
  623. it->ob_freeit = NULL;
  624. return (PyObject *)it;
  625. }
  626. static int Alias_Convert(PyObject *v, AliasHandle *p_itself)
  627. {
  628. if (!Alias_Check(v))
  629. {
  630. PyErr_SetString(PyExc_TypeError, "Alias required");
  631. return 0;
  632. }
  633. *p_itself = ((AliasObject *)v)->ob_itself;
  634. return 1;
  635. }
  636. static void Alias_dealloc(AliasObject *self)
  637. {
  638. if (self->ob_freeit && self->ob_itself)
  639. {
  640. self->ob_freeit(self->ob_itself);
  641. }
  642. self->ob_itself = NULL;
  643. self->ob_type->tp_free((PyObject *)self);
  644. }
  645. #ifndef __LP64__
  646. static PyObject *Alias_ResolveAlias(AliasObject *_self, PyObject *_args)
  647. {
  648. PyObject *_res = NULL;
  649. OSErr _err;
  650. FSSpec fromFile__buf__;
  651. FSSpec *fromFile = &fromFile__buf__;
  652. FSSpec target;
  653. Boolean wasChanged;
  654. if (!PyArg_ParseTuple(_args, "O&",
  655. myPyMac_GetOptFSSpecPtr, &fromFile))
  656. return NULL;
  657. _err = ResolveAlias(fromFile,
  658. _self->ob_itself,
  659. &target,
  660. &wasChanged);
  661. if (_err != noErr) return PyMac_Error(_err);
  662. _res = Py_BuildValue("O&b",
  663. FSSpec_New, &target,
  664. wasChanged);
  665. return _res;
  666. }
  667. static PyObject *Alias_GetAliasInfo(AliasObject *_self, PyObject *_args)
  668. {
  669. PyObject *_res = NULL;
  670. OSErr _err;
  671. AliasInfoType index;
  672. Str63 theString;
  673. if (!PyArg_ParseTuple(_args, "h",
  674. &index))
  675. return NULL;
  676. _err = GetAliasInfo(_self->ob_itself,
  677. index,
  678. theString);
  679. if (_err != noErr) return PyMac_Error(_err);
  680. _res = Py_BuildValue("O&",
  681. PyMac_BuildStr255, theString);
  682. return _res;
  683. }
  684. static PyObject *Alias_ResolveAliasWithMountFlags(AliasObject *_self, PyObject *_args)
  685. {
  686. PyObject *_res = NULL;
  687. OSErr _err;
  688. FSSpec fromFile__buf__;
  689. FSSpec *fromFile = &fromFile__buf__;
  690. FSSpec target;
  691. Boolean wasChanged;
  692. unsigned long mountFlags;
  693. if (!PyArg_ParseTuple(_args, "O&l",
  694. myPyMac_GetOptFSSpecPtr, &fromFile,
  695. &mountFlags))
  696. return NULL;
  697. _err = ResolveAliasWithMountFlags(fromFile,
  698. _self->ob_itself,
  699. &target,
  700. &wasChanged,
  701. mountFlags);
  702. if (_err != noErr) return PyMac_Error(_err);
  703. _res = Py_BuildValue("O&b",
  704. FSSpec_New, &target,
  705. wasChanged);
  706. return _res;
  707. }
  708. static PyObject *Alias_FollowFinderAlias(AliasObject *_self, PyObject *_args)
  709. {
  710. PyObject *_res = NULL;
  711. OSErr _err;
  712. FSSpec fromFile__buf__;
  713. FSSpec *fromFile = &fromFile__buf__;
  714. Boolean logon;
  715. FSSpec target;
  716. Boolean wasChanged;
  717. if (!PyArg_ParseTuple(_args, "O&b",
  718. myPyMac_GetOptFSSpecPtr, &fromFile,
  719. &logon))
  720. return NULL;
  721. _err = FollowFinderAlias(fromFile,
  722. _self->ob_itself,
  723. logon,
  724. &target,
  725. &wasChanged);
  726. if (_err != noErr) return PyMac_Error(_err);
  727. _res = Py_BuildValue("O&b",
  728. FSSpec_New, &target,
  729. wasChanged);
  730. return _res;
  731. }
  732. #endif /* !__LP64__ */
  733. static PyObject *Alias_FSResolveAliasWithMountFlags(AliasObject *_self, PyObject *_args)
  734. {
  735. PyObject *_res = NULL;
  736. OSErr _err;
  737. FSRef fromFile__buf__;
  738. FSRef *fromFile = &fromFile__buf__;
  739. FSRef target;
  740. Boolean wasChanged;
  741. unsigned long mountFlags;
  742. if (!PyArg_ParseTuple(_args, "O&l",
  743. myPyMac_GetOptFSRefPtr, &fromFile,
  744. &mountFlags))
  745. return NULL;
  746. _err = FSResolveAliasWithMountFlags(fromFile,
  747. _self->ob_itself,
  748. &target,
  749. &wasChanged,
  750. mountFlags);
  751. if (_err != noErr) return PyMac_Error(_err);
  752. _res = Py_BuildValue("O&b",
  753. FSRef_New, &target,
  754. wasChanged);
  755. return _res;
  756. }
  757. static PyObject *Alias_FSResolveAlias(AliasObject *_self, PyObject *_args)
  758. {
  759. PyObject *_res = NULL;
  760. OSErr _err;
  761. FSRef fromFile__buf__;
  762. FSRef *fromFile = &fromFile__buf__;
  763. FSRef target;
  764. Boolean wasChanged;
  765. if (!PyArg_ParseTuple(_args, "O&",
  766. myPyMac_GetOptFSRefPtr, &fromFile))
  767. return NULL;
  768. _err = FSResolveAlias(fromFile,
  769. _self->ob_itself,
  770. &target,
  771. &wasChanged);
  772. if (_err != noErr) return PyMac_Error(_err);
  773. _res = Py_BuildValue("O&b",
  774. FSRef_New, &target,
  775. wasChanged);
  776. return _res;
  777. }
  778. static PyObject *Alias_FSFollowFinderAlias(AliasObject *_self, PyObject *_args)
  779. {
  780. PyObject *_res = NULL;
  781. OSErr _err;
  782. FSRef fromFile;
  783. Boolean logon;
  784. FSRef target;
  785. Boolean wasChanged;
  786. if (!PyArg_ParseTuple(_args, "b",
  787. &logon))
  788. return NULL;
  789. _err = FSFollowFinderAlias(&fromFile,
  790. _self->ob_itself,
  791. logon,
  792. &target,
  793. &wasChanged);
  794. if (_err != noErr) return PyMac_Error(_err);
  795. _res = Py_BuildValue("O&O&b",
  796. FSRef_New, &fromFile,
  797. FSRef_New, &target,
  798. wasChanged);
  799. return _res;
  800. }
  801. static PyMethodDef Alias_methods[] = {
  802. #ifndef __LP64__
  803. {"ResolveAlias", (PyCFunction)Alias_ResolveAlias, 1,
  804. PyDoc_STR("(FSSpec fromFile) -> (FSSpec target, Boolean wasChanged)")},
  805. {"GetAliasInfo", (PyCFunction)Alias_GetAliasInfo, 1,
  806. PyDoc_STR("(AliasInfoType index) -> (Str63 theString)")},
  807. {"ResolveAliasWithMountFlags", (PyCFunction)Alias_ResolveAliasWithMountFlags, 1,
  808. PyDoc_STR("(FSSpec fromFile, unsigned long mountFlags) -> (FSSpec target, Boolean wasChanged)")},
  809. {"FollowFinderAlias", (PyCFunction)Alias_FollowFinderAlias, 1,
  810. PyDoc_STR("(FSSpec fromFile, Boolean logon) -> (FSSpec target, Boolean wasChanged)")},
  811. #endif /* !__LP64__ */
  812. {"FSResolveAliasWithMountFlags", (PyCFunction)Alias_FSResolveAliasWithMountFlags, 1,
  813. PyDoc_STR("(FSRef fromFile, unsigned long mountFlags) -> (FSRef target, Boolean wasChanged)")},
  814. {"FSResolveAlias", (PyCFunction)Alias_FSResolveAlias, 1,
  815. PyDoc_STR("(FSRef fromFile) -> (FSRef target, Boolean wasChanged)")},
  816. {"FSFollowFinderAlias", (PyCFunction)Alias_FSFollowFinderAlias, 1,
  817. PyDoc_STR("(Boolean logon) -> (FSRef fromFile, FSRef target, Boolean wasChanged)")},
  818. {NULL, NULL, 0}
  819. };
  820. static PyObject *Alias_get_data(AliasObject *self, void *closure)
  821. {
  822. int size;
  823. PyObject *rv;
  824. size = GetHandleSize((Handle)self->ob_itself);
  825. HLock((Handle)self->ob_itself);
  826. rv = PyString_FromStringAndSize(*(Handle)self->ob_itself, size);
  827. HUnlock((Handle)self->ob_itself);
  828. return rv;
  829. }
  830. #define Alias_set_data NULL
  831. static PyGetSetDef Alias_getsetlist[] = {
  832. {"data", (getter)Alias_get_data, (setter)Alias_set_data, "Raw data of the alias object"},
  833. {NULL, NULL, NULL, NULL},
  834. };
  835. #define Alias_compare NULL
  836. #define Alias_repr NULL
  837. #define Alias_hash NULL
  838. static int Alias_tp_init(PyObject *_self, PyObject *_args, PyObject *_kwds)
  839. {
  840. AliasHandle itself = NULL;
  841. char *rawdata = NULL;
  842. int rawdatalen = 0;
  843. Handle h;
  844. static char *kw[] = {"itself", "rawdata", 0};
  845. if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "|O&s#", kw, Alias_Convert, &itself, &rawdata, &rawdatalen))
  846. return -1;
  847. if (itself && rawdata)
  848. {
  849. PyErr_SetString(PyExc_TypeError, "Only one of itself or rawdata may be specified");
  850. return -1;
  851. }
  852. if (!itself && !rawdata)
  853. {
  854. PyErr_SetString(PyExc_TypeError, "One of itself or rawdata must be specified");
  855. return -1;
  856. }
  857. if (rawdata)
  858. {
  859. if ((h = NewHandle(rawdatalen)) == NULL)
  860. {
  861. PyErr_NoMemory();
  862. return -1;
  863. }
  864. HLock(h);
  865. memcpy((char *)*h, rawdata, rawdatalen);
  866. HUnlock(h);
  867. ((AliasObject *)_self)->ob_itself = (AliasHandle)h;
  868. return 0;
  869. }
  870. ((AliasObject *)_self)->ob_itself = itself;
  871. return 0;
  872. }
  873. #define Alias_tp_alloc PyType_GenericAlloc
  874. static PyObject *Alias_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
  875. {
  876. PyObject *self;
  877. if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;
  878. ((AliasObject *)self)->ob_itself = NULL;
  879. return self;
  880. }
  881. #define Alias_tp_free PyObject_Del
  882. static PyTypeObject Alias_Type = {
  883. PyObject_HEAD_INIT(NULL)
  884. 0, /*ob_size*/
  885. "Carbon.File.Alias", /*tp_name*/
  886. sizeof(AliasObject), /*tp_basicsize*/
  887. 0, /*tp_itemsize*/
  888. /* methods */
  889. (destructor) Alias_dealloc, /*tp_dealloc*/
  890. 0, /*tp_print*/
  891. (getattrfunc)0, /*tp_getattr*/
  892. (setattrfunc)0, /*tp_setattr*/
  893. (cmpfunc) Alias_compare, /*tp_compare*/
  894. (reprfunc) Alias_repr, /*tp_repr*/
  895. (PyNumberMethods *)0, /* tp_as_number */
  896. (PySequenceMethods *)0, /* tp_as_sequence */
  897. (PyMappingMethods *)0, /* tp_as_mapping */
  898. (hashfunc) Alias_hash, /*tp_hash*/
  899. 0, /*tp_call*/
  900. 0, /*tp_str*/
  901. PyObject_GenericGetAttr, /*tp_getattro*/
  902. PyObject_GenericSetAttr, /*tp_setattro */
  903. 0, /*tp_as_buffer*/
  904. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
  905. 0, /*tp_doc*/
  906. 0, /*tp_traverse*/
  907. 0, /*tp_clear*/
  908. 0, /*tp_richcompare*/
  909. 0, /*tp_weaklistoffset*/
  910. 0, /*tp_iter*/
  911. 0, /*tp_iternext*/
  912. Alias_methods, /* tp_methods */
  913. 0, /*tp_members*/
  914. Alias_getsetlist, /*tp_getset*/
  915. 0, /*tp_base*/
  916. 0, /*tp_dict*/
  917. 0, /*tp_descr_get*/
  918. 0, /*tp_descr_set*/
  919. 0, /*tp_dictoffset*/
  920. Alias_tp_init, /* tp_init */
  921. Alias_tp_alloc, /* tp_alloc */
  922. Alias_tp_new, /* tp_new */
  923. Alias_tp_free, /* tp_free */
  924. };
  925. /* --------------------- End object type Alias ---------------------- */
  926. /* ----------------------- Object type FSSpec ----------------------- */
  927. #ifndef __LP64__
  928. static PyTypeObject FSSpec_Type;
  929. #define FSSpec_Check(x) ((x)->ob_type == &FSSpec_Type || PyObject_TypeCheck((x), &FSSpec_Type))
  930. typedef struct FSSpecObject {
  931. PyObject_HEAD
  932. FSSpec ob_itself;
  933. } FSSpecObject;
  934. static PyObject *FSSpec_New(FSSpec *itself)
  935. {
  936. FSSpecObject *it;
  937. if (itself == NULL) return PyMac_Error(resNotFound);
  938. it = PyObject_NEW(FSSpecObject, &FSSpec_Type);
  939. if (it == NULL) return NULL;
  940. it->ob_itself = *itself;
  941. return (PyObject *)it;
  942. }
  943. static void FSSpec_dealloc(FSSpecObject *self)
  944. {
  945. /* Cleanup of self->ob_itself goes here */
  946. self->ob_type->tp_free((PyObject *)self);
  947. }
  948. static PyObject *FSSpec_FSpOpenDF(FSSpecObject *_self, PyObject *_args)
  949. {
  950. PyObject *_res = NULL;
  951. OSErr _err;
  952. SInt8 permission;
  953. short refNum;
  954. if (!PyArg_ParseTuple(_args, "b",
  955. &permission))
  956. return NULL;
  957. _err = FSpOpenDF(&_self->ob_itself,
  958. permission,
  959. &refNum);
  960. if (_err != noErr) return PyMac_Error(_err);
  961. _res = Py_BuildValue("h",
  962. refNum);
  963. return _res;
  964. }
  965. static PyObject *FSSpec_FSpOpenRF(FSSpecObject *_self, PyObject *_args)
  966. {
  967. PyObject *_res = NULL;
  968. OSErr _err;
  969. SInt8 permission;
  970. short refNum;
  971. if (!PyArg_ParseTuple(_args, "b",
  972. &permission))
  973. return NULL;
  974. _err = FSpOpenRF(&_self->ob_itself,
  975. permission,
  976. &refNum);
  977. if (_err != noErr) return PyMac_Error(_err);
  978. _res = Py_BuildValue("h",
  979. refNum);
  980. return _res;
  981. }
  982. static PyObject *FSSpec_FSpCreate(FSSpecObject *_self, PyObject *_args)
  983. {
  984. PyObject *_res = NULL;
  985. OSErr _err;
  986. OSType creator;
  987. OSType fileType;
  988. ScriptCode scriptTag;
  989. if (!PyArg_ParseTuple(_args, "O&O&h",
  990. PyMac_GetOSType, &creator,
  991. PyMac_GetOSType, &fileType,
  992. &scriptTag))
  993. return NULL;
  994. _err = FSpCreate(&_self->ob_itself,
  995. creator,
  996. fileType,
  997. scriptTag);
  998. if (_err != noErr) return PyMac_Error(_err);
  999. Py_INCREF(Py_None);
  1000. _res = Py_None;
  1001. return _res;
  1002. }
  1003. static PyObject *FSSpec_FSpDirCreate(FSSpecObject *_self, PyObject *_args)
  1004. {
  1005. PyObject *_res = NULL;
  1006. OSErr _err;
  1007. ScriptCode scriptTag;
  1008. long createdDirID;
  1009. if (!PyArg_ParseTuple(_args, "h",
  1010. &scriptTag))
  1011. return NULL;
  1012. _err = FSpDirCreate(&_self->ob_itself,
  1013. scriptTag,
  1014. &createdDirID);
  1015. if (_err != noErr) return PyMac_Error(_err);
  1016. _res = Py_BuildValue("l",
  1017. createdDirID);
  1018. return _res;
  1019. }
  1020. static PyObject *FSSpec_FSpDelete(FSSpecObject *_self, PyObject *_args)
  1021. {
  1022. PyObject *_res = NULL;
  1023. OSErr _err;
  1024. if (!PyArg_ParseTuple(_args, ""))
  1025. return NULL;
  1026. _err = FSpDelete(&_self->ob_itself);
  1027. if (_err != noErr) return PyMac_Error(_err);
  1028. Py_INCREF(Py_None);
  1029. _res = Py_None;
  1030. return _res;
  1031. }
  1032. static PyObject *FSSpec_FSpGetFInfo(FSSpecObject *_self, PyObject *_args)
  1033. {
  1034. PyObject *_res = NULL;
  1035. OSErr _err;
  1036. FInfo fndrInfo;
  1037. if (!PyArg_ParseTuple(_args, ""))
  1038. return NULL;
  1039. _err = FSpGetFInfo(&_self->ob_itself,
  1040. &fndrInfo);
  1041. if (_err != noErr) return PyMac_Error(_err);
  1042. _res = Py_BuildValue("O&",
  1043. FInfo_New, &fndrInfo);
  1044. return _res;
  1045. }
  1046. static PyObject *FSSpec_FSpSetFInfo(FSSpecObject *_self, PyObject *_args)
  1047. {
  1048. PyObject *_res = NULL;
  1049. OSErr _err;
  1050. FInfo fndrInfo;
  1051. if (!PyArg_ParseTuple(_args, "O&",
  1052. FInfo_Convert, &fndrInfo))
  1053. return NULL;
  1054. _err = FSpSetFInfo(&_self->ob_itself,
  1055. &fndrInfo);
  1056. if (_err != noErr) return PyMac_Error(_err);
  1057. Py_INCREF(Py_None);
  1058. _res = Py_None;
  1059. return _res;
  1060. }
  1061. static PyObject *FSSpec_FSpSetFLock(FSSpecObject *_self, PyObject *_args)
  1062. {
  1063. PyObject *_res = NULL;
  1064. OSErr _err;
  1065. if (!PyArg_ParseTuple(_args, ""))
  1066. return NULL;
  1067. _err = FSpSetFLock(&_self->ob_itself);
  1068. if (_err != noErr) return PyMac_Error(_err);
  1069. Py_INCREF(Py_None);
  1070. _res = Py_None;
  1071. return _res;
  1072. }
  1073. static PyObject *FSSpec_FSpRstFLock(FSSpecObject *_self, PyObject *_args)
  1074. {
  1075. PyObject *_res = NULL;
  1076. OSErr _err;
  1077. if (!PyArg_ParseTuple(_args, ""))
  1078. return NULL;
  1079. _err = FSpRstFLock(&_self->ob_itself);
  1080. if (_err != noErr) return PyMac_Error(_err);
  1081. Py_INCREF(Py_None);
  1082. _res = Py_None;
  1083. return _res;
  1084. }
  1085. static PyObject *FSSpec_FSpRename(FSSpecObject *_self, PyObject *_args)
  1086. {
  1087. PyObject *_res = NULL;
  1088. OSErr _err;
  1089. Str255 newName;
  1090. if (!PyArg_ParseTuple(_args, "O&",
  1091. PyMac_GetStr255, newName))
  1092. return NULL;
  1093. _err = FSpRename(&_self->ob_itself,
  1094. newName);
  1095. if (_err != noErr) return PyMac_Error(_err);
  1096. Py_INCREF(Py_None);
  1097. _res = Py_None;
  1098. return _res;
  1099. }
  1100. static PyObject *FSSpec_FSpCatMove(FSSpecObject *_self, PyObject *_args)
  1101. {
  1102. PyObject *_res = NULL;
  1103. OSErr _err;
  1104. FSSpec dest;
  1105. if (!PyArg_ParseTuple(_args, "O&",
  1106. FSSpec_Convert, &dest))
  1107. return NULL;
  1108. _err = FSpCatMove(&_self->ob_itself,
  1109. &dest);
  1110. if (_err != noErr) return PyMac_Error(_err);
  1111. Py_INCREF(Py_None);
  1112. _res = Py_None;
  1113. return _res;
  1114. }
  1115. static PyObject *FSSpec_FSpExchangeFiles(FSSpecObject *_self, PyObject *_args)
  1116. {
  1117. PyObject *_res = NULL;
  1118. OSErr _err;
  1119. FSSpec dest;
  1120. if (!PyArg_ParseTuple(_args, "O&",
  1121. FSSpec_Convert, &dest))
  1122. return NULL;
  1123. _err = FSpExchangeFiles(&_self->ob_itself,
  1124. &dest);
  1125. if (_err != noErr) return PyMac_Error(_err);
  1126. Py_INCREF(Py_None);
  1127. _res = Py_None;
  1128. return _res;
  1129. }
  1130. static PyObject *FSSpec_FSpMakeFSRef(FSSpecObject *_self, PyObject *_args)
  1131. {
  1132. PyObject *_res = NULL;
  1133. OSErr _err;
  1134. FSRef newRef;
  1135. if (!PyArg_ParseTuple(_args, ""))
  1136. return NULL;
  1137. _err = FSpMakeFSRef(&_self->ob_itself,
  1138. &newRef);
  1139. if (_err != noErr) return PyMac_Error(_err);
  1140. _res = Py_BuildValue("O&",
  1141. FSRef_New, &newRef);
  1142. return _res;
  1143. }
  1144. static PyObject *FSSpec_NewAliasMinimal(FSSpecObject *_self, PyObject *_args)
  1145. {
  1146. PyObject *_res = NULL;
  1147. OSErr _err;
  1148. AliasHandle alias;
  1149. if (!PyArg_ParseTuple(_args, ""))
  1150. return NULL;
  1151. _err = NewAliasMinimal(&_self->ob_itself,
  1152. &alias);
  1153. if (_err != noErr) return PyMac_Error(_err);
  1154. _res = Py_BuildValue("O&",
  1155. Alias_New, alias);
  1156. return _res;
  1157. }
  1158. static PyObject *FSSpec_IsAliasFile(FSSpecObject *_self, PyObject *_args)
  1159. {
  1160. PyObject *_res = NULL;
  1161. OSErr _err;
  1162. Boolean aliasFileFlag;
  1163. Boolean folderFlag;
  1164. if (!PyArg_ParseTuple(_args, ""))
  1165. return NULL;
  1166. _err = IsAliasFile(&_self->ob_itself,
  1167. &aliasFileFlag,
  1168. &folderFlag);
  1169. if (_err != noErr) return PyMac_Error(_err);
  1170. _res = Py_BuildValue("bb",
  1171. aliasFileFlag,
  1172. folderFlag);
  1173. return _res;
  1174. }
  1175. static PyObject *FSSpec_as_pathname(FSSpecObject *_self, PyObject *_args)
  1176. {
  1177. PyObject *_res = NULL;
  1178. char strbuf[1024];
  1179. OSErr err;
  1180. if (!PyArg_ParseTuple(_args, ""))
  1181. return NULL;
  1182. err = _PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
  1183. if ( err ) {
  1184. PyMac_Error(err);
  1185. return NULL;
  1186. }
  1187. _res = PyString_FromString(strbuf);
  1188. return _res;
  1189. }
  1190. static PyObject *FSSpec_as_tuple(FSSpecObject *_self, PyObject *_args)
  1191. {
  1192. PyObject *_res = NULL;
  1193. if (!PyArg_ParseTuple(_args, ""))
  1194. return NULL;
  1195. _res = Py_BuildValue("(iis#)", _self->ob_itself.vRefNum, _self->ob_itself.parID,
  1196. &_self->ob_itself.name[1], _self->ob_itself.name[0]);
  1197. return _res;
  1198. }
  1199. static PyMethodDef FSSpec_methods[] = {
  1200. {"FSpOpenDF", (PyCFunction)FSSpec_FSpOpenDF, 1,
  1201. PyDoc_STR("(SInt8 permission) -> (short refNum)")},
  1202. {"FSpOpenRF", (PyCFunction)FSSpec_FSpOpenRF, 1,
  1203. PyDoc_STR("(SInt8 permission) -> (short refNum)")},
  1204. {"FSpCreate", (PyCFunction)FSSpec_FSpCreate, 1,
  1205. PyDoc_STR("(OSType creator, OSType fileType, ScriptCode scriptTag) -> None")},
  1206. {"FSpDirCreate", (PyCFunction)FSSpec_FSpDirCreate, 1,
  1207. PyDoc_STR("(ScriptCode scriptTag) -> (long createdDirID)")},
  1208. {"FSpDelete", (PyCFunction)FSSpec_FSpDelete, 1,
  1209. PyDoc_STR("() -> None")},
  1210. {"FSpGetFInfo", (PyCFunction)FSSpec_FSpGetFInfo, 1,
  1211. PyDoc_STR("() -> (FInfo fndrInfo)")},
  1212. {"FSpSetFInfo", (PyCFunction)FSSpec_FSpSetFInfo, 1,
  1213. PyDoc_STR("(FInfo fndrInfo) -> None")},
  1214. {"FSpSetFLock", (PyCFunction)FSSpec_FSpSetFLock, 1,
  1215. PyDoc_STR("() -> None")},
  1216. {"FSpRstFLock", (PyCFunction)FSSpec_FSpRstFLock, 1,
  1217. PyDoc_STR("() -> None")},
  1218. {"FSpRename", (PyCFunction)FSSpec_FSpRename, 1,
  1219. PyDoc_STR("(Str255 newName) -> None")},
  1220. {"FSpCatMove", (PyCFunction)FSSpec_FSpCatMove, 1,
  1221. PyDoc_STR("(FSSpec dest) -> None")},
  1222. {"FSpExchangeFiles", (PyCFunction)FSSpec_FSpExchangeFiles, 1,
  1223. PyDoc_STR("(FSSpec dest) -> None")},
  1224. {"FSpMakeFSRef", (PyCFunction)FSSpec_FSpMakeFSRef, 1,
  1225. PyDoc_STR("() -> (FSRef newRef)")},
  1226. {"NewAliasMinimal", (PyCFunction)FSSpec_NewAliasMinimal, 1,
  1227. PyDoc_STR("() -> (AliasHandle alias)")},
  1228. {"IsAliasFile", (PyCFunction)FSSpec_IsAliasFile, 1,
  1229. PyDoc_STR("() -> (Boolean aliasFileFlag, Boolean folderFlag)")},
  1230. {"as_pathname", (PyCFunction)FSSpec_as_pathname, 1,
  1231. PyDoc_STR("() -> string")},
  1232. {"as_tuple", (PyCFunction)FSSpec_as_tuple, 1,
  1233. PyDoc_STR("() -> (vRefNum, dirID, name)")},
  1234. {NULL, NULL, 0}
  1235. };
  1236. static PyObject *FSSpec_get_data(FSSpecObject *self, void *closure)
  1237. {
  1238. return PyString_FromStringAndSize((char *)&self->ob_itself, sizeof(self->ob_itself));
  1239. }
  1240. #define FSSpec_set_data NULL
  1241. static PyGetSetDef FSSpec_getsetlist[] = {
  1242. {"data", (getter)FSSpec_get_data, (setter)FSSpec_set_data, "Raw data of the FSSpec object"},
  1243. {NULL, NULL, NULL, NULL},
  1244. };
  1245. #define FSSpec_compare NULL
  1246. static PyObject * FSSpec_repr(FSSpecObject *self)
  1247. {
  1248. char buf[512];
  1249. PyOS_snprintf(buf, sizeof(buf), "%s((%d, %ld, '%.*s'))",
  1250. self->ob_type->tp_name,
  1251. self->ob_itself.vRefNum,
  1252. self->ob_itself.parID,
  1253. self->ob_itself.name[0], self->ob_itself.name+1);
  1254. return PyString_FromString(buf);
  1255. }
  1256. #define FSSpec_hash NULL
  1257. static int FSSpec_tp_init(PyObject *_self, PyObject *_args, PyObject *_kwds)
  1258. {
  1259. PyObject *v = NULL;
  1260. char *rawdata = NULL;
  1261. int rawdatalen = 0;
  1262. static char *kw[] = {"itself", "rawdata", 0};
  1263. if (!PyArg_ParseTupleAndKeywords(_args, _kwds, "|Os#", kw, &v, &rawdata, &rawdatalen))
  1264. return -1;
  1265. if (v && rawdata)
  1266. {
  1267. PyErr_SetString(PyExc_TypeError, "Only one of itself or rawdata may be specified");
  1268. return -1;
  1269. }
  1270. if (!v && !rawdata)
  1271. {
  1272. PyErr_SetString(PyExc_TypeError, "One of itself or rawdata must be specified");
  1273. return -1;
  1274. }
  1275. if (rawdata)
  1276. {
  1277. if (rawdatalen != sizeof(FSSpec))
  1278. {
  1279. PyErr_SetString(PyExc_TypeError, "FSSpec rawdata incorrect size");
  1280. return -1;
  1281. }
  1282. memcpy(&((FSSpecObject *)_self)->ob_itself, rawdata, rawdatalen);
  1283. return 0;
  1284. }
  1285. if (PyMac_GetFSSpec(v, &((FSSpecObject *)_self)->ob_itself)) return 0;
  1286. return -1;
  1287. }
  1288. #define FSSpec_tp_alloc PyType_GenericAlloc
  1289. static PyObject *FSSpec_tp_new(PyTypeObject *type, PyObject *_args, PyObject *_kwds)
  1290. {
  1291. PyObject *self;
  1292. if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;
  1293. memset(&((FSSpecObject *)self)->ob_itself, 0, sizeof(FSSpec));
  1294. return self;
  1295. }
  1296. #define FSSpec_tp_free PyObject_Del
  1297. static PyTypeObject FSSpec_Type = {
  1298. PyObject_HEAD_INIT(NULL)
  1299. 0, /*ob_size*/
  1300. "Carbon.File.FSSpec", /*tp_name*/
  1301. sizeof(FSSpecObject), /*tp_basicsize*/
  1302. 0, /*tp_itemsize*/
  1303. /* methods */
  1304. (destructor) FSSpec_dealloc, /*tp_dealloc*/
  1305. 0, /*tp_print*/
  1306. (getattrfunc)0, /*tp_getattr*/
  1307. (setattrfunc)0, /*tp_setattr*/
  1308. (cmpfunc) FSSpec_compare, /*tp_compare*/
  1309. (reprfunc) FSSpec_repr, /*tp_repr*/
  1310. (PyNumberMethods *)0, /* tp_as_number */
  1311. (PySequenceMethods *)0, /* tp_as_sequence */
  1312. (PyMappingMethods *)0, /* tp_as_mapping */
  1313. (hashfunc) FSSpec_hash, /*tp_hash*/
  1314. 0, /*tp_call*/
  1315. 0, /*tp_str*/
  1316. PyObject_GenericGetAttr, /*tp_getattro*/
  1317. PyObject_GenericSetAttr, /*tp_setattro */
  1318. 0, /*tp_as_buffer*/
  1319. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
  1320. 0, /*tp_doc*/
  1321. 0, /*tp_traverse*/
  1322. 0, /*tp_clear*/
  1323. 0, /*tp_richcompare*/
  1324. 0, /*tp_weaklistoffset*/
  1325. 0, /*tp_iter*/
  1326. 0, /*tp_iternext*/
  1327. FSSpec_methods, /* tp_methods */
  1328. 0, /*tp_members*/
  1329. FSSpec_getsetlist, /*tp_getset*/
  1330. 0, /*tp_base*/
  1331. 0, /*tp_dict*/
  1332. 0, /*tp_descr_get*/
  1333. 0, /*tp_descr_set*/
  1334. 0, /*tp_dictoffset*/
  1335. FSSpec_tp_init, /* tp_init */
  1336. FSSpec_tp_alloc, /* tp_alloc */
  1337. FSSpec_tp_new, /* tp_new */
  1338. FSSpec_tp_free, /* tp_free */
  1339. };
  1340. #endif /* !__LP64__ */
  1341. /* --------------------- End object type FSSpec --------------------- */
  1342. /* ----------------------- Object type FSRef ------------------------ */
  1343. static PyTypeObject FSRef_Type;
  1344. #define FSRef_Check(x) ((x)->ob_type == &FSRef_Type || PyObject_TypeCheck((x), &FSRef_Type))
  1345. typedef struct FSRefObject {
  1346. PyObject_HEAD
  1347. FSRef ob_itself;
  1348. } FSRefObject;
  1349. static PyObject *FSRef_New(FSRef *itself)
  1350. {
  1351. FSRefObject *it;
  1352. if (itself == NULL) return PyMac_Error(resNotFound);
  1353. it = PyObject_NEW(FSRefObject, &FSRef_Type);
  1354. if (it == NULL) return NULL;
  1355. it->ob_itself = *itself;
  1356. return (PyObject *)it;
  1357. }
  1358. static void FSRef_dealloc(FSRefObject *self)
  1359. {
  1360. /* Cleanup of self->ob_itself goes here */
  1361. self->ob_type->tp_free((PyObject *)self);
  1362. }
  1363. static PyObject *FSRef_FSMakeFSRefUnicode(FSRefObject *_self, PyObject *_args)
  1364. {
  1365. PyObject *_res = NULL;
  1366. OSErr _err;
  1367. UniChar *nameLength__in__;
  1368. UniCharCount nameLength__len__;
  1369. int nameLength__in_len__;
  1370. TextEncoding textEncodingHint;
  1371. FSRef newRef;
  1372. if (!PyArg_ParseTuple(_args, "u#l",
  1373. &nameLength__in__, &nameLength__in_len__,
  1374. &textEncodingHint))
  1375. return NULL;
  1376. nameLength__len__ = nameLength__in_len__;
  1377. _err = FSMakeFSRefUnicode(&_self->ob_itself,
  1378. nameLength__len__, nameLength__in__,
  1379. textEncodingHint,
  1380. &newRef);
  1381. if (_err != noErr) return PyMac_Error(_err);
  1382. _res = Py_BuildValue("O&",
  1383. FSRef_New, &newRef);
  1384. return _res;
  1385. }
  1386. static PyObject *FSRef_FSCompareFSRefs(FSRefObject *_self, PyObject *_args)
  1387. {
  1388. PyObject *_res = NULL;
  1389. OSErr _err;
  1390. FSRef ref2;
  1391. if (!PyArg_ParseTuple(_args, "O&",
  1392. FSRef_Convert, &ref2))
  1393. return NULL;
  1394. _err = FSCompareFSRefs(&_self->ob_itself,
  1395. &ref2);
  1396. if (_err != noErr) return PyMac_Error(_err);
  1397. Py_INCREF(Py_None);
  1398. _res = Py_None;
  1399. return _res;
  1400. }
  1401. static PyObject *FSRef_FSCreateFileUnicode(FSRefObject *_self, PyObject *_args)
  1402. {
  1403. PyObject *_res = NULL;
  1404. OSErr _err;
  1405. UniChar *nameLength__in__;
  1406. UniCharCount nameLength__len__;
  1407. int nameLength__in_len__;
  1408. FSCatalogInfoBitmap whichInfo;
  1409. FSCatalogInfo catalogInfo;
  1410. FSRef newRef;
  1411. #ifndef __LP64__
  1412. FSSpec newSpec;
  1413. #endif
  1414. if (!PyArg_ParseTuple(_args, "u#lO&",
  1415. &nameLength__in__, &nameLength__in_len__,
  1416. &whichInfo,
  1417. FSCatalogInfo_Convert, &catalogInfo))
  1418. return NULL;
  1419. nameLength__len__ = nameLength__in_len__;
  1420. _err = FSCreateFileUnicode(&_self->ob_itself,
  1421. nameLength__len__, nameLength__in__,
  1422. whichInfo,
  1423. &catalogInfo,
  1424. &newRef,
  1425. #ifndef __LP64__
  1426. &newSpec
  1427. #else /* __LP64__ */
  1428. NULL
  1429. #endif /* __LP64__*/
  1430. );
  1431. if (_err != noErr) return PyMac_Error(_err);
  1432. #ifndef __LP64__
  1433. _res = Py_BuildValue("O&O&",
  1434. FSRef_New, &newRef,
  1435. FSSpec_New, &newSpec);
  1436. #else /* __LP64__ */
  1437. _res = Py_BuildValue("O&O", FSRef_New, &newRef, Py_None);
  1438. #endif /* __LP64__ */
  1439. return _res;
  1440. }
  1441. static PyObject *FSRef_FSCreateDirectoryUnicode(FSRefObject *_self, PyObject *_args)
  1442. {
  1443. PyObject *_res = NULL;
  1444. OSErr _err;
  1445. UniChar *nameLength__in__;
  1446. UniCharCount nameLength__len__;
  1447. int nameLength__in_len__;
  1448. FSCatalogInfoBitmap whichInfo;
  1449. FSCatalogInfo catalogInfo;
  1450. FSRef newRef;
  1451. #ifndef __LP64__
  1452. FSSpec newSpec;
  1453. #endif /* !__LP64__ */
  1454. UInt32 newDirID;
  1455. if (!PyArg_ParseTuple(_args, "u#lO&",
  1456. &nameLength__in__, &nameLength__in_len__,
  1457. &whichInfo,
  1458. FSCatalogInfo_Convert, &catalogInfo))
  1459. return NULL;
  1460. nameLength__len__ = nameLength__in_len__;
  1461. _err = FSCreateDirectoryUnicode(&_self->ob_itself,
  1462. nameLength__len__, nameLength__in__,
  1463. whichInfo,
  1464. &catalogInfo,
  1465. &newRef,
  1466. #ifndef __LP64__
  1467. &newSpec,
  1468. #else /* !__LP64__ */
  1469. NULL,
  1470. #endif /* !__LP64__ */
  1471. &newDirID);
  1472. if (_err != noErr) return PyMac_Error(_err);
  1473. #ifndef __LP64__
  1474. _res = Py_BuildValue("O&O&l",
  1475. FSRef_New, &newRef,
  1476. FSSpec_New, &newSpec,
  1477. newDirID);
  1478. #else /* __LP64__ */
  1479. _res = Py_BuildValue("O&Ol",
  1480. FSRef_New, &newRef,
  1481. Py_None,
  1482. newDirID);
  1483. #endif /* __LP64__ */
  1484. return _res;
  1485. }
  1486. static PyObject *FSRef_FSDeleteObject(FSRefObject *_self, PyObject *_args)
  1487. {
  1488. PyObject *_res = NULL;
  1489. OSErr _err;
  1490. if (!PyArg_ParseTuple(_args, ""))
  1491. return NULL;
  1492. _err = FSDeleteObject(&_self->ob_itself);
  1493. if (_err != noErr) return PyMac_Error(_err);
  1494. Py_INCREF(Py_None);
  1495. _res = Py_None;
  1496. return _res;
  1497. }
  1498. static PyObject *FSRef_FSMoveObject(FSRefObject *_self, PyObject *_args)
  1499. {
  1500. PyObject *_res = NULL;
  1501. OSErr _err;
  1502. FSRef destDirectory;
  1503. FSRef newRef;
  1504. if (!PyArg_ParseTuple(_args, "O&",
  1505. FSRef_Convert, &destDirectory))
  1506. return NULL;
  1507. _err = FSMoveObject(&_self->ob_itself,
  1508. &destDirectory,
  1509. &newRef);
  1510. if (_err != noErr) return PyMac_Error(_err);
  1511. _res = Py_BuildValue("O&",
  1512. FSRef_New, &newRef);
  1513. return _res;
  1514. }
  1515. static PyObject *FSRef_FSExchangeObjects(FSRefObject *_self, PyObject *_args)
  1516. {
  1517. PyObject *_res = NULL;
  1518. OSErr _err;
  1519. FSRef destRef;
  1520. if (!PyArg_ParseTuple(_args, "O&",
  1521. FSRef_Convert, &destRef))
  1522. return NULL;
  1523. _err = FSExchangeObjects(&_self->ob_itself,
  1524. &destRef);
  1525. if (_err != noErr) return PyMac_Error(_e