testobj.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Program Testobj;
  2. uses Objects;
  3. const
  4. { Possible error codes returned to DOS by this program }
  5. EXIT_NOERROR = 0;
  6. EXIT_NOTIFF = 1;
  7. EXIT_DOSERROR = 2;
  8. (*************************************************************************)
  9. (* Create a stream error procedure which will be called on error of the *)
  10. (* stream. Will Terminate executing program, as well as display info *)
  11. (* on the type of error encountered. *)
  12. (*************************************************************************)
  13. Procedure StreamErrorProcedure(Var S: TStream); FAR;
  14. Begin
  15. If S.Status = StError then
  16. Begin
  17. WriteLn('ERROR: General Access failure. Halting');
  18. Halt(EXIT_DOSERROR);
  19. end;
  20. If S.Status = StInitError then
  21. Begin
  22. Write('ERROR: Cannot Init Stream. Halting. ');
  23. { SPECIFIC TO DOS STREAMS }
  24. Case S.ErrorInfo of
  25. 2: WriteLn('File not found.');
  26. 3: WriteLn('Path not found.');
  27. 5: Writeln('Access denied.');
  28. else
  29. WriteLn;
  30. end;
  31. Halt(EXIT_DOSERROR);
  32. end;
  33. If S.Status = StReadError then
  34. Begin
  35. WriteLn('ERROR: Read beyond end of Stream. Halting');
  36. Halt(EXIT_DOSERROR);
  37. end;
  38. If S.Status = StWriteError then
  39. Begin
  40. WriteLn('ERROR: Cannot expand Stream. Halting');
  41. Halt(EXIT_DOSERROR);
  42. end;
  43. If S.Status = StGetError then
  44. Begin
  45. WriteLn('ERROR: Get of Unregistered type. Halting');
  46. Halt(EXIT_DOSERROR);
  47. end;
  48. If S.Status = StPutError then
  49. Begin
  50. WriteLn('ERROR: Put of Unregistered type. Halting');
  51. Halt(EXIT_DOSERROR);
  52. end;
  53. end;
  54. Procedure WriteInformation;
  55. { Writes information about the program }
  56. Begin
  57. WriteLn('Usage: fname.ext to check');
  58. Halt(EXIT_NOERROR);
  59. end;
  60. { Program to demonstrate the TDosStream object. }
  61. Const S : String = '0123456789';
  62. P : Pchar = '9876543210';
  63. Var Stream : TDosStream;
  64. Buf : String;
  65. L : word;
  66. begin
  67. StreamError:= @StreamErrorProcedure;
  68. Writeln ('Writing to stream : "01234567899876543210"');
  69. Stream.Init('testobj.tmp',stCreate);
  70. Stream.WriteStr (@S);
  71. Stream.StrWrite (P);
  72. Writeln ('Closing stream.');
  73. Stream.Done;
  74. Writeln ('Reading from stream : ');
  75. Stream.Init('testobj.tmp',StOpenRead);
  76. WriteLn('After opening');
  77. Writeln ('Reading (',S,') : ',Stream.ReadStr^);
  78. Writeln ('Reading (',P,') : ',Stream.StrRead);
  79. Writeln ('Closing stream.');
  80. Stream.Done;
  81. Writeln ('Same thing, using raw read method : ');
  82. Writeln ('Reading from stream : ');
  83. Stream.Init('testobj.tmp',StOpenRead);
  84. Stream.Read (Buf,11);
  85. Writeln ('Reading (',S,') : ',Buf);
  86. Stream.Read (L,2);
  87. Stream.Read (Buf[1],L);
  88. Buf[0]:=chr(L);
  89. Writeln ('Reading (',P,') : ',Buf);
  90. Writeln ('Closing stream.');
  91. Stream.Done;
  92. Writeln ('Statistics about stream : ');
  93. Stream.Init('testobj.tmp',StOpenRead);
  94. Writeln ('Size : ',Stream.GetSize);
  95. Writeln ('Position : ',Stream.GetPos);
  96. Writeln ('Reading (',S,') : ',Stream.ReadStr^);
  97. L:=Stream.GetPos;
  98. Writeln ('Position : ',L);
  99. Writeln ('Closing stream.');
  100. Stream.Done;
  101. Writeln ('Reading from stream : ');
  102. Stream.Init('testobj.tmp',StOpenRead);
  103. Writeln ('Seek to position :',L);
  104. Stream.Seek(L);
  105. Writeln ('Reading (',P,') : ',Stream.StrRead);
  106. Writeln ('Closing stream.');
  107. Stream.Done;
  108. Writeln ('Truncating stream to zero length.');
  109. Stream.Init('testobj.tmp',StOpenWrite);
  110. Stream.Truncate;
  111. Stream.Done;
  112. end.