home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / SYSTOOLS / SETUP.EXE / STSTRB.PAS
Encoding:
Pascal/Delphi Source File  |  1998-05-27  |  10.6 KB  |  426 lines

  1. {*********************************************************}
  2. {*                    STSTRB.PAS 2.00                    *}
  3. {*     Copyright (c) TurboPower Software Co., 1996-98    *}
  4. {*                 All rights reserved.                  *}
  5. {*********************************************************}
  6.  
  7. {$I STDEFINE.INC}
  8.  
  9. {$A-} {Packed records}
  10. {$B-} {Incomplete boolean evaluation}
  11. {$W-} {No special Windows stack frames}
  12. {$X+} {Extended syntax}
  13.  
  14. {$IFNDEF WIN32}
  15.   {$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
  16. {$ENDIF}
  17.  
  18. {
  19.   This unit is provided as an extra value to the SysTools package.  It
  20.   contains some character and string routines that we thought might be
  21.   useful to some of our customers...most are pretty simple, and some are
  22.   merely wrappers around Windows API calls.  TurboPower's support of this
  23.   unit is limited to bug fixes only.  Source code comments are currently
  24.   the only documentation. TurboPower Software offers no warranty, express
  25.   or implied, for these BONUS routines. You may use the routines within any
  26.   application that you write, without restriction.
  27. }
  28.  
  29. unit StStrB;
  30.  
  31. interface
  32.  
  33. uses
  34. {$IFDEF WIN32}
  35.   Windows,
  36. {$ELSE}
  37.   WinProcs,
  38.   WinTypes,
  39. {$ENDIF}
  40.   SysUtils,
  41.   StConst,
  42.   StBase,
  43.   StStrS,
  44.   StStrZ;
  45.  
  46. const
  47.   FileNameSet : string = '\/:*? <>|';
  48.   PathNameSet : string = '/*? <>|';
  49.   NumberSet   : string = '1234567890';
  50.   HexCharSet  : string = 'abcdefABCDEF0123456789';
  51.   WildCharSet : string = '*?';
  52.   DriveSet    : string = 'abcdefghijklmnopqrstuvwxyz';
  53.  
  54. { Character Routines }
  55.  
  56. function IsAlpha(C : AnsiChar) : Boolean;
  57.   { Returns True if C is an alpha character (by Windows definition)}
  58.  
  59. function IsAlphaNum(C : AnsiChar) : Boolean;
  60.   { Returns True if C is an alphanumeric character (by Windows definition)}
  61.  
  62. function IsCtrl(C : AnsiChar) : Boolean;
  63.   { Returns True if C is a control character }
  64.  
  65. function IsDrive(C : AnsiChar) : Boolean;
  66.   { Returns True if C is a valid drive letter (does not check if drive exists)}
  67.  
  68. function IsFileName(C : AnsiChar) : Boolean;
  69.   { Returns True if C is a valid character for a file name }
  70.  
  71. function IsFilePath(C : AnsiChar) : Boolean;
  72.   { Returns True if C is a valid character for a path name }
  73.  
  74. function IsHex(C : AnsiChar) : Boolean;
  75.   { Returns True if C is a valid character for a hex number }
  76.  
  77. function IsLower(C : AnsiChar) : Boolean;
  78.   { Returns True if C is lower case or a number }
  79.  
  80. function IsNum(C : AnsiChar) : Boolean;
  81.   { Returns True if C is a number }
  82.  
  83. function IsSpace(C : AnsiChar) : Boolean;
  84.   { Returns True if C is a space }
  85.  
  86. function IsUpper(C : AnsiChar) : Boolean;
  87.   { Returns True if C is upper case or a number }
  88.  
  89. function IsWildCard(C : AnsiChar) : Boolean;
  90.   { Returns True if C is a wild card character }
  91.  
  92.  
  93. { Short String Routines }
  94.  
  95. function BoolToStrS(B : Boolean) : ShortString;
  96.   { Returns a 'TRUE' or 'FALSE' depending on the value of B.  These strings
  97.     are retrieved from a resource (STCONST). }
  98.  
  99. function GetSystemDirS : ShortString;
  100.   { Returns the Windows System directory }
  101.  
  102. function GetTempDirS : ShortString;
  103.   { Returns the Windows Temp directory }
  104.  
  105. function GetWindowsDirS : ShortString;
  106.   { Returns the Windows directory }
  107.  
  108. function StrAnsiToOEMS(S : ShortString) : ShortString;
  109.   { Translates a string from the Windows character set into the specified
  110.     OEM character set. }
  111.  
  112. function StrOEMToAnsiS(S : ShortString) : ShortString;
  113.   { Translates a string from the specified OEM character set to the
  114.   Windows character. }
  115.  
  116. function StrToBoolS(S : ShortString) : Boolean;
  117.   { Compares S to True string (in STCONST resource) -- returns True if
  118.     match, otherwise returns False.  The compare is not case-sensitive. }
  119.  
  120.  
  121. { Null Terminated String Routines }
  122.  
  123. function BoolToStrZ(Dest : PAnsiChar; B : Boolean) : PAnsiChar;
  124.   { Returns a 'TRUE' or 'FALSE' depending on the value of B.  These strings
  125.     are retrieved from a resource (STCONST). }
  126.  
  127. function GetSystemDirZ(Dest : PAnsiChar) : PAnsiChar;
  128.   { Returns the Windows System directory }
  129.  
  130. function GetTempDirZ(Dest : PAnsiChar) : PAnsiChar;
  131.   { Returns the Windows Temp directory }
  132.  
  133. function GetWindowsDirZ(Dest : PAnsiChar) : PAnsiChar;
  134.   { Returns the Windows directory }
  135.  
  136. function StrAnsiToOEMZ(Dest, S : PAnsiChar) : PAnsiChar;
  137.   { Translates a string from the Windows character set into the specified
  138.     OEM character set. }
  139.  
  140. function StrOEMToAnsiZ(Dest, S : PAnsiChar) : PAnsiChar;
  141.   { Translates a string from the specified OEM character set to the
  142.   Windows character. }
  143.  
  144. function StrToBoolZ(S : PAnsiChar) : Boolean;
  145.   { Compares S to True string (in STCONST resource) -- returns True if
  146.     match, otherwise returns False.  The compare is not case-sensitive. }
  147.  
  148.  
  149. { Long String Routines }
  150.  
  151. {$IFDEF Win32}
  152. function BoolToStrL(B : Boolean) : AnsiString;
  153.   { Returns a 'TRUE' or 'FALSE' depending on the value of B.  These strings
  154.     are retrieved from a resource (STCONST). }
  155.  
  156. function GetSystemDirL : AnsiString;
  157.   { Returns the Windows System directory }
  158.  
  159. function GetTempDirL : AnsiString;
  160.   { Returns the Windows Temp directory }
  161.  
  162. function GetWindowsDirL : AnsiString;
  163.   { Returns the Windows directory }
  164.  
  165. function StrAnsiToOEML(S : AnsiString) : AnsiString;
  166.   { Translates a string from the Windows character set into the specified
  167.     OEM character set. }
  168.  
  169. function StrOEMToAnsiL(S : AnsiString) : AnsiString;
  170.   { Translates a string from the specified OEM character set to the
  171.   Windows character. }
  172.  
  173. function StrToBoolL(S : AnsiString) : Boolean;
  174.   { Compares S to True string (in STCONST resource) -- returns True if
  175.     match, otherwise returns False.  The compare is not case-sensitive. }
  176.  
  177. {$ENDIF}
  178.  
  179. implementation
  180.  
  181. { Character Routines }
  182.  
  183. function IsAlpha(C : AnsiChar) : Boolean;
  184. begin
  185.   Result := IsCharAlpha(C);
  186. end;
  187.  
  188. function IsAlphaNum(C : AnsiChar) : Boolean;
  189. begin
  190.   Result := IsCharAlphaNumeric(C);
  191. end;
  192.  
  193. function IsCtrl(C : AnsiChar) : Boolean;
  194. begin
  195.   Result := (Ord(C) > 0) and (Ord(C) < 32);
  196. end;
  197.  
  198. function IsDrive(C : AnsiChar) : Boolean;
  199. begin
  200.   Result := CharExistsS(DriveSet, Locase(C));
  201. end;
  202.  
  203. function IsFileName(C : AnsiChar) : Boolean;
  204. begin
  205.   Result := (C > #32) and (C < #128) and not (CharExistsS(FileNameSet, C));
  206. end;
  207.  
  208. function IsFilePath(C : AnsiChar) : Boolean;
  209. begin
  210.   Result := (C > #32) and (C < #128) and not (CharExistsS(PathNameSet,C));
  211. end;
  212.  
  213. function IsHex(C : AnsiChar) : Boolean;
  214. begin
  215.   Result := CharExistsS(HexCharSet, C);
  216. end;
  217.  
  218. function IsLower(C : AnsiChar) : Boolean;
  219. begin
  220.   if not IsNum(C) then
  221.     Result := IsCharLower(C)
  222.   else
  223.     Result := True;
  224. end;
  225.  
  226. function IsNum(C : AnsiChar) : Boolean;
  227. begin
  228.   Result := CharExistsS(NumberSet, C);
  229. end;
  230.  
  231. function IsSpace(C : AnsiChar) : Boolean;
  232. begin
  233.   Result := Ord(C) = 32;
  234. end;
  235.  
  236. function IsUpper(C : AnsiChar) : Boolean;
  237. begin
  238.   if not IsNum(C) then
  239.     Result := IsCharUpper(C)
  240.   else
  241.     Result := True;
  242. end;
  243.  
  244. function IsWildCard(C : AnsiChar) : Boolean;
  245. begin
  246.   Result := CharExistsS(WildCharSet, C);
  247. end;
  248.  
  249.  
  250. { Short String Routines }
  251.  
  252. function BoolToStrS(B : Boolean) : ShortString;
  253. begin
  254.   if B then
  255.     Result := SysToolStr[stscTrueString]
  256.   else
  257.     Result := SysToolStr[stscFalseString];
  258. end;
  259.  
  260. function GetSystemDirS : ShortString;
  261. var
  262.   B : array[0..255] of AnsiChar;
  263. begin
  264.   GetSystemDirectory(B, SizeOf(B));
  265.   Result := StrPas(B);
  266. end;
  267.  
  268. function GetTempDirS : ShortString;
  269. var
  270.   B : array[0..255] of AnsiChar;
  271. begin
  272. {$IFDEF WIN32}
  273.   GetTempPath(255, B);
  274.   Result := StrPas(B);
  275.   SetLength(Result, Pred(Length(Result)));
  276. {$ELSE}
  277.   GetTempFileName(GetTempDrive('a'), 'aaa', 0, B);
  278.   Result := JustPathNameS(StrPas(B));
  279. {$ENDIF}
  280. end;
  281.  
  282. function GetWindowsDirS : ShortString;
  283. var
  284.   B : array[0..255] of AnsiChar;
  285. begin
  286.   GetWindowsDirectory(B, SizeOf(B));
  287.   Result := StrPas(B);
  288. end;
  289.  
  290. function StrAnsiToOEMS(S : ShortString) : ShortString;
  291. var
  292.   B : array[0..255] of AnsiChar;
  293. begin
  294.   StrPCopy(B, S);
  295.   AnsiToOem(B, B);
  296.   Result := StrPas(B);
  297. end;
  298.  
  299. function StrOEMToAnsiS(S : ShortString) : ShortString;
  300. var
  301.   B : array[0..255] of AnsiChar;
  302. begin
  303.   StrPCopy(B, S);
  304.   OemToAnsi(B, B);
  305.   Result := StrPas(B);
  306. end;
  307.  
  308. function StrToBoolS(S : ShortString) : Boolean;
  309. begin
  310.   Result := CompUCStringS(S, SysToolStr[stscTrueString]) = 0;
  311. end;
  312.  
  313.  
  314. { Null Terminated String Routines }
  315.  
  316. function BoolToStrZ(Dest : PAnsiChar; B : Boolean) : PAnsiChar;
  317. begin
  318.   Result := Dest;
  319.   if B then
  320.     StrPCopy(Dest, SysToolStr[stscTrueString])
  321.   else
  322.     StrPCopy(Dest, SysToolStr[stscFalseString]);
  323. end;
  324.  
  325. function GetSystemDirZ(Dest : PAnsiChar) : PAnsiChar;
  326. begin
  327.   Result := Dest;
  328.   GetSystemDirectory(Dest, MaxFileLen);
  329. end;
  330.  
  331. function GetTempDirZ(Dest : PAnsiChar) : PAnsiChar;
  332. begin
  333.   Result := Dest;
  334. {$IFDEF WIN32}
  335.   GetTempPath(MaxFileLen, Dest);
  336.   StrChDeleteZ(Dest, Dest, Pred(StrLen(Dest)));
  337. {$ELSE}
  338.   GetTempFileName(GetTempDrive('a'), 'aaa', 0, Dest);
  339.   JustPathNameZ(Dest, Dest);
  340. {$ENDIF}
  341. end;
  342.  
  343. function GetWindowsDirZ(Dest : PAnsiChar) : PAnsiChar;
  344. begin
  345.   Result := Dest;
  346.   GetWindowsDirectory(Dest, MaxFileLen);
  347. end;
  348.  
  349. function StrAnsiToOEMZ(Dest, S : PAnsiChar) : PAnsiChar;
  350. begin
  351.   Result := Dest;
  352. {$IFDEF WIN32}
  353.   CharToOEM(S, Dest);
  354. {$ELSE}
  355.   AnsiToOem(S, Dest);
  356. {$ENDIF}
  357. end;
  358.  
  359. function StrOEMToAnsiZ(Dest, S : PAnsiChar) : PAnsiChar;
  360. begin
  361.   Result := Dest;
  362. {$IFDEF WIN32}
  363.   OEMtoChar(S, Dest);
  364. {$ELSE}
  365.   OEMToAnsi(S, Dest);
  366. {$ENDIF}
  367. end;
  368.  
  369. function StrToBoolZ(S : PAnsiChar) : Boolean;
  370. var
  371.   B : array[0..255] of AnsiChar;
  372. begin
  373.   StrPCopy(B, SysToolStr[stscTrueString]);
  374.   Result := StrIComp(S, B) = 0;
  375. end;
  376.  
  377.  
  378. { Long String Routines }
  379.  
  380. {$IFDEF Win32}
  381. function BoolToStrL(B : Boolean) : AnsiString;
  382. begin
  383.   if B then
  384.     Result := SysToolStr[stscTrueString]
  385.   else
  386.     Result := SysToolStr[stscFalseString];
  387. end;
  388.  
  389. function GetSystemDirL : AnsiString;
  390. begin
  391.   SetLength(Result, MaxFileLen);
  392.   GetSystemDirectory(PChar(Result), MaxFileLen);
  393. end;
  394.  
  395. function GetTempDirL : AnsiString;
  396. begin
  397.   SetLength(Result, MaxFileLen);
  398.   GetTempPath(MaxFileLen, PChar(Result));
  399.   SetLength(Result, Pred(StrLen(PChar(Result))));
  400. end;
  401.  
  402. function GetWindowsDirL : AnsiString;
  403. begin
  404.   SetLength(Result, MaxFileLen);
  405.   GetWindowsDirectory(PChar(Result), MaxFileLen);
  406. end;
  407.  
  408. function StrAnsiToOEML(S : AnsiString) : AnsiString;
  409. begin
  410.   Result := S;
  411.   CharToOem(PChar(Result), PChar(Result));
  412. end;
  413.  
  414. function StrOEMToAnsiL(S : AnsiString) : AnsiString;
  415. begin
  416.   Result := S;
  417.   OemToChar(PChar(Result), PChar(Result));
  418. end;
  419.  
  420. function StrToBoolL(S : AnsiString) : Boolean;
  421. begin
  422.   Result := AnsiCompareText(S, SysToolStr[stscTrueString]) = 0;
  423. end;
  424. {$ENDIF}
  425.  
  426. end.