home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol9n04.zip / HELP2.PAS < prev    next >
Pascal/Delphi Source File  |  1990-01-22  |  2KB  |  106 lines

  1. HELP2.PAS
  2.  
  3.  
  4. (* QuickPascal program *)
  5.  
  6. UNIT Help2;
  7. (**********************)
  8. (**)    INTERFACE   (**)
  9. (**********************)
  10. USES Help,Dos;
  11. TYPE
  12.   NameStr = String[8];
  13.   HelpSet = OBJECT
  14.     size, number : Word;
  15.     Data : File;
  16.     Indx : File of WORD;
  17.     PROCEDURE InitHelp(iSize : Word);
  18.     FUNCTION  AddMessage(M : String):Boolean;
  19.     FUNCTION  GetMessage(B : Word):String;
  20.     PROCEDURE DoneWithHelp;
  21.   END;
  22.  
  23. (**********************)
  24. (**) IMPLEMENTATION (**)
  25. (**********************)
  26.  
  27.   FUNCTION RandomName : NameStr;
  28.   VAR N : Byte;
  29.     temp : NameStr;
  30.   BEGIN
  31.     FOR N := 1 to 8 DO
  32.       temp[N] := Char(random(128)+128);
  33.     temp[0] := #8;
  34.     RandomName := temp;
  35.   END;
  36.  
  37.   PROCEDURE HelpSet.InitHelp(iSize:Word);
  38.   {In a real-world application, you would deal with
  39.    real-world problems -- already-existing filename,
  40.    invalid filename, full disk, etc.  In order to
  41.    keep this example short we omit that kind of checking.
  42.   }
  43.   VAR name : NameStr;
  44.   BEGIN
  45.     Randomize;
  46.     REPEAT name := RandomName;
  47.     UNTIL FSearch(name+'.DAT','')='';
  48.     Assign(self.data,name+'.DAT');
  49.     ReWrite(self.data,1);
  50.     REPEAT name := RandomName;
  51.     UNTIL FSearch(name+'.IDX','')='';
  52.     Assign(self.indx,name+'.IDX');
  53.     ReWrite(self.indx);
  54.     self.size := iSize;
  55.     self.number := 0;
  56.   END;
  57.  
  58.   FUNCTION HelpSet.AddMessage(M : String):Boolean;
  59.   VAR loc : Word;
  60.   BEGIN
  61.     IF self.number = self.size THEN AddMessage := FALSE
  62.     ELSE
  63.       BEGIN
  64.     Inc(self.number);
  65.     loc := FilePos(self.data);
  66.     Write(self.indx, Loc);
  67.     BlockWrite(self.data,M,succ(length(M)));
  68.     AddMessage := TRUE;
  69.       END;
  70.   END;
  71.  
  72.   FUNCTION HelpSet.GetMessage(B : Word): String;
  73.   VAR temp : String;
  74.     Loc : Word;
  75.   BEGIN
  76.     IF B > self.number THEN GetMessage := ''
  77.     ELSE
  78.       BEGIN
  79.     {Get the location from the index file}
  80.     Seek(self.indx, pred(B));
  81.     Read(self.indx, Loc);
  82.     {Set the index file to receive more messages}
  83.     Seek(self.indx, FileSize(self.indx));
  84.     {Find and read the message itself}
  85.     Seek(self.data, loc);
  86.     BlockRead(self.Data,temp,1);
  87.     BlockRead(self.data,temp[1],ord(temp[0]));
  88.     {Set the data file to receive more messages}
  89.     Seek(self.data, FileSize(self.data));
  90.     GetMessage := temp;
  91.       END;
  92.   END;
  93.  
  94.   PROCEDURE HelpSet.DoneWithHelp;
  95.   BEGIN
  96.     Close(self.data);
  97.     Erase(self.data);
  98.     Close(self.indx);
  99.     Erase(self.indx);
  100.   END;
  101.  
  102. END.
  103.  
  104.  
  105.  
  106.