xml2.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. procedure fpcxmlFree(mem: pointer); EXTDECL;
  29. begin
  30. FreeMem(mem);
  31. end;
  32. function fpcxmlMalloc(size: csize_t): pointer; EXTDECL;
  33. begin
  34. GetMem(Result, size);
  35. end;
  36. function fpcxmlRealloc(mem: pointer; size: csize_t): pointer; EXTDECL;
  37. begin
  38. Result := mem;
  39. ReallocMem(Result, size);
  40. end;
  41. procedure fpcxmlStructuredErrorHandler(userData: pointer; error: xmlErrorPtr); EXTDECL;
  42. begin
  43. writeln('struct error');
  44. end;
  45. (*
  46. * macros from xmlversion.inc
  47. *)
  48. procedure LIBXML_TEST_VERSION;
  49. begin
  50. xmlCheckVersion(LIBXML_VERSION);
  51. end;
  52. (*
  53. * macros from xmlversion.inc
  54. *)
  55. (*
  56. * macros from chvalid.inc
  57. *)
  58. function xmlIsBaseChar_ch(c: cint): cbool;
  59. begin
  60. Result :=
  61. ((c >= $41) and (c <= $5A)) or
  62. ((c >= $61) and (c <= $7A)) or
  63. ((c >= $C0) and (c <= $D6)) or
  64. ((c >= $D8) and (c <= $F6)) or
  65. (c >= $F8);
  66. end;
  67. function xmlIsBaseCharQ(c: cint): cbool;
  68. begin
  69. if c < $100 then
  70. Result := xmlIsBaseChar_ch(c)
  71. else
  72. Result := xmlCharInRange(c, __xmlIsBaseCharGroup);
  73. end;
  74. function xmlIsBlank_ch(c: cint): cbool;
  75. begin
  76. Result := (c = $20) or ((c >= $9) and (c <= $A)) or (c = $D);
  77. end;
  78. function xmlIsBlankQ(c: cint): cbool;
  79. begin
  80. if c < $100 then
  81. Result := xmlIsBaseChar_ch(c)
  82. else
  83. Result := false;
  84. end;
  85. function xmlIsChar_ch(c: cint): cbool;
  86. begin
  87. Result := ((c >= $9) and (c <= $A)) or (c = $D) or (c >= $20);
  88. end;
  89. function xmlIsCharQ(c: cint): cbool;
  90. begin
  91. if c < $100 then
  92. Result := xmlIsChar_ch(c)
  93. else
  94. Result :=
  95. ((c >= $000100) and (c <= $00D7FF)) or
  96. ((c >= $00E000) and (c <= $00FFFD)) or
  97. ((c >= $010000) and (c <= $10FFFF));
  98. end;
  99. function xmlIsCombiningQ(c: cint): cbool;
  100. begin
  101. if c < $100 then
  102. Result := false
  103. else
  104. Result := xmlCharInRange(c, __xmlIsCombiningGroup);
  105. end;
  106. function xmlIsDigit_ch(c: cint): cbool;
  107. begin
  108. Result := (c >= $30) and (c <= $39);
  109. end;
  110. function xmlIsDigitQ(c: cint): cbool;
  111. begin
  112. if c < $100 then
  113. Result := xmlIsDigit_ch(c)
  114. else
  115. Result := xmlCharInRange(c, __xmlIsDigitGroup);
  116. end;
  117. function xmlIsExtender_ch(c: cint): cbool;
  118. begin
  119. Result := c = $B7;
  120. end;
  121. function xmlIsExtenderQ(c: cint): cbool;
  122. begin
  123. if c < $100 then
  124. Result := xmlIsExtender_ch(c)
  125. else
  126. Result := xmlCharInRange(c, __xmlIsExtenderGroup);
  127. end;
  128. function xmlIsIdeographicQ(c: cint): cbool;
  129. begin
  130. if c < $100 then
  131. Result := false
  132. else
  133. Result :=
  134. ((c >= $4E00) and (c <= $9FA5)) or
  135. (c = $3007) or
  136. ((c >= $3021) and (c <= $3029));
  137. end;
  138. function xmlIsPubidChar_ch(c: cint): cbool;
  139. begin
  140. if (c >= 0) and (c <= 255) then
  141. Result := __xmlIsPubidChar_tab^[c]
  142. else
  143. Result := false;
  144. end;
  145. function xmlIsPubidCharQ(c: cint): cbool;
  146. begin
  147. if c < $100 then
  148. Result := xmlIsPubidChar_ch(c)
  149. else
  150. Result := false;
  151. end;
  152. (*
  153. * macros from HTMLparser.inc
  154. *)
  155. function htmlDefaultSubelement(elt: htmlElemDescPtr): pchar;
  156. begin
  157. Result := elt^.defaultsubelt;
  158. end;
  159. function htmlElementAllowedHereDesc(parent: htmlElemDescPtr; elt: htmlElemDescPtr): cint;
  160. begin
  161. Result := htmlElementAllowedHere(parent, xmlCharPtr(elt^.name));
  162. end;
  163. function htmlRequiredAttrs(elt: htmlElemDescPtr): ppchar;
  164. begin
  165. Result := elt^.attrs_req;
  166. end;
  167. (*
  168. * macros from tree.inc
  169. *)
  170. function XML_GET_CONTENT(n: pointer): xmlCharPtr;
  171. begin
  172. if xmlNodePtr(n)^._type = XML_ELEMENT_NODE then
  173. Result := nil
  174. else
  175. Result := xmlNodePtr(n)^.content;
  176. end;
  177. (*
  178. * macros from xpath.inc
  179. *)
  180. function xmlXPathNodeSetGetLength(ns: xmlNodeSetPtr): cint;
  181. begin
  182. if assigned(ns) then
  183. Result := ns^.nodeNr
  184. else
  185. Result := 0;
  186. end;
  187. function xmlXPathNodeSetItem(ns: xmlNodeSetPtr; index: cint): xmlNodePtr;
  188. begin
  189. if assigned(ns) and (index >= 0) and (index < ns^.nodeNr) then
  190. Result := ns^.nodeTab[index]
  191. else
  192. Result := nil;
  193. end;
  194. function xmlXPathNodeSetIsEmpty(ns: xmlNodeSetPtr): boolean;
  195. begin
  196. Result := not assigned(ns) or (ns^.nodeNr = 0) or (ns^.nodeTab = nil);
  197. end;
  198. {$IFDEF NO_EXTERNAL_VARS}
  199. procedure LoadExternalVariables;
  200. var
  201. libHandle: THandle;
  202. begin
  203. libHandle := LoadLibrary(xml2lib);
  204. if libHandle <> 0 then
  205. begin
  206. { xmlregexp.inc }
  207. {__emptyExp := xmlExpNodePtrPtr(GetProcAddress(libHandle, 'emptyExp'));
  208. __forbiddenExp := xmlExpNodePtrPtr(GetProcAddress(libHandle, 'forbiddenExp'));}
  209. { paserInternals.inc }
  210. //__xmlParserMaxDepth := PCardinal(GetProcAddress(libHandle, 'xmlParserMaxDepth'));
  211. { }
  212. {xmlStringComment := PChar(GetProcAddress(libHandle, 'xmlStringComment'));
  213. xmlStringText := PChar(GetProcAddress(libHandle, 'xmlStringText'));
  214. xmlStringTextNoenc := PChar(GetProcAddress(libHandle, 'xmlStringTextNoenc'));}
  215. { chvalid.inc }
  216. __xmlIsBaseCharGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsBaseCharGroup'));
  217. __xmlIsCharGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsCharGroup'));
  218. __xmlIsCombiningGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsCombiningGroup'));
  219. __xmlIsDigitGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsDigitGroup'));
  220. __xmlIsExtenderGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsExtenderGroup'));
  221. __xmlIsIdeographicGroup := xmlChRangeGroupPtr(GetProcAddress(libHandle, 'xmlIsIdeographicGroup'));
  222. __xmlIsPubidChar_tab := GetProcAddress(libHandle, 'xmlIsPubidChar_tab');
  223. { globals.inc }
  224. xmlMalloc := xmlMallocFunc(GetProcAddress(libHandle, 'xmlMalloc'));
  225. xmlMallocAtomic := xmlMallocFunc(GetProcAddress(libHandle, 'xmlMallocAtomic'));
  226. xmlRealloc := xmlReallocFunc(GetProcAddress(libHandle, 'xmlRealloc'));
  227. xmlFree := xmlFreeFunc(GetProcAddress(libHandle, 'xmlFree'));
  228. xmlMemStrdup := xmlStrdupFunc(GetProcAddress(libHandle, 'xmlMemStrdup'));
  229. { xpath.inc }
  230. {__xmlXPathNAN := PDouble(GetProcAddress(libHandle, 'xmlXPathNAN'));
  231. __xmlXPathNINF := PDouble(GetProcAddress(libHandle, 'xmlXPathNINF'));
  232. __xmlXPathPINF := PDouble(GetProcAddress(libHandle, 'xmlXPathPINF'));}
  233. FreeLibrary(libHandle);
  234. end;
  235. end;
  236. {$ENDIF}
  237. initialization
  238. {$IFDEF NO_EXTERNAL_VARS}
  239. LoadExternalVariables;
  240. {$ENDIF}
  241. (*
  242. * this initialize the library and check potential ABI mismatches
  243. * between the version it was compiled for and the actual shared
  244. * library used.
  245. *)
  246. LIBXML_TEST_VERSION;
  247. (*
  248. * overloading the memory functions
  249. *)
  250. xmlMemSetup(@fpcxmlFree, @fpcxmlMalloc, @fpcxmlRealloc, nil);
  251. (*
  252. * overloading the error functions
  253. *)
  254. //xmlSetGenericErrorFunc(nil, @fpcxmlGenericErrorHandler);
  255. //xmlSetStructuredErrorFunc(nil, @fpcxmlStructuredErrorHandler);
  256. finalization
  257. (*
  258. * Cleanup function for the XML library.
  259. *)
  260. //xmlCleanupParser();
  261. (*
  262. * this is to debug memory for regression tests
  263. *)
  264. xmlMemoryDump();
  265. end.