2
0

xml2.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. {
  2. Translation of the libxml2 headers for FreePascal
  3. Copyright (C) 2008 by Ivo Steinmann
  4. }
  5. unit xml2;
  6. {$mode objfpc}
  7. {$H+}
  8. {$macro on}
  9. {$ALIGN 8}
  10. {$MINENUMSIZE 4}
  11. interface
  12. uses
  13. dynlibs,
  14. ctypes;
  15. const
  16. {$IF Defined(WINDOWS)}
  17. xml2lib = 'libxml2.'+sharedsuffix;
  18. {$DEFINE EXTDECL := cdecl}
  19. {$DEFINE NO_EXTERNAL_VARS}
  20. {$ELSEIF Defined(UNIX)}
  21. xml2lib = 'libxml2.'+sharedsuffix;
  22. {$DEFINE EXTDECL := cdecl}
  23. {$ELSE}
  24. {$MESSAGE ERROR 'Platform not supported right now'}
  25. {$IFEND}
  26. {$i xml2.inc}
  27. implementation
  28. {$IFDEF NO_EXTERNAL_VARS}
  29. function GetxmlMalloc: xmlMallocFunc; inline;
  30. begin
  31. Result := varxmlMalloc^;
  32. end;
  33. procedure SetxmlMalloc(AValue: xmlMallocFunc); inline;
  34. begin
  35. varxmlMalloc^ := AValue;
  36. end;
  37. function GetxmlMallocAtomic: xmlMallocFunc; inline;
  38. begin
  39. Result := varxmlMallocAtomic^;
  40. end;
  41. procedure SetxmlMallocAtomic(AValue: xmlMallocFunc); inline;
  42. begin
  43. varxmlMallocAtomic^ := AValue;
  44. end;
  45. function GetxmlRealloc: xmlReallocFunc; inline;
  46. begin
  47. Result := varxmlRealloc^;
  48. end;
  49. procedure SetxmlRealloc(AValue: xmlReallocFunc); inline;
  50. begin
  51. varxmlRealloc^ := AValue;
  52. end;
  53. function GetxmlFree: xmlFreeFunc; inline;
  54. begin
  55. Result := varxmlFree^;
  56. end;
  57. procedure SetxmlFree(AValue: xmlFreeFunc); inline;
  58. begin
  59. varxmlFree^ := AValue;
  60. end;
  61. function GetxmlMemStrdup: xmlStrdupFunc; inline;
  62. begin
  63. Result := varxmlMemStrdup^;
  64. end;
  65. procedure SetxmlMemStrdup(AValue: xmlStrdupFunc); inline;
  66. begin
  67. varxmlMemStrdup^ := AValue;
  68. end;
  69. {$ENDIF}
  70. procedure fpcxmlFree(mem: pointer); EXTDECL;
  71. begin
  72. FreeMem(mem);
  73. end;
  74. function fpcxmlMalloc(size: csize_t): pointer; EXTDECL;
  75. begin
  76. GetMem(Result, size);
  77. end;
  78. function fpcxmlRealloc(mem: pointer; size: csize_t): pointer; EXTDECL;
  79. begin
  80. Result := mem;
  81. ReallocMem(Result, size);
  82. end;
  83. function fpcxmlStrdup(str: PAnsiChar): PAnsiChar; EXTDECL;
  84. var
  85. L: SizeInt;
  86. begin
  87. L := Length(str) + 1;
  88. Getmem(Result, L);
  89. if Result <> nil then
  90. Move(str^, Result^, L);
  91. end;
  92. procedure fpcxmlStructuredErrorHandler(userData: pointer; error: xmlErrorPtr); EXTDECL;
  93. begin
  94. writeln('struct error');
  95. end;
  96. (*
  97. * macros from xmlversion.inc
  98. *)
  99. procedure LIBXML_TEST_VERSION;
  100. begin
  101. xmlCheckVersion(LIBXML_VERSION);
  102. end;
  103. (*
  104. * macros from xmlversion.inc
  105. *)
  106. (*
  107. * macros from chvalid.inc
  108. *)
  109. function xmlIsBaseChar_ch(c: cint): cbool;
  110. begin
  111. Result :=
  112. ((c >= $41) and (c <= $5A)) or
  113. ((c >= $61) and (c <= $7A)) or
  114. ((c >= $C0) and (c <= $D6)) or
  115. ((c >= $D8) and (c <= $F6)) or
  116. (c >= $F8);
  117. end;
  118. function xmlIsBaseCharQ(c: cint): cbool;
  119. begin
  120. if c < $100 then
  121. Result := xmlIsBaseChar_ch(c)
  122. else
  123. Result := xmlCharInRange(c, __xmlIsBaseCharGroup);
  124. end;
  125. function xmlIsBlank_ch(c: cint): cbool;
  126. begin
  127. Result := (c = $20) or ((c >= $9) and (c <= $A)) or (c = $D);
  128. end;
  129. function xmlIsBlankQ(c: cint): cbool;
  130. begin
  131. if c < $100 then
  132. Result := xmlIsBaseChar_ch(c)
  133. else
  134. Result := false;
  135. end;
  136. function xmlIsChar_ch(c: cint): cbool;
  137. begin
  138. Result := ((c >= $9) and (c <= $A)) or (c = $D) or (c >= $20);
  139. end;
  140. function xmlIsCharQ(c: cint): cbool;
  141. begin
  142. if c < $100 then
  143. Result := xmlIsChar_ch(c)
  144. else
  145. Result :=
  146. ((c >= $000100) and (c <= $00D7FF)) or
  147. ((c >= $00E000) and (c <= $00FFFD)) or
  148. ((c >= $010000) and (c <= $10FFFF));
  149. end;
  150. function xmlIsCombiningQ(c: cint): cbool;
  151. begin
  152. if c < $100 then
  153. Result := false
  154. else
  155. Result := xmlCharInRange(c, __xmlIsCombiningGroup);
  156. end;
  157. function xmlIsDigit_ch(c: cint): cbool;
  158. begin
  159. Result := (c >= $30) and (c <= $39);
  160. end;
  161. function xmlIsDigitQ(c: cint): cbool;
  162. begin
  163. if c < $100 then
  164. Result := xmlIsDigit_ch(c)
  165. else
  166. Result := xmlCharInRange(c, __xmlIsDigitGroup);
  167. end;
  168. function xmlIsExtender_ch(c: cint): cbool;
  169. begin
  170. Result := c = $B7;
  171. end;
  172. function xmlIsExtenderQ(c: cint): cbool;
  173. begin
  174. if c < $100 then
  175. Result := xmlIsExtender_ch(c)
  176. else
  177. Result := xmlCharInRange(c, __xmlIsExtenderGroup);
  178. end;
  179. function xmlIsIdeographicQ(c: cint): cbool;
  180. begin
  181. if c < $100 then
  182. Result := false
  183. else
  184. Result :=
  185. ((c >= $4E00) and (c <= $9FA5)) or
  186. (c = $3007) or
  187. ((c >= $3021) and (c <= $3029));
  188. end;
  189. function xmlIsPubidChar_ch(c: cint): cbool;
  190. begin
  191. if (c >= 0) and (c <= 255) then
  192. Result := __xmlIsPubidChar_tab^[c]
  193. else
  194. Result := false;
  195. end;
  196. function xmlIsPubidCharQ(c: cint): cbool;
  197. begin
  198. if c < $100 then
  199. Result := xmlIsPubidChar_ch(c)
  200. else
  201. Result := false;
  202. end;
  203. (*
  204. * macros from HTMLparser.inc
  205. *)
  206. function htmlDefaultSubelement(elt: htmlElemDescPtr): PAnsiChar;
  207. begin
  208. Result := elt^.defaultsubelt;
  209. end;
  210. function htmlElementAllowedHereDesc(parent: htmlElemDescPtr; elt: htmlElemDescPtr): cint;
  211. begin
  212. Result := htmlElementAllowedHere(parent, xmlCharPtr(elt^.name));
  213. end;
  214. function htmlRequiredAttrs(elt: htmlElemDescPtr): PPAnsiChar;
  215. begin
  216. Result := elt^.attrs_req;
  217. end;
  218. (*
  219. * macros from tree.inc
  220. *)
  221. function XML_GET_CONTENT(n: pointer): xmlCharPtr;
  222. begin
  223. if xmlNodePtr(n)^._type = XML_ELEMENT_NODE then
  224. Result := nil
  225. else
  226. Result := xmlNodePtr(n)^.content;
  227. end;
  228. (*
  229. * macros from xpath.inc
  230. *)
  231. function xmlXPathNodeSetGetLength(ns: xmlNodeSetPtr): cint;
  232. begin
  233. if assigned(ns) then
  234. Result := ns^.nodeNr
  235. else
  236. Result := 0;
  237. end;
  238. function xmlXPathNodeSetItem(ns: xmlNodeSetPtr; index: cint): xmlNodePtr;
  239. begin
  240. if assigned(ns) and (index >= 0) and (index < ns^.nodeNr) then
  241. Result := ns^.nodeTab[index]
  242. else
  243. Result := nil;
  244. end;
  245. function xmlXPathNodeSetIsEmpty(ns: xmlNodeSetPtr): boolean;
  246. begin
  247. Result := not assigned(ns) or (ns^.nodeNr = 0) or (ns^.nodeTab = nil);
  248. end;
  249. {$IFDEF NO_EXTERNAL_VARS}
  250. procedure LoadExternalVariables;
  251. var
  252. libHandle: THandle;
  253. begin
  254. libHandle := LoadLibrary(xml2lib);
  255. if libHandle <> 0 then
  256. begin
  257. { xmlregexp.inc }
  258. {__emptyExp := xmlExpNodePtrPtr(GetProcAddress(libHandle, 'emptyExp'));
  259. __forbiddenExp := xmlExpNodePtrPtr(GetProcAddress(libHandle, 'forbiddenExp'));}
  260. { paserInternals.inc }
  261. //__xmlParserMaxDepth := PCardinal(GetProcAddress(libHandle, 'xmlParserMaxDepth'));
  262. { }
  263. {xmlStringComment := PAnsiChar(GetProcAddress(libHandle, 'xmlStringComment'));
  264. xmlStringText := PAnsiChar(GetProcAddress(libHandle, 'xmlStringText'));
  265. xmlStringTextNoenc := PAnsiChar(GetProcAddress(libHandle, 'xmlStringTextNoenc'));}
  266. { chvalid.inc }
  267. __xmlIsBaseCharGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsBaseCharGroup'));
  268. __xmlIsCharGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsCharGroup'));
  269. __xmlIsCombiningGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsCombiningGroup'));
  270. __xmlIsDigitGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsDigitGroup'));
  271. __xmlIsExtenderGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsExtenderGroup'));
  272. __xmlIsIdeographicGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsIdeographicGroup'));
  273. __xmlIsPubidChar_tab := GetProcAddress(libHandle, 'xmlIsPubidChar_tab');
  274. { globals.inc }
  275. varxmlMalloc := PxmlMallocFunc(GetProcAddress(libHandle, 'xmlMalloc'));
  276. varxmlMallocAtomic := PxmlMallocFunc(GetProcAddress(libHandle, 'xmlMallocAtomic'));
  277. varxmlRealloc := PxmlReallocFunc(GetProcAddress(libHandle, 'xmlRealloc'));
  278. varxmlFree := PxmlFreeFunc(GetProcAddress(libHandle, 'xmlFree'));
  279. varxmlMemStrdup := PxmlStrdupFunc(GetProcAddress(libHandle, 'xmlMemStrdup'));
  280. { xpath.inc }
  281. {__xmlXPathNAN := PDouble(GetProcAddress(libHandle, 'xmlXPathNAN'));
  282. __xmlXPathNINF := PDouble(GetProcAddress(libHandle, 'xmlXPathNINF'));
  283. __xmlXPathPINF := PDouble(GetProcAddress(libHandle, 'xmlXPathPINF'));}
  284. FreeLibrary(libHandle);
  285. end;
  286. end;
  287. {$ENDIF}
  288. initialization
  289. {$IFDEF NO_EXTERNAL_VARS}
  290. LoadExternalVariables;
  291. {$ENDIF}
  292. (*
  293. * overloading the memory functions
  294. *)
  295. xmlMemSetup(@fpcxmlFree, @fpcxmlMalloc, @fpcxmlRealloc, @fpcxmlStrdup);
  296. (*
  297. * this initialize the library and check potential ABI mismatches
  298. * between the version it was compiled for and the actual shared
  299. * library used.
  300. *)
  301. LIBXML_TEST_VERSION;
  302. (*
  303. * overloading the error functions
  304. *)
  305. //xmlSetGenericErrorFunc(nil, @fpcxmlGenericErrorHandler);
  306. //xmlSetStructuredErrorFunc(nil, @fpcxmlStructuredErrorHandler);
  307. finalization
  308. (*
  309. * Cleanup function for the XML library.
  310. *)
  311. xmlCleanupParser();
  312. (*
  313. * this is to debug memory for regression tests
  314. *)
  315. //xmlMemoryDump();
  316. end.