home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / lxlt121s.zip / lxLite_src / common / Streams.pas < prev    next >
Pascal/Delphi Source File  |  1997-01-15  |  11KB  |  474 lines

  1. {$A-,B-,D+,E-,F-,G-,I-,L+,N-,O-,P-,Q-,R-,S-,T-,V-,X+,Y+}
  2. {════════════════════════════════════════════════════════════════════════════}
  3. { Streams                                                                    }
  4. { Portable source code (tested on DOS and OS/2)                              }
  5. { Copyright (c) 1996 by Andrew Zabolotny, FRIENDS software                   }
  6. {════════════════════════════════════════════════════════════════════════════}
  7. Unit Streams;
  8.  
  9. Interface uses use32, miscUtil;
  10.  
  11. const
  12.  steOK               = 0;
  13.  steNoSuchFile       = 1;
  14.  steCreateError      = 2;
  15.  steInvalidFormat    = 3;
  16.  steInvalidOpenMode  = 4;
  17.  steReadError        = 5;
  18.  steWriteError       = 6;
  19.  steNoMemory         = 7;
  20.  steSeekError        = 8;
  21.  steNotApplicable    = 9;
  22.  
  23. type
  24.  pStream = ^tStream;
  25.  tStream = object(tObject)
  26.   Error       : Word16;
  27.   function    Name : string; virtual;
  28.   function    Put(var Data; bytes : word) : word; virtual;
  29.   function    Get(var Data; bytes : word) : word; virtual;
  30.   procedure   Skip(bytes : longint); virtual;
  31.   procedure   Seek(newPos : longint); virtual;
  32.   function    GetPos : longint; virtual;
  33.   function    Size : longint; virtual;
  34.   function    EOS : boolean; virtual;
  35.   procedure   PutStr(var S : string);
  36.   function    GetStr : string;
  37.   procedure   PutZTstr(S : pChar);
  38.   function    GetZTstr : pChar;
  39.   function    CopyFrom(var S : tStream; bytes : longint) : longint;
  40.  end;
  41.  
  42.  pFilter = ^tFilter;
  43.  tFilter = object(tStream)
  44.   ChainStream : pStream;
  45.   constructor Create(Chain : pStream);
  46.   function    Name : string; virtual;
  47.   function    Put(var Data; bytes : word) : word; virtual;
  48.   function    Get(var Data; bytes : word) : word; virtual;
  49.   procedure   Skip(bytes : longint); virtual;
  50.   function    EOS : boolean; virtual;
  51.  end;
  52.  
  53. const
  54.  stmReadOnly         = $0000; { ---- ---- ---- -000 }
  55.  stmWriteOnly        = $0001; { ---- ---- ---- -001 }
  56.  stmReadWrite        = $0002; { ---- ---- ---- -010 }
  57.  stmAccessMask       = $0007; { ---- ---- ---- -111 }
  58.  stsDenyReadWrite    = $0010; { ---- ---- -001 ---- }
  59.  stsDenyWrite        = $0020; { ---- ---- -010 ---- }
  60.  stsDenyRead         = $0030; { ---- ---- -011 ---- }
  61.  stsDenyNone         = $0040; { ---- ---- -100 ---- }
  62.  stfNoInherit        = $0080; { ---- ---- 1--- ---- }
  63.  stfNo_Locality      = $0000; { ---- -000 ---- ---- }
  64.  stfSequential       = $0100; { ---- -001 ---- ---- }
  65.  stfRandom           = $0200; { ---- -010 ---- ---- }
  66.  stfRandomSequential = $0300; { ---- -011 ---- ---- }
  67.  stfNoCache          = $1000; { ---1 ---- ---- ---- }
  68.  stfFailOnError      = $2000; { --1- ---- ---- ---- }
  69.  stfWriteThrough     = $4000; { -1-- ---- ---- ---- }
  70.  stfDASD             = $8000; { 1--- ---- ---- ---- }
  71. type
  72.  pFileStream = ^tFileStream;
  73.  tFileStream = object(tStream)
  74.   F           : File;
  75.   constructor Create(const fName : string; openMode : Word);
  76.   function    Name : string; virtual;
  77.   function    Put(var Data; bytes : word) : word; virtual;
  78.   function    Get(var Data; bytes : word) : word; virtual;
  79.   procedure   Skip(bytes : longint); virtual;
  80.   procedure   Seek(newPos : longint); virtual;
  81.   function    GetPos : longint; virtual;
  82.   function    Size : longint; virtual;
  83.   function    EOS : boolean; virtual;
  84.   function    GetTime : longint; virtual;
  85.   procedure   SetTime(Time : longint); virtual;
  86.   function    GetAttr : longint;
  87.   procedure   SetAttr(Attr : longint);
  88.   procedure   Truncate;
  89.   procedure   Free; virtual;
  90.   destructor  Erase;
  91.  end;
  92.  
  93. Implementation uses Dos, Strings;
  94.  
  95. function tStream.Name;
  96. begin
  97.  Name := '';
  98. end;
  99.  
  100. function tStream.Get;
  101. begin
  102.  Get := 0;
  103.  if Error = steOK
  104.   then Error := steNotApplicable;
  105. end;
  106.  
  107. function tStream.Put;
  108. begin
  109.  Put := 0;
  110.  if Error = steOK
  111.   then Error := steNotApplicable;
  112. end;
  113.  
  114. procedure tStream.Skip;
  115. var
  116.  buff  : Pointer;
  117.  bsz,I : Word;
  118. begin
  119.  if Error = steOK
  120.   then begin
  121.         Seek(GetPos + bytes);
  122.         if Error <> steOK
  123.          then begin
  124.                Error := steOK;
  125.                bsz := minL(minL(maxAvail, $FFF0), bytes);
  126.                GetMem(buff, bsz);
  127.                if buff <> nil
  128.                 then begin
  129.                       While (Error = steOK) and (bytes > 0) do
  130.                        begin
  131.                         I := minL(bytes, bsz);
  132.                         Dec(bytes, Get(buff^, I));
  133.                        end;
  134.                       FreeMem(buff, bsz);
  135.                      end
  136.                 else Error := steNoMemory;
  137.               end;
  138.        end;
  139. end;
  140.  
  141. procedure tStream.Seek;
  142. begin
  143.  if Error = steOK
  144.   then Error := steNotApplicable;
  145. end;
  146.  
  147. function tStream.GetPos;
  148. begin
  149.  GetPos := -1;
  150.  if Error = steOK
  151.   then Error := steNotApplicable;
  152. end;
  153.  
  154. function tStream.Size;
  155. begin
  156.  Size := -1;
  157.  if Error = steOK
  158.   then Error := steNotApplicable;
  159. end;
  160.  
  161. function tStream.EOS;
  162. begin
  163.  EOS := TRUE;
  164.  if Error = steOK
  165.   then Error := steNotApplicable;
  166. end;
  167.  
  168. procedure tStream.PutStr;
  169. begin
  170.  Put(S, succ(length(S)));
  171. end;
  172.  
  173. function tStream.GetStr;
  174. var
  175.  S : string;
  176. begin
  177.  S := '';
  178.  Get(S[0], 1);
  179.  Get(S[1], length(S));
  180.  GetStr := S;
  181. end;
  182.  
  183. procedure tStream.PutZTstr;
  184. var
  185.  I : SmallWord;
  186. begin
  187.  I := strLen(S);
  188.  Put(I, sizeOf(I));
  189.  Put(S^, I);
  190. end;
  191.  
  192. function tStream.GetZTstr;
  193. var
  194.  I : SmallWord;
  195.  S : pChar;
  196. begin
  197.  Get(I, sizeOf(I));
  198.  if Error = steOK
  199.   then begin
  200.         GetMem(S, succ(I));
  201.         Get(S^, I);
  202.         pByteArray(S)^[I] := 0;
  203.        end
  204.   else S := nil;
  205.  GetZTstr := S;
  206. end;
  207.  
  208. function tStream.CopyFrom;
  209. var
  210.  Buff : Pointer;
  211.  bSz  : Word;
  212.  i,rc : longint;
  213. begin
  214.  CopyFrom := 0;
  215.  bSz := minL($FFF0, maxAvail);
  216.  GetMem(Buff, bSz);
  217.  if Buff = nil then begin Error := steNoMemory; exit; end;
  218.  rc := 0;
  219.  While (not S.EOS) and (bytes <> 0) and (Error = steOK) do
  220.   begin
  221.    if bytes = -1
  222.     then i := bSz
  223.     else i := minL(bytes, bSz);
  224.    i := S.Get(Buff^, i);
  225.    Put(Buff^, i);
  226.    if bytes <> -1
  227.     then Dec(bytes, i);
  228.    Inc(rc, i);
  229.   end;
  230.  FreeMem(Buff, bSz);
  231.  CopyFrom := rc;
  232. end;
  233.  
  234. constructor tFilter.Create;
  235. begin
  236.  inherited Create;
  237.  ChainStream := Chain;
  238. end;
  239.  
  240. function tFilter.Name;
  241. begin
  242.  if ChainStream <> nil
  243.   then Name := ChainStream^.Name
  244.   else Name := inherited Name;
  245. end;
  246.  
  247. function tFilter.Get;
  248. begin
  249.  if Error = steOK
  250.   then if ChainStream <> nil
  251.         then begin
  252.               Get := ChainStream^.Get(Data, bytes);
  253.               Error := ChainStream^.Error;
  254.              end
  255.         else Get := inherited Get(Data, bytes)
  256.   else Get := 0;
  257. end;
  258.  
  259. function tFilter.Put;
  260. begin
  261.  if Error = steOK
  262.   then if ChainStream <> nil
  263.         then begin
  264.               Put := ChainStream^.Put(Data, bytes);
  265.               Error := ChainStream^.Error;
  266.              end
  267.         else Put := inherited Put(Data, bytes)
  268.   else Put := 0;
  269. end;
  270.  
  271. procedure tFilter.Skip;
  272. begin
  273.  if Error = steOK
  274.   then if (ChainStream <> nil)
  275.         then begin
  276.               ChainStream^.Skip(bytes);
  277.               Error := ChainStream^.Error;
  278.              end
  279.         else inherited Skip(bytes);
  280. end;
  281.  
  282. function tFilter.EOS;
  283. begin
  284.  if ChainStream <> nil
  285.   then begin
  286.         EOS := ChainStream^.EOS;
  287.         Error := ChainStream^.Error;
  288.        end
  289.   else EOS := inherited EOS;
  290. end;
  291.  
  292. constructor tFileStream.Create;
  293. label
  294.  fCreate;
  295. var
  296.  oldMode : Integer;
  297. begin
  298.  inherited Create;
  299.  Assign(F, fName);
  300.  oldMode := FileMode;
  301.  FileMode := openMode;
  302.  case openMode and stmAccessMask of
  303.   stmReadOnly,
  304.   stmReadWrite : begin
  305.                   Reset(F, 1);
  306.                   if ioResult <> 0
  307.                    then if openMode and stmAccessMask = stmReadWrite
  308.                          then goto fCreate
  309.                          else Error := steNoSuchFile;
  310.                  end;
  311.   stmWriteOnly : begin
  312. fCreate:          Rewrite(F, 1);
  313.                   if ioResult <> 0
  314.                    then Error := steCreateError;
  315.                  end;
  316.   else Error := steInvalidOpenMode;
  317.  end;
  318.  FileMode := oldMode;
  319. end;
  320.  
  321. function tFileStream.Name;
  322. begin
  323.  Name := strPas(FileRec(F).Name);
  324. end;
  325.  
  326. function tFileStream.Put;
  327. var
  328.  L : Word;
  329. begin
  330.  Put := 0;
  331.  if Error = steOK
  332.   then begin
  333.         blockWrite(F, Data, bytes, L);
  334.         if ioResult <> 0 then Error := steWriteError;
  335.         Put := L;
  336.        end;
  337. end;
  338.  
  339. function tFileStream.Get;
  340. var
  341.  L : Word;
  342. begin
  343.  Get := 0;
  344.  if Error = steOK
  345.   then begin
  346.         blockRead(F, Data, bytes, L);
  347.         if ioResult <> 0 then Error := steReadError;
  348.         Get := L;
  349.        end;
  350. end;
  351.  
  352. procedure tFileStream.Skip;
  353. begin
  354.  if Error = steOK
  355.   then begin
  356.         inOutRes := 0;
  357.         System.Seek(F, filePos(F) + bytes);
  358.         if ioResult <> 0 {not a random-access file}
  359.          then inherited Skip(bytes);
  360.        end;
  361. end;
  362.  
  363. function tFileStream.GetPos;
  364. begin
  365.  if Error = steOK
  366.   then begin
  367.         inOutRes := 0;
  368.         GetPos := FilePos(F);
  369.         if ioResult <> 0 then Error := steSeekError;
  370.        end
  371.   else GetPos := -1;
  372. end;
  373.  
  374. procedure tFileStream.Seek;
  375. begin
  376.  if Error = steOK
  377.   then begin
  378.         System.Seek(F, newPos);
  379.         if ioResult <> 0 then Error := steSeekError;
  380.        end;
  381. end;
  382.  
  383. function tFileStream.Size;
  384. begin
  385.  if Error = steOK
  386.   then begin
  387.         inOutRes := 0;
  388.         Size := System.FileSize(F);
  389.         if ioResult <> 0 then Error := steNotApplicable;
  390.        end
  391.   else Size := -1;
  392. end;
  393.  
  394. function tFileStream.EOS;
  395. begin
  396.  if Error = steOK
  397.   then begin
  398.         inOutRes := 0;
  399.         EOS := System.EOF(F);
  400.         if ioResult <> 0 then Error := steNotApplicable;
  401.        end
  402.   else EOS := TRUE;
  403. end;
  404.  
  405. function tFileStream.GetTime;
  406. var
  407.  L : longint;
  408. begin
  409.  if Error = steOK
  410.   then begin
  411.         GetFTime(F, L);
  412.         GetTime := L;
  413.         if ioResult <> 0 then Error := steNotApplicable;
  414.        end
  415.   else GetTime := 0;
  416. end;
  417.  
  418. procedure tFileStream.SetTime;
  419. begin
  420.  if (Error = steOK) and (Time <> 0)
  421.   then begin
  422.         SetFTime(F, Time);
  423.         if ioResult <> 0 then Error := steNotApplicable;
  424.        end;
  425. end;
  426.  
  427. function tFileStream.GetAttr;
  428. var
  429.  W : word;
  430. begin
  431.  if Error = steOK
  432.   then begin
  433.         GetFAttr(F, W);
  434.         GetAttr := W;
  435.         if ioResult <> 0 then Error := steNotApplicable;
  436.        end
  437.   else GetAttr := 0;
  438. end;
  439.  
  440. procedure tFileStream.SetAttr;
  441. begin
  442.  if (Error = steOK) and (Attr <> 0)
  443.   then begin
  444.         SetFAttr(F, Attr);
  445.         if ioResult <> 0 then Error := steNotApplicable;
  446.        end;
  447. end;
  448.  
  449. procedure tFileStream.Truncate;
  450. begin
  451.  if Error = steOK
  452.   then begin
  453.         System.Truncate(F);
  454.         if ioResult <> 0 then Error := steNotApplicable;
  455.        end;
  456. end;
  457.  
  458. procedure tFileStream.Free;
  459. begin
  460.  inOutRes := 0;
  461.  Close(F);
  462.  inOutRes := 0;
  463. end;
  464.  
  465. destructor tFileStream.Erase;
  466. begin
  467.  Free;
  468.  System.Erase(F);
  469.  inOutRes := 0;
  470. end;
  471.  
  472. end.
  473.  
  474.