home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / RADOOR30.ZIP / LOWLEVEL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-04-10  |  15.2 KB  |  546 lines

  1. {╔═════════════════════════════════════════════════════════════════════════╗
  2.  ║                                                                         ║
  3.  ║                   (c) CopyRight LiveSystems 1990, 1994                  ║
  4.  ║                                                                         ║
  5.  ║ Author    : Gerhard Hoogterp                                            ║
  6.  ║ FidoNet   : 2:282/100.5   2:283/7.33                                    ║
  7.  ║ BitNet    : GERHARD@LOIPON.WLINK.NL                                     ║
  8.  ║                                                                         ║
  9.  ║ SnailMail : Kremersmaten 108                                            ║
  10.  ║             7511 LC Enschede                                            ║
  11.  ║             The Netherlands                                             ║
  12.  ║                                                                         ║
  13.  ║        This module is part of the RADoor BBS doorwriters toolbox.       ║
  14.  ║                                                                         ║
  15.  ╚═════════════════════════════════════════════════════════════════════════╝}
  16. {---------------------------------------------------------------------------|
  17.  
  18. Description:
  19.  
  20.  This unit contains all the lowlevel stuff used while writing doors. Lots of
  21.  it is used by the ToolBox itself too. The procedures and functions cover
  22.  FileName handling, timer routines, String routines, conversion and the
  23.  commandline.
  24.  
  25. CompilerDirectives:
  26.  
  27.  BBSAwear  Some procedures can make usage of the information inside the
  28.            GlobalInfo record. F.e. MakeString uses the graphical abilities
  29.            of the user to use AVATARS simple RLE compression. If you want
  30.            to use this unit outside the scope of the toolbox, you can turn
  31.            this off by disabling the BBSAwear directive.
  32.  
  33. |--------------------------------------------------------------------------}
  34. {Define BBSAwear}
  35.  
  36. Unit LowLevel;
  37. Interface
  38. Uses Dos;
  39.  
  40. {---------------------------------------------------------------------------|
  41.   The commandline routines.
  42.  
  43.   GrabParameter looks for a commandline parameter indentified by the
  44.   KEY string preceded by an '-' It returns the value of the parameter.
  45.   f.e. GrabParameter('T:') looks for the indentifier -T: and returns the
  46.   part behind the semicolumn a result.
  47.  
  48.   ExistParameter works the same, but only returns true or false. True
  49.   when the parameter is found, false if it isn't.
  50. |--------------------------------------------------------------------------}
  51.  
  52. Type IndentString  = String[10];
  53.  
  54. Function GrabParameter(Key : IndentString):ComStr;
  55. Function ExistParameter(Key : IndentString):Boolean;
  56.  
  57.  
  58. {---------------------------------------------------------------------------|
  59.   The basic char and string procedures.
  60.  
  61.  Function MlUpCase(C : Char):Char;    Same as TP's Upcase function, but also
  62.                                       for foreighn characters.
  63.  Function MlDownCase(C : Char):Char;  Returns the lowercase char. Also for
  64.                                       foreighn characters.
  65.  FormatLine  Formats a string in the RA/QBBS/SBBS way, with all lowercase
  66.              except for the beginning of words.
  67.  UpStr       Converts a string to all uppercase
  68.  DownStr     Converts a string to all lowercase
  69.  Center      Centers a string on the screen. Thisone uses the MakeString
  70.              function and thereby the information in the GlobalInfo record.
  71.  MakeString  Returns a string of the given character and the given length.
  72.              Uses AVATARS RLE compression when the BBSawear compiler directive
  73.              is enabled.
  74.  FindToken   Returnes the everything before a given delimiter.
  75.  
  76.  SimplifyDelimiters   Simplify double delimiters
  77.  SkipLeadingSpaces    Deletes leading spaces
  78.  DeleteTrailingSpaces Deletes trailing spaces.
  79.  DeleteNoise          Delete certain characters from a string
  80.  ReplaceToken         Replace a token with an other
  81.  TimeStamp            Returns a fullfeatured TimeStamp string
  82.  
  83. |--------------------------------------------------------------------------}
  84.  
  85. Function FormatLine(Line : String):String;
  86. Function UpStr(St : String):String;
  87. Function DownStr(Line : String):String;
  88. Function Center(CL:String):String;
  89. Function MakeString(Len : Byte; InpChar : Char):String;
  90.  
  91. Type Str40       = String[40];
  92.      TimeString  = String[20];
  93.  
  94. Function FindToken(Var Line : String;Delimiters : Str40):String;
  95. Procedure SimplifyDelimiters(Var Line : String;Delimiters : Str40);
  96. Procedure SkipLeadingSpaces(Var Line : String);
  97. Procedure DeleteTrailingSpaces(Var Line : String);
  98. Procedure DeleteNoise(Var Line : String;Noise : Str40);
  99. Procedure ReplaceToken(Var Line : String;Tok1,Tok2 : String);
  100.  
  101. Function TimeStamp:TimeString;
  102.  
  103. {---------------------------------------------------------------------------|
  104.   The conversion procedures
  105.  
  106.   S          Converts a Word to a string of the given length. Note, that if
  107.              you set the length to short, the result will be as long a needed!
  108.   SL         Same as S for longints.
  109.   SF         same as S, but leading spaces are converted to zero's
  110.   I          Same as S, but for integer
  111.   Str2Nr     Converts a string to a word. Returns Zero if illegal characters
  112.              are found or the string is empty.
  113. |--------------------------------------------------------------------------}
  114.  
  115. Type NrString = String[15];
  116.      HexStr   = String[4];
  117.  
  118. Function S(Num : Word;Len : Byte):NrString;
  119. Function SL(Num : LongInt; Len : Byte):NrString;
  120. Function SF(Num : Word;Len : Byte):NrString;
  121.  
  122. Function I(Num : Integer; Len : Byte):NrString;
  123. Function Str2Nr(S : NrString):Word;
  124. Function HexWord(number : Word):HexStr;
  125.  
  126. {---------------------------------------------------------------------------|
  127.  FileName and path functions:
  128.  
  129.  CompletePath     Makes sure that the given path is a full-featured dos path
  130.                   including the drive and a terminating backslash.
  131.  CompleteFilename Returns the first matching FileName of a given name
  132.                   specification in the PATH directory.
  133.  ExistPath        Returns true if the given path exists.
  134.  ExistFile        Returns true if the given file exists.
  135.  DeleteFile       Deletes one or more files specified by the FileSpec.
  136.                   Use with care!
  137.  StripPath        Strips the path and returns the FileName and extention
  138.                   only.
  139. |--------------------------------------------------------------------------}
  140.  
  141. Type Str12 = String[12];
  142.      Str8  = String[8];
  143.      Str4  = String[4];
  144.  
  145.  
  146. Procedure CompletePath(Var Path : ComStr);
  147. Procedure CompleteFileName(Path : ComStr;Var FileName : Str12);
  148. Function ExistPath(Path : ComStr):Boolean;
  149. Function ExistFile(FilePath : ComStr):Boolean;
  150. Function DeleteFile(FileSpec : ComStr): Boolean;
  151. Procedure StripPath(Var FileSpec : ComStr);
  152.  
  153.  
  154. Implementation
  155.  
  156. {$IfDef BBSAwear}
  157.  Uses GlobInfo;
  158. {$EndIf}
  159.  
  160. {---- Conversion procedures -----------------------------------------------}
  161.  
  162. Function HexWord(number : Word):HexStr;
  163. Const HexNum : Array[0..15] Of Char = '0123456789ABCDEF';
  164. Begin
  165. HexWord:=HexNum[(Hi(Number) And $F0) Shr 4] + HexNum[(Hi(Number) And $0F)]+
  166.          HexNum[(Lo(Number) And $F0) Shr 4] + HexNum[(Lo(Number) And $0F)];
  167. End;
  168.  
  169.  
  170. Function S(Num : Word; Len : Byte):NrString;
  171. Var Temp : String[20];
  172. Begin
  173. Str(Num:Len,Temp);
  174. S:=Temp;
  175. End;
  176.  
  177. Function SL(Num : LongInt; Len : Byte):NrString;
  178. Var Temp : NrString;
  179. Begin
  180. Str(Num:Len,Temp);
  181. SL:=Temp;
  182. End;
  183.  
  184.  
  185. Function SF(Num : Word; Len : Byte):NrString;
  186. Var Temp : String[20];
  187. Begin
  188. Str(Num:Len,Temp);
  189. For Len:=1 To Length(Temp) Do
  190.  If Temp[Len]=' '
  191.     Then Temp[Len]:='0';
  192. SF:=Temp;
  193. End;
  194.  
  195. Function I(Num : Integer; Len : Byte):NRString;
  196. Var Temp : String[20];
  197. Begin
  198. Str(Num:Len,Temp);
  199. I:=Temp;
  200. End;
  201.  
  202.  
  203.  
  204. Function Str2Nr(S : NrString):Word;
  205. Var Temp : Word;
  206.     Err  : Word;
  207. Begin
  208. Val(S,Temp,Err);
  209. If Err<>0
  210.    Then Str2Nr:=0
  211.    Else Str2Nr:=Temp;
  212. End;
  213.  
  214.  
  215. {---- String procedures ----------------------------------------------------}
  216.  
  217. Function MlUpCase(C : Char):Char;
  218. Const HiUpChars : String[9] ='ÜÉâÄÅÇÆÖÑ';
  219. Begin
  220. If (Ord(C)>127) And
  221.    (Pos(C,'üéâäåçæöñ')>0)
  222.    then MlUpCase:=HiUpChars[Pos(C,'üéâäåçæöñ')]
  223.    Else MlUpCase:=UpCase(C);
  224. End;
  225.  
  226. Function MlDownCase(C : Char):Char;
  227. Const HiLowChars : String[9] = 'üéâäåçæöñ';
  228. Begin
  229. If (Ord(C)>127) And
  230.    (Pos(C,'ÜÉâÄÅÇÆÖÑ')>0)
  231.    then MlDownCase:=HiLowChars[Pos(C,'ÜÉâÄÅÇÆÖÑ')]
  232.    Else Begin
  233.         If C In ['A'..'Z']
  234.            Then MlDownCase:=Chr(Ord(C)+$20)
  235.            Else MlDownCase:=C;
  236.         End;
  237. End;
  238.  
  239.  
  240. Function MakeString(Len : Byte; InpChar : Char):String;
  241. Var HStr : String;
  242. Begin
  243. {$IfDef BBSAwear}
  244.   If (GlobalInfo.UseGraphics And GlobalInfo.UseAVATAR)
  245.      Then MakeString:=^Y+InpChar+Char(Len)
  246.      Else Begin
  247.           FillChar(HStr,255,InpChar);
  248.           HStr[0]:=Char(Len);
  249.           MakeString:=HStr;
  250.           End;
  251. {$Else}
  252.   FillChar(HStr,255,InpChar);
  253.   HStr[0]:=Char(Len);
  254.   MakeString:=HStr;
  255. {$EndIf}
  256. End;
  257.  
  258. Function FormatLine(Line : String):String;
  259. Var C : Byte;
  260. Begin
  261. Line:=DownStr(Line);
  262. Line[1]:=MlUpcase(Line[1]);
  263. For C:=2 To Length(Line) Do
  264.  Begin
  265.  If Not (MlUpcase(Line[C-1]) In ['A'..'Z',#128..#154,#160..#165])
  266.     Then Line[C]:=MlUpcase(Line[C]);
  267.  If Line[C]='_'
  268.     Then Line[C]:=' ';
  269.  End;
  270. FormatLine:=Line;
  271. End;
  272.  
  273. Function DownStr(Line : String):String;
  274. Var Tel : Byte;
  275. Begin
  276. For Tel:=1 to Length(Line) Do
  277.  Line[Tel]:=MlDownCase(Line[Tel]);
  278. DownStr:=Line;
  279. End;
  280.  
  281. Function UpStr(St : String):String;
  282. Var Count : Byte;
  283. Begin
  284. For Count:=1 To Length(ST) Do
  285.  ST[Count]:=MlUpcase(St[Count]);
  286. UpStr:=St;
  287. End;
  288.  
  289. Function Center(CL:String):String;
  290. Var Temp : String;
  291.     Count: Byte;
  292.     Len  : Byte;
  293. Begin
  294. {$IfDef BBSAwear}
  295.   If Not (GlobalInfo.UseGraphics And GlobalInfo.UseAVATAR)
  296.      Then Len:=Length(CL)
  297.      Else Begin
  298.           Len:=0;
  299.           Count:=1;
  300.           While Count<=Length(CL) Do
  301.            Begin
  302.            If CL[Count]=^Y
  303.               Then Begin
  304.                    Inc(Len,Ord(CL[Count+2]));
  305.                    Inc(Count,2);
  306.                    End
  307.               Else Inc(Len);
  308.            Inc(Count);
  309.            End
  310.           End;
  311.   Temp:=MakeString((GlobalInfo.ScreenWidth-Len) Div 2,' ');
  312.   Center:=Temp+CL;
  313. {$Else}
  314.   Temp:=MakeString((80-Length(CL)) Div 2,' ');
  315.   Center:=Temp+CL;
  316. {$EndIf}
  317. End;
  318.  
  319. Function FindToken(Var Line : String;Delimiters : Str40):String;
  320. Var HStr : String;
  321.     Tel  : Byte;
  322. Begin
  323. HStr:='';
  324. Tel:=1;
  325. While (Tel<=Length(Line)) And
  326.       Not Boolean(Pos(UpCase(Line[Tel]),Delimiters)) Do
  327.  Begin
  328.  HStr:=HStr+Line[Tel];
  329.  Inc(Tel);
  330.  End;
  331. FindToken:=HStr;
  332. Delete(Line,1,Tel);
  333. End;
  334.  
  335. Procedure SimplifyDelimiters(Var Line : String;Delimiters : Str40);
  336. Var DelTel  : Byte;
  337. Begin
  338. DelTel:=1;
  339. Repeat
  340. If Boolean(Pos(Line[DelTel],Delimiters))
  341.    And Boolean(Pos(Line[DelTel+1],Delimiters))
  342.    Then Delete(Line,DelTel,1)
  343.    Else Inc(DelTel);
  344. Until DelTel>=(Length(Line)-1);
  345. End;
  346.  
  347. Procedure SkipLeadingSpaces(Var Line : String);
  348. Var Tel : Byte;
  349. Begin
  350. Tel:=1;
  351. While (Tel<=Byte(Line[0])) And (Line[Tel]=' ') Do
  352.  Inc(Tel);
  353. Delete(Line,1,Tel-1);
  354. End;
  355.  
  356. Procedure DeleteTrailingSpaces(Var Line : String);
  357. Var Tel : Byte;
  358. Begin
  359. Tel:=Byte(Line[0]);
  360. While (Tel>0) And (Line[Tel]=' ') Do
  361.  Dec(Tel);
  362. Delete(Line,Tel+1,255);
  363. End;
  364.  
  365. Procedure DeleteNoise(Var Line : String;Noise : Str40);
  366. Var NoiseTel : Byte;
  367.     PosNoise : Byte;
  368. Begin
  369. For NoiseTel:=1 To Length(Noise) Do
  370.  Begin
  371.  Repeat
  372.  PosNoise:=Pos(Noise[NoiseTel],Line);
  373.  If PosNoise>0
  374.     Then Delete(Line,PosNoise,1);
  375.  Until PosNoise=0;
  376.  End;
  377. End;
  378.  
  379. Procedure ReplaceToken(Var Line : String;Tok1,Tok2 : String);
  380. Var Tok1Pos : Byte;
  381.     HStr    : String;
  382. Begin
  383. HStr:=Line;
  384. HStr:=UpStr(HStr);
  385. Tok1:=UpStr(Tok1);
  386. Repeat
  387.  Tok1Pos:=Pos(Tok1,HStr);
  388.  If Tok1Pos>0
  389.     Then Begin
  390.          Delete(Line,Tok1Pos,Length(Tok1));
  391.          Insert(Tok2,Line,Tok1Pos);
  392.          HStr:=Line;
  393.          HStr:=UpStr(HStr);
  394.          End;
  395. Until Tok1Pos=0;
  396. End;
  397.  
  398.  
  399. {---- Commandline procedures -----------------------------------------------}
  400.  
  401. Function GrabParameter(Key : IndentString):ComStr;
  402. Var PCount : Byte;
  403.     PKey   : Char;
  404.     PStr   : ComStr;
  405. Begin
  406. Key:=UpStr(Key);
  407. Pcount:=0;
  408. PKey:='-';
  409. PStr:='';
  410. Repeat
  411.  Inc(PCount);
  412.  PStr:=UpStr(ParamStr(PCount));
  413. Until (PCount>ParamCount) Or (Pos(Key,PStr)=2);
  414.  
  415. If Pcount>ParamCount
  416.    Then GrabParameter:=''
  417.    Else GrabParameter:=Copy( PStr,Length(Key)+2,Length(PStr)-Length(Key));
  418. End;
  419.  
  420. Function ExistParameter(Key : IndentString):Boolean;
  421. Var PCount : Byte;
  422.     PKey   : Char;
  423.     PStr   : ComStr;
  424. Begin
  425. Key:=UpStr(Key);
  426. Pcount:=0;
  427. PKey:='-';
  428. PStr:='';
  429. Repeat
  430.  Inc(PCount);
  431.  PStr:=UpStr(ParamStr(PCount));
  432. Until (PCount>ParamCount) Or (Pos(Key,PStr)=2);
  433. ExistParameter:=PCount<=ParamCount;
  434. End;
  435.  
  436.  
  437.  
  438. {---- Timing procedures ---------------------------------------------------}
  439.  
  440.  
  441. Const MonthList : Array[1..12] Of String[3] =
  442.        ('Jan','Feb','Mar','Apr','May','Jun',
  443.         'Jul','Aug','Sep','Oct','Nov','Dec');
  444.  
  445.       DayList   : Array[0..6] Of String[10] =
  446.        ('Sunday','Monday','Tuesday','Wednesday',
  447.        'Thursday','Friday','Saturday');
  448.  
  449.  
  450.  
  451. Function TimeStamp:TimeString;
  452. Var Year,Month,Day,
  453.     Hour,Minute,Seconds     : Word;
  454.     Dof                     : Word;
  455.     Dum                     : Word;
  456.     OutStr                  : TimeString;
  457.     Step                    : Byte;
  458. Begin
  459. GetTime(Hour,Minute,Seconds,Dum);
  460. GetDate(Year,Month,Day,DOF);
  461.  
  462. OutStr:= DayList[DOF]+', '+
  463.          SF(Day,2)          +' '+
  464.          MonthList[Month] +' '+
  465.          S(Year,4)         +'  '+
  466.          SF(Hour,2)         +':'+
  467.          SF(Minute,2)       +':'+
  468.          SF(Seconds,2);
  469.  
  470. TimeStamp:=OutStr;
  471. End;
  472.  
  473.  
  474. {---- Filename/path functions --------------------------------------------}
  475.  
  476. Procedure CompletePath(Var Path : ComStr);
  477. Begin
  478. Path:=FExpand(Path);
  479. If (Path[Length(Path)]<>'\') And
  480.    (Path[Length(Path)]<>':')
  481.    Then Path:=Path+'\';
  482. End;
  483.  
  484. Procedure CompleteFileName(Path : ComStr;Var FileName : Str12);
  485. Var SR : SearchRec;
  486. Begin
  487. FindFirst(Path+FileName,Archive,SR);
  488. If DosError=0
  489.    Then FileName:=Sr.Name
  490.    Else FileName:='';
  491. End;
  492.  
  493. Function ExistPath(Path : ComStr):Boolean;
  494. Var Zoek : SearchRec;
  495. Begin
  496. FindFirst(Path+'*.*',AnyFile,Zoek);
  497. ExistPath:=(DosError<>3) And (Path<>'');
  498. End;
  499.  
  500. Function ExistFile(FilePath : ComStr):Boolean;
  501. Var Zoek: SearchRec;
  502. Begin
  503. If FilePath<>''
  504.    Then Begin
  505.         FindFirst(FilePath,AnyFile,Zoek);
  506.         ExistFile:=(DosError=0);
  507.         End
  508.    Else ExistFile:=False;
  509. End;
  510.  
  511. Function DeleteFile(FileSpec : ComStr): Boolean;
  512. Var Search : SearchRec;
  513.     Path   : ComStr;
  514.     Tel    : Byte;
  515.     Inp    : File;
  516. Begin
  517. DeleteFile:=True;
  518. Tel:=Length(FileSpec);
  519. While (Tel>0) And Not (FileSpec[Tel] In ['\',':']) Do
  520.  Dec(Tel);
  521. Path:=Copy(FileSpec,1,Tel);
  522. FindFirst(FileSpec,Archive,Search);
  523. While DosError=0 Do
  524.  Begin
  525.  Assign(Inp,Path+Search.Name);
  526.  Erase(Inp);
  527.  If IoResult<>0
  528.     Then Begin
  529.          DeleteFile:=False;
  530.          Exit;
  531.          End;
  532.  FindNext(Search);
  533.  End;
  534. End;
  535.  
  536. Procedure StripPath(Var FileSpec : ComStr);
  537. Var Dum  : String;
  538.     Name : Str8;
  539.     Ext  : Str4;
  540. Begin
  541. FSplit(FileSpec,Dum,Name,Ext);
  542. FileSpec:=Name+Ext;
  543. End;
  544.  
  545. End.
  546.