objp.php 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675
  1. <?php
  2. // Includes
  3. require_once("utilities.php");
  4. require_once("objp_base.php");
  5. // Constants
  6. define("CAST_HANDLE", true);
  7. define("DONT_CAST_HANDLE", false);
  8. define("ACCESS_HANDLE_DIRECT", 1);
  9. define("ACCESS_HANDLE_FUNCTION", 2);
  10. define("REGISTER_SEL", true);
  11. define("DONT_REGISTER_SEL", false);
  12. define("USE_HANDLE", true);
  13. define("DONT_USE_HANDLE", false);
  14. class ObjectivePParser extends ObjectivePParserBase {
  15. /**
  16. * UTILITIES
  17. */
  18. // Skips blocks in the current file being parsed
  19. function SkipBlock ($line) {
  20. if ($line != "") {
  21. foreach ($this->skip_blocks as $key => $value) {
  22. if (@ereg($key, $line)) {
  23. $this->parser_skipping = true;
  24. }
  25. if (@ereg($value, $line)) {
  26. $this->parser_skipping = false;
  27. }
  28. }
  29. }
  30. return $this->parser_skipping;
  31. }
  32. function IsKeywordReserved($keyword) {
  33. $keyword = strtolower($keyword);
  34. if (in_array($keyword, $this->reserved_keywords)) return true;
  35. if (($this->current_class != null) &&
  36. ($this->dump["master"][$this->current_class["name"]] ["field_names"] != null) &&
  37. in_array($keyword, $this->dump["master"][$this->current_class["name"]] ["field_names"])) return true;
  38. }
  39. // Extracts the name from an Objective-C method definition
  40. function ExtractObjcMethodName ($method) {
  41. // cut out comments first
  42. $method = eregi_replace("(/\*.*\*/)", "", $method);
  43. $method = eregi_replace("//.*$", "", $method);
  44. $method = trim($method, " ");
  45. $params = explode(":", $method);
  46. $name = "";
  47. if (count($params) > 1) {
  48. foreach ($params as $value) {
  49. $value = trim($value, " ");
  50. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $value, $captures)) $name .= $captures[1].":";
  51. /*
  52. // paremeter with no label (or first parameter)
  53. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*([a-zA-Z0-9]+)[[:space:]]*$", $value, $captures)) {
  54. if ($name != "") {
  55. $name .= ":";
  56. } else {
  57. $name .= $captures[1].":";
  58. }
  59. continue;
  60. }
  61. // parameter with label
  62. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*[a-zA-Z0-9_]+[[:space:]]+([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1].":";
  63. */
  64. }
  65. } else {
  66. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*(;)*$", $method, $captures)) $name = $captures[1];
  67. }
  68. return $name;
  69. }
  70. // in case of a non-function pointer and non-pointer type,
  71. // split a C field expression (e.g. "long afield" or
  72. // "long int :32") into its type and field name. Necessary
  73. // to recognise name that "long int :32" means "a field
  74. // without a name whose type/alignment is "long int" and that is
  75. // bitpacked
  76. function ExtractCFieldSimpleTypeAndSingleName($param) {
  77. // print("Converting field $param\n");
  78. // remove all "const" occurrences to simplify matching
  79. $clean_param = str_replace_word("const", "", $param);
  80. $got_type = false;
  81. // assume not a pointer type
  82. if (preg_match("!^\s*(unsigned|signed)\b!", $clean_param, $signedunsigned)) {
  83. $got_type = true;
  84. $clean_param = preg_replace("!^\s*(unsigned|signed)\b!", "", $clean_param);
  85. }
  86. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", $clean_param, $inttype)) {
  87. $got_type = true;
  88. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", "", $clean_param);
  89. }
  90. if ($got_type) {
  91. // remove bitfield and array, are encoded later
  92. $result["name"] = trim(preg_replace("!(?::.*)?(?:\[[^]]*\])?!","",$clean_param));
  93. $result["type"] = trim(preg_replace("!\s+!", " ", $signedunsigned[0] . " " . $inttype[0]));
  94. } else {
  95. // remove "struct"
  96. $clean_param = str_replace_word("struct", "", $clean_param);
  97. // has to be "type fieldname"
  98. preg_match("!^\s*(\w+)\b\s*(\w+)(?:\[[0-9]*\])?\s*!", $clean_param, $matches);
  99. $result["name"] = $matches[2]; // can be empty
  100. $result["type"] = $matches[1];
  101. }
  102. // print("field conversion result: ".$result["name"].": ".$result["type"]."\n");
  103. return $result;
  104. }
  105. function ExtractCParaNameAndType($param) {
  106. // print("Converting $param\n");
  107. // remove all "const" occurrences to simplify matching
  108. $clean_param = str_replace_word("const", "", $param);
  109. // remove all spaces between multiple pointer modifiers
  110. $clean_param = trim(preg_replace("![*]\s+[*]!", "**", $clean_param));
  111. // 1) varargs
  112. if ($clean_param == "...") {
  113. $result["name"] = "";
  114. $result["pointermods"] = "";
  115. $result["type"] = "...";
  116. // 2) type is a function pointer (does not yet match arrays of function
  117. // pointers!)
  118. } elseif (preg_match($this->pregex_function_pointer_c_paratype, $clean_param, $matches)) {
  119. $result["name"] = $matches[3];
  120. // not correct; simply assume no "pointer to function pointer" types are
  121. // used
  122. $result["pointermods"] = "";
  123. $funcptrtype = $this->ParseFunctionDeclaration($matches[1], $matches[2], "", $matches[4], false, "");
  124. $result["type"] = $this->AddCallback($matches[3], $funcptrtype);
  125. // 3) any other pointer type (including arrays)
  126. } elseif (preg_match("!^([^*]*)([*]+)\s*(\w+(?:\[[0-9]*\])?)?\s*$!", $clean_param, $matches)){
  127. $result["name"] = $matches[3]; // can be empty
  128. $result["pointermods"] = $matches[2];
  129. $result["type"] = trim($matches[1]);
  130. // 4) basic C type (including arrays)
  131. } else {
  132. // definitely not a pointer type
  133. $result["pointermods"] = "";
  134. $got_type = false;
  135. if (preg_match("!^\s*(unsigned|signed)!", $clean_param, $signedunsigned)) {
  136. $got_type = true;
  137. $clean_param = preg_replace("!^\s*(unsigned|signed)!", "", $clean_param);
  138. }
  139. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", $clean_param, $inttype)) {
  140. $got_type = true;
  141. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", "", $clean_param);
  142. }
  143. if ($got_type) {
  144. $result["name"] = trim($clean_param);
  145. $result["type"] = trim($signedunsigned[0] . " " . $inttype[0]);
  146. } else {
  147. // remove "struct"
  148. $clean_param = str_replace_word("struct", "", $clean_param);
  149. // has to be "type paraname", or just "type"
  150. preg_match("!^\s*(\w+)\b\s*(\w+(?:\[[0-9]*\])?)?\s*!", $clean_param, $matches);
  151. $result["name"] = $matches[2]; // can be empty
  152. $result["type"] = $matches[1];
  153. }
  154. }
  155. // print("param conversion result: ".$result["name"].": ".$result["pointermods"].$result["type"]."\n");
  156. return $result;
  157. }
  158. // Returns the parameter modifier string for a callback parameter type
  159. function GetCallbackParameterModifiers ($type, $name) {
  160. $modifiers = "";
  161. $type = trim($type, "*");
  162. // if the name starts with * it's a pointer
  163. // don't process framework classes since they're always pointers
  164. if (ereg("^[[:space:]]*\*", $name)) {
  165. if (!in_array($type, $this->cocoa_classes)) $modifiers = "var ";
  166. }
  167. // double ** are always var
  168. if (ereg("^[[:space:]]*\*\*", $name)) {
  169. $modifiers = "var ";
  170. }
  171. return $modifiers;
  172. }
  173. // Print string to output file
  174. function PrintOutput ($indent, $string) {
  175. for ($i=0; $i < $indent; $i++) {
  176. $indent_string .= " ";
  177. }
  178. if (($this->output) && (!$this->show)) fwrite($this->output, "$indent_string$string\n");
  179. if ($this->show) print("$indent_string$string\n");
  180. }
  181. // Returns a class hierarchy array
  182. function GetClassHierarchy ($class, &$hierarchy) {
  183. if (!$hierarchy) $hierarchy = array();
  184. $hierarchy[] = $class["name"];
  185. if ($class["super_class"]) {
  186. $hierarchy[] = $this->GetClassHierarchy($class["super_class"], $hierarchy);
  187. } else {
  188. $hierarchy[] = "NSObject";
  189. }
  190. return $class["name"];
  191. }
  192. // Returns all protected keywords in a class hierarchy
  193. function GetProtectedKeywords ($in_class) {
  194. $this->GetClassHierarchy($in_class, $hierarchy);
  195. $keywords = array();
  196. foreach ($hierarchy as $class) {
  197. if ($this->dump["master"][$class]["protected_keywords"]) {
  198. foreach ($this->dump["master"][$class]["protected_keywords"] as $keyword) $keywords[] = strtolower($keyword);
  199. }
  200. }
  201. return $keywords;
  202. }
  203. // Gets the preferred property name from attributes
  204. function GetPropertyName ($kind, $params, &$name) {
  205. foreach ($params as $value) {
  206. $pair = explode("=", $value);
  207. if ($pair[0] == $kind) {
  208. $name = $pair[1];
  209. return true;
  210. break;
  211. }
  212. }
  213. }
  214. // Checks if a line has a property and returns the parts
  215. function LineHasProperty ($line, &$parts) {
  216. if (eregi($this->regex_objc_property_attributes, $line, $captures)) {
  217. $parts = $captures;
  218. //print_r($parts);
  219. return true;
  220. } elseif (eregi($this->regex_objc_property, $line, $captures)) {
  221. $parts = $captures;
  222. //print_r($parts);
  223. return true;
  224. }
  225. }
  226. // Returns header a category should be moved to
  227. function FindCategoryHeader ($category) {
  228. foreach ($this->dump as $name => $header) {
  229. if ((@array_key_exists($category, $header["classes"])) && ($category != "NSObject")) {
  230. return $name;
  231. }
  232. }
  233. }
  234. // Checks if $class (array) contains $method (array)
  235. function ClassContainsMethod ($class, $method) {
  236. if ($class["methods"]) {
  237. foreach ($class["methods"] as $key) {
  238. if ($key["name"] == $method["name"]) return true;
  239. }
  240. }
  241. }
  242. function GetAnonBitFieldName() {
  243. $name = "_anonbitfield_".$this->current_header["name_clean"].$this->current_header["anonbitfields"];
  244. $this->current_header["anonbitfields"]++;
  245. return $name;
  246. }
  247. // create a variant record with a first anonymous field of type
  248. // first_bitpacked_type so that the compiler may correctly align it
  249. // (it does not do that currently, but this at least provides a
  250. // hint to the programmer)
  251. function BitPackedForceAlignment($first_bitpacked_type, $firstindent, $otherindents) {
  252. $result = $firstindent . "case byte of\n" . $otherindents ."0: (" . $this->GetAnonBitFieldName() . ": $first_bitpacked_type);\n" . $otherindents . "1: (";
  253. return $result;
  254. }
  255. function EncodePointerModifiers($type, $pointertype) {
  256. if ($pointertype[0] == "*") $type = $this->ReplacePointerType($type);
  257. if ($pointertype[1] == "*") {
  258. if ($type == "Pointer") {
  259. $type = "PPointer";
  260. } elseif ($type == "PChar") {
  261. $type = "PPChar";
  262. } elseif (in_array($type, $this->cocoa_classes)) {
  263. $type = $type.$this->class_pointer_suffix;
  264. } else {
  265. // to match MacOSAll types
  266. $type = $type."Ptr";
  267. }
  268. }
  269. return $type;
  270. }
  271. /**
  272. * ADDING LANGUAGE STRUCTURE UTILITIES
  273. */
  274. // Adds a method structure to a class and performs checks for overloaded methods
  275. function AddMethodToClass (&$method, &$class) {
  276. // ignore methods
  277. // if (in_array($method["name"], $this->ignore_methods)) return false;
  278. // add comment to the method
  279. $method["comment"] = $this->InsertCurrentComment();
  280. if (@!in_array($method["name"], $class["declared_methods"])) {
  281. $class["all"][$method["name"]] = $method;
  282. $class["protected_keywords"][] = $method["name"];
  283. $class["declared_methods"][] = $method["name"];
  284. $this->dump["all_methods"][$class["name"]][] = $method["objc_method"];
  285. if ($this->show_added_messages) print(" @ Added ".$method["name"]." to ".$class["name"]."\n");
  286. $this->method_count ++;
  287. return true;
  288. } else {
  289. print(" ! ".$method["def"]." already exists in ".$class["name"]." defined as ".$class["all"][$method["name"]]["def"]."\n");
  290. }
  291. }
  292. // Adds a typedef to the header and handles organization to prevent order conflicts
  293. function AddTypeDef (&$header, $typedef) {
  294. //$this->AppendCurrentMacro($header["types"]["typedef"]);
  295. $this->AppendCurrentComment($header["types"]["typedef"]);
  296. if ($this->comment_terminated) $header["types"]["typedef"][] = $this->InsertCurrentComment();
  297. $header["types"]["typedef"][] = $typedef;
  298. }
  299. // adds a function callback type with name $name, and returns
  300. // the name of the callback type
  301. function AddCallback($name, $function_pointer) {
  302. if ($this->current_header) {
  303. $type = $this->current_header["name_clean"].ucwords($name);
  304. if (!@in_array($function_pointer, $this->current_header["types"]["callbacks"])) {
  305. $count = 0;
  306. while (@array_key_exists($type, $this->current_header["types"]["callbacks"])) {
  307. $count ++;
  308. $type = "$type$count";
  309. }
  310. // append the new type to the the current class
  311. $this->current_header["types"]["callbacks"][$type] = $function_pointer;
  312. } else {
  313. // Use the name of the existing callback of matching type
  314. $type = array_search($function_pointer, $this->current_header["types"]["callbacks"]);
  315. }
  316. return $type;
  317. }
  318. return "invalid_callback_type_because_no_current_header";
  319. }
  320. /**
  321. * OBJC FORMATTING UTILITIES
  322. */
  323. // Performs additional formatting on Objective-c type i.e. (out NSObject **)
  324. function FormatObjcType ($type, &$modifiers) {
  325. $modifiers = "";
  326. // toss out all const identifiers
  327. $type = istr_replace_word("const", "", $type);
  328. // replace inout paramaters
  329. $type = istr_replace_word("inout", "", $type);
  330. $type = istr_replace_word("out", "", $type);
  331. $type_clean = trim($type, "* ");
  332. // Replace types before cleaning
  333. $type = $this->ReplaceObjcType($type);
  334. // Remove protocol which type conforms to (id <NSURLHandleClient>)
  335. $type = eregi_replace("<.*>", "", $type);
  336. // Remove array brackets (NSUInteger[])p
  337. $type = eregi_replace("\[[0-9]*\]", "", $type);
  338. // var params to non-object types (NSRange *)
  339. if (ereg("([a-zA-Z0-9_]+)[[:space:]]*[*]+$", $type, $captures)) {
  340. if (!in_array($captures[1], $this->cocoa_classes)) {
  341. $type = $this->ReplaceReferenceParameterType($type_clean);
  342. //$modifiers = "var ";
  343. }
  344. }
  345. // Handle NS*** pointers (NSError **)
  346. if (ereg("(NS[a-zA-Z0-9_]+)[[:space:]]*\*\*$", $type, $captures)) {
  347. if (in_array($captures[1], $this->cocoa_classes)) {
  348. $type = "$type_clean$this->class_pointer_suffix";
  349. }
  350. }
  351. // clean the type
  352. $type = trim($type, "* ");
  353. //print("$type\n");
  354. return $type;
  355. }
  356. // Performs additional formatting on Objective-c parameter types
  357. function FormatObjcParams ($string, &$variable_arguments) {
  358. $params = explode(":", $string);
  359. $string = "";
  360. $variable_arguments = false;
  361. // print_r($params);
  362. if (count($params) > 0) {
  363. foreach ($params as $value) {
  364. // parameter is varargs
  365. if (eregi("(.*),[[:space:]]*\.\.\.", $value)) {
  366. $string .= ":(id)$this->varargs_param_name";
  367. $variable_arguments = true;
  368. //print("$value\n");
  369. continue;
  370. }
  371. if (preg_match("!^[^(]*$!",$value)) {
  372. // parameter without type -> id
  373. $value = "(id)".$value;
  374. }
  375. // normal parameter
  376. if (ereg("\((.*)\)", $value, $captures)) {
  377. $new_value = $this->ReplaceObjcType($captures[1]);
  378. if ($new_value != $captures[1]) $value = ereg_replace("\((.*)\)", "($new_value)", $value);
  379. $string .= ":$value";
  380. continue;
  381. }
  382. }
  383. }
  384. $string = ltrim($string, ":");
  385. return $string;
  386. }
  387. /**
  388. * SYNTAX MAKING UTILITIES
  389. */
  390. // Makes a paramater list string with options to modify
  391. function MakeParamList ($param_array, $use_handle, $cast_handle, $direct, $register_selector) {
  392. $params = "";
  393. foreach ($param_array as $pair) {
  394. // register selector parameters
  395. if (($register_selector) && ($pair["type"] == "SEL")) {
  396. $params .= "sel_registerName(".$pair["name"]."), ";
  397. continue;
  398. }
  399. // use the object handle for NSObject descendants
  400. if ($use_handle) {
  401. if (in_array($pair["type"], $this->cocoa_classes)) {
  402. // cast the param to the original class type
  403. if ($cast_handle) {
  404. if ($direct == ACCESS_HANDLE_DIRECT) {
  405. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  406. } else {
  407. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  408. }
  409. } else {
  410. if ($direct == ACCESS_HANDLE_DIRECT) {
  411. $params .= $pair["name"].".Handle, ";
  412. } else {
  413. $params .= "GetHandle(".$pair["name"]."), ";
  414. }
  415. }
  416. } else {
  417. if (($this->objects_are_wrappers) && ($pair["type"] == $this->objc_id)) { // id is always a wrapper
  418. if ($direct == ACCESS_HANDLE_DIRECT) {
  419. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  420. } else {
  421. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  422. }
  423. } else {
  424. $params .= $pair["name"].", ";
  425. }
  426. }
  427. } else { // append without modification
  428. $params .= $pair["name"].", ";
  429. }
  430. }
  431. return trim($params, ", ");
  432. }
  433. // Makes a list of paramameter variables with NS*** class types cast to "id" or the original class
  434. function MakeObjcTypeParamList ($param_array, $objc_type) {
  435. $params = "";
  436. foreach ($param_array as $pair) {
  437. if (in_array($pair["type"], $this->cocoa_classes)) {
  438. if ($objc_type) {
  439. $params .= "$this->objc_id(".$pair["name"]."), ";
  440. } else {
  441. $params .= $pair["type"]."(".$pair["name"]."), ";
  442. }
  443. } else {
  444. $params .= $pair["name"].", ";
  445. }
  446. }
  447. return trim($params, ", ");
  448. }
  449. // Makes a struct field into an inline array (or returns field un-changed)
  450. function MakeFieldInlineArray ($io_field, $line, $name, $type) {
  451. if (eregi("\[([0-9]+)\];", $line, $array_size)) {
  452. $length = (int)$array_size[1] - 1;
  453. if ($length > 0) {
  454. $io_field = " $name: array[0..$length] of $type;";
  455. }
  456. }
  457. return $io_field;
  458. }
  459. // Makes a type bitpacked (or returns type un-changed)
  460. function MakeFieldBitPacked ($ioType, $field, &$bitpacked) {
  461. $bitpacked = false;
  462. if (preg_match("!:([0-9]+)\s*(?:__attribute__\(\([^)]*\)\))?\s*;$!", $field, $bitpack)) {
  463. $length = (int)$bitpack[1];
  464. if ($length > 1) {
  465. $ioType = "0..((1 shl $length)-1)";
  466. } else {
  467. $ioType = "0..$length";
  468. }
  469. $bitpacked = true;
  470. }
  471. return $ioType;
  472. }
  473. /**
  474. * REPLACEMENT UTILITIES
  475. */
  476. // Replace type with pointer equivalent
  477. function ReplacePointerType ($type) {
  478. $found = false;
  479. // all classes are implicit pointer types
  480. if (in_array($type, $this->cocoa_classes)) return $type;
  481. // function pointer types are sometimes also implicit
  482. // pointers
  483. if (in_array($type, $this->implicit_function_pointer_types)) return $type;
  484. // PPointer = ^Pointer
  485. if ($type == "Pointer") return "PPointer";
  486. if ($type == "PChar") return "PPChar";
  487. // use preferred pointer type
  488. foreach ($this->pointer_types as $objc_type => $replace_type) {
  489. if ($objc_type == $type) {
  490. $found = true;
  491. $type = $replace_type;
  492. break;
  493. }
  494. }
  495. //$type = "Pointer {".$type."}";
  496. // use generic pointer type
  497. if (!$found)
  498. $type = $type."Ptr";
  499. return $type;
  500. }
  501. // Replace objc type with preferred type
  502. function ReplaceObjcType ($type) {
  503. foreach ($this->replace_types as $objc_type => $replace_type) {
  504. if ($objc_type == $type) {
  505. $type = $replace_type;
  506. break;
  507. }
  508. }
  509. return $type;
  510. }
  511. // Replace garbage collector hints
  512. function ReplaceGarbageCollectorHints ($string, &$io_hint) {
  513. $io_hint = false;
  514. foreach ($this->garbage_collector_hints as $hint) {
  515. $out_string = str_ireplace($hint, "", $string);
  516. if ($out_string != $string) {
  517. $io_hint = $hint;
  518. $string = $out_string;
  519. }
  520. }
  521. return $string;
  522. }
  523. // Replace remote message passing modifiers
  524. function ReplaceRemoteMessagingModifiers ($string, &$io_modifier) {
  525. $io_hint = false;
  526. foreach ($this->remote_messaging_modifiers as $modifier) {
  527. $out_string = preg_replace("!\b".$modifier."\b!", "", $string);
  528. if ($out_string != $string) {
  529. $io_modifier = $modifier;
  530. $string = $out_string;
  531. }
  532. }
  533. return trim($string);
  534. }
  535. // Replace type of reference parameter with pointer
  536. function ReplaceReferenceParameterType ($type) {
  537. $type = $this->ReplaceObjcType($type);
  538. return $this->ReplacePointerType($type);
  539. }
  540. // Replace a framework class with generic id and comment hint
  541. function ReplaceFrameworkClassWithID ($string) {
  542. foreach ($this->cocoa_classes as $class) {
  543. $string = istr_replace_word($class, $this->objc_id, $string);
  544. }
  545. return $string;
  546. }
  547. /**
  548. * MACRO UTILITIES
  549. */
  550. // Insert macro blocks to replace c-style blocks
  551. function InsertMacroBlocks ($line, &$in_macro_block) {
  552. // only insert if we are in a block already.
  553. // NOTE: this does not handle nesting!
  554. if ($in_macro_block) {
  555. // macro else statment
  556. if (eregi("#else", $line)) {
  557. return "{\$else}";
  558. }
  559. // macro endif statment
  560. if (eregi("#endif", $line)) {
  561. $in_macro_block = false;
  562. return "{\$endif}";
  563. }
  564. }
  565. foreach ($this->macro_blocks as $key => $value) {
  566. if (eregi($key, $line, $captures)) {
  567. $in_macro_block = true;
  568. // replace the c-macro with a Pascal version
  569. if ($value == "*") {
  570. $captures[0] = trim($captures[0], "#");
  571. return "{\$".$captures[0]."}";
  572. } else {
  573. return "{".$value."}";
  574. }
  575. }
  576. }
  577. }
  578. // Appends the current macro to a recipient and resets it's reference
  579. function AppendCurrentMacro (&$recipient) {
  580. if ($this->in_macro_block) {
  581. $this->in_macro_block = false;
  582. $recipient[] = $this->macro_block;
  583. //if (is_array($recipient)) $recipient[] = $this->macro_block;
  584. //if (is_string($recipient)) $recipient .= $this->macro_block;
  585. }
  586. }
  587. // Returns the current macro and resets it's reference
  588. function InsertCurrentMacro () {
  589. if ($this->in_macro_block) {
  590. $this->in_macro_block = false;
  591. return $this->macro_block;
  592. } else {
  593. return null;
  594. }
  595. }
  596. // Build the current macro block
  597. function BuildMacroBlocks ($line) {
  598. // only insert if we are in a block already.
  599. // NOTE: this does not handle nesting!
  600. if ($this->in_macro_block) {
  601. // macro else statment
  602. if (eregi("#else", $line)) {
  603. $this->macro_block = "{\$else}";
  604. }
  605. // macro endif statment
  606. if (eregi("#endif", $line)) {
  607. $this->in_macro_block = false;
  608. $this->macro_block = "{\$endif}";
  609. }
  610. }
  611. foreach ($this->macro_blocks as $key => $value) {
  612. if (eregi($key, $line, $captures)) {
  613. $this->in_macro_block = true;
  614. // replace the c-macro with a Pascal version
  615. if ($value == "*") {
  616. $captures[0] = trim($captures[0], "#");
  617. $this->macro_block = "{\$".$captures[0]."}";
  618. } else {
  619. $this->macro_block = "{".$value."}";
  620. }
  621. }
  622. }
  623. }
  624. // Remove OS X versions macros from a line
  625. // NOTE: These should be re-inlcuded in Pascal
  626. function RemoveVersionMacros ($line, &$deprecatedmods) {
  627. if (preg_match($this->pregex_deprecated_macro,$line)) {
  628. $deprecatedmods = DeprecatedMacroToDirective($line);
  629. } else {
  630. $deprecatedmods = "";
  631. }
  632. foreach ($this->version_macros as $macro) {
  633. $line = eregi_replace($macro, "", $line);
  634. }
  635. return $line;
  636. }
  637. /**
  638. * CONVERTING UTILITIES
  639. */
  640. // Converts an Objective-c parameter string to Pascal
  641. function ConvertObjcParamsToPascal ($string, $protected_keywords) {
  642. $params = explode(":", $string);
  643. $list = array();
  644. $list["pairs"] = array();
  645. $param_list = array();
  646. //print_r($params);
  647. //print("$string\n");
  648. if (count($params) > 0) {
  649. foreach ($params as $value) {
  650. $value = trim($value);
  651. $valid = false;
  652. $modifiers = "";
  653. $value = $this->ReplaceRemoteMessagingModifiers($value, $null);
  654. // function pointer (callback)
  655. if (preg_match($this->pregex_function_pointer_objc_paratype, $value, $captures)) {
  656. $name = $captures[5];
  657. $function_pointer = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[4], false, "");
  658. $type = $this->AddCallback($name, $function_pointer);
  659. $valid = true;
  660. } elseif ((eregi("\(([a-zA-Z_]+).*\)([a-zA-Z_]+).*\.\.\.", $value, $captures)) || (eregi("(.*),[[:space:]]*\.\.\.", $value, $captures))) { // variable arguments
  661. $name = $captures[2];
  662. $type = $captures[1];
  663. $valid = true;
  664. } elseif (eregi("\((.*)\)[[:space:]]*([a-zA-Z_]+)", $value, $captures)) { // standard parameter
  665. $captures[1] = trim($captures[1]);
  666. $type = $this->FormatObjcType($captures[1], $modifiers);
  667. $name = $captures[2];
  668. $valid = true;
  669. }
  670. if ($valid) {
  671. // protect reserved keywords
  672. if ($this->IsKeywordReserved($name)) $name .= "_";
  673. if (!in_array($type, $this->reserved_types)) {
  674. if ($this->IsKeywordReserved($type)) $type .= "_";
  675. }
  676. // can't have "boolean: boolean;" parameters
  677. if (preg_match("!\b$name\b!i",$type)) $name .= "_";
  678. // print("para name: \"$name\"\n");
  679. // print_r("protected: \"$protected_keywords\"\n");
  680. while (@in_array(strtolower($name), $protected_keywords)) $name .= "_";
  681. // case-insensitive in_array:
  682. // if( preg_match("/\b$name\b/i", join(" ", array_values($protected_keywords))) ) $name .= "_";
  683. // if (@in_array($type, $protected_keywords)) $type .= "_";
  684. // replace objc types
  685. $type = $this->ReplaceObjcType($type);
  686. // make sure we label duplicate params, which are allowed in Objective-C
  687. while (in_array($name, $param_list)) {
  688. $count ++;
  689. $name = "$name$count";
  690. }
  691. // id is always a wrapper
  692. if (($this->objects_are_wrappers) && ($type == $this->objc_id)) {
  693. $name_list = "$type(GetHandle($name))";
  694. } else {
  695. $name_list = $name;
  696. }
  697. // add modifiers to the name if there are any
  698. $name_with_modifiers = $modifiers.$name;
  699. // create pair array
  700. $pair["name"] = $name;
  701. $pair["type"] = $type;
  702. // append list
  703. $list["pairs"][] = $pair;
  704. $list["string_with_modifiers"] .= "$name_with_modifiers: $type; ";
  705. $list["string"] .= "$name: $type; ";
  706. $list["list"] .= "$name_list, ";
  707. $param_list[] = $name;
  708. }
  709. }
  710. }
  711. // clean up the strings
  712. $list["string"] = trim($list["string"], "; ");
  713. $list["string_with_modifiers"] = trim($list["string_with_modifiers"], "; ");
  714. $list["list"] = trim($list["list"], ", ");
  715. return $list;
  716. }
  717. // Converts an Objective-C method to Pascal format
  718. function ConvertObjcMethodToPascal ($class, $source, $parts, $protected_keywords, $has_params, $deprecatedmods) {
  719. //print("$source\n");
  720. //print_r($parts);
  721. // replace "hinted" params comment with hinted type
  722. if ($this->replace_hinted_params) {
  723. // param string
  724. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[4], $captures)) {
  725. // ??? change the parameter to the hinted type
  726. //$parts[4] = eregi_replace("(/\*.*\*/)", $captures[2], $parts[4]);
  727. //$parts[4] = trim($parts[4], " ");
  728. }
  729. // return type
  730. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[2], $captures)) $parts[2] = $this->ReplaceRemoteMessagingModifiers($captures[2], $null);
  731. //print_r($parts);
  732. } else { // remove comments from params and return type
  733. $parts[4] = eregi_replace("(/\*.*\*/)", "", $parts[4]);
  734. $parts[4] = trim($parts[4]);
  735. $parts[2] = eregi_replace("(/\*.*\*/)", "", $parts[2]);
  736. $parts[2] = $this->ReplaceRemoteMessagingModifiers($parts[2], $null);
  737. }
  738. $return_type_clean = $parts[2];
  739. $return_type_pointers = preg_replace("![^*]+!e", "", $return_type_clean);
  740. $return_type_clean = trim($return_type_clean,"* ");
  741. // perform preformatting before attempting to protect keywords
  742. $parts[2] = $this->FormatObjcType($parts[2], $modifiers);
  743. $parts[4] = $this->FormatObjcParams($parts[4], $variable_arguments);
  744. //print($parts[4]."\n");
  745. if ($has_params) {
  746. $name = $this->ConvertObjcMethodName($source);
  747. // merge default protected keywords for the class/category
  748. if ($this->default_protected["*"]) $protected_keywords = array_merge($this->default_protected["*"], $protected_keywords);
  749. if ($this->default_protected[$class]) $protected_keywords = array_merge($this->default_protected[$class], $protected_keywords);
  750. $param_array = $this->ConvertObjcParamsToPascal($parts[4], $protected_keywords);
  751. $params = "(".$param_array["string"].")";
  752. $params_with_modifiers = "(".$param_array["string_with_modifiers"].")";
  753. } else {
  754. $params = "";
  755. $params_with_modifiers = "";
  756. // no parameters -> definitely no underscore normally, but there are some
  757. // conflicts...
  758. $name = $parts[3];
  759. // clean it up
  760. if ($this->trailing_underscore) {
  761. if (in_array($source, $this->trailing_underscore_methods)) $name = $name . "_";
  762. }
  763. $param_array = null;
  764. $variable_arguments = false;
  765. }
  766. // protect method name from keywords
  767. if ($this->IsKeywordReserved($name)) $name .= "_";
  768. // replace objc type
  769. $return_type = $this->ConvertReturnType($return_type_clean,$return_type_pointers);
  770. $virtual = "";
  771. $class_prefix = "";
  772. // determine the type based on return value
  773. if (ereg($this->regex_procedure_type, $return_type_clean.$return_type_pointers)) {
  774. $kind = "procedure";
  775. } else {
  776. $kind = "function";
  777. }
  778. // determine if this is a class method
  779. if ($parts[1] == "+") {
  780. $class_prefix = "class ";
  781. // These methods probably return the an allocated instance of the class, a typical convenience method.
  782. // ??? Ack! $class may be the category or protocol name
  783. //if ($return_type == $this->objc_id) $return_type = $class;
  784. }
  785. // Replace SEL with the string equivalent
  786. if ($this->register_selectors) {
  787. $params_with_modifiers = str_replace_word("SEL", $this->sel_string, $params_with_modifiers);
  788. }
  789. // make method templates
  790. if ($kind != "function") {
  791. if ($variable_arguments) $modifier .= " varargs;";
  792. $method = "$class_prefix$kind $name$params_with_modifiers;$modifier$virtual";
  793. $method_template = "[KIND] [PREFIX]$name"."[PARAMS];$modifier";
  794. } else {
  795. if ($variable_arguments) $return_type = "$return_type; varargs";
  796. $method = $class_prefix."function $name$params_with_modifiers: $return_type;$modifier$virtual";
  797. $method_template = "[KIND] [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  798. }
  799. $method_template_procedure = "procedure [PREFIX]$name"."[PARAMS];$modifier";
  800. $method_template_function = "function [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  801. // build structure
  802. $struct["def"] = $method;
  803. $struct["template"] = $method_template;
  804. $struct["template_function"] = $method_template_function;
  805. $struct["template_procedure"] = $method_template_procedure;
  806. $struct["objc_method"] = $this->ExtractObjcMethodName($source);
  807. $struct["class_prefix"] = $class_prefix;
  808. if ($deprecatedmods != "") $struct["deprecated"] = $deprecatedmods.";";
  809. //$struct["def_objc"] = eregi("(.*);", $source, $captures[1]);
  810. if ($return_type == "void") $return_type = "";
  811. $struct["return"] = $return_type;
  812. if (in_array($return_type, $this->cocoa_classes)) $struct["returns_wrapper"] = true;
  813. $struct["param_string_clean"] = trim($params, "()");
  814. $struct["param_string_clean_with_modifiers"] = trim($params_with_modifiers, "()");
  815. $struct["param_string"] = $params;
  816. $struct["param_string_with_modifiers"] = $params_with_modifiers;
  817. $struct["param_array"] = $param_array["pairs"];
  818. $struct["param_list"] = $param_array["list"];
  819. $struct["class"] = $class;
  820. $struct["name"] = $name;
  821. $struct["kind"] = $kind;
  822. if ($struct["param_array"] != null) $struct["has_params"] = true;
  823. // FPC bug work around
  824. if (strlen($name) > $this->maximum_method_length) {
  825. $struct["can_override"] = false;
  826. print(" # WARNING: method $name can't override because the name is too long\n");
  827. $this->warning_count ++;
  828. }
  829. return $struct;
  830. }
  831. // Converts a C parameter string to Pascal
  832. function ConvertCParamsPascal ($string) {
  833. // print("params: $string\n");
  834. if ((trim($string) == "void") || (trim($string) == "")) return "";
  835. $params = explode(",", $string);
  836. $count = 0;
  837. $param_string = "";
  838. foreach ($params as $param) {
  839. $name_type = $this->ExtractCParaNameAndType($param);
  840. $type = $name_type["type"];
  841. $name = $name_type["name"];
  842. $pointertype = $name_type["pointermods"];
  843. // create name if none is specified
  844. if ($name == "") {
  845. $count ++;
  846. $name = "param$count";
  847. }
  848. // remove const keywords
  849. $type = str_replace_word("const", "", $type);
  850. $type = $this->ReplaceObjcType($type);
  851. // Remove array brackets (NSUInteger[])p
  852. if (eregi("\[[0-9]*\]", $name)) {
  853. $orgtype = $this->EncodePointerModifiers($type,$pointertype);
  854. $pointertype .= "*";
  855. $type = $this->EncodePointerModifiers($type,$pointertype)." {array of $orgtype}";
  856. $name = eregi_replace("\[[0-9]*\]", "", $name);
  857. } else {
  858. $type = $this->EncodePointerModifiers($type,$pointertype);
  859. }
  860. $modifier = "";
  861. if ($this->IsKeywordReserved($name)) $name .= "_";
  862. // multiple parameters
  863. if ($type == "...") {
  864. $param_string .= "varargs: array of const";
  865. break;
  866. }
  867. $param_string .= "$modifier$name: $type; ";
  868. }
  869. $param_string = trim($param_string, "; ");
  870. //print("$param_string\n");
  871. return $param_string;
  872. }
  873. // Converts an Objective-c method name to Pascal
  874. function ConvertObjcMethodName ($method) {
  875. $params = explode(":", $method);
  876. $name = "";
  877. $count = 0;
  878. if (count($params) > 1) {
  879. foreach ($params as $value) {
  880. if (eregi("([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1]."_";
  881. }
  882. } else {
  883. if (eregi("([a-zA-Z0-9]+)(;)*$", $params[0], $captures)) $name .= $captures[1]."_";
  884. }
  885. // clean it up
  886. if ($this->trailing_underscore) {
  887. if (!in_array($method, $this->trailing_underscore_methods)) $name = trim($name, "_");
  888. }
  889. $name = $this->ReplaceObjcType($name);
  890. return $name;
  891. }
  892. // Convert a method return type to Pascal
  893. function ConvertReturnType ($type, $pointertype) {
  894. $type = $this->ReplaceGarbageCollectorHints($type, $null);
  895. $type = $this->ReplaceRemoteMessagingModifiers($type, $null);
  896. // format the return type to make sure it's clean
  897. $type = $this->FormatObjcType($type, $null_modifier);
  898. // translate type to Pascal
  899. $type = $this->ReplaceObjcType($type);
  900. // incorportate pointer modifiers
  901. $type = $this->EncodePointerModifiers($type,$pointertype);
  902. return $type;
  903. }
  904. /**
  905. * USER PATCHES
  906. */
  907. function InsertPatches ($header) {
  908. $path = "$this->root/patches/".$header["name_clean"].".patch";
  909. if ($handle = @fopen($path, "r")) {
  910. $text = file_get_contents($path);
  911. $this->PrintOutput(0, $text);
  912. fclose($handle);
  913. }
  914. }
  915. function HeaderContainsPatch ($header) {
  916. if ($handle = @fopen("$this->root/patches/".$header["name_clean"].".patch", "r")) {
  917. fclose($handle);
  918. return true;
  919. }
  920. }
  921. /**
  922. * COMMENTS
  923. */
  924. // Trim a comment string
  925. function TrimComment ($comment, $preserve_line_endings) {
  926. // trim line breaks
  927. if (!$preserve_line_endings) $comment = trim($comment, "\n");
  928. // remove remains of C comments
  929. $comment = eregi_replace("^[!*/ ]+", "", $comment);
  930. $comment = eregi_replace("[!*/ ]+$", "", $comment);
  931. // remove all Pascal comment characters to prevent nesting
  932. $comment = str_replace(array("{", "}"), "", $comment);
  933. //print("$comment\n");
  934. return $comment;
  935. }
  936. // Builds comments from $line spaning multiple lines or at the end of lines
  937. // Returns a terminated comment string or a fragment in $fragment for futher processing
  938. function BuildComment ($line, $file) {
  939. // set the current comment header being parsed
  940. $this->comment_header = $file;
  941. // comment parsing is off, bail!
  942. if (!$this->parse_comments) return;
  943. // single-line comment at start of line
  944. if (eregi("^[[:space:]]*//(.*)", $line, $captures)) {
  945. //print("$captures[1]\n");
  946. // ignore comment
  947. foreach ($this->ignore_comments as $pattern) {
  948. if (eregi($pattern, $captures[1])) return;
  949. }
  950. $this->comment_terminated = $this->TrimComment($captures[1], false);
  951. return;
  952. } elseif (eregi("[[:space:]]+//(.*)", $line, $captures)) { // single-line comments at end of line
  953. //print("$captures[1]\n");
  954. // ignore comment
  955. foreach ($this->ignore_comments as $pattern) {
  956. if (eregi($pattern, $captures[1])) return;
  957. }
  958. $this->comment_eol = $captures[1];
  959. return;
  960. }
  961. // multi-line comments terminated
  962. if (eregi("/\*(.*)\*/", $line, $captures)) {
  963. //print("$captures[1]\n");
  964. // ignore comment
  965. foreach ($this->ignore_comments as $pattern) {
  966. if (eregi($pattern, $captures[1])) return;
  967. }
  968. $this->comment_terminated = $this->TrimComment($captures[1], false);
  969. return;
  970. }
  971. // terminate comment fragment
  972. if ($this->comment_fragment_open) {
  973. if (eregi("(.*)\*/", $line, $captures)) {
  974. // append fragment
  975. $comment = $this->TrimComment($captures[1], false);
  976. if ($comment) $this->comment_fragment .= $comment;
  977. // closed comment block
  978. if (!$captures[1]) {
  979. $this->comment_block_closed = true;
  980. }
  981. // set terminated comment with fragment
  982. $this->comment_terminated = $this->comment_fragment;
  983. // add extra line break for comment blocks
  984. if (($this->comment_block_open) && ($this->comment_block_closed)) {
  985. // ??? the printing will cut this out if we add line endings!
  986. $this->comment_terminated = "$this->comment_fragment";
  987. }
  988. $this->comment_fragment = null;
  989. $this->comment_fragment_open = false;
  990. $this->comment_block_open = false;
  991. $this->comment_block_closed = false;
  992. return;
  993. } else {
  994. // build the fragment
  995. $comment = $this->TrimComment($line, true);
  996. // ignore comment and stop building fragment
  997. foreach ($this->ignore_comments as $pattern) {
  998. if (eregi($pattern, $comment)) {
  999. $this->comment_fragment = null;
  1000. $this->comment_fragment_open = false;
  1001. $this->comment_block_open = false;
  1002. $this->comment_block_closed = false;
  1003. return;
  1004. }
  1005. }
  1006. if (($this->comment_fragment_previous != $line) && ($comment)) $this->comment_fragment .= $comment."\n";
  1007. $this->comment_fragment_previous = $line;
  1008. return;
  1009. }
  1010. }
  1011. // start comment fragment
  1012. if (eregi("/\*(.*)", $line, $captures)) {
  1013. $this->comment_terminated = null;
  1014. // ignore comment
  1015. foreach ($this->ignore_comments as $pattern) {
  1016. if (eregi($pattern, $captures[1])) return;
  1017. }
  1018. $this->comment_fragment_open = true;
  1019. $this->comment_fragment = "";
  1020. $this->comment_block_open = true;
  1021. $this->comment_block_closed = false;
  1022. // prevent against empty comments
  1023. if ((!$captures[1]) || ($captures[1] == "\n")) {
  1024. $this->comment_block_open = true;
  1025. return;
  1026. }
  1027. // append the line if valid
  1028. $comment = $this->TrimComment($captures[1], false);
  1029. if ($comment) $this->comment_fragment .= $comment;
  1030. return;
  1031. }
  1032. }
  1033. // Resets current comment references
  1034. function ResetComment () {
  1035. $this->comment_fragment = null;
  1036. $this->comment_eol = null;
  1037. $this->comment_terminated = null;
  1038. $this->comment_block_open = false;
  1039. $this->comment_block_closed = false;
  1040. }
  1041. // Checks/sets if a comment is a duplicate in the file
  1042. function CheckDuplicateComment ($comment) {
  1043. if ((@!in_array($comment, $this->dump[$this->comment_header]["comments"])) && ($comment)) {
  1044. $this->dump[$this->comment_header]["comments"][] = $comment;
  1045. return true;
  1046. } else {
  1047. return false;
  1048. }
  1049. }
  1050. // Appends the eol comment to the output and clears the reference
  1051. function AppendEOLComment () {
  1052. if ($this->comment_eol) {
  1053. $comment = " // ".$this->TrimComment($this->comment_eol, false);
  1054. $this->comment_eol = "";
  1055. return $comment;
  1056. } else {
  1057. return null;
  1058. }
  1059. }
  1060. // Inserts the recently terminated comment to the output on a new line and clears the reference
  1061. function InsertCurrentComment () {
  1062. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated))) {
  1063. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  1064. $this->comment_previous = $this->comment_terminated;
  1065. $this->comment_terminated = "";
  1066. return $comment;
  1067. } else {
  1068. return null;
  1069. }
  1070. }
  1071. // Appends the recently terminated comment to a recipient and clears the reference
  1072. function AppendCurrentComment (&$recipient) {
  1073. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated)) && (gettype($recipient) == "array")) {
  1074. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  1075. $recipient[] = $comment;
  1076. //if (is_array($recipient)) $recipient[] = $comment;
  1077. //if (is_string($recipient)) $recipient .= $comment;
  1078. $this->comment_previous = $this->comment_terminated;
  1079. $this->comment_terminated = "";
  1080. }
  1081. }
  1082. // Removes all comments from a line
  1083. function RemoveComments ($line) {
  1084. // remove single-line comments
  1085. $line = eregi_replace("[[:space:]]+//(.*)", "", $line);
  1086. // remove multi-line comments /* ... */
  1087. $line = eregi_replace("/\*.*\*/", "", $line);
  1088. return $line;
  1089. }
  1090. /**
  1091. * PRINTING METHODS
  1092. */
  1093. function PrintClass ($class) {
  1094. $this->PrintOutput(0, "");
  1095. $this->PrintOutput(0, "{ ".$class["name"]." }");
  1096. if ($class["comment"]) $this->PrintOutput(0, $class["comment"]);
  1097. //print_r($class["methods"]);
  1098. // print super class or protocol which the class conforms to
  1099. if ($class["adopts"]) {
  1100. $this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].", ".$class["adopts"].")");
  1101. } elseif ($class["super"]) {
  1102. $this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].")");
  1103. }
  1104. // print instance variables
  1105. if ($class["ivars"]) {
  1106. $this->PrintOutput(1, "private");
  1107. foreach ($class["ivars"] as $ivar) {
  1108. $this->PrintOutput(2, $ivar);
  1109. }
  1110. }
  1111. // print alloc method for the class
  1112. $this->PrintOutput(2, "");
  1113. $this->PrintOutput(1, "public");
  1114. $this->PrintOutput(2, "class function alloc: ".$class["name"]."; message 'alloc';");
  1115. // print class-level methods
  1116. if ($class["methods"]) {
  1117. $this->PrintOutput(0, "");
  1118. foreach ($class["methods"] as $method) {
  1119. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1120. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1121. }
  1122. }
  1123. // print adopted protocol methods
  1124. if (count($class["protocols"]) > 0) {
  1125. $this->PrintOutput(0, "");
  1126. $this->PrintOutput(2, "{ Adopted Protocols }");
  1127. //print_r($this->dump["protocols"]);
  1128. foreach ($class["protocols"] as $name) {
  1129. if ($this->dump["protocols"][$name]) {
  1130. foreach ($this->dump["protocols"][$name] as $method) {
  1131. if (!$this->ClassContainsMethod($class, $method)) $this->PrintOutput(2, $method["def"]);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. $this->PrintOutput(1, "end; external;");
  1137. }
  1138. function PrintCategory ($class, $category) {
  1139. // declare real category if external
  1140. if ($category["external"]) {
  1141. $new_name = " name '".$category["external_name"]."'";
  1142. }
  1143. $category_name = $category["name"].$this->category_suffix;
  1144. $this->PrintOutput(0, "");
  1145. $this->PrintOutput(0, "{ $category_name }");
  1146. if ($category["comment"]) $this->PrintOutput(0, $category["comment"]);
  1147. // print super class or protocol which the class conforms to
  1148. $this->PrintOutput(1, "$category_name = objccategory(".$category["super"].")");
  1149. // print methods
  1150. if ($category["methods"]) {
  1151. foreach ($category["methods"] as $method) {
  1152. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1153. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1154. }
  1155. }
  1156. $this->PrintOutput(1, "end; external$new_name;");
  1157. }
  1158. function PrintHeader ($header) {
  1159. global $version;
  1160. //print_r($header);
  1161. //print_r($this->dump["categories"]);
  1162. // open the output file if we not printing to terminal
  1163. if (!$this->show) {
  1164. if ($this->merge_headers) {
  1165. $this->output = fopen($header["path_merge"], "w+");
  1166. } else {
  1167. $this->output = fopen($header["path"], "w+");
  1168. }
  1169. }
  1170. $this->PrintOutput(0, "{ Parsed from ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  1171. $date = @date("D M j G:i:s T Y");
  1172. $this->PrintOutput(0, "{ Version: $version - $date }");
  1173. $this->PrintOutput(0, "");
  1174. $macro = strtoupper(substr($header["name"], 0, (strripos($header["name"], "."))));
  1175. $this->PrintOutput(0, "");
  1176. $this->PrintOutput(0, "{\$ifdef TYPES}");
  1177. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_T}");
  1178. $this->PrintOutput(0, "{\$define $macro"."_PAS_T}");
  1179. $this->PrintTypes($header, false);
  1180. $this->PrintOutput(0, "");
  1181. $this->PrintOutput(0, "{\$endif}");
  1182. $this->PrintOutput(0, "{\$endif}");
  1183. $this->PrintOutput(0, "");
  1184. $this->PrintOutput(0, "{\$ifdef RECORDS}");
  1185. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_R}");
  1186. $this->PrintOutput(0, "{\$define $macro"."_PAS_R}");
  1187. // Records from types
  1188. $this->PrintRecords($header);
  1189. $this->PrintOutput(0, "");
  1190. $this->PrintOutput(0, "{\$endif}");
  1191. $this->PrintOutput(0, "{\$endif}");
  1192. $this->PrintOutput(0, "");
  1193. $this->PrintOutput(0, "{\$ifdef FUNCTIONS}");
  1194. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_F}");
  1195. $this->PrintOutput(0, "{\$define $macro"."_PAS_F}");
  1196. $this->PrintFunctions($header);
  1197. $this->PrintOutput(0, "");
  1198. $this->PrintOutput(0, "{\$endif}");
  1199. $this->PrintOutput(0, "{\$endif}");
  1200. $this->PrintOutput(0, "");
  1201. $this->PrintOutput(0, "{\$ifdef EXTERNAL_SYMBOLS}");
  1202. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_S}");
  1203. $this->PrintOutput(0, "{\$define $macro"."_PAS_S}");
  1204. $this->PrintExternalSymbols($header);
  1205. $this->PrintOutput(0, "");
  1206. $this->PrintOutput(0, "{\$endif}");
  1207. $this->PrintOutput(0, "{\$endif}");
  1208. // insert user patches
  1209. if ($this->HeaderContainsPatch($header)) {
  1210. $this->PrintOutput(0, "");
  1211. $this->PrintOutput(0, "{\$ifdef USER_PATCHES}");
  1212. //$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_PATCH}");
  1213. //$this->PrintOutput(0, "{\$define $macro"."_PAS_PATCH}");
  1214. $this->InsertPatches($header);
  1215. $this->PrintOutput(0, "");
  1216. //$this->PrintOutput(0, "{\$endif}");
  1217. $this->PrintOutput(0, "{\$endif}");
  1218. }
  1219. if (($header["classes"]) || ($header["protocols"])) {
  1220. $this->PrintOutput(0, "");
  1221. $this->PrintOutput(0, "{\$ifdef FORWARD}");
  1222. if ($header["protocols"]) {
  1223. foreach ($header["protocols"] as $protocol) $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol;");
  1224. }
  1225. if ($header["classes"]) {
  1226. foreach ($header["classes"] as $class) {
  1227. if ($class["name"]) {
  1228. $this->PrintOutput(1, $class["name"]." = objcclass;");
  1229. $this->PrintOutput(1, $class["name"].$this->class_pointer_suffix." = ^".$class["name"].";");
  1230. // for consistency also offer Ptr-name variant
  1231. $this->PrintOutput(1, $class["name"]."Ptr = ".$class["name"].$this->class_pointer_suffix.";");
  1232. }
  1233. }
  1234. }
  1235. $this->PrintOutput(0, "");
  1236. $this->PrintOutput(0, "{\$endif}");
  1237. }
  1238. if ($header["classes"]) {
  1239. $this->PrintOutput(0, "");
  1240. $this->PrintOutput(0, "{\$ifdef CLASSES}");
  1241. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_C}");
  1242. $this->PrintOutput(0, "{\$define $macro"."_PAS_C}");
  1243. foreach ($header["classes"] as $class) {
  1244. if ($class["name"]) $this->PrintClass($class);
  1245. }
  1246. if (count($header["categories"]) > 0) {
  1247. foreach ($header["categories"] as $category) {
  1248. $this->PrintCategory($class, $category);
  1249. }
  1250. }
  1251. $this->PrintOutput(0, "");
  1252. $this->PrintOutput(0, "{\$endif}");
  1253. $this->PrintOutput(0, "{\$endif}");
  1254. }
  1255. if ($header["protocols"]) {
  1256. $this->PrintOutput(0, "{\$ifdef PROTOCOLS}");
  1257. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_P}");
  1258. $this->PrintOutput(0, "{\$define $macro"."_PAS_P}");
  1259. foreach ($header["protocols"] as $protocol) {
  1260. $this->PrintOutput(1, "");
  1261. $this->PrintOutput(0, "{ ".$protocol["name"]." Protocol }");
  1262. if ($protocol["comment"]) $this->PrintOutput(0, $protocol["comment"]);
  1263. $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol");
  1264. // print methods
  1265. if ($protocol["methods"]) {
  1266. foreach ($protocol["methods"] as $name => $method) {
  1267. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1268. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1269. }
  1270. }
  1271. $this->PrintOutput(1, "end; external name '".$protocol["name"]."';");
  1272. }
  1273. $this->PrintOutput(0, "{\$endif}");
  1274. $this->PrintOutput(0, "{\$endif}");
  1275. }
  1276. }
  1277. // Prints all externally defined symbols
  1278. function PrintExternalSymbols ($header) {
  1279. if (!$this->dump[$header["name"]]["types"]) return;
  1280. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1281. // External string constants
  1282. if ($key == "string_constant") {
  1283. $this->PrintOutput(0, "");
  1284. $this->PrintOutput(0, "{ External string constants }");
  1285. $this->PrintOutput(0, "var");
  1286. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1287. }
  1288. if ($key == "external_symbol") {
  1289. $this->PrintOutput(0, "");
  1290. $this->PrintOutput(0, "{ External symbols }");
  1291. $this->PrintOutput(0, "var");
  1292. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1293. }
  1294. }
  1295. }
  1296. // Prints all types in the header
  1297. function PrintTypes ($header) {
  1298. if (!$this->dump[$header["name"]]["types"]) return;
  1299. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1300. $section_printed = false;
  1301. // External defines
  1302. if ($key == "defines") {
  1303. foreach ($type_array as $type) {
  1304. if (!$section_printed) {
  1305. $this->PrintOutput(0, "");
  1306. $this->PrintOutput(0, "{ Defines }");
  1307. $this->PrintOutput(0, "const");
  1308. $section_printed = true;
  1309. }
  1310. $this->PrintOutput(1, $type);
  1311. }
  1312. }
  1313. // Enumerations
  1314. if ($key == "enums") {
  1315. $this->PrintOutput(0, "");
  1316. $this->PrintOutput(0, "{ Constants }");
  1317. foreach ($type_array as $block) {
  1318. $section_printed = false;
  1319. foreach ($block as $type) {
  1320. if (!$section_printed) {
  1321. $this->PrintOutput(0, "");
  1322. $this->PrintOutput(0, "const");
  1323. $section_printed = true;
  1324. }
  1325. $this->PrintOutput(1, $type);
  1326. }
  1327. }
  1328. }
  1329. // Typedefs
  1330. if (($key == "typedef") || ($key == "named_enums")) {
  1331. foreach ($type_array as $type) {
  1332. if (!$section_printed) {
  1333. $this->PrintOutput(0, "");
  1334. $this->PrintOutput(0, "{ Types }");
  1335. $this->PrintOutput(0, "type");
  1336. $section_printed = true;
  1337. }
  1338. $this->PrintOutput(1, $type);
  1339. }
  1340. }
  1341. // CallBacks
  1342. if ($key == "callbacks") {
  1343. foreach ($type_array as $name => $type) {
  1344. if (!$section_printed) {
  1345. $this->PrintOutput(0, "");
  1346. $this->PrintOutput(0, "{ Callbacks }");
  1347. $this->PrintOutput(0, "type");
  1348. $section_printed = true;
  1349. }
  1350. $this->PrintOutput(1, "$name = $type");
  1351. }
  1352. }
  1353. }
  1354. }
  1355. // Prints all records in the header
  1356. function PrintRecords ($header) {
  1357. if (!$this->dump[$header["name"]]["types"]) return;
  1358. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1359. // Structures
  1360. if ($key == "structs") {
  1361. $this->PrintOutput(0, "");
  1362. $this->PrintOutput(0, "{ Records }");
  1363. foreach ($type_array as $type) {
  1364. $this->PrintOutput(0, "type");
  1365. $this->PrintOutput(1, $type);
  1366. }
  1367. }
  1368. }
  1369. }
  1370. // Prints all external functions in the header
  1371. function PrintFunctions ($header) {
  1372. if (!$this->dump[$header["name"]]["types"]) return;
  1373. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1374. if ($key == "functions") {
  1375. $this->PrintOutput(0, "");
  1376. $this->PrintOutput(0, "{ Functions }");
  1377. foreach ($type_array as $type) $this->PrintOutput(0, $type);
  1378. }
  1379. }
  1380. }
  1381. // Prints all classes from the header in reference format (not for compiling)
  1382. function PrintHeaderReference ($header, $path) {
  1383. $this->output = fopen($path, "w+");
  1384. //$this->PrintOutput(0, "{ ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  1385. $this->PrintOutput(0, "unit ".$header["name_clean"].";");
  1386. $this->PrintOutput(0, "interface");
  1387. $this->PrintOutput(0, "uses");
  1388. $this->PrintOutput(1, "ctypes, objc, MacOSAll, AnonClassDefinitions;");
  1389. if ($header["classes"]) {
  1390. foreach ($header["classes"] as $class) {
  1391. $this->PrintOutput(0, "");
  1392. $this->PrintOutput(0, "type");
  1393. $this->PrintOutput(1, $class["name"]."Ref = ".$this->objc_id.";");
  1394. $this->PrintOutput(1, $class["name"]."Ptr = Pointer;");
  1395. }
  1396. }
  1397. // types
  1398. $this->PrintTypes($header);
  1399. $this->PrintRecords($header);
  1400. $this->PrintFunctions($header);
  1401. $this->PrintExternalSymbols($header);
  1402. if ($header["classes"]) {
  1403. foreach ($header["classes"] as $class) {
  1404. if (in_array($class["name"], $this->cocoa_classes)) {
  1405. $this->PrintOutput(0, "");
  1406. $this->PrintOutput(0, "type");
  1407. $this->PrintOutput(1, $class["name"]." = object(".$class["super"].")");
  1408. // print class-level methods
  1409. if (count($class["methods"]) > 0) {
  1410. $this->PrintOutput(0, "");
  1411. foreach ($class["methods"] as $method) {
  1412. $this->PrintOutput(2, $method["def"]);
  1413. }
  1414. }
  1415. // print category-level methods
  1416. if (count($class["categories"]) > 0) {
  1417. foreach ($class["categories"] as $name => $category) {
  1418. $this->PrintOutput(0, "");
  1419. $this->PrintOutput(2, "{ Category: $name }");
  1420. if ($category["methods"]) {
  1421. foreach ($category["methods"] as $method) {
  1422. $this->PrintOutput(2, $method["def"]);
  1423. }
  1424. }
  1425. }
  1426. }
  1427. $this->PrintOutput(1, "end;");
  1428. }
  1429. }
  1430. }
  1431. // print procedural protocols
  1432. if ($header["protocols"]) {
  1433. foreach ($header["protocols"] as $protocol) {
  1434. if ($protocol["methods"]) {
  1435. $this->PrintOutput(0, "");
  1436. $this->PrintOutput(0, "{ Protocol: ".$protocol["name"]." }");
  1437. foreach ($protocol["methods"] as $name => $method) {
  1438. if ($method["kind"] != "constructor") {
  1439. $this->PrintProtocolDeclaration($protocol, $method);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. }
  1445. $this->PrintOutput(0, "");
  1446. $this->PrintOutput(0, "implementation");
  1447. $this->PrintOutput(0, "end.");
  1448. }
  1449. // Merges two headers by difference
  1450. function MergeHeader ($header) {
  1451. /*
  1452. diff QLPreviewPanel_ORIGINAL.inc QLPreviewPanel_UPDATED.inc > diff.patch
  1453. patch QLPreviewPanel_ORIGINAL.inc -i diff.patch -o output.txt
  1454. patch QLPreviewPanel.inc -i header.patch -o QLPreviewPanel.inc.merged
  1455. also add -section="types,classes" which only prints into the sections and copies text from
  1456. the previous version if not specified
  1457. */
  1458. // set the paths to use
  1459. $path = $header["path"];
  1460. $patch = "$this->root$this->out/$this->framework/header.patch";
  1461. $merged = $header["path_merge"];
  1462. // create a patch using diff
  1463. exec("/usr/bin/diff \"$path\" \"$merged\" > \"$patch\"");
  1464. // parse the diff file by mode
  1465. $lines = file($patch);
  1466. $content = "";
  1467. $section_lines = null;
  1468. $section_part = null;
  1469. $count = 0;
  1470. $section_id = 0;
  1471. //print_r($lines);
  1472. if ($lines) {
  1473. foreach ($lines as $line) {
  1474. $count++;
  1475. // remove line endings to aid regex
  1476. $line = trim($line, "\n");
  1477. // parse section
  1478. if (($section_lines) && (!eregi("^[0-9]+", $line))) {
  1479. // append line to section
  1480. $section_lines[] = "$line\n";
  1481. // the section id is adding
  1482. if (eregi("^>", $line)) $section_part[$section_id] = true;
  1483. if (eregi("^<", $line)) $section_part[$section_id] = 0;
  1484. // section is changing type
  1485. if ($line == "---") {
  1486. $section_id++;
  1487. continue;
  1488. }
  1489. }
  1490. // line is a new section or eof
  1491. if ((eregi("^[0-9]+", $line)) || ($count == count($lines))) {
  1492. // append the section to the content
  1493. // we only accept sections where the first part (before ---) contains additions ">"
  1494. //print_r($section_part);
  1495. if (($section_part[0]) && (!$section_part[1])) $content .= implode($section_lines);
  1496. // init the new section
  1497. $section_lines = array();
  1498. $section_lines[] = "$line\n";
  1499. $section_id = 0;
  1500. $section_part = array(null, null);
  1501. continue;
  1502. }
  1503. }
  1504. //print($content);
  1505. // write the parsed patch back to the file
  1506. if ($content) {
  1507. file_put_contents($patch, $content);
  1508. } else {
  1509. $content = null;
  1510. }
  1511. }
  1512. // patch the header to the merged file
  1513. if ($content) {
  1514. exec("/usr/bin/patch \"$path\" -i \"$patch\" -o \"$merged\" ");
  1515. // swap the contents of the merged file with the original
  1516. file_put_contents($path, file_get_contents($merged));
  1517. }
  1518. // delete the temporary files
  1519. unlink($patch);
  1520. unlink($merged);
  1521. }
  1522. function PrintGlobalClassInfo($all_classes, $defined_classes, $anon_classes) {
  1523. // add all classes as anonymous external classes to a separate unit.
  1524. // They will be overridden by the actual definitions in the translated
  1525. // headers part of the main unit, but this way they can appear as record
  1526. // field types and as callback parameters
  1527. // open the output file if we not printing to terminal
  1528. if (!$this->show) {
  1529. $this->output = fopen("$this->root$this->out/AnonClassDefinitions".ucfirst($this->framework).".pas", "w+");
  1530. }
  1531. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  1532. $date = @date("D M j G:i:s T Y");
  1533. $this->PrintOutput(0, "");
  1534. // allows parameter names conflicting with field names
  1535. $this->PrintOutput(0, "{\$mode delphi}");
  1536. $this->PrintOutput(0, "{\$modeswitch objectivec1}");
  1537. // enables "external" after the semi-colon
  1538. $this->PrintOutput(0, "{\$modeswitch cvar}");
  1539. $this->PrintOutput(0, "");
  1540. $this->PrintOutPut(0,"unit AnonClassDefinitions".ucfirst($this->framework).";");
  1541. $this->PrintOutput(0, "");
  1542. $this->PrintOutput(0, "interface");
  1543. $this->PrintOutput(0, "");
  1544. $this->PrintOutput(0, "type");
  1545. foreach ($all_classes as $class)
  1546. $this->PrintOutput(1, $class." = objcclass; external;");
  1547. $this->PrintOutput(0, "");
  1548. $this->PrintOutput(0, "implementation");
  1549. $this->PrintOutput(0, "");
  1550. $this->PrintOutput(0, "end.");
  1551. // Now all anonymous external classes that have no real definition to an
  1552. // include file that is added to the main unit. This way it is possible
  1553. // to declare variables of these types in user programs without having to
  1554. // include the unit above will all anonymous classes (should not be used)
  1555. // open the output file if we not printing to terminal
  1556. if (!$this->show) {
  1557. $this->output = fopen("$this->root$this->out/$this->framework/AnonIncludeClassDefinitions".ucfirst($this->framework).".inc", "w+");
  1558. }
  1559. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  1560. $date = @date("D M j G:i:s T Y");
  1561. // add all classes as anonymous external classes. They will be overridden
  1562. // by the actual definitions in the translated headers, but this way they
  1563. // can appear as record field types and as callback parameters
  1564. $first = true;
  1565. foreach ($anon_classes as $class) {
  1566. if (!in_array($class,$defined_classes)) {
  1567. if ($first) {
  1568. $this->PrintOutput(0, "type");
  1569. $first = false;
  1570. }
  1571. $this->PrintOutput(1, $class." = objcclass; external;");
  1572. }
  1573. }
  1574. }
  1575. // Prints all headers parsed
  1576. function PrintAllHeaders ($output_path, $ignore_output, $only_files, $print_header_references) {
  1577. //print("• Printing ".count($this->dump)." headers...\n");
  1578. foreach ($this->dump as $file => $header) {
  1579. // the framework is set to not print, ignore the header
  1580. if (!$this->frameworks[$header["framework"]]["print"]) continue;
  1581. if (eregi("^[a-zA-Z0-9]+\.h", $file)) {
  1582. // ignore these files
  1583. if (@in_array($header["path_partial"], $ignore_output)) continue;
  1584. // only parse these files
  1585. if ((@count($only_files) > 0) && (@!in_array($header["name"], $only_files))) continue;
  1586. $name_clean = substr($file, 0, (strripos($file, ".")));
  1587. // assign output path
  1588. if ($output_path != "") {
  1589. $header["path"] = $output_path."/".$name_clean.".inc";
  1590. }
  1591. // print the header
  1592. $this->PrintHeader($header);
  1593. // merge the headers
  1594. if (($this->merge_headers) && (!$this->show)) {
  1595. $this->MergeHeader($header);
  1596. }
  1597. if ($print_header_references) $this->PrintHeaderReference($header, $this->root.$this->out."/reference/".$name_clean.".pas");
  1598. if (!$this->show) print("* Printed $name_clean.h to ".$header["path"]."\n");
  1599. }
  1600. }
  1601. // print global stuff (for Obj-P: a unit containing all classes as
  1602. // anonymous external classes, so they can be used before they are
  1603. // declared)
  1604. $this->PrintGlobalClassInfo($this->cocoa_classes, $this->defined_cocoa_classes, $this->anon_cocoa_classes);
  1605. }
  1606. /**
  1607. * PRE-PARSING METHODS
  1608. */
  1609. // Parse all "pre-defined" category methods in a header
  1610. function PreparseCategoryMethods ($file) {
  1611. $contents = file_get_contents($file);
  1612. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  1613. $lines = explode("\n", $contents);
  1614. foreach ($lines as $line) {
  1615. // skip blocks
  1616. if ($this->SkipBlock($line)) continue;
  1617. // parse category
  1618. if ($got_category) {
  1619. // build method fragment
  1620. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  1621. // found method fragment termination
  1622. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  1623. $line = $method_fragment;
  1624. $method_fragment = null;
  1625. }
  1626. // found method
  1627. $method = null;
  1628. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  1629. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), true, "");
  1630. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  1631. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), false, "");
  1632. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  1633. $method_fragment = $line;
  1634. }
  1635. // append to classes
  1636. if (($method) && ($current_class)) {
  1637. $this->dump[$category_owner]["category_methods"][] = $method["name"];
  1638. //print($method["name"]."\n");
  1639. }
  1640. // found the end
  1641. if (ereg("^@end", $line)) $got_category = false;
  1642. }
  1643. // got category
  1644. if (eregi($this->regex_objc_category, $line, $captures)) {
  1645. $category_owner = $this->FindCategoryHeader($captures[1]);
  1646. if ($category_owner) {
  1647. $got_category = true;
  1648. $current_category = $captures[2];
  1649. $current_class = $captures[1];
  1650. } else {
  1651. $current_class = null;
  1652. }
  1653. }
  1654. }
  1655. return $this->dump[$category_owner]["category_methods"];
  1656. }
  1657. // Preparses a class for protected keywords
  1658. function PreparseClass ($lines, $line_count) {
  1659. $protected_keywords = array();
  1660. for ($i=$line_count; $i < count($lines); $i++) {
  1661. $line = $lines[$i - 1];
  1662. // skip blocks
  1663. if ($this->SkipBlock($line)) continue;
  1664. // build method fragment
  1665. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  1666. // found method fragment termination
  1667. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  1668. $line = $method_fragment;
  1669. $method_fragment = null;
  1670. }
  1671. // found method
  1672. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  1673. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, true, "");
  1674. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  1675. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  1676. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, false, "");
  1677. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  1678. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  1679. $method_fragment = $line;
  1680. }
  1681. // class ended
  1682. if (ereg("^@end", $line)) return $protected_keywords;
  1683. }
  1684. }
  1685. /**
  1686. * PARSING METHODS
  1687. */
  1688. function ParseNormalField($line, $protected_keywords, &$field_bitpacked, &$bitpacked_real_type, &$this_class_field_names) {
  1689. if (preg_match("!^\s*([^-{}*@+,<;:]*struct[^-*@+,<;:]+|[^-{}*@+,<;:]+)\b\s*(?:<([^>]*)>)?([*]*)\s*(\w+)((?:\s*,\s*[^:;[]+)*)?\s*(:[0-9]+)?\s*(\[.*\])?\s*(?:__attribute__\(\(([^)]*)\)\))?\s*;!", $line, $captures)) { // regular field
  1690. // captures[1]: type name (may be multiple words, and may not be complete
  1691. // in case of an anonymous bitfield without a name)
  1692. // Curly braces are only allowed in case of structs
  1693. // captures[2]: in case of id<protocol list>, the protocol list, empty
  1694. // otherwise
  1695. // captures[3]: all pointer modifiers (*, **, ...), but only "*" and "**"
  1696. // currently handled
  1697. // captures[4]: the field name (may be last part of type in case of
  1698. // anonymous bit field, e.g. "unsigned long : 32")
  1699. // captures[5]: in case of multiple fields, the other field names (note
  1700. // that things will go wrong if some of the fields in the
  1701. // list are arrays/bitfields, that is only supported for
  1702. // a single field)
  1703. // captures[6]: bitfield specification if any, empty otherwise
  1704. // captures[7]: array specification if any, empty otherwise
  1705. // captures[8]: attributes if any, empty otherwise
  1706. if ($captures[3] != "") { // pointer type -> ok (cannot be wrongly interpreted)
  1707. $type = trim($captures[1]);
  1708. $pointertype = $captures[3];
  1709. $name = trim($captures[4]);
  1710. // print("pointer field: $name: $pointertype $type\n");
  1711. } else {
  1712. // make sure we properly interpret stuff like "unsigned int :32"
  1713. // print("regular: type = \"$captures[1]\", name = $captures[3]\n");
  1714. $pair = $this->ExtractCFieldSimpleTypeAndSingleName($captures[1]." ".$captures[4]);
  1715. $type = $pair["type"];
  1716. $name = $pair["name"];
  1717. if ($name == "") $name = $this->GetAnonBitFieldName();
  1718. // print("regular field: \"$name\": $type\n");
  1719. }
  1720. // print("field \"$name\": \"$type\", attr: $captures[8]\n");
  1721. // if we have id <protocollist> and there's only one protocol in the
  1722. // in the list, we can replace id with the protocol
  1723. if (($type == "id") && ($captures[2] != "") && !strstr(",", $captures[2])) {
  1724. // print("id<protocol>: $type -> $captures[2]Protocol\n");
  1725. $type = $captures[2]."Protocol";
  1726. }
  1727. // in case of "type field1, field2, field3", ", field2, field3" gets
  1728. // stored in othernames
  1729. $othernames = $captures[5];
  1730. $field_prefix = "";
  1731. // Multiple Objective-C fields cannot have the same name, but they
  1732. // are case-insensitive and some only differ in case (only "reserved"
  1733. // fields until now, but that can change)
  1734. while (in_array(strtolower("$field_prefix$name"),$protected_keywords)) $field_prefix.="_";
  1735. if ($this_class_field_names != null) {
  1736. while (in_array(strtolower("$field_prefix$name"),$this_class_field_names)) $field_prefix.="_";
  1737. }
  1738. if ($this->IsKeywordReserved($field_prefix.$name)) $field_prefix .= "_";
  1739. // protect the name of these fields, in case there are methods with
  1740. // the same name
  1741. $this_class_field_names[] = strtolower($field_prefix.$name);
  1742. if ($othernames != "") {
  1743. $other_lower_case_names = preg_split("/\s*,\s*/",$othernames,-1,PREG_SPLIT_NO_EMPTY);
  1744. array_walk($other_lower_case_names,strtolowerref);
  1745. // should actually also check these for conflicts and add underscores if necessary
  1746. $this_class_field_names = array_merge ($this_class_field_names, $other_lower_case_names);
  1747. }
  1748. /*
  1749. // we should also add prefixes to the other names if required, but my
  1750. // php-fu is too weak to replace the elements in original string
  1751. // efficiently, and this problem does not occur in the supported headers
  1752. foreach (explode($othernames, ",") as $othername) {
  1753. while (in_array(strtolower("$field_prefix$othername"),$protected_keywords)) $field_prefix.="_";
  1754. if ($this->IsKeywordReserved($field_prefix.$othernamename)) $field_prefix .= "_";
  1755. }
  1756. */
  1757. // remove "struct" from the type
  1758. $type = preg_replace("!(\b)struct\b!","\1",$type);
  1759. // clean/convert type
  1760. $type = $this->ReplaceObjcType($type);
  1761. $bitpacked_real_type = $type;
  1762. $type = $this->MakeFieldBitPacked($type, $line, $field_bitpacked);
  1763. // add pointer modifiers
  1764. $type = $this->EncodePointerModifiers($type,$pointertype);
  1765. $field = "$field_prefix$name$othernames: $type";
  1766. if ($captures[8] && strstr("deprecated",$captures[8])) $field .= " deprecated";
  1767. $field .= ";";
  1768. $field = $this->MakeFieldInlineArray($field, $line, $name, $type);
  1769. $field = eregi_replace("<.*>", "", $field);
  1770. return $field;
  1771. }
  1772. return "";
  1773. }
  1774. function ParseInstanceVariables ($line, &$struct, $protected_keywords, &$this_class_field_names) {
  1775. $field = null;
  1776. $field_bitpacked = false;
  1777. // print("$line\n");
  1778. // insert macros
  1779. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) {
  1780. if ($struct["valid"]) {
  1781. $struct["fields"][] = $macro;
  1782. return null;
  1783. } else {
  1784. return $macro;
  1785. }
  1786. }
  1787. // got inline struct, probably a reference to a private struct
  1788. if (eregi("[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\*([a-zA-Z0-9_]+)", $line, $captures)) {
  1789. if ($struct["valid"]) {
  1790. // ??? These don't compile as inline records which I thought they did...
  1791. //$struct["fields"][] = "$captures[1] = record end;";
  1792. //$struct["fields"][] = "$captures[1]Pointer = ^$captures[1];";
  1793. $struct["fields"][] = "$captures[2]: Pointer;";
  1794. $struct["ignore"][] = "$captures[2]";
  1795. return null;
  1796. } else {
  1797. //$fields = array();
  1798. //$fields[] = "$captures[1] = record end;";
  1799. //$fields[] = "$captures[1]Ptr = ^$captures[1];";
  1800. $field = "_$captures[2]: Pointer;";
  1801. return $field;
  1802. }
  1803. }
  1804. // got struct
  1805. if (eregi("^[[:space:]]*struct.*{", $line)) {
  1806. $struct["valid"] = true;
  1807. $struct["ispsuedostruct"] = false;
  1808. $struct["fieldnames"] = array();
  1809. return null;
  1810. }
  1811. // create an anonymous struct in case we have bitpacked fields without a
  1812. // surrounding struct
  1813. if (!$struct["valid"] && preg_match("!.*:[0-9]+\s*;!", $line)) {
  1814. $struct["valid"] = true;
  1815. $struct["ispsuedostruct"] = true;
  1816. $struct["fieldnames"] = array();
  1817. }
  1818. // end of bunch of bitfields -> go to normal mode again
  1819. if ($struct["ispsuedostruct"] && !preg_match("!.*:[0-9]+\s*;!", $line)) {
  1820. $struct["name"] = "_anoninternstruct_".$this->current_header["name_clean"].$this->current_header["anoninternstrucs"];
  1821. $this->current_header["anoninternstrucs"]++;
  1822. $struct["isfinished"] = true;
  1823. // make sure the current field isn't added anymore
  1824. $struct["valid"] = false;
  1825. }
  1826. // end of struct
  1827. if (eregi("^[[:space:]]*}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  1828. $struct["name"] = "_".trim($captures[1], " ");
  1829. //print_r($struct);
  1830. $struct["isfinished"] = true;
  1831. return "struct";
  1832. }
  1833. // set field prefix to protect scope
  1834. if (!$struct["valid"]) $field_prefix = "_";
  1835. // remove null-defined macros:
  1836. $line = str_ireplace($this->null_macros, "", $line);
  1837. // replace garbage collector hints in the field
  1838. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  1839. if (preg_match($this->pregex_function_pointer, $line, $captures)) { // function pointer
  1840. // print("function pointer: $line\n");
  1841. $field =$this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1842. } else {
  1843. if (!$struct["valid"])
  1844. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$this_class_field_names);
  1845. else
  1846. // don't register the names of fields of embedded structs and field names of the current
  1847. // class, but do track them for the current struct as there may be conflicts due to
  1848. // Pascal's case-insensitivity
  1849. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$struct["fieldnames"]);
  1850. }
  1851. // mark the field as having a garbage collector field
  1852. if ($garbage_collector_hint) $field = "$field {garbage collector: $garbage_collector_hint }";
  1853. // return field
  1854. if ($struct["valid"]) {
  1855. if (!$struct["bitpacked"]) $struct["bitpacked_first_type"] = $bitpacked_real_type;
  1856. if ($field_bitpacked) $struct["bitpacked"] = true;
  1857. $struct["fields"][] = $field;
  1858. } else {
  1859. return $field;
  1860. }
  1861. }
  1862. // Parses a struct field into a list
  1863. function ParseStructList ($line, $input, $name, $type) {
  1864. $field = "";
  1865. $list = explode(",", $input);
  1866. if (count($list) > 1) {
  1867. $field = " ";
  1868. foreach ($list as $key) {
  1869. $key = trim($key, " ");
  1870. $field .= "$key, ";
  1871. }
  1872. $field = rtrim($field, ", ");
  1873. $field .= ": $type;\n";
  1874. } else {
  1875. $field = " $name: $type;";
  1876. $field = $this->MakeFieldInlineArray($field, $line, $name, $type)."\n";
  1877. }
  1878. return $field;
  1879. }
  1880. // Parses $line into a function declaration string. Handles both
  1881. // function pointers (isexternfunc = false) and external functions
  1882. // (isexternfunc = true)
  1883. function ParseFunctionDeclaration($rettypestr, $retpointertypestr, $funcname, $parastr, $isexternfunc, $deprecatedmods) {
  1884. if ($deprecatedmods != "") $deprecatedmods .= ";";
  1885. if ($this->IsKeywordReserved($funcname)) $funcname .= "_";
  1886. $rettype = trim(str_replace_word("const","",$rettypestr));
  1887. $rettype = $this->ReplaceObjcType($rettype);
  1888. $rettype = $this->EncodePointerModifiers($rettype,$retpointertypestr);
  1889. $params = $this->ConvertCParamsPascal($parastr);
  1890. if ($rettype == "void")
  1891. $result = "procedure ";
  1892. else
  1893. $result = "function ";
  1894. // if no name specified, the caller will add it
  1895. if ($funcname != "") {
  1896. if (!$isexternfunc)
  1897. $result = "$funcname: " . $result;
  1898. else
  1899. $result .= $funcname;
  1900. }
  1901. if ($params != "") $params = "(" . $params . ")";
  1902. if ($rettype == "void")
  1903. $result .= $params . "; cdecl;";
  1904. else
  1905. $result .= $params . ": " . $rettype . "; cdecl;";
  1906. if ($isexternfunc)
  1907. $result .= " external;$deprecatedmods";
  1908. else
  1909. $result .= "\n";
  1910. return $result;
  1911. }
  1912. // Parses $line into the combined $struct_fields string
  1913. function ParseStructFields ($line, $protected_keywords, &$struct_fields, &$found_any_bitpacked, &$all_bitpacked, &$first_bitpacked_type) {
  1914. if (preg_match($this->pregex_function_pointer, $line, $captures)) {
  1915. $struct_fields .= " " . $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1916. $all_bitpacked = false;
  1917. } else {
  1918. // better: keep for entire struct, so we can escape conflicting names due to
  1919. // case-insensitivity
  1920. $tempnewprotected = array();
  1921. $new_field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$tempnewprotected);
  1922. // print("field: '$new_field', bitpacked: $field_bitpacked, any: $found_any_bitpacked, all: $all_bitpacked\n");
  1923. if ($new_field != "") {
  1924. $found_any_bitpacked |= $field_bitpacked;
  1925. if ($struct_fields == "") {
  1926. $all_bitpacked = $field_bitpacked;
  1927. $first_bitpacked_type = $bitpacked_real_type;
  1928. }
  1929. else $all_bitpacked &= $field_bitpacked;
  1930. $struct_fields .= " " . $new_field . $this->AppendEOLComment() . "\n";
  1931. }
  1932. // print(" after: any: $found_any_bitpacked, all: $all_bitpacked\n");
  1933. }
  1934. }
  1935. // Parse a single enum field
  1936. function ParseEnumFields ($line, $file_name, &$block_count, &$auto_increment) {
  1937. // insert macros
  1938. //if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $this->dump[$file_name]["types"]["enums"][$block_count][] = $macro;
  1939. if (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([a-zA-Z_]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // string value
  1940. $captures[2] = trim($captures[2], ", ");
  1941. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1942. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9-]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // integer value
  1943. $captures[2] = trim($captures[2], ", ");
  1944. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1945. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9]+[xX]+[a-fA-F0-9]+)[)]*", $line, $captures)) { // hexadecimal value
  1946. $captures[2] = trim($captures[2], ", ");
  1947. $captures[2] = eregi_replace("^0x", "$", $captures[2]);
  1948. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1949. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)", $line, $captures)) { // << shl value, no ()
  1950. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  1951. // remove integer type hints
  1952. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  1953. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  1954. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1955. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*\(([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)\)", $line, $captures)) { // << shl value
  1956. $captures[2] = trim($captures[2], ", ");
  1957. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  1958. // remove integer type hints
  1959. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  1960. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  1961. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1962. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*[,}]*[[:space:]]*$", $line, $captures)) { // non-value
  1963. // omit lines which started nested structures.
  1964. // bad practice but the single-line regex parser can't handle them
  1965. if (!eregi("[=|]+", $line)) {
  1966. $captures[1] = trim($captures[1], ", ");
  1967. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$auto_increment.";";
  1968. $auto_increment ++;
  1969. }
  1970. }
  1971. }
  1972. // Parse external symbols, enums and typedef's from the header
  1973. function ParseHeaderTypes ($file) {
  1974. $contents = file_get_contents($file);
  1975. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  1976. $any_field_bitpacked = false;
  1977. $all_fields_bitpacked = false;
  1978. // reset comments from previous parsing sections
  1979. $this->ResetComment();
  1980. $lines = explode("\n", $contents);
  1981. foreach ($lines as $line) {
  1982. // skip blocks
  1983. if ($this->SkipBlock($line)) continue;
  1984. // ignore lines
  1985. if (in_array($line, $this->ignore_lines)) continue;
  1986. // build comments
  1987. $this->BuildComment($line, $file_name);
  1988. // build macro blocks
  1989. $this->BuildMacroBlocks($line);
  1990. // garbage collector hints
  1991. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  1992. // remove macros
  1993. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  1994. // remove comments
  1995. $line = $this->RemoveComments($line);
  1996. $line = trim($line, " ");
  1997. if ($got_struct) {
  1998. // insert macros
  1999. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $struct_fields .= "$macro\n";
  2000. // collect fields
  2001. $this->ParseStructFields($line, array(), $struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2002. // got end of struct
  2003. if (ereg("^}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  2004. if ($struct_name == "") {
  2005. $struct_name = $captures[1];
  2006. } else {
  2007. $struct_type = $captures[1];
  2008. }
  2009. // ignore this struct
  2010. if (in_array($struct_name, $this->ignore_types)) continue;
  2011. if ($all_fields_bitpacked) {
  2012. $struct = "$struct_comment$struct_name = $this->bitpacked_record_keyword\n";
  2013. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2014. $struct_fields = str_replace(" "," ",$struct_fields);
  2015. $struct_fields .= " );\n";
  2016. } else {
  2017. $struct = "$struct_comment$struct_name = $this->record_keyword\n";
  2018. }
  2019. $struct .= $struct_fields;
  2020. $struct .= " end$deprecatedmods;\n";
  2021. if (($struct_type) && ($struct_name != $struct_type)) {
  2022. $struct .= "$struct_type = $struct_name;\n";
  2023. }
  2024. // pointer type
  2025. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2026. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2027. $this->dump["global_structs"][] = $struct_name;
  2028. $got_struct = false;
  2029. $any_field_bitpacked = false;
  2030. $all_fields_bitpacked = false;
  2031. }
  2032. }
  2033. // got single-line struct
  2034. if (ereg("^typedef[[:space:]]+struct[[:space:]]+{(.*)}[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2035. $struct_name = trim($captures[2], " ");
  2036. if (!in_array($struct_name, $this->ignore_types)) {
  2037. // break the struct into lines
  2038. $single_struct_fields = "";
  2039. $fields = explode(";", $captures[1]);
  2040. $comment = $this->InsertCurrentComment();
  2041. $this->ResetComment();
  2042. // parse each line
  2043. foreach ($fields as $field) {
  2044. $field = trim($field);
  2045. $this->ParseStructFields($field.";", array(), $single_struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2046. }
  2047. // merge the fields into the definition
  2048. $struct = "$comment\n"."$struct_name = ";
  2049. if ($all_fields_bitpacked) {
  2050. $struct .= "$this->bitpacked_record_keyword\n";
  2051. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2052. $single_struct_fields = str_replace(" "," ",$single_struct_fields);
  2053. $single_struct_fields .= " );\n";
  2054. } else $struct .= "$this->record_keyword\n";
  2055. $struct .= $single_struct_fields;
  2056. $struct .= " end$deprecatedmods;\n";
  2057. // pointer type
  2058. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2059. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2060. $this->dump["global_structs"][] = $struct_name;
  2061. $any_field_bitpacked = false;
  2062. $all_fields_bitpacked = false;
  2063. //print("$single_struct_fields\n");
  2064. }
  2065. // got begin of struct
  2066. } elseif (ereg("^typedef struct(.*){", $line, $captures)) {
  2067. $struct_name = trim($captures[1], " ");
  2068. if (!in_array($struct_name, $this->ignore_types)) {
  2069. $struct_type = null;
  2070. $struct_fields = "";
  2071. $struct_comment = $this->InsertCurrentComment();
  2072. $this->ResetComment();
  2073. if ($struct_comment != "") $struct_comment = "$struct_comment\n";
  2074. $got_struct = true;
  2075. }
  2076. }
  2077. // got function pointer type
  2078. if (preg_match($this->pregex_function_pointer_typedef, $line, $captures)) {
  2079. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[6], false, $deprecatedmods);
  2080. $functypename = $captures[7];
  2081. if ($functypename == "") $functypename = $captures[5];
  2082. if ($functypename == "") $functypename = $captures[4];
  2083. $this->dump[$file_name]["types"]["callbacks"][$functypename] = $typestr;
  2084. // record if it is a function type instead of a function pointer type
  2085. if ($captures[3] == "") $this->implicit_function_pointer_types[] = $functypename;
  2086. continue;
  2087. }
  2088. // #defines
  2089. $got_define = false;
  2090. if (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\(\(.*\)(.*)\)", $line, $captures)) { // named constant with type case
  2091. $got_define = true;
  2092. } elseif (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+[(]*([0-9.-]+)[)]*", $line, $captures)) { //integer
  2093. $got_define = true;
  2094. }
  2095. if ($got_define) {
  2096. $define_name = $captures[1];
  2097. if (!in_array($define_name, $this->ignore_types)) {
  2098. $define_name = $this->ReplaceObjcType($define_name);
  2099. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["defines"][] = $this->InsertCurrentComment();
  2100. $this->AppendCurrentComment($this->dump[$file_name]["types"]["defines"]);
  2101. $this->dump[$file_name]["types"]["defines"][] = $define_name." = ".$captures[2].";".$this->AppendEOLComment();
  2102. } else {
  2103. $this->ResetComment();
  2104. }
  2105. }
  2106. // parse enum fields
  2107. if (($got_enum) || ($got_named_enum)) {
  2108. // print($line."\n");
  2109. $this->ParseEnumFields($line, $file_name, &$block_count, &$auto_increment);
  2110. // found the end
  2111. if (ereg("^};", $line)) $got_enum = false;
  2112. }
  2113. // ==== got inline named enum ===
  2114. if (ereg("^[[:space:]]*enum[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]*{(.*)};", $line, $captures)) {
  2115. //print("$line\n");
  2116. $enum_name = trim($captures[1], " ");
  2117. if (!in_array($enum_name, $this->ignore_types)) {
  2118. $block_count ++;
  2119. $auto_increment = 0;
  2120. // break the enum into lines
  2121. $fields = explode(",", $captures[2]);
  2122. //$this->AppendCurrentMacro($this->dump[$file_name]["types"]["enums"][$block_count]);
  2123. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2124. if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2125. $this->ResetComment();
  2126. // parse each line
  2127. foreach ($fields as $field) {
  2128. $field = trim($field, " ");
  2129. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2130. }
  2131. }
  2132. continue;
  2133. }
  2134. // ==== got inline enum ===
  2135. if (ereg("^[[:space:]]*enum[[:space:]]*{(.*)};", $line, $captures)) {
  2136. //print("$line\n");
  2137. $block_count ++;
  2138. $auto_increment = 0;
  2139. // break the enum into lines
  2140. $fields = explode(",", $captures[1]);
  2141. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2142. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2143. $this->ResetComment();
  2144. // parse each line
  2145. foreach ($fields as $field) {
  2146. $field = trim($field, " ");
  2147. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2148. }
  2149. continue;
  2150. }
  2151. // ==== got enum ===
  2152. if (ereg("^enum", $line)) {
  2153. $got_enum = true;
  2154. $block_count ++;
  2155. $auto_increment = 0;
  2156. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2157. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2158. }
  2159. // terminate named enum
  2160. if ($got_named_enum) {
  2161. if (ereg("^}[[:space:]]*([a-zA-Z0-9_]+);", $line, $captures)) {
  2162. $got_named_enum = false;
  2163. $named_enum = trim($named_enum, ", \n");
  2164. $name = $captures[1];
  2165. if (!in_array($name, $this->ignore_types)) {
  2166. $this->dump[$file_name]["types"]["named_enums"][] = "$name = culong;";
  2167. $this->dump["global_types"][$name] = $name;
  2168. }
  2169. }
  2170. }
  2171. // ==== got named enum ===
  2172. if (ereg("^typedef enum {", $line)) {
  2173. $got_named_enum = true;
  2174. $named_enum = "";
  2175. $auto_increment = 0;
  2176. $block_count ++;
  2177. $this->AppendCurrentComment($this->dump[$file_name]["types"]["named_enums"][$block_count]);
  2178. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["named_enums"][] = $this->InsertCurrentComment();
  2179. }
  2180. // ==== external functions ===
  2181. // doesn't work when $this->external_string_macros is added to
  2182. // the string at initialisation time, because it can still change
  2183. // later (while loading frameworks.xml)
  2184. if (preg_match("!^(?:$this->external_string_macros)+".$this->pregex_external_function_end, $line, $captures)) {
  2185. // ignore symbols
  2186. if (in_array($captures[3], $this->ignore_types)) continue;
  2187. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], true, $deprecatedmods);
  2188. $this->dump[$file_name]["types"]["functions"][] = $typestr;
  2189. continue;
  2190. }
  2191. // ==== external string constant ===
  2192. if (eregi("^($this->external_string_macros)+[[:space:]]+NSString[[:space:]]*\*[[:space:]]*(const)*[[:space:]]*([a-zA-Z0-9_]+)", $line, $captures)) {
  2193. $name = $captures[3];
  2194. if (in_array($name, $this->ignore_types)) continue;
  2195. // insert comments
  2196. $this->AppendCurrentComment($this->dump[$file_name]["types"]["string_constant"]);
  2197. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["string_constant"][] = $this->InsertCurrentComment();
  2198. $this->dump[$file_name]["types"]["string_constant"][] = "$name: $this->string_macro$deprecatedmods; cvar; external;";
  2199. }
  2200. // ==== external symbol ===
  2201. if (eregi("^($this->external_string_macros)+[[:space:]]+([a-zA-Z0-9_ ]+)[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2202. $name = $captures[3];
  2203. $type = $captures[2];
  2204. // ignore symbols
  2205. if (in_array($name, $this->ignore_types)) continue;
  2206. $type = istr_replace_word("const", "", $type);
  2207. $type = $this->ReplaceObjcType(trim($type, " "));
  2208. $this->AppendCurrentComment($this->dump[$file_name]["types"]["external_symbol"]);
  2209. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["external_symbol"][] = $this->InsertCurrentComment();
  2210. $this->dump[$file_name]["types"]["external_symbol"][] = "$name: $type$deprecatedmods; cvar; external;";
  2211. }
  2212. // ==== got typedef ===
  2213. if (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // defined struct type
  2214. $real_type = $captures[1];
  2215. $struct_type = $captures[1];
  2216. $new_type = $captures[2];
  2217. // ignore types
  2218. if (in_array($struct_type, $this->ignore_types)) continue;
  2219. if (in_array($new_type, $this->ignore_types)) continue;
  2220. $this->AddTypeDef($this->dump[$file_name], "$struct_type = record end$deprecatedmods;");
  2221. $struct_type = $this->ReplaceObjcType($struct_type);
  2222. if ($new_type != $struct_type) {
  2223. $this->AddTypeDef($this->dump[$file_name], "$new_type = $struct_type$deprecatedmods;");
  2224. $this->dump["global_types"][$new_type] = $real_type;
  2225. }
  2226. $this->opaque_structs[] = $struct_type;
  2227. // also add pointer type to the opaque struct
  2228. $this->AddTypeDef($this->dump[$file_name], $new_type."Ptr = ^$new_type$deprecatedmods;");
  2229. $this->dump["global_types"][$struct_type] = "record end";
  2230. $this->dump["global_types"][$new_type."Ptr"] = "^".$new_type;
  2231. } elseif (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // pointer to struct
  2232. $real_type = $captures[1];
  2233. $clean_name = trim($captures[2], "*");
  2234. $pointer_type = $captures[1];
  2235. // ignore types
  2236. if (in_array($clean_name, $this->ignore_types)) continue;
  2237. $pointer_type = "Pointer";
  2238. $this->AddTypeDef($this->dump[$file_name], "$clean_name = $pointer_type$deprecatedmods;");
  2239. $this->dump["global_types"][$clean_name] = $real_type;
  2240. // also add pointer type
  2241. $this->AddTypeDef($this->dump[$file_name], $clean_name."Ptr = ^$clean_name$deprecatedmods;");
  2242. $this->dump["global_types"][$clean_name."Ptr"] = "^".$clean_name;
  2243. } elseif (ereg("^typedef[[:space:]]+(const)*[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_*]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // struct type (complex)
  2244. $real_type = $captures[1];
  2245. $typedef_name = $captures[3];
  2246. // ignore types
  2247. if (in_array($typedef_name, $this->ignore_types)) continue;
  2248. $captures[2] = $this->FormatObjcType($captures[2], $modifiers);
  2249. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = ".$captures[2].$deprecatedmods.";");
  2250. $this->dump["global_types"][$typedef_name] = $real_type;
  2251. // also add pointer type
  2252. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2253. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2254. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // single-word type
  2255. $real_type = $captures[1];
  2256. // type is a pointer
  2257. if ($captures[2][0] == "*") {
  2258. $captures[2] = trim($captures[2], "*");
  2259. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2260. // ignore types
  2261. if (in_array($captures[2], $this->ignore_types)) continue;
  2262. $this->AddTypeDef($this->dump[$file_name], $captures[2]." = ^".$captures[1]."$deprecatedmods;");
  2263. $this->dump["global_types"][$captures[2]] = $real_type;
  2264. } else {
  2265. $captures[2] = trim($captures[2], "*");
  2266. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2267. // ignore types
  2268. if (in_array($captures[2], $this->ignore_types)) continue;
  2269. $this->AddTypeDef($this->dump[$file_name],$captures[2]." = ".$captures[1]."$deprecatedmods;");
  2270. }
  2271. // also add pointer type
  2272. $this->AddTypeDef($this->dump[$file_name], $captures[2]."Ptr = ^$captures[2]$deprecatedmods;");
  2273. $this->dump["global_types"][$captures[2]."Ptr"] = "^".$captures[2];
  2274. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // double-word type
  2275. $real_type = $captures[1];
  2276. $typedef_name = trim($captures[3], "*");
  2277. $long_type = $captures[1]." ".$captures[2];
  2278. $long_type = $this->ReplaceObjcType($long_type);
  2279. // ignore types
  2280. if (in_array($captures[2], $this->ignore_types)) continue;
  2281. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = $long_type$deprecatedmods;");
  2282. $this->dump["global_types"][$typedef_name] = $real_type;
  2283. // also add pointer type
  2284. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2285. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2286. }
  2287. }
  2288. //print_r($this->dump[$file_name]["types"]);
  2289. }
  2290. // Parse all protocols in a header
  2291. function ParseHeaderProtocols ($file) {
  2292. $contents = file_get_contents($file);
  2293. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2294. // reset comments from previous parsing sections
  2295. $this->ResetComment();
  2296. $lines = explode("\n", $contents);
  2297. foreach ($lines as $line) {
  2298. // skip blocks
  2299. if ($this->SkipBlock($line)) continue;
  2300. // ignore lines
  2301. if (in_array($line, $this->ignore_lines)) continue;
  2302. // remove macros
  2303. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2304. // build comments
  2305. if (!$got_got_protocol) $this->BuildComment($line, $file_name);
  2306. // parse protocol
  2307. if ($got_protocol) {
  2308. // build comments
  2309. $this->BuildComment($line, $file_name);
  2310. // remove comments
  2311. $line = $this->RemoveComments($line);
  2312. // found property
  2313. if ($this->LineHasProperty($line, $captures)) {
  2314. $properties = $this->ParseClassProperty($current_protocol, $captures, $deprecatedmods);
  2315. foreach ($properties as $property) {
  2316. if ($property["setter"]) {
  2317. $property["setter"]["comment"] = $this->InsertCurrentComment();
  2318. $this->current_header["protocols"][$current_protocol]["methods"][$property["setter"]["objc_method"]] = $property["setter"];
  2319. // append to master list of protocols
  2320. $this->dump["protocols"][$current_protocol][] = $property["setter"];
  2321. }
  2322. if ($property["getter"]) {
  2323. $property["getter"]["comment"] = $this->InsertCurrentComment();
  2324. $this->current_header["protocols"][$current_protocol]["methods"][$property["getter"]["objc_method"]] = $property["getter"];
  2325. // append to master list of protocols
  2326. $this->dump["protocols"][$current_protocol][] = $property["getter"];
  2327. }
  2328. }
  2329. continue;
  2330. }
  2331. // found method
  2332. $method = null;
  2333. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2334. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), true, $deprecatedmods);
  2335. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2336. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), false, $deprecatedmods);
  2337. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2338. $method_fragment = $line;
  2339. }
  2340. // append to classes
  2341. if (($method) /* && (!in_array($method["name"], $this->ignore_methods))*/ ) {
  2342. // add comment to the method
  2343. $method["comment"] = $this->InsertCurrentComment();
  2344. $this->current_header["protocols"][$current_protocol]["methods"][$method["objc_method"]] = $method;
  2345. // append to master list of protocols
  2346. $this->dump["protocols"][$current_protocol][] = $method;
  2347. }
  2348. // found the end
  2349. if (ereg("^@end", $line)) {
  2350. $this->ResetComment();
  2351. $got_protocol = false;
  2352. }
  2353. }
  2354. // got protocol
  2355. if ((eregi($this->regex_objc_protocol, $line, $captures)) && (!eregi(".*;$", $line))) {
  2356. $got_protocol = true;
  2357. $current_protocol = $captures[1];
  2358. print("+ Protocol $current_protocol\n");
  2359. if ($this->comment_terminated) $this->current_header["protocols"][$current_protocol]["comment"] = $this->InsertCurrentComment();
  2360. $this->current_header["protocols"][$current_protocol]["name"] = $captures[1];
  2361. }
  2362. }
  2363. //print_r($this->current_class);
  2364. }
  2365. // Parse all categories in a header
  2366. function ParseHeaderCategories ($file) {
  2367. $contents = file_get_contents($file);
  2368. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2369. // reset comments from previous parsing sections
  2370. $this->ResetComment();
  2371. $lines = explode("\n", $contents);
  2372. foreach ($lines as $line) {
  2373. // skip blocks
  2374. if ($this->SkipBlock($line)) continue;
  2375. // ignore lines
  2376. if (in_array($line, $this->ignore_lines)) continue;
  2377. // remove macros
  2378. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2379. // build comments
  2380. if (!$got_category) $this->BuildComment($line, $file_name);
  2381. // parse category
  2382. if ($got_category) {
  2383. // build comments
  2384. $this->BuildComment($line, $file_name);
  2385. // remove comments
  2386. $line = $this->RemoveComments($line);
  2387. // found property
  2388. if ($this->LineHasProperty($line, $captures)) {
  2389. $properties = $this->ParseClassProperty($current_category, $captures, $deprecatedmods);
  2390. if (!in_array($current_category, $this->ignore_categories)) {
  2391. foreach ($properties as $property) {
  2392. if ($property["setter"]) {
  2393. if ($this->AddMethodToClass($property["setter"], $this->current_class)) {
  2394. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["setter"];
  2395. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["setter"];
  2396. }
  2397. }
  2398. if ($property["getter"]) {
  2399. if ($this->AddMethodToClass($property["getter"], $this->current_class)) {
  2400. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["getter"];
  2401. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["getter"];
  2402. }
  2403. }
  2404. }
  2405. }
  2406. continue;
  2407. }
  2408. // build method fragment
  2409. if ($method_fragment) $method_fragment .= " ".trim($line);
  2410. // found method fragment termination
  2411. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2412. $line = $method_fragment;
  2413. $method_fragment = null;
  2414. }
  2415. // found method
  2416. $method = null;
  2417. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2418. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2419. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2420. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2421. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2422. $method_fragment = $line;
  2423. }
  2424. // append to classes
  2425. if (($method) && !in_array($current_category, $this->ignore_categories)) {
  2426. if ($current_class) {
  2427. if ($this->AddMethodToClass($method, $this->current_class)) {
  2428. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $method;
  2429. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $method;
  2430. }
  2431. }
  2432. }
  2433. // found the end
  2434. if (ereg("^@end", $line)) {
  2435. $got_category = false;
  2436. continue;
  2437. }
  2438. }
  2439. // got category
  2440. if (eregi($this->regex_objc_category, $line, $captures)) {
  2441. $got_category = true;
  2442. $category_owner = $file_name;
  2443. $category_name = $captures[2];
  2444. $current_class = $captures[1];
  2445. $current_category = $category_name;
  2446. if (!in_array($current_category, $this->ignore_categories)) {
  2447. // Protect category names against duplicate identifiers by appending the class it extends to the name
  2448. if ((count($this->dump["categories"][$current_category]) > 0) || (in_array($category_name, $this->cocoa_classes))) {
  2449. $current_category = $category_name."_".$current_class;
  2450. $this->dump[$file_name]["categories"][$current_category]["external"] = true;
  2451. } else {
  2452. $this->dump[$file_name]["categories"][$current_category]["external"] = false;
  2453. }
  2454. $this->current_class = &$this->dump[$category_owner]["classes"][$current_class];
  2455. // insert into headers category array
  2456. $this->dump[$file_name]["categories"][$current_category]["name"] = $current_category;
  2457. $this->dump[$file_name]["categories"][$current_category]["super"] = $current_class;
  2458. $this->dump[$file_name]["categories"][$current_category]["comment"] = $this->InsertCurrentComment();
  2459. $this->dump[$file_name]["categories"][$current_category]["external_name"] = $category_name;
  2460. // append to master list of categories
  2461. $this->dump["categories"][$category_name][] = $current_class;
  2462. }
  2463. }
  2464. }
  2465. //print_r($this->current_class);
  2466. }
  2467. // Parse a property into accessor methods
  2468. function ParseClassProperty ($class, $parts, $deprecatedmods) {
  2469. $method = array();
  2470. //print_r($parts);
  2471. // property has attributes
  2472. if (count($parts) == 5) {
  2473. $property["parameters"] = explode(",", $parts[1]);
  2474. $attributes = $parts[1];
  2475. $type = $parts[2];
  2476. $pointertype = $parts[3];
  2477. $content = $parts[4];
  2478. } else {
  2479. $property["parameters"] = array();
  2480. $type = $parts[1];
  2481. $pointertype = $parts[2];
  2482. $content = $parts[3];
  2483. }
  2484. // unspecified type -> id
  2485. if ($type == "") $type = "id";
  2486. // get property list
  2487. $list = explode(",", $content);
  2488. if (count($list) > 1) {
  2489. $property_list = array();
  2490. foreach ($list as $key) {
  2491. // clean the name and remove the return type
  2492. $property_list[] = trim($key);
  2493. }
  2494. //print_r($property_list);
  2495. } else {
  2496. $property_list = array($content);
  2497. }
  2498. $methods = array();
  2499. foreach ($property_list as $property_name) {
  2500. // property name
  2501. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $property_name, $captures)) $property["name"] = ucwords($captures[1]);
  2502. // property type
  2503. $type = $this->ConvertReturnType($type,$pointertype);
  2504. // prepare for appending
  2505. if ($deprecatedmods != "") $deprecatedmods .= ";";
  2506. // setter
  2507. if (!in_array("readonly", $property["parameters"])) {
  2508. $method["setter"] = array();
  2509. $name = $property["name"];
  2510. if (!$this->GetPropertyName("setter", $property["parameters"], $name)) {
  2511. $name = "set$name";
  2512. }
  2513. // protect method name from keywords
  2514. if ($this->IsKeywordReserved($name)) $name .= "_";
  2515. $method["setter"]["def"] = "procedure $name (newValue: $type);";
  2516. $method["setter"]["objc_method"] = "$name:";
  2517. $method["setter"]["class"] = $class;
  2518. $method["setter"]["name"] = $name;
  2519. $method["setter"]["kind"] = "procedure";
  2520. $method["setter"]["deprecated"]=$deprecatedmods;
  2521. //$method["setter"]["comment"] = $this->InsertCurrentComment();
  2522. }
  2523. // getter
  2524. $method["getter"] = array();
  2525. $name = $property["name"];
  2526. if (!$this->GetPropertyName("getter", $property["parameters"], $name)) {
  2527. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  2528. }
  2529. // protect method name from keywords
  2530. if ($this->IsKeywordReserved($name)) $name .= "_";
  2531. $method["getter"]["def"] = "function $name: $type;";
  2532. $method["getter"]["objc_method"] = $name;
  2533. $method["getter"]["class"] = $class;
  2534. $method["getter"]["name"] = $name;
  2535. $method["getter"]["kind"] = "function";
  2536. $method["getter"]["deprecated"]=$deprecatedmods;
  2537. //$method["getter"]["comment"] = $this->InsertCurrentComment();
  2538. // append to array of methods
  2539. $methods[] = $method;
  2540. }
  2541. //print_r($methods);
  2542. return $methods;
  2543. }
  2544. // Parse header classes and methods
  2545. function ParseHeaderClasses ($file) {
  2546. $contents = file_get_contents($file);
  2547. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2548. $line_count = 0;
  2549. // reset comments from previous parsing sections
  2550. $this->ResetComment();
  2551. $lines = explode("\n", $contents);
  2552. foreach ($lines as $line) {
  2553. $line_count++;
  2554. // skip blocks
  2555. if ($this->SkipBlock($line)) continue;
  2556. // ignore lines
  2557. if (in_array($line, $this->ignore_lines)) continue;
  2558. // remove macros
  2559. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2560. // remove external class macros
  2561. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2562. // build comments
  2563. if (!$got_class) $this->BuildComment($line, $file_name);
  2564. // parse instance vars
  2565. if ($got_instance_vars) {
  2566. // scope compiler directive
  2567. if (eregi($this->regex_scope_compiler_directive, $line, $captures)) {
  2568. $this->instance_var_scope = $captures[1];
  2569. continue;
  2570. }
  2571. // remove comments
  2572. $line = $this->RemoveComments($line);
  2573. // parse instance variables
  2574. $result = $this->ParseInstanceVariables($line, $struct, $this->GetProtectedKeywords($this->current_class),$this->dump["master"][$this->current_class["name"]]["field_names"]);
  2575. // parse structures
  2576. if ($struct["isfinished"]) {
  2577. //print_r($struct);
  2578. //$this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": $current"."_".$struct["name"].";";
  2579. $this->dump[$file_name]["classes"][$current]["ivars_structs"][] = $struct;
  2580. // print inline-record type
  2581. if ($struct["bitpacked"]) {
  2582. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->bitpacked_record_keyword;
  2583. $this->dump[$file_name]["classes"][$current]["ivars"][] = $this->BitPackedForceAlignment($struct["bitpacked_first_type"], " ", " ");
  2584. } else {
  2585. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->record_keyword;
  2586. }
  2587. // print fields
  2588. if ($struct["fields"]) {
  2589. foreach ($struct["fields"] as $field) $this->dump[$file_name]["classes"][$current]["ivars"][] = " ".$field;
  2590. }
  2591. if ($struct["bitpacked"]) $this->dump[$file_name]["classes"][$current]["ivars"][] = " );";
  2592. $this->dump[$file_name]["classes"][$current]["ivars"][] = " end;";
  2593. $struct = null;
  2594. }
  2595. if(($result != null) && ($result != "struct")) {
  2596. //print($result);
  2597. // add a single string or an array of fields to the ivars array
  2598. if (count($result) <= 1) {
  2599. $this->dump[$file_name]["classes"][$current]["ivars"][] = $result;
  2600. } else {
  2601. foreach ($result as $field) {
  2602. $this->dump[$file_name]["classes"][$current]["ivars"][] = $field;
  2603. }
  2604. }
  2605. }
  2606. // instance var section terminated.
  2607. if (eregi("^\s*}\s*[;]*$", $line)) {
  2608. $struct = null;
  2609. $got_instance_vars = false;
  2610. $this->instance_var_scope = null;
  2611. }
  2612. } elseif ($got_class) { // parse the class
  2613. // the instance variable section started after the class line and no other ivar's were parsed yet
  2614. if (!$this->dump[$file_name]["classes"][$current]["ivars"]) {
  2615. if (eregi("{\s*$", $line)) {
  2616. $got_instance_vars = true;
  2617. continue;
  2618. }
  2619. }
  2620. // build comments
  2621. $this->BuildComment($line, $file_name);
  2622. // remove comments
  2623. $line = $this->RemoveComments($line);
  2624. // found property
  2625. if ($this->LineHasProperty($line, $captures)) {
  2626. $properties = $this->ParseClassProperty($current, $captures, $deprecatedmods);
  2627. foreach ($properties as $property) {
  2628. if ($property["setter"]) {
  2629. if ($this->AddMethodToClass($property["setter"], $this->dump[$file_name]["classes"][$current])) {
  2630. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["setter"];
  2631. }
  2632. }
  2633. if ($property["getter"]) {
  2634. if ($this->AddMethodToClass($property["getter"], $this->dump[$file_name]["classes"][$current])) {
  2635. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["getter"];
  2636. }
  2637. }
  2638. }
  2639. continue;
  2640. }
  2641. // build method fragment
  2642. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2643. // found method fragment termination
  2644. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2645. $line = $method_fragment;
  2646. $method_fragment = null;
  2647. }
  2648. // found method
  2649. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2650. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2651. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2652. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2653. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2654. }
  2655. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2656. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2657. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2658. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2659. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2660. }
  2661. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2662. $method_fragment = $line;
  2663. }
  2664. // found the end
  2665. if (ereg("^@end", $line)) {
  2666. $got_class = false;
  2667. $this->ResetComment();
  2668. }
  2669. }
  2670. // ==== got class ====
  2671. if ((eregi($this->regex_objc_class, $line, $captures)) || (eregi($this->regex_objc_class_no_super, $line, $captures))) {
  2672. $current = $captures[1];
  2673. $got_class = true;
  2674. // check for instance variable section
  2675. if (eregi("{\s*$", $line)) $got_instance_vars = true;
  2676. // get the protocols which the class adopts
  2677. if (eregi($this->regex_objc_class, $line, $captures)) {
  2678. if ($captures[3]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[3];
  2679. } else {
  2680. if ($captures[2]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[2];
  2681. }
  2682. // clean up the conforms string
  2683. if ($this->dump[$file_name]["classes"][$current]["adopts"]) {
  2684. $conform_protocols = explode(",", $this->dump[$file_name]["classes"][$current]["adopts"]);
  2685. $protocol_list = array();
  2686. foreach ($conform_protocols as $protocol) {
  2687. $protocol = trim($protocol, "<> ");
  2688. $protocol_clean .= $protocol."$this->protocol_suffix, ";
  2689. $protocol_list[] = $protocol;
  2690. }
  2691. $protocol_clean = trim($protocol_clean, ", ");
  2692. $this->dump[$file_name]["classes"][$current]["adopts"] = $protocol_clean;
  2693. $this->dump[$file_name]["classes"][$current]["protocols"] = $protocol_list;
  2694. $protocol_clean = "";
  2695. }
  2696. $this->dump[$file_name]["classes"][$current]["name"] = $captures[1];
  2697. $this->dump[$file_name]["classes"][$current]["super"] = $captures[2];
  2698. $this->dump[$file_name]["classes"][$current]["super_class"] = &$this->dump["master"][$captures[2]];
  2699. $this->dump[$file_name]["classes"][$current]["file_name"] = $file_name;
  2700. $this->dump[$file_name]["classes"][$current]["file_clean"] = substr($file_name, 0, (strripos($file_name, ".")));
  2701. $this->dump[$file_name]["classes"][$current]["protected_keywords"] = array();
  2702. $this->dump[$file_name]["classes"][$current]["declared_methods"] = array();
  2703. $this->dump[$file_name]["classes"][$current]["comment"] = $this->InsertCurrentComment();
  2704. $this->dump[$file_name]["category_methods"] = array();
  2705. $this->current_class = &$this->dump[$file_name]["classes"][$current];
  2706. // append master class listing
  2707. $this->dump["master"][$current] = &$this->dump[$file_name]["classes"][$current];
  2708. // preparse for protected keywords
  2709. $this->PreparseClass($lines, $line_count);
  2710. // preparse for category methods that may present naming conflicts
  2711. $category_methods = $this->PreparseCategoryMethods($file);
  2712. // add category methods to protected keywords
  2713. if ($category_methods) $this->current_class["protected_keywords"] = array_merge($this->current_class["protected_keywords"], $category_methods);
  2714. // print class hierarchy
  2715. if ($this->show_class_hierarchy) {
  2716. $this->GetClassHierarchy($this->current_class, $hierarchy);
  2717. $hierarchy_string = "";
  2718. foreach ($hierarchy as $value) {
  2719. $hierarchy_string .= "$value->";
  2720. }
  2721. $hierarchy_string = trim($hierarchy_string, "->");
  2722. print(" - $current: $hierarchy_string\n");
  2723. }
  2724. $this->class_count ++;
  2725. //print_r($this->dump[$file_name]["classes"][$current]);
  2726. }
  2727. }
  2728. //print_r($this->dump[$file_name]["classes"][$current]);
  2729. }
  2730. // Parse categories which depend on another header
  2731. function ParseHeaderDependents ($file) {
  2732. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2733. $this->ParseHeaderCategories($file);
  2734. print("+ Parsed $file_name for dependents\n");
  2735. }
  2736. // Main entry to parse a header
  2737. function ParseHeader ($file) {
  2738. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2739. $name_clean = substr($file_name, 0, (strripos($file_name, ".")));
  2740. // get the framework we're parsing from if it was specified during a batch parse
  2741. if ((!$this->framework) && (eregi("/([a-zA-Z]+)\.framework", $file, $captures))) $this->framework = strtolower($captures[1]);
  2742. // set the dump for the header
  2743. $this->dump[$file_name]["path"] = "$this->root$this->out/$this->framework/$name_clean.inc";
  2744. $this->dump[$file_name]["path_partial"] = "$this->framework/$name_clean.inc";
  2745. $this->dump[$file_name]["path_merge"] = $this->dump[$file_name]["path"].".merge";
  2746. $this->dump[$file_name]["framework"] = $this->framework;
  2747. $this->dump[$file_name]["name"] = $file_name;
  2748. $this->dump[$file_name]["name_clean"] = $name_clean;
  2749. $this->dump[$file_name]["anoninternstrucs"] = 0;
  2750. $this->dump[$file_name]["anonbitfields"] = 0;
  2751. $this->current_header = &$this->dump[$file_name];
  2752. // parse each section of the header
  2753. $this->ParseHeaderTypes($file);
  2754. $this->ParseHeaderProtocols($file);
  2755. $this->ParseHeaderClasses($file);
  2756. print("+ Parsed $file_name\n");
  2757. }
  2758. // Parse all AppKit and Foundation framework headers
  2759. function ParseAllFrameworks ($ignore_files, $parse_only) {
  2760. foreach ($this->frameworks as $framework_name => $framework_info) {
  2761. // framework is disabled
  2762. if ($framework_info["enabled"] != 1) continue;
  2763. // set the current framework being parsed
  2764. $this->framework = $framework_name;
  2765. if ($this->out != "/") {
  2766. $path = $this->root.$this->out."/".$framework_info["root"];
  2767. } else {
  2768. $path = $this->root.$framework_info["root"];
  2769. }
  2770. // Load the header if found
  2771. if (file_exists($path)) {
  2772. $contents = file_get_contents($path);
  2773. $lines = explode("\n", $contents);
  2774. foreach ($lines as $line) {
  2775. $header = null;
  2776. $path = null;
  2777. // parse the header path from the {$include} macro
  2778. if (eregi($framework_info["include_pattern"], $line, $captures)) {
  2779. $header = $captures[1].".h";
  2780. $path = $framework_info["headers"]."/$header";
  2781. }
  2782. // parse the header path from {-parse} directive
  2783. if (eregi("^\{-parse[[:space:]]+(.*)[[:space:]]*\}", $line, $captures)) {
  2784. $header = $captures[1];
  2785. $path = $framework_info["headers"]."/$header";
  2786. }
  2787. // parse the header if valid
  2788. if (file_exists($path)) {
  2789. // main header
  2790. if ($parse_only) {
  2791. if (@in_array($header, $parse_only)) $this->ParseHeader($path);
  2792. } elseif (@!in_array($header, $ignore_files)) {
  2793. $this->ParseHeader($path);
  2794. }
  2795. // header dependents
  2796. if ($parse_only) {
  2797. if (@in_array($header, $parse_only)) $this->ParseHeaderDependents($path);
  2798. } elseif (@!in_array($header, $ignore_files)) {
  2799. $this->ParseHeaderDependents($path);
  2800. }
  2801. } elseif ($header) {
  2802. print("*** The header $path could not be found. ***\n");
  2803. $this->warning_count++;
  2804. }
  2805. }
  2806. } else {
  2807. die("FATAL: The master include \"$path\" is missing.\n");
  2808. }
  2809. }
  2810. // diagnostics
  2811. print("\n• Parsed $this->method_count methods in $this->class_count classes.\n\n");
  2812. if ($this->warning_count > 0) print("• $this->warning_count warnings were encountered.\n\n");
  2813. }
  2814. /**
  2815. * MAIN METHODS
  2816. */
  2817. // Parse all classes/categories (non-delegate) from the header
  2818. function CollectHeaderClasses ($file) {
  2819. // can't find the header, bail
  2820. if (!file_exists($file)) return;
  2821. $contents = file_get_contents($file);
  2822. $lines = explode("\n", $contents);
  2823. foreach ($lines as $line) {
  2824. // remove external class macros
  2825. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2826. // classes
  2827. if (eregi($this->regex_objc_class, $line, $captures)) {
  2828. $this->defined_cocoa_classes[] = $captures[1];
  2829. // may already have been parsed as an anonymous class
  2830. if (!in_array($captures[1], $this->cocoa_classes))
  2831. $this->cocoa_classes[] = $captures[1];
  2832. }
  2833. if (eregi($this->regex_objc_class_no_super, $line, $captures)) {
  2834. $this->defined_cocoa_classes[] = $captures[1];
  2835. // may already have been parsed as an anonymous class
  2836. if (!in_array($captures[1], $this->cocoa_classes))
  2837. $this->cocoa_classes[] = $captures[1];
  2838. }
  2839. // anonymous classes ===
  2840. if (eregi($this->regex_objc_anon_class, $line, $captures)) {
  2841. $anon_classes = explode(",", $captures[1]);
  2842. foreach ($anon_classes as $anon_class) {
  2843. $anon_class=trim($anon_class);
  2844. // may already have been parsed as a regular class
  2845. if (!in_array($anon_class, $this->cocoa_classes)) {
  2846. $this->cocoa_classes[] = $anon_class;
  2847. if (!in_array($anon_class, $this->anon_cocoa_classes)) {
  2848. $this->anon_cocoa_classes[] = $anon_class;
  2849. }
  2850. }
  2851. }
  2852. }
  2853. // categories
  2854. if (eregi($this->regex_objc_category, $line, $captures)) {
  2855. $this->cocoa_categories[] = $captures[1];
  2856. }
  2857. }
  2858. }
  2859. // Build array of all known classes in frameworks
  2860. function BuildFrameworkClasses () {
  2861. foreach ($this->frameworks as $framework_name => $framework_info) {
  2862. // framework is disabled
  2863. if ($framework_info["enabled"] != 1) continue;
  2864. if ($handle = @opendir($framework_info["headers"])) {
  2865. while (($file = readdir($handle)) !== false) {
  2866. if (eregi($framework_info["header_pattern"], $file)) {
  2867. $this->CollectHeaderClasses($framework_info["headers"]."/$file");
  2868. }
  2869. }
  2870. closedir($handle);
  2871. } else {
  2872. die("FATAL: The framework \"$framework_name\" can not be located at ".$framework_info["headers"]);
  2873. }
  2874. }
  2875. }
  2876. // Process a single and print output
  2877. function ProcessFile ($file, $print) {
  2878. // set the current framework to null so it's parsed from the framework
  2879. $this->framework = null;
  2880. $this->ParseHeader($file);
  2881. $this->ParseHeaderDependents($file);
  2882. if ($print) $this->PrintAllHeaders("", null, null, false);
  2883. }
  2884. // Loads parser settings from the XML file
  2885. function LoadFrameworksXML ($framework_path) {
  2886. $xml = new SimpleXMLElement(file_get_contents("frameworks.xml"));
  2887. foreach ($xml as $framework) {
  2888. $this->frameworks[(string) $framework->name]["root"] = (string) $framework->root;
  2889. $this->frameworks[(string) $framework->name]["headers"] = (string) $framework->headers;
  2890. if ($framework_path != "")
  2891. $this->frameworks[(string) $framework->name]["headers"] = preg_replace("!^.*/System/Library/Frameworks!", $framework_path, $this->frameworks[(string) $framework->name]["headers"]);
  2892. $this->frameworks[(string) $framework->name]["include_pattern"] = (string) $framework->include_pattern;
  2893. $this->frameworks[(string) $framework->name]["header_pattern"] = (string) $framework->header_pattern;
  2894. $this->frameworks[(string) $framework->name]["external_macro"] = (string) $framework->external_macro;
  2895. $this->frameworks[(string) $framework->name]["ignore_types"] = (string) $framework->ignore_types;
  2896. $this->frameworks[(string) $framework->name]["ignore_methods"] = (string) $framework->ignore_methods;
  2897. $this->frameworks[(string) $framework->name]["replace_types"] = $framework->replace_types;
  2898. $this->frameworks[(string) $framework->name]["ignore_lines"] = $framework->ignore_lines;
  2899. $this->frameworks[(string) $framework->name]["ignore_comments"] = $framework->ignore_comments;
  2900. $this->frameworks[(string) $framework->name]["enabled"] = false;
  2901. $this->frameworks[(string) $framework->name]["print"] = true;
  2902. }
  2903. }
  2904. function __construct ($directory, $out_directory, $frameworks, $frameworks_path, $show) {
  2905. $this->root = $directory;
  2906. $this->out = $out_directory;
  2907. $this->show = $show;
  2908. // load defined frameworks from xml
  2909. $this->LoadFrameworksXML($frameworks_path);
  2910. // enable frameworks requested by the command line options
  2911. if ($frameworks) {
  2912. foreach ($frameworks as $name) {
  2913. $name_clean = trim($name, "^ ");
  2914. $this->frameworks[$name_clean]["enabled"] = true;
  2915. // apply options from framework definition
  2916. if ($this->frameworks[$name_clean]["external_macro"]) $this->external_string_macros .= "|".$this->frameworks[$name_clean]["external_macro"];
  2917. if ($this->frameworks[$name_clean]["ignore_types"]) $this->ignore_types = array_merge($this->ignore_types, explode(",", $this->frameworks[$name_clean]["ignore_types"]));
  2918. if ($this->frameworks[$name_clean]["ignore_methods"]) $this->ignore_methods = array_merge($this->ignore_methods, explode(",", $this->frameworks[$name_clean]["ignore_methods"]));
  2919. if ($this->frameworks[$name_clean]["ignore_lines"]) {
  2920. foreach ($this->frameworks[$name_clean]["ignore_lines"]->line as $line) {
  2921. if (!in_array($line, $this->ignore_lines)) $this->ignore_lines[] = (string)$line;
  2922. }
  2923. }
  2924. if ($this->frameworks[$name_clean]["ignore_comments"]) {
  2925. foreach ($this->frameworks[$name_clean]["ignore_comments"]->line as $line) {
  2926. if (!in_array($line, $this->ignore_comments)) $this->ignore_comments[] = (string)$line;
  2927. }
  2928. }
  2929. if ($this->frameworks[$name_clean]["replace_types"]) {
  2930. foreach ($this->frameworks[$name_clean]["replace_types"]->type as $type) {
  2931. $pair = explode("=", (string)$type);
  2932. $this->replace_types[$pair[0]] = $pair[1];
  2933. }
  2934. }
  2935. // print mode
  2936. if ($name[0] == "^") $this->frameworks[$name_clean]["print"] = false;
  2937. }
  2938. }
  2939. //print_r($this->ignore_comments);
  2940. //print_r($this->ignore_lines);
  2941. //print_r($this->frameworks);
  2942. //exit;
  2943. $this->BuildFrameworkClasses();
  2944. }
  2945. }
  2946. ?>