aspell.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. {$DEFINE Dynamic}
  13. const aspelllib='/usr/lib/libaspell.so.15';
  14. {$ENDIF}
  15. {$IFDEF FreeBSD}
  16. {$DEFINE Static}
  17. const aspelllib='/usr/local/lib/libaspell.so.15';
  18. {$ENDIF}
  19. {$IFDEF darwin}
  20. {$DEFINE Dynamic}
  21. const aspelllib='libaspell.dylib';
  22. {$ENDIF}
  23. {$IFDEF windows}
  24. {$DEFINE Dynamic}
  25. const aspelllib='aspell-%s.dll';
  26. {$ENDIF}
  27. {$IFDEF BeOS}
  28. {$DEFINE Dynamic}
  29. const aspelllib='/boot/home/config/lib/libaspell.so';
  30. {$ENDIF}
  31. {$IFDEF SkyOS}
  32. {$DEFINE Static}
  33. {$LINKLIB aspell}
  34. const aspelllib='aspell';
  35. {$ENDIF}
  36. type aspellconfig=pointer;
  37. aspelldictinfolist=pointer;
  38. aspelldictinfoenumeration=pointer;
  39. aspellstringlist=pointer;
  40. aspellcanhaveerror=pointer;
  41. aspellspeller=pointer;
  42. aspellwordlist=pointer;
  43. aspellstringenumeration=pointer;
  44. taspellmoduleinfo=record name:pchar;
  45. order_num:double;
  46. lib_dir:pchar;
  47. dict_dirs:aspellstringlist;
  48. dict_exts:aspellstringlist;
  49. end;
  50. aspellmoduleinfo=^taspellmoduleinfo;
  51. taspelldictinfo=record name,code,jargon:pchar;
  52. size:cint32;
  53. size_str:pchar;
  54. module:aspellmoduleinfo;
  55. end;
  56. aspelldictinfo=^taspelldictinfo;
  57. var aspellpresent:longbool;
  58. alib: TLibHandle;
  59. {$IFDEF Dynamic}
  60. new_aspell_config:function():aspellconfig; cdecl;
  61. get_aspell_dict_info_list:function (config:aspellconfig):aspelldictinfolist; cdecl;
  62. delete_aspell_config:procedure (config:aspellconfig); cdecl;
  63. aspell_dict_info_list_elements:function (ths:aspelldictinfolist):aspelldictinfoenumeration; cdecl;
  64. aspell_dict_info_enumeration_next:function (ths:aspelldictinfoenumeration):aspelldictinfo; cdecl;
  65. aspell_dict_info_enumeration_at_end:function (ths:aspelldictinfoenumeration):cint32; cdecl;
  66. delete_aspell_dict_info_enumeration:procedure (ths:aspelldictinfoenumeration); cdecl;
  67. aspell_config_replace:function (config:aspellconfig;key,value:pchar):cint32; cdecl;
  68. new_aspell_speller:function (config:aspellconfig):aspellcanhaveerror; cdecl;
  69. aspell_error_number:function (ths:aspellcanhaveerror):word; cdecl;
  70. to_aspell_speller:function (ths:aspellcanhaveerror):aspellspeller; cdecl;
  71. aspell_speller_check:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  72. aspell_speller_suggest:function (ths:aspellspeller;myword:pchar;wordsize:cint32):aspellwordlist; cdecl;
  73. aspell_word_list_elements:function (ths:aspellwordlist):aspellstringenumeration; cdecl;
  74. aspell_string_enumeration_next:function (ths:aspellstringenumeration):pchar; cdecl;
  75. delete_aspell_string_enumeration:procedure (ths:aspellstringenumeration); cdecl;
  76. aspell_speller_add_to_session:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  77. aspell_speller_add_to_personal:function (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl;
  78. aspell_speller_store_replacement:function (ths:aspellspeller;mis:pchar;missize:cint32;cor:pchar;corsize:cint32):cint32; cdecl;
  79. aspell_speller_save_all_word_lists:function (ths:aspellspeller):cint32; cdecl;
  80. delete_aspell_speller:procedure (ths:aspellspeller); cdecl;
  81. {$ENDIF}
  82. procedure aspell_init(const libn: ansistring);
  83. procedure aspell_done;
  84. {$IFDEF Static}
  85. function new_aspell_config():aspellconfig; cdecl; external aspelllib;
  86. function get_aspell_dict_info_list (config:aspellconfig):aspelldictinfolist; cdecl; external aspelllib;
  87. procedure delete_aspell_config (config:aspellconfig); cdecl; external aspelllib;
  88. function aspell_dict_info_list_elements (ths:aspelldictinfolist):aspelldictinfoenumeration; cdecl; external aspelllib;
  89. function aspell_dict_info_enumeration_next (ths:aspelldictinfoenumeration):aspelldictinfo; cdecl; external aspelllib;
  90. function aspell_dict_info_enumeration_at_end (ths:aspelldictinfoenumeration):cint32; cdecl; external aspelllib;
  91. procedure delete_aspell_dict_info_enumeration (ths:aspelldictinfoenumeration); cdecl; external aspelllib;
  92. function aspell_config_replace (config:aspellconfig;key,value:pchar):cint32; cdecl; external aspelllib;
  93. function new_aspell_speller (config:aspellconfig):aspellcanhaveerror; cdecl; external aspelllib;
  94. function aspell_error_number (ths:aspellcanhaveerror):word; cdecl; external aspelllib;
  95. function to_aspell_speller (ths:aspellcanhaveerror):aspellspeller; cdecl; external aspelllib;
  96. function aspell_speller_check (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  97. function aspell_speller_suggest (ths:aspellspeller;myword:pchar;wordsize:cint32):aspellwordlist; cdecl; external aspelllib;
  98. function aspell_word_list_elements (ths:aspellwordlist):aspellstringenumeration; cdecl; external aspelllib;
  99. function aspell_string_enumeration_next (ths:aspellstringenumeration):pchar; cdecl; external aspelllib;
  100. procedure delete_aspell_string_enumeration (ths:aspellstringenumeration); cdecl; external aspelllib;
  101. function aspell_speller_add_to_session (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  102. function aspell_speller_add_to_personal (ths:aspellspeller;myword:pchar;wordsize:cint32):cint32; cdecl; external aspelllib;
  103. function aspell_speller_store_replacement (ths:aspellspeller;mis:pchar;missize:cint32;cor:pchar;corsize:cint32):cint32; cdecl; external aspelllib;
  104. function aspell_speller_save_all_word_lists (ths:aspellspeller):cint32; cdecl; external aspelllib;
  105. procedure delete_aspell_speller (ths:aspellspeller); cdecl; external aspelllib;
  106. {$ENDIF}
  107. implementation
  108. {$ifdef Dynamic}
  109. {$ifdef windows}
  110. uses
  111. SysUtils;
  112. {$endif}
  113. {$endif}
  114. {$IFDEF Dynamic}
  115. function loadsymbol (name:pchar;proc:pointer):longbool;
  116. var addr:pointer;
  117. tproc:^pointer;
  118. begin
  119. loadsymbol:=false;
  120. addr:=GetProcedureAddress(alib,name);
  121. if addr=nil then
  122. exit;
  123. tproc:=proc;
  124. tproc^:=addr;
  125. loadsymbol:=true;
  126. end;
  127. {$ENDIF}
  128. procedure aspell_init(const libn: ansistring);
  129. var
  130. mylib:string;
  131. {$ifdef windows}
  132. bversion,path:string;
  133. version:dword;
  134. {$endif}
  135. begin
  136. {$IFDEF Static}
  137. aspellpresent:=true;
  138. {$ELSE}
  139. aspellpresent:=false;
  140. mylib:=libn;
  141. {$IFDEF windows}
  142. bversion:=RegistryQueryValue (regLocalMachine,'SOFTWARE\Aspell','AspellVersion');
  143. move (bversion[1],version,4);
  144. path:=RegistryQueryValue (regLocalMachine,'SOFTWARE\Aspell','Path');
  145. // will work if they passed %s, won't bork if they passed absolute
  146. mylib:=path + PathDelim + StringReplace(libn, '%s', IntToStr(Version), [rfReplaceAll]);
  147. {$ENDIF}
  148. alib := LoadLibrary(mylib);
  149. if alib = NilHandle then
  150. exit;
  151. if loadsymbol ('new_aspell_config',@new_aspell_config)=false then exit;
  152. if loadsymbol ('get_aspell_dict_info_list',@get_aspell_dict_info_list)=false then exit;
  153. if loadsymbol ('delete_aspell_config',@delete_aspell_config)=false then exit;
  154. if loadsymbol ('aspell_dict_info_list_elements',@aspell_dict_info_list_elements)=false then exit;
  155. if loadsymbol ('aspell_dict_info_enumeration_next',@aspell_dict_info_enumeration_next)=false then exit;
  156. if loadsymbol ('aspell_dict_info_enumeration_at_end',@aspell_dict_info_enumeration_at_end)=false then exit;
  157. if loadsymbol ('delete_aspell_dict_info_enumeration',@delete_aspell_dict_info_enumeration)=false then exit;
  158. if loadsymbol ('aspell_config_replace',@aspell_config_replace)=false then exit;
  159. if loadsymbol ('new_aspell_speller',@new_aspell_speller)=false then exit;
  160. if loadsymbol ('aspell_error_number',@aspell_error_number)=false then exit;
  161. if loadsymbol ('to_aspell_speller',@to_aspell_speller)=false then exit;
  162. if loadsymbol ('aspell_speller_check',@aspell_speller_check)=false then exit;
  163. if loadsymbol ('aspell_speller_suggest',@aspell_speller_suggest)=false then exit;
  164. if loadsymbol ('aspell_word_list_elements',@aspell_word_list_elements)=false then exit;
  165. if loadsymbol ('aspell_string_enumeration_next',@aspell_string_enumeration_next)=false then exit;
  166. if loadsymbol ('delete_aspell_string_enumeration',@delete_aspell_string_enumeration)=false then exit;
  167. if loadsymbol ('aspell_speller_add_to_session',@aspell_speller_add_to_session)=false then exit;
  168. if loadsymbol ('aspell_speller_add_to_personal',@aspell_speller_add_to_personal)=false then exit;
  169. if loadsymbol ('aspell_speller_store_replacement',@aspell_speller_store_replacement)=false then exit;
  170. if loadsymbol ('aspell_speller_save_all_word_lists',@aspell_speller_save_all_word_lists)=false then exit;
  171. if loadsymbol ('delete_aspell_speller',@delete_aspell_speller)=false then exit;
  172. aspellpresent:=true;
  173. {$ENDIF}
  174. end;
  175. procedure aspell_done;
  176. begin
  177. {$IFDEF Dynamic}
  178. UnloadLibrary(alib);
  179. {$ENDIF}
  180. end;
  181. initialization
  182. aspell_init(aspelllib);
  183. finalization
  184. aspell_done;
  185. end.