You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

201 lines
7.9 KiB

  1. unit ultsContext;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils,
  6. utsTextSuite, utsUtils,
  7. ultsTypes;
  8. type
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. TltsContext = class(TtsContext)
  11. public
  12. function DelSlave(const aSlave: TtsRefManager): Boolean; override;
  13. end;
  14. function ltsContextCreate (): TltsContextHandle; stdcall;
  15. function ltsContextGetCodePage (const aHandle: TltsContextHandle; out aCodePage: TtsCodePage): TltsErrorCode; stdcall;
  16. function ltsContextGetDefaultChar (const aHandle: TltsContextHandle; out aValue: WideChar): TltsErrorCode; stdcall;
  17. function ltsContextSetCodePage (const aHandle: TltsContextHandle; aCodePage: TtsCodePage): TltsErrorCode; stdcall;
  18. function ltsContextSetDefaultChar (const aHandle: TltsContextHandle; aValue: WideChar): TltsErrorCode; stdcall;
  19. function ltsContextAnsiToWide (const aHandle: TltsContextHandle; aText: PAnsiChar): PWideChar; stdcall;
  20. function ltsContextDestroy (const aHandle: TltsContextHandle): TltsErrorCode; stdcall;
  21. implementation
  22. uses
  23. ultsUtils {$IFDEF DEBUG}, uutlLogger{$ENDIF};
  24. var
  25. WideStringBuffer: WideString;
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. //TltsContext///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. function TltsContext.DelSlave(const aSlave: TtsRefManager): Boolean;
  30. begin
  31. DelReference(aSlave);
  32. result := inherited DelSlave(aSlave);
  33. end;
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. //Context///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. function ltsContextCreate: TltsContextHandle; stdcall;
  38. var
  39. c: TltsContext;
  40. begin
  41. try
  42. result := nil;
  43. if not CheckIfInitialized then
  44. exit;
  45. c := TltsContext.Create;
  46. AddReference(ltsObjTypeContext, c);
  47. result := c;
  48. except
  49. on ex: Exception do begin
  50. SetLastError(ex);
  51. result := nil;
  52. end;
  53. end;
  54. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextCreate=%p', [result]);{$ENDIF}
  55. end;
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. function ltsContextGetCodePage(const aHandle: TltsContextHandle; out aCodePage: TtsCodePage): TltsErrorCode; stdcall;
  58. var
  59. c: TtsContext;
  60. begin
  61. try
  62. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextGetCodePage(Handle=%p; aCodePage=%p)', [aHandle, @aCodePage]);{$ENDIF}
  63. result := ltsErrNone;
  64. if CheckContextHandle(aHandle, c)
  65. then aCodePage := c.CodePage
  66. else result := LastErrorCode;
  67. except
  68. on ex: Exception do begin
  69. SetLastError(ex);
  70. result := LastErrorCode;
  71. end;
  72. end;
  73. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextGetCodePage=%d', [Integer(result)]);{$ENDIF}
  74. end;
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. function ltsContextGetDefaultChar(const aHandle: TltsContextHandle; out aValue: WideChar): TltsErrorCode; stdcall;
  77. var
  78. c: TtsContext;
  79. begin
  80. try
  81. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextGetDefaultChar(Handle=%p; aValue=%p)', [aHandle, @aValue]);{$ENDIF}
  82. result := ltsErrNone;
  83. if CheckContextHandle(aHandle, c)
  84. then aValue := c.DefaultChar
  85. else result := LastErrorCode;
  86. except
  87. on ex: Exception do begin
  88. SetLastError(ex);
  89. result := LastErrorCode;
  90. end;
  91. end;
  92. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextGetDefaultChar=%d', [Integer(result)]);{$ENDIF}
  93. end;
  94. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. function ltsContextSetCodePage(const aHandle: TltsContextHandle; aCodePage: TtsCodePage): TltsErrorCode; stdcall;
  96. var
  97. c: TtsContext;
  98. begin
  99. try
  100. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextSetCodePage(Handle=%p; aCodePage=%d)', [aHandle, Integer(aCodePage)]);{$ENDIF}
  101. result := ltsErrNone;
  102. if CheckContextHandle(aHandle, c) then begin
  103. if not ValidateCodePage(aCodePage) then begin
  104. SetLastError(ltsErrInvalidEnum, Format('%d is not a valid enum value for CodePage', [aCodePage]));
  105. result := LastErrorCode;
  106. end else
  107. c.CodePage := aCodePage;
  108. end else
  109. result := LastErrorCode;
  110. except
  111. on ex: Exception do begin
  112. SetLastError(ex);
  113. result := LastErrorCode;
  114. end;
  115. end;
  116. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextSetCodePage=%d', [Integer(result)]);{$ENDIF}
  117. end;
  118. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  119. function ltsContextSetDefaultChar(const aHandle: TltsContextHandle; aValue: WideChar): TltsErrorCode; stdcall;
  120. var
  121. c: TtsContext;
  122. begin
  123. try
  124. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextSetDefaultChar(Handle=%p; aValue=%d)', [aHandle, Integer(aValue)]);{$ENDIF}
  125. result := ltsErrNone;
  126. if CheckContextHandle(aHandle, c)
  127. then c.DefaultChar := aValue
  128. else result := LastErrorCode;
  129. except
  130. on ex: Exception do begin
  131. SetLastError(ex);
  132. result := LastErrorCode;
  133. end;
  134. end;
  135. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextSetDefaultChar=%d', [Integer(result)]);{$ENDIF}
  136. end;
  137. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  138. function ltsContextAnsiToWide(const aHandle: TltsContextHandle; aText: PAnsiChar): PWideChar; stdcall;
  139. var
  140. c: TtsContext;
  141. w: PWideChar;
  142. begin
  143. try
  144. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextAnsiToWide(Handle=%p; aText=%p)', [aHandle, aText]);{$ENDIF}
  145. result := nil;
  146. if CheckContextHandle(aHandle, c) then begin
  147. w := c.AnsiToWide(aText);
  148. if not Assigned(w) then
  149. exit;
  150. WideStringBuffer := w;
  151. tsStrDispose(w);
  152. result := PWideChar(WideStringBuffer);
  153. end else
  154. result := nil;
  155. except
  156. on ex: Exception do begin
  157. SetLastError(ex);
  158. result := nil;
  159. end;
  160. end;
  161. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextAnsiToWide=%p', [result]);{$ENDIF}
  162. end;
  163. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. function ltsContextDestroy(const aHandle: TltsContextHandle): TltsErrorCode; stdcall;
  165. var
  166. c: TtsContext;
  167. begin
  168. try
  169. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextDestroy(Handle=%p)', [aHandle]);{$ENDIF}
  170. result := ltsErrNone;
  171. if CheckContextHandle(aHandle, c)
  172. then DelReference(ltsObjTypeContext, c)
  173. else result := LastErrorCode;
  174. except
  175. on ex: Exception do begin
  176. SetLastError(ex);
  177. result := LastErrorCode;
  178. end;
  179. end;
  180. {$IFDEF DEBUG}utlLogger.Log(nil, 'ltsContextDestroy=%d', [Integer(result)]);{$ENDIF}
  181. end;
  182. end.