|
| 1 | +// Copyright 2026, Jerome Shidel |
| 2 | +// The Clear BSD License |
| 3 | +// All rights reserved. |
| 4 | + |
| 5 | +program fixids; |
| 6 | + |
| 7 | +{$mode objfpc}{$H+} |
| 8 | + |
| 9 | +{$I patches.pp} // Various compiler directives to "fix" things. |
| 10 | + |
| 11 | +uses |
| 12 | + {$IFDEF USES_CWString} cwstring, {$ENDIF} |
| 13 | + {$IFDEF UNIX} |
| 14 | + cthreads, |
| 15 | + {$ENDIF} |
| 16 | + Classes, SysUtils |
| 17 | + { you can add units after this }, |
| 18 | + PasExt; |
| 19 | + |
| 20 | +type |
| 21 | + TSectionStyle = (ssNormal, ssGlossary, ssInterrupts, ssTables, ssSMM, ssMSR, |
| 22 | + ssI2C, ssMemory, ssCMOS, ssFarCall, ssPorts); |
| 23 | + |
| 24 | +const |
| 25 | + SRCDIR='../../../source/'; |
| 26 | + |
| 27 | +var |
| 28 | + BAR : String; |
| 29 | + GROUP : String; |
| 30 | + COUNT: integer; |
| 31 | + SectionStyle : TSectionStyle; |
| 32 | + |
| 33 | +function CompressWhitespace(S : RawByteString) : RawByteString; |
| 34 | +begin |
| 35 | + Result:=Trim(StringReplace(S, TAB, SPACE, [rfReplaceAll])); |
| 36 | + repeat |
| 37 | + S:=Result; |
| 38 | + Result:=StringReplace(S, SPACE+SPACE, SPACE, [rfReplaceAll]); |
| 39 | + until Result=S; |
| 40 | +end; |
| 41 | + |
| 42 | +function GenerateId(Data : String) : String; |
| 43 | +begin |
| 44 | + Result:=''; |
| 45 | +end; |
| 46 | + |
| 47 | +procedure ProcessFile(FileName : String); |
| 48 | +var |
| 49 | + Orig, Work, Dest, Title, S : RawByteString; |
| 50 | + Needed : Boolean; |
| 51 | + A, B : TArrayOfRawByteString; |
| 52 | + I : Integer; |
| 53 | + DT : TDateTime; |
| 54 | + NewID, OldID : String; |
| 55 | +begin |
| 56 | + if FileLoad(Filename, Orig) <> 0 then begin |
| 57 | + WriteLn('Error loading file: ' + FileName); |
| 58 | + Halt(1); |
| 59 | + end; |
| 60 | + if not FileAge(FileName, DT) then DT:=-1; |
| 61 | + Orig:=NormalizeLineEndings(Orig, CRLF); |
| 62 | + Work:=Orig; |
| 63 | + NewID:= ''; |
| 64 | + OldId:=''; |
| 65 | + // Search to start of header. "Should be" the first line, but just in case... |
| 66 | + repeat |
| 67 | + S := PopDelim(Work, CRLF); |
| 68 | + if Copy(S, 1, 75) = Copy(BAR, 1, 75) then Break; |
| 69 | + if Work = '' then begin |
| 70 | + // File has no header. |
| 71 | + Exit; |
| 72 | + end; |
| 73 | + until false; |
| 74 | + Dest:=''; |
| 75 | + // Parse current header |
| 76 | + repeat |
| 77 | + S := PopDelim(Work, CRLF); |
| 78 | + if HasLeading(Trim(S), 'Unique ID:', false) then begin |
| 79 | + PopDelim(S, COLON); |
| 80 | + OldId:=CompressWhitespace(S); |
| 81 | + Continue; |
| 82 | + end; |
| 83 | + Cat(Dest, S + CRLF); |
| 84 | + if Copy(S, 1, 75) = Copy(BAR, 1, 75) then Break; |
| 85 | + if Work = '' then begin |
| 86 | + WriteLn('File header not terminated: ' + Filename); |
| 87 | + Exit; |
| 88 | + end; |
| 89 | + until false; |
| 90 | + // Get Entry Title |
| 91 | + repeat |
| 92 | + S := Trim(PopDelim(Work, CRLF)); |
| 93 | + until (S <> '') or (Work = ''); |
| 94 | + if S = '' then begin |
| 95 | + WriteLn('Entry Title not found: ' + FileName); |
| 96 | + Exit; |
| 97 | + end; |
| 98 | + Title:=CompressWhiteSpace(S); |
| 99 | + |
| 100 | + // File roughly validated as an entry, display Group Once |
| 101 | + if GROUP <> '' then begin |
| 102 | + WriteLn(GROUP); |
| 103 | + GROUP:=''; |
| 104 | + end; |
| 105 | + |
| 106 | + NewID:=GenerateID(Title + CRLF + Work); |
| 107 | + |
| 108 | + // Create entry with updated header |
| 109 | + Dest:=BAR+CRLF + LeftPad('UniqueID:', 11) + SPACE + NewID + CRLF + Dest; |
| 110 | + Cat(Dest, CRLF + Title + CRLF + Work); // Copy body into Dest |
| 111 | + |
| 112 | + // If no change, we are done. |
| 113 | + if Dest = Orig then Exit; |
| 114 | + // Test for significant changes |
| 115 | + A:=Explode(Orig, CRLF); |
| 116 | + B:=Explode(Dest, CRLF); |
| 117 | + if High(A) = High(B) then begin |
| 118 | + Needed:=False; |
| 119 | + for I := 0 to High(A) do |
| 120 | + if CompressWhiteSpace(A[I]) <> CompressWhiteSpace(B[I]) then begin |
| 121 | + Needed:=True; |
| 122 | + Break; |
| 123 | + end; |
| 124 | + if not Needed then Exit; |
| 125 | + end; |
| 126 | + // WriteLn(TAB + FileName); |
| 127 | + Inc(COUNT); |
| 128 | + exit; |
| 129 | + WriteLn(StringOf('*', 80)); |
| 130 | + WriteLn(OldID, ' -> ', NewID); |
| 131 | + WriteLn(NormalizeLineEndings(Orig)); |
| 132 | + WriteLn(NormalizeLineEndings(Dest)); |
| 133 | + if count > 3 then Halt; |
| 134 | + exit; |
| 135 | + if FileSave(Filename, Dest) <> 0 then begin |
| 136 | + WriteLn('Error saving file: ' + FileName); |
| 137 | + Halt(1); |
| 138 | + end; |
| 139 | + if DT <> -1 then FileSetDate(Filename, DateTimeToFileDate(DT)); |
| 140 | +end; |
| 141 | + |
| 142 | +procedure ProcessGroup(G : String); |
| 143 | +var |
| 144 | + I : Integer; |
| 145 | + L : TStringList; |
| 146 | +begin |
| 147 | + case G of |
| 148 | + 'Interrupt List' : SectionStyle:=ssInterrupts; |
| 149 | + (* |
| 150 | + 'Model-Specific Registers' : SectionStyle:=ssMSR; |
| 151 | + 'Bus Devices I2C' : SectionStyle:=ssI2C; |
| 152 | + 'Memory Map' : SectionStyle:=ssMemory; |
| 153 | + 'Cmos-Memory Map' : SectionStyle:=ssCMOS; |
| 154 | + 'Far Call Interface' : SectionStyle:=ssFarCall; |
| 155 | + 'Ports List' : SectionStyle:=ssPorts; |
| 156 | + *) |
| 157 | + else |
| 158 | + Exit; |
| 159 | + end; |
| 160 | + L := TStringList.Create; |
| 161 | + GROUP:='Working: ' + G; |
| 162 | + WriteLn(G); |
| 163 | + DirScan(SRCDIR + G + PathDelimiter + WildCard, L, [dsFiles, dsRecursive]); |
| 164 | + for I := 0 to L.Count - 1 do |
| 165 | + if (not HasLeading(L[I], UNDERSCORE)) and (HasTrailing(L[I], '.txt', false)) then |
| 166 | + ProcessFile(SRCDIR + G + PathDelimiter + L[I]); |
| 167 | + L.Free; |
| 168 | +end; |
| 169 | + |
| 170 | +procedure ProcessAll; |
| 171 | +var |
| 172 | + I : Integer; |
| 173 | + G : TArrayOfString; |
| 174 | +begin |
| 175 | + COUNT:=0; |
| 176 | + DirScan(SRCDIR + Wildcard, G, [dsDirectories]); |
| 177 | + for I := 0 to High(G) do |
| 178 | + if LowerCase(G[I]) <> 'miscellaneous' then |
| 179 | + ProcessGroup(G[I]); |
| 180 | + WriteLn(COUNT, ' files updated'); |
| 181 | +end; |
| 182 | + |
| 183 | +begin |
| 184 | + BAR:=StringOf('-', 80); |
| 185 | + ProcessAll; |
| 186 | +end. |
| 187 | + |
| 188 | + |
0 commit comments