home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / BSUTILS.ZIP / bsutils.pas next >
Encoding:
Pascal/Delphi Source File  |  1998-10-22  |  10.2 KB  |  380 lines

  1. {************************************************************}
  2. {  bsutils.pas                                               }
  3. {  (c)1998 Business Software http://members.xoom.com/bsoft/  }
  4. { Collection of String / System / Math utility classes       }
  5. {         for Borland Delphi(r) Programmers.                 }
  6. {                                                            }
  7. { Author: Eddie Bond ebinfo@compuserve.com                   }
  8. {************************************************************}
  9.  
  10. // FileName:            bsutils.zip
  11. // Program:        BSUTILS collection of Pascal utility classes
  12. // Ver:            1.0
  13. // Date:        22 October 1998
  14. // Copyright:        (c)1998 Business Software
  15. // Web:            http://members.xoom.com/bsoft/
  16. // Author:        Eddie Bond
  17. // E-Mail:        ebinfo@compuserve.com
  18. // Status:        FreeWare
  19. // Restrictions:    None.
  20. // Delphi:        32-bit versions
  21. // Platform:        Windows 32-bit versions.
  22. // Distribution:    Freely distribute ENTIRE package.
  23. //
  24. // NB.
  25. // This source code is distributed by Business Software as FREEWARE
  26. // with the author's permission.
  27. // IT IS NOT PUBLIC DOMAIN!
  28. // You may use these utilities in your applications, whether private
  29. // or commercial, without payment or royalties.
  30. // You may distribute this file in unadulterated and unmodified form,
  31. // or include this file together, or as part of your own distributed
  32. // project's source code provided that this header, and all comments
  33. // remain attached and readable.
  34. // you may 'cut and paste' program segments from this file, to incorporate
  35. // into your own projects, but if you publish the source code you should
  36. // show the following comment below the program segment:
  37.  
  38.   {from bsutils.pas
  39.   (c)1998 Business Software http://members.xoom.com/bsoft/}
  40.  
  41. // NB This code is provided without warranty or support of any kind. You use
  42. // this code entirely at your own risk.
  43.  
  44. {*************************************************************}
  45. {                                                             }
  46. { CHECK OUT OUR SITE http://members.xoom.com/bsoft/ for more  }
  47. { DELPHI FREEWARE - SHAREWARE - ADVICE - DOWNLOADS            }
  48. {                                                             }
  49. {*************************************************************}
  50.  
  51. {===============================================================}
  52.  
  53. unit bsutils;
  54. {$B-,H+}
  55.  
  56. interface
  57.  
  58.  
  59. uses sysutils,windows,registry,DB,dbtables,BDE;
  60.  
  61. {================= String Utils =================}
  62.  
  63. function slash(value:string):string;
  64. {ensures that value has '\' as last character (for directory strings)}
  65.  
  66. function capfirst(value:string):string;
  67. {Capitalise first character of each word, lowercase remaining chars}
  68. {example: capfirst('bOrLANd delPHi FOR windOWs') = 'Borland Delphi For Windows'}
  69.  
  70. function striptags(value:string):string;
  71. {strip HTML tags from value}
  72. {example: striptags('<TR><TD Align="center">Hello World</TD>') = 'Hello World'}
  73.  
  74. function replace(str,s1,s2:string;casesensitive:boolean):string;
  75. {replace all incidences of s1 in str with s2}
  76. {example: replace('We know what we want','we','I',false) = 'I Know what I want'}
  77.  
  78. function CopyFromChar(s:string;c:char;l:integer):string;
  79. {copy l characters from string s starting at first incidence of c}
  80. {example: Copyfromchar('Borland Delphi','a',3) = 'and'}
  81.  
  82.  
  83. {================= System Utils =================}
  84.  
  85. function getwinsysdir:string;
  86. {returns Windows System Path (inc drive)}
  87. {example: getwinsysdir = 'C:\WINDOWS\SYSTEM\'}
  88.  
  89. function getwindir:string;
  90. {returns windows directory path (inc Drive)}
  91. {example: getwindir = 'C:\WINDOWS\'}
  92.  
  93. function getinstalldir:string;
  94. {returns install directory of EXE using this library}
  95. {example: getinstalldir = 'C:\PROGRAM FILES\BORLAND\DELPHI\DEMOS\'}
  96.  
  97. function getcurrentdir:string;
  98. {returns current directory}
  99. {example: getcurrentdir = 'D:\DELPHI PROJECTS\CLASSES\UTILS\'}
  100.  
  101. function getregvalue(root:integer;key,value:string):string;
  102. {reads a registry value}
  103. {example: getregvalue(HKEY_LOCAL_MACHINE,'network\logon\','username') = 'Eddie Bond'}
  104.  
  105. function getaliaspath(dbset:Tdataset):string;
  106. {returns DOS path of an ACTIVE dataset's (TTable or TQuery) database alias}
  107. {example getaliaspath(Table1) = 'C:\Program Files\Borland\Delphi\Demos\Data\'}
  108.  
  109. function getfiledate(filename:string):Tdatetime;
  110. {returns a file's date in TDateTime format}
  111.  
  112.  
  113. {================= Arithmetic Utils =================}
  114.  
  115. function StrToFloatDef(const s:string;def:Extended):Extended;
  116. {converts S into a number. If S is invalid, returns the number passed in Def.}
  117. {example: strtofloatdef('$10.25',0) = 0}
  118.  
  119. function VolSphere(radius:single):extended;
  120. {volume of sphere of given radius}
  121.  
  122. function AreaSphere(radius:single):extended;
  123. {surface area of sphere of given radius}
  124.  
  125. function VolCylinder(radius,height:single):extended;
  126. {volume of cylinder of given radius and height}
  127.  
  128. function AreaCylinder(radius,height:single):extended;
  129. {surface area of cylinder of given radius and height}
  130.  
  131. function MinExt(const A:array of Extended):Extended;
  132. {returns minimum value of an array of extended}
  133.  
  134. function MaxExt(const A:array of Extended):Extended;
  135. {returns maximum value of an array of extended}
  136.  
  137. function MinInteger(const A:array of Integer):Integer;
  138. {returns minimum value of an array of integers}
  139.  
  140. function MaxInteger(const A:array of integer):Integer;
  141. {returns maximum value of an array of integers}
  142.  
  143. function InverseSum(const a:array of single):single;
  144. {solves formulae of type 1/r = 1/a + 1/b +...1/n (eg electrical resistance in parallel)}
  145.  
  146. {================= Financial Utils =================}
  147.  
  148. function MarkUp(profit:single):single;
  149. {returns markup percentage required to return a profit of profit percent}
  150. {example: MarkUp(25) = 20 }
  151.  
  152. function SellingPrice(net:double;markup:single):double;
  153. {returns selling price after adding markup percent to net}
  154. {example: SellingPrice(199.50,22.5) = 244.3875}
  155.  
  156. function NetPrice(gross:double;taxrate:single):double;
  157. {returns the net value of an item of gross value containing tax at taxrate percent}
  158. {example: NetPrice(199.99,17.5) = 170.204255319149}
  159.  
  160.  
  161. implementation
  162.  
  163. function slash(value:string):string;
  164. begin
  165. if (value[length(value)]<>'\') then result:=value+'\' else result:=value;
  166. end;
  167.  
  168.  
  169. function capfirst(value:string):string;
  170. var
  171. i:integer;
  172. s:string;
  173. begin
  174. s:=uppercase(value[1]);
  175. for i:=2 to length(value) do
  176.     if (ord(value[i-1])<33) then s:=s+uppercase(value[i]) else s:=s+lowercase(value[i]);
  177. result:=s;
  178. end;
  179.  
  180. function striptags(value:string):string;
  181. var
  182. i:integer;
  183. s:string;
  184. begin
  185. i:=1;
  186. s:='';
  187. while i<=length(value) do
  188.       begin
  189.       if value[i]='<' then repeat inc(i) until (value[i]='>') else s:=s+value[i];
  190.       inc(i);
  191.       end;
  192. result:=s;
  193. end;
  194.  
  195. function replace(str,s1,s2:string;casesensitive:boolean):string;
  196. var
  197. i:integer;
  198. s,t:string;
  199. begin
  200. s:='';
  201. t:=str;
  202.        repeat
  203.        if casesensitive then i:=pos(s1,t) else i:=pos(lowercase(s1),lowercase(t));
  204.        if i>0 then
  205.           begin
  206.           s:=s+Copy(t,1,i-1)+s2;
  207.           t:=Copy(t,i+Length(s1),MaxInt);
  208.           end
  209.        else s:=s+t;
  210.        until i<=0;
  211. result:=s;
  212. end;
  213.  
  214. function CopyFromChar(s:string;c:char;l:integer):string;
  215. var i:integer;
  216. begin
  217. i:=pos(c,s);
  218. result:=copy(s,i,l);
  219. end;
  220.  
  221. function getwinsysdir:string;
  222. var p:pchar;
  223.     z:integer;
  224. begin
  225. z:=255;
  226. getmem(p,z);
  227. getsystemdirectory(p,z);
  228. result:=slash(string(p));
  229. freemem(p,z);
  230. end;
  231.  
  232. function getwindir:string;
  233. var p:pchar;
  234.     z:integer;
  235. begin
  236. z:=255;
  237. getmem(p,z);
  238. getwindowsdirectory(p,z);
  239. result:=slash(string(p));
  240. freemem(p,z);
  241. end;
  242.  
  243. function getcurrentdir:string;
  244. var p:pchar;
  245.     z:integer;
  246. begin
  247. z:=255;
  248. getmem(p,z);
  249. getcurrentdirectory(z,p);
  250. result:=slash(string(p));
  251. freemem(p,z);
  252. end;
  253.  
  254.  
  255. function getinstalldir:string;
  256. begin
  257. result:=slash(extractfiledir(paramstr(0)));
  258. end;
  259.  
  260.  
  261. function getregvalue(root:integer;key,value:string):string;
  262. var
  263. rg:Tregistry;
  264. begin
  265. rg:=Tregistry.create;
  266.   try
  267.   rg.rootkey:=root;
  268.   if rg.OpenKey(key,false) then result:=rg.readString(value) else result:='';
  269.   finally
  270.   rg.free;
  271.   end;
  272. end;
  273.  
  274. function getaliaspath(dbset:Tdataset):string;
  275. var
  276. vDBDesc:DBDesc;
  277. s:string;
  278. begin
  279. result:='';
  280. if not (dbset.active) then exit;
  281. if (dbset is TTable) then s:=(dbset as ttable).databasename;
  282. if (dbset is TQuery) then s:=(dbset as tquery).databasename;
  283. Check(DbiGetDatabaseDesc(PChar(s),@vDBDesc));
  284. result:=slash(string(vDBDesc.szPhyName));
  285. end;
  286.  
  287. function getfiledate(filename:string):Tdatetime;
  288. begin
  289. if fileexists(filename) then
  290.    result:=filedatetodatetime(fileage(filename)) else result:=maxint;
  291. end;
  292.  
  293.  
  294. function strtofloatdef(const s:string;def:Extended):Extended;
  295. begin
  296.      try
  297.      result:=strtofloat(s);
  298.      except
  299.      result:=def;
  300.      end;
  301. end;
  302.  
  303. function volsphere(radius:single):extended;
  304. begin
  305. result:=((4/3)*pi*radius*radius*radius);
  306. end;
  307.  
  308. function areasphere(radius:single):extended;
  309. begin
  310. result:=(4*pi*radius*radius);
  311. end;
  312.  
  313. function volcylinder(radius,height:single):extended;
  314. begin
  315. result:=(pi*radius*radius*height);
  316. end;
  317.  
  318. function areacylinder(radius,height:single):extended;
  319. begin
  320. result:=(2*pi*radius*height);
  321. end;
  322.  
  323. function MinExt(const A:array of Extended):Extended;
  324. var
  325. i:integer;
  326. begin
  327. Result:=A[Low(A)];
  328. for i:=Low(A)+1 to High(A) do if A[i]<Result then Result:=A[I];
  329. end;
  330.  
  331. function MaxExt(const A:array of Extended):Extended;
  332. var
  333. i:integer;
  334. begin
  335. Result:=A[Low(A)];
  336. for i:=Low(A)+1 to High(A) do if A[i]>Result then Result:=A[I];
  337. end;
  338.  
  339. function MinInteger(const A:array of Integer):Integer;
  340. var
  341. i:integer;
  342. begin
  343. Result:=A[Low(A)];
  344. for i:=Low(A)+1 to High(A) do if A[i]<Result then Result:=A[I];
  345. end;
  346.  
  347. function MaxInteger(const A:array of integer):Integer;
  348. var
  349. i:integer;
  350. begin
  351. Result:=A[Low(A)];
  352. for i:=Low(A)+1 to High(A) do if A[i]>Result then Result:=A[I];
  353. end;
  354.  
  355. function InverseSum(const a:array of single):single;
  356. var i:integer;
  357. begin
  358. result:=0;
  359. for i:=low(a) to high(a) do result:=result+(1/a[i]);
  360. result:=(1/result);
  361. end;
  362.  
  363. function MarkUp(profit:single):single;
  364. begin
  365. result:=(100-(10000/(100+profit)));
  366. end;
  367.  
  368. function SellingPrice(net:double;markup:single):double;
  369. begin
  370. result:=net+(net*markup/100);
  371. end;
  372.  
  373. function NetPrice(gross:double;taxrate:single):double;
  374. begin
  375. result:=gross-(gross*(taxrate)/(100+taxrate));
  376. end;
  377.  
  378. end.
  379.  
  380.