Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.

214 lines
5.3 KiB

3 years ago
  1. unit LibDelphi;
  2. {$ifdef FPC}
  3. {$MODE OBJFPC}
  4. {$endif}
  5. interface
  6. uses
  7. SysUtils;
  8. type
  9. va_list = Pointer;
  10. {$IFNDEF FPC}
  11. {$IF CompilerVersion <= 18.5}
  12. SizeInt = Integer;
  13. PtrUInt = LongWord;
  14. {$ELSE}
  15. SizeInt = NativeInt;
  16. PtrUInt = NativeUInt;
  17. {$IFEND}
  18. {$ENDIF}
  19. const
  20. {$IFDEF MSWINDOWS}
  21. SRuntimeLib = 'msvcrt.dll';
  22. {$ELSE}
  23. SRuntimeLib = 'libc.so';
  24. {$ENDIF}
  25. function fprintf(stream: Pointer; format: Pointer; arguments: va_list): Integer; cdecl; {$ifdef FPC}[public];{$endif}
  26. function sprintf(buffer: Pointer; format: Pointer; arguments: va_list): Integer; cdecl; {$ifdef FPC}[public];{$endif}
  27. function snprintf(buffer: Pointer; n: Integer; format: Pointer; arguments: va_list): Integer; cdecl; {$ifdef FPC}[public];{$endif}
  28. function fputs(s: Pointer; stream: Pointer): Integer; cdecl; external SRuntimeLib;
  29. function fputc(c: Integer; stream: Pointer): Integer; cdecl; external SRuntimeLib;
  30. function isprint(c: Integer): Integer; cdecl; external SRuntimeLib;
  31. procedure memset(a: Pointer; b: Integer; c: SizeInt); cdecl; {$ifdef FPC}[public];{$endif}
  32. function memcpy(dest: Pointer; const src: Pointer; count: SizeInt): Pointer; cdecl; {$ifdef FPC}[public];{$endif}
  33. function memcmp(a, b: Pointer; c:SizeInt):Integer; cdecl; {$ifdef FPC}[public];{$endif}
  34. function malloc(s: Longint): Pointer; cdecl; {$ifdef FPC}[public];{$endif}
  35. procedure free(p: Pointer); cdecl; {$ifdef FPC}[public];{$endif}
  36. {$ifndef FPC}
  37. function _ftol: Integer; cdecl; external SRuntimeLib;
  38. function _ltolower(ch: Integer): Integer; cdecl; external SRuntimeLib;
  39. function _ltoupper(ch: Integer): Integer; cdecl; external SRuntimeLib;
  40. function _ltowlower(ch: Integer): Integer; cdecl; external SRuntimeLib;
  41. function _ltowupper(ch: Integer): Integer; cdecl; external SRuntimeLib;
  42. function fwrite(ptr:pointer; size, count:SizeInt; stream:pointer ):SizeInt; cdecl; external SRuntimeLib;
  43. {$endif}
  44. function strcpy(dest: Pointer; src: Pointer): Pointer; cdecl; {$ifdef FPC}[public];{$endif}
  45. {$ifdef FPC}
  46. function fwrite(ptr:pointer; size, count:SizeInt; stream:pointer ):SizeInt; cdecl; {$ifdef FPC}[public];{$endif}
  47. function __udivdi3(a,b:int64):int64; cdecl; [public];
  48. function {$ifdef CPUX86}_imp__isprint{$else}__imp_isprint{$endif}(c: char): integer; cdecl; [public];
  49. {$endif}
  50. {$ifndef FPC}
  51. var
  52. __turboFloat: LongBool = False;
  53. _streams: Integer;
  54. {$else}
  55. type
  56. // from mingw - stdio.h
  57. cIoBuf = record
  58. _ptr:Pointer;
  59. _cnt:LongInt;
  60. _base:Pointer;
  61. _flag:LongInt;
  62. _file:LongInt;
  63. _charbuf:LongInt;
  64. _bufsiz:LongInt;
  65. _tmpfname:Pointer;
  66. end;
  67. pIoBuf = ^cIoBuf;
  68. var
  69. _imp___iob:array[0..2] of cIoBuf; cvar; // stdin,stdout,stderr
  70. iob:pIoBuf; cvar;
  71. {$endif}
  72. implementation
  73. {$ifndef FPC}
  74. uses
  75. Windows;
  76. {$endif}
  77. {$ifdef FPC}
  78. function __udivdi3(a, b: int64): int64; cdecl;
  79. begin
  80. Result:=a div b;
  81. end;
  82. function {$ifdef CPUX86}_imp__isprint{$else}__imp_isprint{$endif}(c: char): integer; cdecl;
  83. begin
  84. if (c>=#32)and(c<=#127) then
  85. Result:=1
  86. else
  87. Result:=0;
  88. end;
  89. {$endif}
  90. procedure free(p: Pointer); cdecl;
  91. begin
  92. FreeMem(p);
  93. end;
  94. function malloc(s: Longint): Pointer; cdecl;
  95. begin
  96. Result := AllocMem(s);
  97. end;
  98. function memcpy(dest: Pointer; const src: Pointer; count: SizeInt): Pointer; cdecl;
  99. begin
  100. system.Move(src^,dest^,count);
  101. Result:=dest;
  102. end;
  103. procedure memset(a: Pointer; b: Integer; c: SizeInt); cdecl;
  104. begin
  105. system.FillChar(a^,c,b);
  106. end;
  107. function memcmp(a, b: Pointer; c: SizeInt): Integer; cdecl; {$ifdef FPC}[public];{$endif}
  108. {$ifndef FPC}
  109. var
  110. ma,mb: PByte;
  111. n: Integer;
  112. begin
  113. ma:=a;
  114. mb:=b;
  115. n:=0;
  116. while Cardinal(n)<c do
  117. begin
  118. if ma^<>mb^ then
  119. begin
  120. if ma^<mb^ then
  121. Result:=-1
  122. else
  123. Result:=1;
  124. exit;
  125. end;
  126. Inc(ma);
  127. Inc(mb);
  128. Inc(n);
  129. end;
  130. Result:=0;
  131. {$else}
  132. begin
  133. Result:=CompareMemRange(a,b,c);
  134. {$endif}
  135. end;
  136. function __sprintf(buffer: Pointer; format: Pointer; arguments: Pointer): Integer; cdecl; external SRuntimeLib name 'sprintf';
  137. function sprintf(buffer: Pointer; format: Pointer; arguments: Pointer): Integer; cdecl;
  138. begin
  139. Result := __sprintf(buffer, format, arguments);
  140. end;
  141. function __snprintf(buffer: Pointer; n: Integer; format: Pointer; arguments: va_list): Integer; cdecl; external SRuntimeLib name '_snprintf';
  142. function snprintf(buffer: Pointer; n: Integer; format: Pointer; arguments: va_list): Integer; cdecl;
  143. begin
  144. Result := __snprintf(buffer, n, format, arguments);
  145. end;
  146. function fprintf(stream: Pointer; format: Pointer; arguments: va_list): Integer; cdecl;
  147. var
  148. m: Integer;
  149. n: Pointer;
  150. {$ifndef FPC}
  151. o: Cardinal;
  152. {$endif}
  153. begin
  154. m:=sprintf(nil,format,@arguments);
  155. n:=AllocMem(m);
  156. sprintf(n,format,@arguments);
  157. {$ifndef FPC}
  158. WriteFile(Cardinal(stream),n^,Cardinal(m),o,nil);
  159. {$else}
  160. FileWrite(pIoBuf(stream)^._file,n^,Cardinal(m));
  161. {$endif}
  162. FreeMem(n);
  163. Result := m;
  164. end;
  165. function strcpy(dest: Pointer; src: Pointer): Pointer; cdecl;
  166. begin
  167. Result:=SysUtils.strcopy(PAnsiChar(dest),PAnsiChar(src));
  168. end;
  169. {$ifdef FPC}
  170. function fwrite(ptr: pointer; size, count: SizeInt; stream: pointer): SizeInt; cdecl;
  171. begin
  172. Result:=FileWrite(pIoBuf(stream)^._file,ptr^,size * count);
  173. end;
  174. procedure init_iob;
  175. begin
  176. FillChar(_imp___iob[0],sizeof(cIoBuf)*3,0);
  177. _imp___iob[0]._file:=StdInputHandle;
  178. _imp___iob[1]._file:=StdOutputHandle;
  179. _imp___iob[2]._file:=StdErrorHandle;
  180. iob:=@_imp___iob[0];
  181. end;
  182. initialization
  183. init_iob;
  184. {$endif}
  185. end.