PPlib.pas

Un article de Wikipedia.

  1. { PPlib
  2. Version 7 - November 24, 2004
  3.  
  4. PPlib is a set of common
  5. functions for the Palm OS
  6. Pascal Compiler
  7.  
  8. Made by Laurent Duveau
  9.   & Philippe Guillot
  10.   web site = www.aldweb.com
  11.   web site = http://ppcompiler.free.fr
  12.   e-Mail = info@aldweb.com
  13.   e-Mail = ph.guillot@wanadoo.fr
  14.  
  15. Use: insert {$i PPlib.pas}
  16. in your PP source code
  17.  
  18. Functions:
  19. BoolToInt(B:boolean):integer;
  20. IntToBool(N:integer):boolean;
  21. IntToHexChar(H:integer):char;
  22. IntToString(N:integer):string;
  23. Max(A,B:integer):integer;
  24. Min(A,B:integer):integer;
  25. RealToFix(X:real;D:integer):string;
  26. RealToString(R:real):string;
  27. Spc(N:integer):string;
  28. StringToInt(const S:string):integer;
  29. StringToReal(S:string):real;
  30. SubString(const S:String;iStart,iEnd:integer):string;
  31. }
  32.  
  33.  
  34. function BoolToInt(B:boolean):integer;
  35. begin
  36. BoolToInt:=Ord(B);
  37. end;
  38.  
  39.  
  40. function IntToBool(N:integer):boolean;
  41. begin
  42. IntToBool:=N>0;
  43. end;
  44.  
  45.  
  46. function IntToHexChar(H:integer):char;
  47. begin
  48. if H in [0..9] then
  49. IntToHexChar:=chr(H+ord('0'))
  50. else
  51. if H in [10..15] then
  52. IntToHexChar:=chr(H-10+ord('A'))
  53. else
  54. IntToHexChar:=chr(0);
  55. end;
  56.  
  57.  
  58. function IntToString(N:integer):string;
  59. var
  60. S:string;
  61. procedure StrIToA(var S:string;N:integer); inline($4E4F,$A0C9);
  62. begin
  63. StrIToA(S,N);
  64. IntToString:=S;
  65. end;
  66.  
  67.  
  68. function Max(A,B:integer):integer;
  69. begin
  70. if A>B then
  71. Max:=A
  72. else
  73. Max:=B;
  74. end;
  75.  
  76.  
  77. function Min(A,B:integer):integer;
  78. begin
  79. if A<B then
  80. Min:=A
  81. else
  82. Min:=B;
  83. end;
  84.  
  85.  
  86. function RealToFix(X:real;D:integer):string;
  87. var
  88. I,K:integer;
  89. S:string;
  90. function Pow10(N:integer):real;
  91. var
  92. Mask:Integer;
  93. Y:real;
  94. begin
  95. if N=0 then
  96. Y:=1
  97. else begin
  98. Mask:=$8000000;
  99. Y:=10;
  100. while Mask>N do
  101. Mask:=Mask div 2;
  102. Mask:=Mask div 2;
  103. while mask<>0 do begin
  104. Y:=sqr(Y);
  105. if (N and Mask)<>0 then
  106. Y:=Y*10;
  107. Mask:=Mask div 2;
  108. end
  109. end;
  110. Pow10:=Y;
  111. end;
  112. begin
  113. if X>=0 then begin
  114. X:=X+5/Pow10(D+1);
  115. I:=trunc(X);
  116. X:=X-I;
  117. end
  118. else begin
  119. X:=-X+5/Pow10(D+1);
  120. I:=trunc(X);
  121. X:=X-I;
  122. I:=-I;
  123. end;
  124. S:=IntToString(I)+'.';
  125. for K:=D downto 1 do begin
  126. X:=X*10;
  127. I:=trunc(X);
  128. S:=S+chr(ord('0')+I);
  129. X:=X-i;
  130. end;
  131. RealToFix:=S;
  132. end;
  133.  
  134.  
  135. function RealToString(R:real):string;
  136. type
  137. FlpDouble = record
  138. Hi,Lo:integer;
  139. end;
  140. procedure f_ftod(var Y:FlpDouble;x:real); inline($7400+11,$4E4F,$0306);
  141. procedure FlpFToA(X:FlpDouble;var st:string); inline($7400+1,$4E4F,$0305);
  142. var
  143. D:FlpDouble;
  144. S:string;
  145. begin
  146. f_ftod(D,R);
  147. FlpFToA(D,S);
  148. RealToString:=S;
  149. end;
  150.  
  151.  
  152. function Spc(N:integer):string;
  153. var
  154. I:integer;
  155. S:string;
  156. begin
  157. S:='';
  158. for I:=1 to N do
  159. S:=S+' ';
  160. Spc:=S;
  161. end;
  162.  
  163.  
  164. function StringToInt(const S:string):integer;
  165. function StrAToI(const S:string):integer; inline($4e4f,$a0ce);
  166. begin
  167. StringToInt:=StrAToI(S);
  168. end;
  169.  
  170.  
  171. function StringToReal(S:string):real;
  172. type
  173. FlpDouble = record
  174. Hi,Lo:integer;
  175. end;
  176. procedure FlpAToF(var X:FlpDouble;var St:string); inline($7400+2,$4E4F,$0305);
  177. function d_dtof(X:FlpDouble):real; inline($7400+12,$4E4F,$0306);
  178. var
  179. D:FlpDouble;
  180. begin
  181. FlpAToF(D,S);
  182. StringToReal:=d_dtof(D);
  183. end;
  184.  
  185.  
  186. function SubString(const S:String;iStart,iEnd:integer):string;
  187. var
  188. SubS:String;
  189. i:integer;
  190. begin
  191. SubS:='';
  192. for i:=Max(iStart,1) to Min(iEnd,Length(S)) do
  193. SubS:=SubS+S[i];
  194. SubString:=SubS;
  195. end;