/Mac/Modules/cf/cfscan.py

http://unladen-swallow.googlecode.com/ · Python · 141 lines · 105 code · 15 blank · 21 comment · 10 complexity · 8e557273918ab4cf8b2d726fd752dbbd MD5 · raw file

  1. # Scan an Apple header file, generating a Python file of generator calls.
  2. import sys
  3. from bgenlocations import TOOLBOXDIR, BGENDIR
  4. sys.path.append(BGENDIR)
  5. from scantools import Scanner_OSX
  6. LONG = "CoreFoundation"
  7. SHORT = "cf"
  8. OBJECTS = ("CFTypeRef",
  9. "CFArrayRef", "CFMutableArrayRef",
  10. "CFDataRef", "CFMutableDataRef",
  11. "CFDictionaryRef", "CFMutableDictionaryRef",
  12. "CFStringRef", "CFMutableStringRef",
  13. "CFURLRef",
  14. ## "CFPropertyListRef",
  15. )
  16. # ADD object typenames here
  17. def main():
  18. input = [
  19. "CFBase.h",
  20. "CFArray.h",
  21. ## "CFBag.h",
  22. ## "CFBundle.h",
  23. ## "CFCharacterSet.h",
  24. "CFData.h",
  25. ## "CFDate.h",
  26. "CFDictionary.h",
  27. ## "CFNumber.h",
  28. ## "CFPlugIn.h",
  29. "CFPreferences.h",
  30. "CFPropertyList.h",
  31. ## "CFSet.h",
  32. "CFString.h",
  33. ## "CFStringEncodingExt.h",
  34. ## "CFTimeZone.h",
  35. "CFURL.h",
  36. ]
  37. output = SHORT + "gen.py"
  38. defsoutput = TOOLBOXDIR + LONG + ".py"
  39. scanner = MyScanner(input, output, defsoutput)
  40. scanner.scan()
  41. scanner.gentypetest(SHORT+"typetest.py")
  42. scanner.close()
  43. print "=== Testing definitions output code ==="
  44. execfile(defsoutput, {}, {})
  45. print "=== Done scanning and generating, now importing the generated code... ==="
  46. exec "import " + SHORT + "support"
  47. print "=== Done. It's up to you to compile it now! ==="
  48. class MyScanner(Scanner_OSX):
  49. def destination(self, type, name, arglist):
  50. classname = "Function"
  51. listname = "functions"
  52. if arglist and name[:13] != 'CFPreferences':
  53. t, n, m = arglist[0]
  54. if t in OBJECTS and m == "InMode":
  55. classname = "Method"
  56. listname = t + "_methods"
  57. # Special case for the silly first AllocatorRef argument
  58. if t == 'CFAllocatorRef' and m == 'InMode' and len(arglist) > 1:
  59. t, n, m = arglist[1]
  60. if t in OBJECTS and m == "InMode":
  61. classname = "MethodSkipArg1"
  62. listname = t + "_methods"
  63. return classname, listname
  64. def writeinitialdefs(self):
  65. self.defsfile.write("def FOUR_CHAR_CODE(x): return x\n")
  66. def makeblacklistnames(self):
  67. return [
  68. # Memory allocator functions
  69. "CFAllocatorGetDefault",
  70. "CFAllocatorSetDefault",
  71. "CFAllocatorAllocate",
  72. "CFAllocatorReallocate",
  73. "CFAllocatorDeallocate",
  74. "CFGetAllocator",
  75. # Array functions we skip for now.
  76. "CFArrayGetValueAtIndex",
  77. # Data pointer functions. Skip for now.
  78. "CFDataGetBytePtr",
  79. "CFDataGetMutableBytePtr",
  80. "CFDataGetBytes", # XXXX Should support this one
  81. # String functions
  82. "CFStringGetPascalString", # Use the C-string methods.
  83. "CFStringGetPascalStringPtr", # TBD automatically
  84. "CFStringGetCStringPtr",
  85. "CFStringGetCharactersPtr",
  86. "CFStringGetCString",
  87. "CFStringGetCharacters",
  88. "CFURLCreateStringWithFileSystemPath", # Gone in later releases
  89. "CFStringCreateMutableWithExternalCharactersNoCopy", # Not a clue...
  90. "CFStringSetExternalCharactersNoCopy",
  91. "CFStringGetCharacterAtIndex", # No format for single unichars yet.
  92. "kCFStringEncodingInvalidId", # incompatible constant declaration
  93. "CFPropertyListCreateFromXMLData", # Manually generated
  94. ]
  95. def makegreylist(self):
  96. return []
  97. def makeblacklisttypes(self):
  98. return [
  99. "CFComparatorFunction", # Callback function pointer
  100. "CFAllocatorContext", # Not interested in providing our own allocator
  101. "void_ptr_ptr", # Tricky. This is the initializer for arrays...
  102. "void_ptr", # Ditto for various array lookup methods
  103. "CFArrayApplierFunction", # Callback function pointer
  104. "CFDictionaryApplierFunction", # Callback function pointer
  105. "va_list", # For printf-to-a-cfstring. Use Python.
  106. "const_CFStringEncoding_ptr", # To be done, I guess
  107. ]
  108. def makerepairinstructions(self):
  109. return [
  110. # Buffers in CF seem to be passed as UInt8 * normally.
  111. ([("UInt8_ptr", "*", "InMode"), ("CFIndex", "*", "InMode")],
  112. [("UcharInBuffer", "*", "*")]),
  113. ([("UniChar_ptr", "*", "InMode"), ("CFIndex", "*", "InMode")],
  114. [("UnicodeInBuffer", "*", "*")]),
  115. # Some functions return a const char *. Don't worry, we won't modify it.
  116. ([("const_char_ptr", "*", "ReturnMode")],
  117. [("return_stringptr", "*", "*")]),
  118. # base URLs are optional (pass None for NULL)
  119. ([("CFURLRef", "baseURL", "InMode")],
  120. [("OptionalCFURLRef", "*", "*")]),
  121. # We handle CFPropertyListRef objects as plain CFTypeRef
  122. ([("CFPropertyListRef", "*", "*")],
  123. [("CFTypeRef", "*", "*")]),
  124. ]
  125. if __name__ == "__main__":
  126. main()