dbf_common.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. unit dbf_common;
  2. interface
  3. {$I dbf_common.inc}
  4. uses
  5. SysUtils, Classes, DB
  6. {$ifndef WINDOWS}
  7. , Types, dbf_wtil
  8. {$ifdef KYLIX}
  9. , Libc
  10. {$endif}
  11. {$endif}
  12. ;
  13. const
  14. TDBF_MAJOR_VERSION = 7;
  15. TDBF_MINOR_VERSION = 0;
  16. TDBF_SUB_MINOR_VERSION = 0;
  17. TDBF_TABLELEVEL_FOXPRO = 25;
  18. TDBF_TABLELEVEL_VISUALFOXPRO = 30; {Source: http://www.codebase.com/support/kb/?article=C01059}
  19. JulianDateDelta = 1721425; { number of days between 1.1.4714 BC and "0" }
  20. type
  21. EDbfError = class (EDatabaseError);
  22. EDbfWriteError = class (EDbfError);
  23. TDbfFieldType = char;
  24. TXBaseVersion = (xUnknown, xClipper, xBaseIII, xBaseIV, xBaseV, xFoxPro, xBaseVII, xVisualFoxPro);
  25. TSearchKeyType = (stEqual, stGreaterEqual, stGreater);
  26. TDateTimeHandling = (dtDateTime, dtBDETimeStamp);
  27. //-------------------------------------
  28. PDateTime = ^TDateTime;
  29. {$ifndef FPC_VERSION}
  30. PtrInt = Longint;
  31. {$endif}
  32. PSmallInt = ^SmallInt;
  33. PCardinal = ^Cardinal;
  34. PDouble = ^Double;
  35. PString = ^String;
  36. {$ifdef DELPHI_3}
  37. dword = cardinal;
  38. {$endif}
  39. //-------------------------------------
  40. {$ifndef SUPPORT_FREEANDNIL}
  41. // some procedures for the less lucky who don't have newer versions yet :-)
  42. procedure FreeAndNil(var v);
  43. {$endif}
  44. procedure FreeMemAndNil(var P: Pointer);
  45. //-------------------------------------
  46. {$ifndef SUPPORT_PATHDELIM}
  47. const
  48. {$ifdef WINDOWS}
  49. PathDelim = '\';
  50. {$else}
  51. PathDelim = '/';
  52. {$endif}
  53. {$endif}
  54. {$ifndef SUPPORT_INCLTRAILPATHDELIM}
  55. function IncludeTrailingPathDelimiter(const Path: string): string;
  56. {$endif}
  57. //-------------------------------------
  58. function GetCompletePath(const Base, Path: string): string;
  59. function GetCompleteFileName(const Base, FileName: string): string;
  60. function IsFullFilePath(const Path: string): Boolean; // full means not relative
  61. function DateTimeToBDETimeStamp(aDT: TDateTime): double;
  62. function BDETimeStampToDateTime(aBT: double): TDateTime;
  63. procedure FindNextName(BaseName: string; var OutName: string; var Modifier: Integer);
  64. {$ifdef USE_CACHE}
  65. function GetFreeMemory: Integer;
  66. {$endif}
  67. // Convert word to big endian
  68. function SwapWordBE(const Value: word): word;
  69. // Convert word to little endian
  70. function SwapWordLE(const Value: word): word;
  71. // Convert integer to big endian
  72. function SwapIntBE(const Value: dword): dword;
  73. // Convert integer to little endian
  74. function SwapIntLE(const Value: dword): dword;
  75. {$ifdef SUPPORT_INT64}
  76. // Convert int64 to big endian
  77. procedure SwapInt64BE(Value, Result: Pointer); register;
  78. // Convert int64 to little endian
  79. procedure SwapInt64LE(Value, Result: Pointer); register;
  80. {$endif}
  81. // Translate string between codepages
  82. function TranslateString(FromCP, ToCP: Cardinal; Src, Dest: PChar; Length: Integer): Integer;
  83. // Returns a pointer to the first occurence of Chr in Str within the first Length characters
  84. // Does not stop at null (#0) terminator!
  85. function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
  86. // Delphi 3 does not have a Min function
  87. {$ifdef DELPHI_3}
  88. {$ifndef DELPHI_4}
  89. function Min(x, y: integer): integer;
  90. function Max(x, y: integer): integer;
  91. {$endif}
  92. {$endif}
  93. implementation
  94. {$ifdef WINDOWS}
  95. uses
  96. Windows;
  97. {$endif}
  98. //====================================================================
  99. function GetCompletePath(const Base, Path: string): string;
  100. begin
  101. if IsFullFilePath(Path)
  102. then begin
  103. Result := Path;
  104. end else begin
  105. if Length(Base) > 0 then
  106. Result := ExpandFileName(IncludeTrailingPathDelimiter(Base) + Path)
  107. else
  108. Result := ExpandFileName(Path);
  109. end;
  110. // add last backslash if not present
  111. if Length(Result) > 0 then
  112. Result := IncludeTrailingPathDelimiter(Result);
  113. end;
  114. function IsFullFilePath(const Path: string): Boolean; // full means not relative
  115. begin
  116. {$ifdef WINDOWS}
  117. Result := Length(Path) > 1;
  118. if Result then
  119. // check for 'x:' or '\\' at start of path
  120. Result := ((Path[2]=':') and (upcase(Path[1]) in ['A'..'Z']))
  121. or ((Path[1]='\') and (Path[2]='\'));
  122. {$else} // Linux
  123. Result := Length(Path) > 0;
  124. if Result then
  125. Result := Path[1]='/';
  126. {$endif}
  127. end;
  128. //====================================================================
  129. function GetCompleteFileName(const Base, FileName: string): string;
  130. var
  131. lpath: string;
  132. lfile: string;
  133. begin
  134. lpath := GetCompletePath(Base, ExtractFilePath(FileName));
  135. lfile := ExtractFileName(FileName);
  136. lpath := lpath + lfile;
  137. result := lpath;
  138. end;
  139. function DateTimeToBDETimeStamp(aDT: TDateTime): double;
  140. var
  141. aTS: TTimeStamp;
  142. begin
  143. aTS := DateTimeToTimeStamp(aDT);
  144. Result := TimeStampToMSecs(aTS);
  145. end;
  146. function BDETimeStampToDateTime(aBT: double): TDateTime;
  147. var
  148. aTS: TTimeStamp;
  149. begin
  150. aTS := MSecsToTimeStamp(Round(aBT));
  151. Result := TimeStampToDateTime(aTS);
  152. end;
  153. //====================================================================
  154. {$ifndef SUPPORT_FREEANDNIL}
  155. procedure FreeAndNil(var v);
  156. var
  157. Temp: TObject;
  158. begin
  159. Temp := TObject(v);
  160. TObject(v) := nil;
  161. Temp.Free;
  162. end;
  163. {$endif}
  164. procedure FreeMemAndNil(var P: Pointer);
  165. var
  166. Temp: Pointer;
  167. begin
  168. Temp := P;
  169. P := nil;
  170. FreeMem(Temp);
  171. end;
  172. //====================================================================
  173. {$ifndef SUPPORT_INCLTRAILPATHDELIM}
  174. {$ifndef SUPPORT_INCLTRAILBACKSLASH}
  175. function IncludeTrailingPathDelimiter(const Path: string): string;
  176. var
  177. len: Integer;
  178. begin
  179. Result := Path;
  180. len := Length(Result);
  181. if len = 0 then
  182. Result := PathDelim
  183. else
  184. if Result[len] <> PathDelim then
  185. Result := Result + PathDelim;
  186. end;
  187. {$else}
  188. function IncludeTrailingPathDelimiter(const Path: string): string;
  189. begin
  190. {$ifdef WINDOWS}
  191. Result := IncludeTrailingBackslash(Path);
  192. {$else}
  193. Result := IncludeTrailingSlash(Path);
  194. {$endif}
  195. end;
  196. {$endif}
  197. {$endif}
  198. {$ifdef USE_CACHE}
  199. function GetFreeMemory: Integer;
  200. var
  201. MemStatus: TMemoryStatus;
  202. begin
  203. GlobalMemoryStatus(MemStatus);
  204. Result := MemStatus.dwAvailPhys;
  205. end;
  206. {$endif}
  207. //====================================================================
  208. // Utility routines
  209. //====================================================================
  210. {$ifdef ENDIAN_LITTLE}
  211. function SwapWordBE(const Value: word): word;
  212. {$else}
  213. function SwapWordLE(const Value: word): word;
  214. {$endif}
  215. begin
  216. Result := ((Value and $FF) shl 8) or ((Value shr 8) and $FF);
  217. end;
  218. {$ifdef ENDIAN_LITTLE}
  219. function SwapWordLE(const Value: word): word;
  220. {$else}
  221. function SwapWordBE(const Value: word): word;
  222. {$endif}
  223. begin
  224. Result := Value;
  225. end;
  226. {$ifdef FPC}
  227. function SwapIntBE(const Value: dword): dword;
  228. begin
  229. Result := BEtoN(Value);
  230. end;
  231. function SwapIntLE(const Value: dword): dword;
  232. begin
  233. Result := LEtoN(Value);
  234. end;
  235. procedure SwapInt64BE(Value, Result: Pointer);
  236. begin
  237. PInt64(Result)^ := BEtoN(PInt64(Value)^);
  238. end;
  239. procedure SwapInt64LE(Value, Result: Pointer);
  240. begin
  241. PInt64(Result)^ := LEtoN(PInt64(Value)^);
  242. end;
  243. {$else}
  244. {$ifdef USE_ASSEMBLER_486_UP}
  245. function SwapIntBE(const Value: dword): dword; register; assembler;
  246. asm
  247. BSWAP EAX;
  248. end;
  249. procedure SwapInt64BE(Value {EAX}, Result {EDX}: Pointer); register; assembler;
  250. asm
  251. MOV ECX, dword ptr [EAX]
  252. MOV EAX, dword ptr [EAX + 4]
  253. BSWAP ECX
  254. BSWAP EAX
  255. MOV dword ptr [EDX+4], ECX
  256. MOV dword ptr [EDX], EAX
  257. end;
  258. {$else}
  259. function SwapIntBE(const Value: Cardinal): Cardinal;
  260. begin
  261. PByteArray(@Result)[0] := PByteArray(@Value)[3];
  262. PByteArray(@Result)[1] := PByteArray(@Value)[2];
  263. PByteArray(@Result)[2] := PByteArray(@Value)[1];
  264. PByteArray(@Result)[3] := PByteArray(@Value)[0];
  265. end;
  266. procedure SwapInt64BE(Value, Result: Pointer); register;
  267. var
  268. PtrResult: PByteArray;
  269. PtrSource: PByteArray;
  270. begin
  271. // temporary storage is actually not needed, but otherwise compiler crashes (?)
  272. PtrResult := PByteArray(Result);
  273. PtrSource := PByteArray(Value);
  274. PtrResult[0] := PtrSource[7];
  275. PtrResult[1] := PtrSource[6];
  276. PtrResult[2] := PtrSource[5];
  277. PtrResult[3] := PtrSource[4];
  278. PtrResult[4] := PtrSource[3];
  279. PtrResult[5] := PtrSource[2];
  280. PtrResult[6] := PtrSource[1];
  281. PtrResult[7] := PtrSource[0];
  282. end;
  283. {$endif}
  284. function SwapIntLE(const Value: dword): dword;
  285. begin
  286. Result := Value;
  287. end;
  288. {$ifdef SUPPORT_INT64}
  289. procedure SwapInt64LE(Value, Result: Pointer);
  290. begin
  291. PInt64(Result)^ := PInt64(Value)^;
  292. end;
  293. {$endif}
  294. {$endif}
  295. function TranslateString(FromCP, ToCP: Cardinal; Src, Dest: PChar; Length: Integer): Integer;
  296. var
  297. WideCharStr: array[0..1023] of WideChar;
  298. wideBytes: Cardinal;
  299. begin
  300. if Length = -1 then
  301. Length := StrLen(Src);
  302. Result := Length;
  303. {$ifndef WINCE}
  304. if (FromCP = GetOEMCP) and (ToCP = GetACP) then
  305. OemToCharBuffA(Src, Dest, Length)
  306. else
  307. if (FromCP = GetACP) and (ToCP = GetOEMCP) then
  308. CharToOemBuffA(Src, Dest, Length)
  309. else
  310. {$endif}
  311. if FromCP = ToCP then
  312. begin
  313. if Src <> Dest then
  314. Move(Src^, Dest^, Length);
  315. end else begin
  316. // does this work on Win95/98/ME?
  317. wideBytes := MultiByteToWideChar(FromCP, MB_PRECOMPOSED, Src, Length, LPWSTR(@WideCharStr[0]), 1024);
  318. Result := WideCharToMultiByte(ToCP, 0, LPWSTR(@WideCharStr[0]), wideBytes, Dest, Length, nil, nil);
  319. end;
  320. end;
  321. procedure FindNextName(BaseName: string; var OutName: string; var Modifier: Integer);
  322. var
  323. Extension: string;
  324. begin
  325. Extension := ExtractFileExt(BaseName);
  326. BaseName := Copy(BaseName, 1, Length(BaseName)-Length(Extension));
  327. repeat
  328. Inc(Modifier);
  329. OutName := ChangeFileExt(BaseName+'_'+IntToStr(Modifier), Extension);
  330. until not FileExists(OutName);
  331. end;
  332. {$ifdef FPC}
  333. function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
  334. var
  335. I: Integer;
  336. begin
  337. // Make sure we pass a buffer of bytes instead of a pchar otherwise
  338. // the call will always fail
  339. I := System.IndexByte(PByte(Buffer)^, Length, Chr);
  340. if I = -1 then
  341. Result := nil
  342. else
  343. Result := Buffer+I;
  344. end;
  345. {$else}
  346. function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
  347. asm
  348. PUSH EDI
  349. MOV EDI,Buffer
  350. MOV AL, Chr
  351. MOV ECX,Length
  352. REPNE SCASB
  353. MOV EAX,0
  354. JNE @@1
  355. MOV EAX,EDI
  356. DEC EAX
  357. @@1: POP EDI
  358. end;
  359. {$endif}
  360. {$ifdef DELPHI_3}
  361. {$ifndef DELPHI_4}
  362. function Min(x, y: integer): integer;
  363. begin
  364. if x < y then
  365. result := x
  366. else
  367. result := y;
  368. end;
  369. function Max(x, y: integer): integer;
  370. begin
  371. if x < y then
  372. result := y
  373. else
  374. result := x;
  375. end;
  376. {$endif}
  377. {$endif}
  378. end.