|
17 | 17 | { you can add units after this }, |
18 | 18 | PasExt; |
19 | 19 |
|
| 20 | +const |
| 21 | + SRCDIR='../../../source/'; |
| 22 | + |
| 23 | +var |
| 24 | + BAR : String; |
| 25 | + GROUP : String; |
| 26 | + COUNT: integer; |
| 27 | + |
| 28 | +const |
| 29 | + Flags : array of record |
| 30 | + Flag : String; |
| 31 | + Text : String; |
| 32 | + end = ( |
| 33 | + (Flag:'U'; Text:'undocumented'), |
| 34 | + (Flag:'u'; Text:'partially documented'), |
| 35 | + (Flag:'P'; Text:'available only in protected mode'), |
| 36 | + (Flag:'R'; Text:'available only in real or V86 mode '), |
| 37 | + (Flag:'C'; Text:'callout or callback (usually hooked rather than called)'), |
| 38 | + (Flag:'O'; Text:'obsolete (no longer present in current versions)') |
| 39 | + ); |
| 40 | + |
| 41 | + |
| 42 | +type |
| 43 | + THeadField = record |
| 44 | + Key, Value, Text : String; |
| 45 | + end; |
| 46 | + THeadFields = array of THeadField; |
| 47 | + |
| 48 | +function CompressWhitespace(S : RawByteString) : RawByteString; |
| 49 | +begin |
| 50 | + Result:=Trim(StringReplace(S, TAB, SPACE, [rfReplaceAll])); |
| 51 | + repeat |
| 52 | + S:=Result; |
| 53 | + Result:=StringReplace(S, SPACE+SPACE, SPACE, [rfReplaceAll]); |
| 54 | + until Result=S; |
| 55 | +end; |
| 56 | + |
| 57 | +function GetFlag(Flag : String) : String; |
| 58 | +var |
| 59 | + I : Integer; |
| 60 | +begin |
| 61 | + Result:=''; |
| 62 | + for I := 0 to High(Flags) do |
| 63 | + if Flag=Flags[I].Flag then begin |
| 64 | + Result:=Flags[I].Text; |
| 65 | + Break; |
| 66 | + end; |
| 67 | +end; |
| 68 | + |
| 69 | +procedure ProcessFile(FileName : String); |
| 70 | +var |
| 71 | + Orig, Work, Dest, Title, S, K, V : RawByteString; |
| 72 | + Head : THeadFields; |
| 73 | + A, B : TArrayOfRawByteString; |
| 74 | + Needed : Boolean; |
| 75 | + I : Integer; |
| 76 | + DT : TDateTime; |
| 77 | + |
20 | 78 | begin |
| 79 | + if FileLoad(Filename, Orig) <> 0 then begin |
| 80 | + WriteLn('Error loading file: ' + FileName); |
| 81 | + Halt(1); |
| 82 | + end; |
| 83 | + if not FileAge(FileName, DT) then DT:=-1; |
| 84 | + Orig:=NormalizeLineEndings(Orig, CRLF); |
| 85 | + Work:=Orig; |
| 86 | + // Search to start of header. "Should be" the first line, but just in case... |
| 87 | + repeat |
| 88 | + S := PopDelim(Work, CRLF); |
| 89 | + if Copy(S, 1, 75) = Copy(BAR, 1, 75) then Break; |
| 90 | + if Work = '' then begin |
| 91 | + // File has no header. |
| 92 | + Exit; |
| 93 | + end; |
| 94 | + until false; |
| 95 | + Head:=[]; |
| 96 | + // Parse current header |
| 97 | + repeat |
| 98 | + S := PopDelim(Work, CRLF); |
| 99 | + if Copy(S, 1, 75) = Copy(BAR, 1, 75) then Break; |
| 100 | + if Work = '' then begin |
| 101 | + WriteLn('File header not terminated: ' + Filename); |
| 102 | + Exit; |
| 103 | + end; |
| 104 | + S:=CompressWhitespace(S); |
| 105 | + if S = '' then Continue; |
| 106 | + K:= PopDelim(S, COLON); |
| 107 | + S:=Trim(S); |
| 108 | + V:= PopDelim(S, SPACE); |
| 109 | + S:=Trim(S); |
| 110 | + Case Lowercase(K) of |
| 111 | + 'unique id' : begin |
| 112 | + SetLength(Head, Length(Head)+1); |
| 113 | + Head[High(Head)].Key:='Unique ID'; |
| 114 | + Head[High(Head)].Value:=V; |
| 115 | + Head[High(Head)].Text:=S; |
| 116 | + end; |
| 117 | + 'category' : begin |
| 118 | + SetLength(Head, Length(Head)+1); |
| 119 | + Head[High(Head)].Key:='Category'; |
| 120 | + Head[High(Head)].Value:=V; |
| 121 | + Head[High(Head)].Text:=S; |
| 122 | + end; |
| 123 | + 'flag' : begin |
| 124 | + // Ignore old flags |
| 125 | + (* |
| 126 | + SetLength(Head, Length(Head)+1); |
| 127 | + Head[High(Head)].Key:=K; |
| 128 | + Head[High(Head)].Value:=V; |
| 129 | + Head[High(Head)].Text:=S; |
| 130 | + *) |
| 131 | + end; |
| 132 | + else |
| 133 | + WriteLn('Unknown header field "' + K + '" in: ' + Filename); |
| 134 | + Exit; |
| 135 | + end; |
| 136 | + until false; |
| 137 | + // Get Entry Title |
| 138 | + repeat |
| 139 | + S := Trim(PopDelim(Work, CRLF)); |
| 140 | + until (S <> '') or (Work = ''); |
| 141 | + if S = '' then begin |
| 142 | + WriteLn('Entry Title not found: ' + FileName); |
| 143 | + Exit; |
| 144 | + end; |
| 145 | + Title:=S; |
| 146 | + // File roughly validated as an entry, display Group Once |
| 147 | + if GROUP <> '' then begin |
| 148 | + WriteLn(GROUP); |
| 149 | + GROUP:=''; |
| 150 | + end; |
| 151 | + // Parse the Title Flags |
| 152 | + S:=CompressWhiteSpace(S); |
| 153 | + S:=PopDelim(S, '-'); |
| 154 | + V:=Trim(CutDelim(S, SPACE, 3)); |
| 155 | + if Length(V) = 0 then begin |
| 156 | + SetLength(Head, Length(Head)+1); |
| 157 | + Head[High(Head)].Key:='Flag'; |
| 158 | + Head[High(Head)].Value:='n/a'; |
| 159 | + Head[High(Head)].Text:=''; |
| 160 | + end else begin |
| 161 | + for I := 1 to Length(V) do begin |
| 162 | + SetLength(Head, Length(Head)+1); |
| 163 | + Head[High(Head)].Key:='Flag'; |
| 164 | + Head[High(Head)].Value:=V[I]; |
| 165 | + S:=GetFlag(V[I]); |
| 166 | + if S = '' then begin |
| 167 | + WriteLn('Unknown flag "' + V[I] + '" in file: ' + FileName); |
| 168 | + Exit; |
| 169 | + end; |
| 170 | + Head[High(Head)].Text:=S; |
| 171 | + end; |
| 172 | + end; |
| 173 | + |
| 174 | + // Create entry with updated header |
| 175 | + Dest:=BAR + CRLF; |
| 176 | + for I := 0 to High(Head) do |
| 177 | + Cat(Dest, TrimRight( |
| 178 | + LeftPad(Head[I].Key, 10) + ':' + SPACE + |
| 179 | + RightPad(Head[I].Value, 3) + SPACE + |
| 180 | + Head[I].Text ) + |
| 181 | + CRLF); |
| 182 | + Cat(Dest, BAR + CRLF + CRLF + Title + CRLF + Work); // Copy body into Dest |
| 183 | + |
21 | 184 |
|
| 185 | + // If no change, we are done. |
| 186 | + if Dest = Orig then Exit; |
| 187 | + // Test for significant changes |
| 188 | + A:=Explode(Orig, CRLF); |
| 189 | + B:=Explode(Dest, CRLF); |
| 190 | + if High(A) = High(B) then begin |
| 191 | + Needed:=False; |
| 192 | + for I := 0 to High(A) do |
| 193 | + if CompressWhiteSpace(A[I]) <> CompressWhiteSpace(B[I]) then begin |
| 194 | + Needed:=True; |
| 195 | + Break; |
| 196 | + end; |
| 197 | + if not Needed then Exit; |
| 198 | + end; |
| 199 | + WriteLn(TAB + FileName); |
| 200 | + Inc(COUNT); |
| 201 | + // exit; |
| 202 | + if FileLoad(Filename, Dest) <> 0 then begin |
| 203 | + WriteLn('Error saving file: ' + FileName); |
| 204 | + Halt(1); |
| 205 | + end; |
| 206 | + if DT <> -1 then FileSetDate(Filename, DateTimeToFileDate(DT)); |
| 207 | +end; |
| 208 | + |
| 209 | +procedure ProcessGroup(G : String); |
| 210 | +var |
| 211 | + I : Integer; |
| 212 | + L : TStringList; |
| 213 | +begin |
| 214 | + L := TStringList.Create; |
| 215 | + GROUP:='Working: ' + G; |
| 216 | + DirScan(SRCDIR + G + PathDelimiter + WildCard, L, [dsFiles, dsRecursive]); |
| 217 | + for I := 0 to L.Count - 1 do |
| 218 | + if (not HasLeading(L[I], UNDERSCORE)) and (HasTrailing(L[I], '.txt', false)) then |
| 219 | + ProcessFile(SRCDIR + G + PathDelimiter + L[I]); |
| 220 | + L.Free; |
| 221 | +end; |
| 222 | + |
| 223 | +procedure ProcessAll; |
| 224 | +var |
| 225 | + I : Integer; |
| 226 | + G : TArrayOfString; |
| 227 | +begin |
| 228 | + COUNT:=0; |
| 229 | + DirScan(SRCDIR + Wildcard, G, [dsDirectories]); |
| 230 | + for I := 0 to High(G) do |
| 231 | + if LowerCase(G[I]) <> 'miscellaneous' then |
| 232 | + ProcessGroup(G[I]); |
| 233 | + WriteLn(COUNT, ' files updated'); |
| 234 | +end; |
| 235 | + |
| 236 | +begin |
| 237 | + BAR:=StringOf('-', 80); |
| 238 | + ProcessAll; |
22 | 239 | end. |
23 | 240 |
|
0 commit comments