aspell.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Aspell bindings
  2. // Copyright (C)2006-2007 Pavel Kanzelsberger, All Rights Reserved
  3. // www.kanzelsberger.com, e-mail: pavel at kanzelsberger dot com
  4. // This library is free software. You can modify/redistribute it under
  5. // the terms of a modified LGPL license (see files LICENSE and
  6. // LICENSE.addon
  7. unit aspell;
  8. interface
  9. uses
  10. cTypes, DynLibs;
  11. {$IFDEF Linux}
  12. {$IFDEF Compat64}
  13. {$DEFINE Static}
  14. const aspelllib='aspell';
  15. {$ELSE}
  16. {$DEFINE Dynamic}
  17. const aspelllib='/usr/lib/libaspell.so';
  18. {$ENDIF}
  19. {$ENDIF}
  20. {$IFDEF FreeBSD}
  21. {$DEFINE Dynamic}
  22. const aspelllib='/usr/local/lib/libaspell.so';
  23. {$ENDIF}
  24. {$IFDEF darwin}
  25. {$DEFINE Dynamic}
  26. const aspelllib='libaspell.dylib';
  27. {$ENDIF}
  28. {$IFDEF windows}
  29. {$DEFINE Dynamic}
  30. const aspelllib='aspell-%s.dll';
  31. {$ENDIF}
  32. {$IFDEF BeOS}
  33. {$DEFINE Dynamic}
  34. const aspelllib='/boot/home/config/lib/libaspell.so';
  35. {$ENDIF}
  36. {$IFDEF SkyOS}
  37. {$DEFINE Static}
  38. {$LINKLIB aspell}
  39. const aspelllib='aspell';
  40. {$ENDIF}
  41. type aspellconfig=pointer;
  42. aspelldictinfolist=pointer;
  43. aspelldictinfoenumeration=pointer;
  44. aspellstringlist=pointer;
  45. aspellcanhaveerror=pointer;
  46. aspellspeller=pointer;
  47. aspellwordlist=pointer;
  48. aspellstringenumeration=pointer;
  49. taspellmoduleinfo=record name:pchar;
  50. order_num:double;
  51. lib_dir:pchar;
  52. dict_dirs:aspellstringlist;
  53. dict_exts:aspellstringlist;
  54. end;
  55. aspellmoduleinfo=^taspellmoduleinfo;
  56. taspelldictinfo=record name,code,jargon:pchar;
  57. size:cint32;
  58. size_str:pchar;
  59. module:aspellmoduleinfo;
  60. end;
  61. aspelldictinfo=^taspelldictinfo;
  62. var aspellpresent:longbool;
  63. alib: TLibHandle;
  64. {$IFDEF Dynamic}
  65. new_aspell_config:function():aspellconfig; cdecl;
  66. get_aspell_dict_info_list:function (config:aspellconfig):aspelldictinfolist; cdecl;
  67. delete_aspell_config:procedure (config:aspellconfig); cdecl;
  68. aspell_dict_info_list_elements:function (ths:aspelldictinfolist):aspelldictinfoenumeration; cdecl;
  69. aspell_dict_info_enumeration_next:function (ths:aspelldictinfoenumeration):aspelldictinfo; cdecl;
  70. aspell_dict_info_enumeration_at_end:function (ths:aspelldictinfoenumeration):cint32; cdecl;
  71. delete_aspell_dict_info_enumeration:procedure (ths:aspelldictinfoenumeration); cdecl;
  72. aspell_config_replace:function (config:aspellconfig;key,value:pchar):cint32; cdecl;
  73. new_aspell_speller:function (config:aspellconfig):aspellcanhaveerror; cdecl;
  74. aspell_error_number:function (ths:aspellcanhaveerror):word; cdecl;
  75. to_aspell_speller:function (ths:aspellcanhaveerror):aspellspeller; cdecl;
  76. aspell_speller_check:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  77. aspell_speller_suggest:function (ths:aspellspeller;myword:pchar;wordsize:cint32):aspellwordlist; cdecl;
  78. aspell_word_list_elements:function (ths:aspellwordlist):aspellstringenumeration; cdecl;
  79. aspell_string_enumeration_next:function (ths:aspellstringenumeration):pchar; cdecl;
  80. delete_aspell_string_enumeration:procedure (ths:aspellstringenumeration); cdecl;
  81. aspell_speller_add_to_session:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  82. aspell_speller_add_to_personal:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  83. aspell_speller_store_replacement:function (ths:aspellspeller;mis:pchar;missize:cint32;cor:pchar;corsize:cint32):cint32; cdecl;
  84. aspell_speller_save_all_word_lists:function (ths:aspellspeller):cint32; cdecl;
  85. delete_aspell_speller:procedure (ths:aspellspeller); cdecl;
  86. {$ENDIF}
  87. procedure aspell_init;
  88. procedure aspell_done;
  89. {$IFDEF Static}
  90. function new_aspell_config():aspellconfig; cdecl; external aspelllib;
  91. function get_aspell_dict_info_list (config:aspellconfig):aspelldictinfolist; cdecl; external aspelllib;
  92. procedure delete_aspell_config (config:aspellconfig); cdecl; external aspelllib;
  93. function aspell_dict_info_list_elements (ths:aspelldictinfolist):aspelldictinfoenumeration; cdecl; external aspelllib;
  94. function aspell_dict_info_enumeration_next (ths:aspelldictinfoenumeration):aspelldictinfo; cdecl; external aspelllib;
  95. function aspell_dict_info_enumeration_at_end (ths:aspelldictinfoenumeration):cint32; cdecl; external aspelllib;
  96. procedure delete_aspell_dict_info_enumeration (ths:aspelldictinfoenumeration); cdecl; external aspelllib;
  97. function aspell_config_replace (config:aspellconfig;key,value:pchar):cint32; cdecl; external aspelllib;
  98. function new_aspell_speller (config:aspellconfig):aspellcanhaveerror; cdecl; external aspelllib;
  99. function aspell_error_number (ths:aspellcanhaveerror):word; cdecl; external aspelllib;
  100. function to_aspell_speller (ths:aspellcanhaveerror):aspellspeller; cdecl; external aspelllib;
  101. function aspell_speller_check (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  102. function aspell_speller_suggest (ths:aspellspeller;myword:pchar;wordsize:cint32):aspellwordlist; cdecl; external aspelllib;
  103. function aspell_word_list_elements (ths:aspellwordlist):aspellstringenumeration; cdecl; external aspelllib;
  104. function aspell_string_enumeration_next (ths:aspellstringenumeration):pchar; cdecl; external aspelllib;
  105. procedure delete_aspell_string_enumeration (ths:aspellstringenumeration); cdecl; external aspelllib;
  106. function aspell_speller_add_to_session (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  107. function aspell_speller_add_to_personal (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  108. function aspell_speller_store_replacement (ths:aspellspeller;mis:pchar;missize:cint32;cor:pchar;corsize:cint32):cint32; cdecl; external aspelllib;
  109. function aspell_speller_save_all_word_lists (ths:aspellspeller):cint32; cdecl; external aspelllib;
  110. procedure delete_aspell_speller (ths:aspellspeller); cdecl; external aspelllib;
  111. {$ENDIF}
  112. implementation
  113. {$ifdef windows}
  114. uses
  115. SysUtils;
  116. {$endif}
  117. {$IFDEF Dynamic}
  118. function loadsymbol (name:pchar;proc:pointer):longbool;
  119. var addr:pointer;
  120. tproc:^pointer;
  121. begin
  122. loadsymbol:=false;
  123. addr:=GetProcedureAddress(alib,name);
  124. if addr=nil then
  125. exit;
  126. tproc:=proc;
  127. tproc^:=addr;
  128. loadsymbol:=true;
  129. end;
  130. {$ENDIF}
  131. procedure aspell_init;
  132. var
  133. mylib:string;
  134. {$ifdef windows}
  135. bversion,path:string;
  136. version:dword;
  137. {$endif}
  138. begin
  139. {$IFDEF Static}
  140. aspellpresent:=true;
  141. {$ELSE}
  142. aspellpresent:=false;
  143. mylib:=aspelllib;
  144. {$IFDEF windows}
  145. bversion:=RegistryQueryValue (regLocalMachine,'SOFTWARE\Aspell','AspellVersion');
  146. move (bversion[1],version,4);
  147. path:=RegistryQueryValue (regLocalMachine,'SOFTWARE\Aspell','Path');
  148. mylib:=path + PathDelim + StringReplace(aspelllib, '%s', IntToStr(Version), [rfReplaceAll]);
  149. {$ENDIF}
  150. alib := LoadLibrary(mylib);
  151. if alib = NilHandle then
  152. begin
  153. {$IFDEF LOG}
  154. debuglog (' Error loading spellchecking engine...');
  155. {$ENDIF}
  156. exit;
  157. end;
  158. if loadsymbol ('new_aspell_config',@new_aspell_config)=false then exit;
  159. if loadsymbol ('get_aspell_dict_info_list',@get_aspell_dict_info_list)=false then exit;
  160. if loadsymbol ('delete_aspell_config',@delete_aspell_config)=false then exit;
  161. if loadsymbol ('aspell_dict_info_list_elements',@aspell_dict_info_list_elements)=false then exit;
  162. if loadsymbol ('aspell_dict_info_enumeration_next',@aspell_dict_info_enumeration_next)=false then exit;
  163. if loadsymbol ('aspell_dict_info_enumeration_at_end',@aspell_dict_info_enumeration_at_end)=false then exit;
  164. if loadsymbol ('delete_aspell_dict_info_enumeration',@delete_aspell_dict_info_enumeration)=false then exit;
  165. if loadsymbol ('aspell_config_replace',@aspell_config_replace)=false then exit;
  166. if loadsymbol ('new_aspell_speller',@new_aspell_speller)=false then exit;
  167. if loadsymbol ('aspell_error_number',@aspell_error_number)=false then exit;
  168. if loadsymbol ('to_aspell_speller',@to_aspell_speller)=false then exit;
  169. if loadsymbol ('aspell_speller_check',@aspell_speller_check)=false then exit;
  170. if loadsymbol ('aspell_speller_suggest',@aspell_speller_suggest)=false then exit;
  171. if loadsymbol ('aspell_word_list_elements',@aspell_word_list_elements)=false then exit;
  172. if loadsymbol ('aspell_string_enumeration_next',@aspell_string_enumeration_next)=false then exit;
  173. if loadsymbol ('delete_aspell_string_enumeration',@delete_aspell_string_enumeration)=false then exit;
  174. if loadsymbol ('aspell_speller_add_to_session',@aspell_speller_add_to_session)=false then exit;
  175. if loadsymbol ('aspell_speller_add_to_personal',@aspell_speller_add_to_personal)=false then exit;
  176. if loadsymbol ('aspell_speller_store_replacement',@aspell_speller_store_replacement)=false then exit;
  177. if loadsymbol ('aspell_speller_save_all_word_lists',@aspell_speller_save_all_word_lists)=false then exit;
  178. if loadsymbol ('delete_aspell_speller',@delete_aspell_speller)=false then exit;
  179. aspellpresent:=true;
  180. {$ENDIF}
  181. end;
  182. procedure aspell_done;
  183. begin
  184. {$IFDEF Dynamic}
  185. UnloadLibrary(alib);
  186. {$ENDIF}
  187. end;
  188. initialization
  189. aspell_init;
  190. finalization
  191. aspell_done;
  192. end.