dir-stuff.red 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. % MLG, 6:01am Thursday, 10 June 1982
  2. % Utilities to read and process DIR files
  3. %
  4. IMPORTS '(EXEC);
  5. % -------- Basic File Reader -------------
  6. Fluid '(File);
  7. procedure ReadOneLine;
  8. % Read a single line, return as string
  9. begin scalar c,l;
  10. while ((c:=ReadCh()) NEQ !$EOL!$) do
  11. If c EQ !$EOF!$ then Throw('Filer,'Done)
  12. else l:=c . l;
  13. Return list2string reverse l;
  14. end;
  15. procedure ReadDirFile F;
  16. % Read in a file as vector of strings
  17. begin scalar oldF,x;
  18. OldF:=Rds(F:=Open(F,'input));
  19. File:=NIL;
  20. Catch('Filer,'(ReadAllFile1));
  21. Rds OldF;
  22. Close F;
  23. Return List2vector Reverse File;
  24. end;
  25. procedure ReadAllFile1;
  26. % support for Read Dir File
  27. begin scalar l;
  28. While (l:=ReadOneLine()) do
  29. if Size(l)>=0 then file:= segmentstring(l,char '! ) . file;
  30. return List2Vector reverse file;
  31. end;
  32. %---------------------------------------------------
  33. procedure ReadCleanDir F;
  34. % read in a Dir File without dates, and clean up
  35. Begin scalar x;
  36. x:=ReadDirFile F; % As a vector of strings
  37. %/ x:=ExpandNames x; % Handle .xxx case
  38. x:=RemoveAllVersionNumbers x;
  39. %/ x:=RemoveDuplicates x; % Assume ordered
  40. Return x;
  41. End;
  42. %---- Now take apart the fields
  43. Procedure GetFileName(S); % Find part before dot
  44. begin scalar N,I;
  45. n:=Size S;
  46. i:=0;
  47. While i<=n and S[i] neq Char '!. do i:=i+1;
  48. return Sub(S,0,i-1);
  49. end;
  50. procedure GetExtension(S); % Find second part, after dot
  51. begin scalar N,I;
  52. n:=Size S;
  53. i:=n;
  54. While i>=0 and S[i] neq Char '!. do i:=i-1;
  55. return Sub(S,i+1,n-i-1);
  56. end;
  57. % Dont need to expand names anymore
  58. CommentOutCode <<
  59. procedure ExpandNames(Fvector); % replace .xxxx with yyy.xxx from previous
  60. Begin scalar F;
  61. for i:=1:Size(Fvector) do
  62. <<F:=Fvector[I];
  63. if F[0] EQ char '!.
  64. then Fvector[I]:=concat(GetFileName Fvector[I-1],F)>>;
  65. return Fvector;
  66. end;
  67. >>;
  68. procedure RemoveVersionNumber F; % replace xxxx.yyyy.nnn with xxxx.yyyy
  69. Begin scalar I;
  70. i:=Size(F);
  71. While i>=0 and F[i] NEQ char '!. do i:=i-1;
  72. Return Sub(F,0,i-1);
  73. end;
  74. procedure RemoveAllVersionNumbers(Fvector); % replace xxxx.yyy.nnn with xxx.yyy
  75. Begin
  76. For i:=0:Size(Fvector)
  77. do Fvector[I]:=RemoveVersionNumber Car Fvector[I];
  78. return Fvector;
  79. end;
  80. procedure GetDirInFile(Dstring,FileName);
  81. Docmds List("Dir ",Dstring,",",crlf,
  82. "out ",Filename,crlf,
  83. "no heading ",crlf,
  84. "separate ",crlf,
  85. "no summary ",crlf,
  86. crlf,"pop");
  87. procedure GetCleanDir Dstring;
  88. Begin Scalar x;
  89. GetDirInFile(Dstring,"Junk.Dir");
  90. x:=ReadCleanDir "junk.Dir";
  91. DoCmds List("Del junk.dir,",crlf,
  92. "exp ",crlf,crlf,"pop");
  93. return x
  94. End;
  95. procedure GetDatedDirInFile(Dstring,FileName);
  96. Docmds List("Dir ",Dstring,",",crlf,
  97. "out ",Filename,crlf,
  98. "no heading ",crlf,
  99. "separate ",crlf,
  100. "no summary ",crlf,
  101. "time write ",crlf,
  102. crlf,"pop");
  103. procedure GetCleanDatedDir Dstring;
  104. Begin Scalar x;
  105. GetDatedDirInFile(Dstring,"Junk.Dir");
  106. x:=ReadCleanDatedDir "junk.Dir";
  107. DoCmds List("Del junk.dir,",crlf,
  108. "exp ",crlf,crlf,"pop");
  109. return x
  110. End;
  111. procedure ReadCleanDatedDir F;
  112. begin scalar x;
  113. x:=ReadDirFile F;
  114. %/ x:=ExpandNames x; % Handle .xxx case
  115. For i:=0:Size(x)
  116. do Rplaca(x[i],RemoveVersionNumber Car x[I]);
  117. return x
  118. end;
  119. % Segment a string into fields:
  120. Procedure SegmentString(S,ch); % "parse" string in pieces at CH
  121. Begin scalar s0,sN,sN1, Parts, sa,sb;
  122. s0:=0;
  123. sn:=Size(S);
  124. sN1:=sN+1;
  125. L1:If s0>sn then goto L2;
  126. sa:=NextNonCh(Ch,S,s0,sN);
  127. if sa>sN then goto L2;
  128. sb:=NextCh(Ch,S,sa+1,sN);
  129. if sb>SN1 then goto L2;
  130. Parts:=SubSeq(S,sa,sb) . Parts;
  131. s0:=sb;
  132. goto L1;
  133. L2:Return Reverse Parts;
  134. End;
  135. Procedure NextCh(Ch,S,s1,s2);
  136. <<While (S1<=S2) and not(S[S1] eq Ch) do s1:=s1+1;
  137. S1>>;
  138. Procedure NextNonCh(Ch,S,s1,s2);
  139. <<While (S1<=S2) and (S[S1] eq Ch) do s1:=s1+1;
  140. S1>>;
  141. End;