1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
| program LabelsToNum;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils,
classes,
graphics;
var FFile,TFile:string; //FFile:From File (source File), TFIle:To File (Target)
f2:tstringList;
PileFichiersTraites:TStringList;
BoucleInfinie:TStringList;
IndexDebutBoucle:integer;
LinkedFile:TStringList;
type TTLabel=record
name:string;
line:string;
end;
TTypeLine=(TLNumbered,TLnotNumbered);
TFileContains=set of TTypeLine;
TError=(ECircularLink,EFileMissing);
TErrors=set of TError;
TScanState=(inCode,inString,inComment1,inComment2);
procedure RemoveComments(ASL:TStringList);
const _QUOTE=34;
_SLASH=47;
_BACKSLASH=92;
_STAR=42;
_LF=10; _CR=13;
var StreamIn,StreamOut :TMemoryStream;
c,previous:byte;
ScanState,NewState,PreviousState:TScanState;
begin
StreamIn:=TMemoryStream.Create;
StreamOut:=TMemoryStream.Create;
try
ScanState:=inCode;
NewState:=inCode;
PreviousState:=inCode;
previous:=0;
StreamIn.Clear;
StreamOut.Clear;
ASL.SaveToStream(StreamIn);
StreamIn.Position:=0;
while StreamIn.Position<StreamIn.Size do
begin
StreamIn.Read(c,1);
case ScanState of
inCode:begin
if ((previous=_SLASH) and (not (c in [_SLASH,_STAR]))
and (PreviousState<>inComment2))
then StreamOut.Write(previous,1);
case c of
_QUOTE:begin
NewState:=inString;
StreamOut.Write(c,1);
end;
_SLASH:if previous=_SLASH then NewState:=inComment1
else StreamOut.Write(c,1);
_STAR:if previous=_SLASH then NewState:=inComment2
else StreamOut.Write(c,1);
else StreamOut.Write(c,1);
end;
end;
inString:begin
if (c in [_QUOTE,_CR,_LF]) then NewState:=inCode;
StreamOut.Write(c,1);
end;
inComment1:begin
if c in [_CR,_LF] then begin
NewState:=inCode;
StreamOut.Write(c,1);
end;
end;
inComment2:if ((c=_SLASH) and (previous=_STAR))
then NewState:=inCode;
end;
PreviousState:=ScanState;
ScanState:=NewState;
previous:=c;
end;
StreamOut.position:=0;
ASL.Clear;
ASL.LoadFromStream(StreamOut);
finally
StreamIn.Free;
StreamOut.Free;
end
end;
function ScanFile(Fichier : String):TErrors;
var
Index : Integer;
WorkFile : TStringList;
Dest : String;
s,strimmed:string;
begin
if not FileExists(ExtractFilePath(FFile)+Fichier)
then begin
Result:=[EFileMissing];
exit;
end;
result:=[ECircularLink];
if PileFichiersTraites.indexOf(fichier)>-1
then begin
IndexDebutBoucle:=PileFichiersTraites.indexOf(fichier);
exit; //On sort de la fonction on a un duplicata
end;
result:=[];
PileFichiersTraites.Add(Fichier);
WorkFile := TStringList.Create;
try
WorkFile.LoadFromFile(Fichier);
//remove comments if any
RemoveComments(WorkFile);
Index:=0;
while ((Index<WorkFile.Count) and (result=[])) do
begin
s:=WorkFile.Strings[Index];
strimmed:=Trim(s);
if strimmed='' then begin
inc(Index);
Continue;
end;
if strimmed[1]='#' then
begin
Dest:=Copy(WorkFile.Strings[Index],2,Length(strimmed) - 1);
result:=ScanFile(Dest);
if result<>[] //On a eu une boucle infinie ou un fichier manquant
//alors on prépare un rapport d'erreurs
then if IndexDebutBoucle<=PileFichiersTraites.Count
then BoucleInfinie.Insert(0,Dest);
end
else LinkedFile.Add(s);
inc(Index);
end;
PileFichiersTraites.Delete(PileFichiersTraites.Count-1);
finally
WorkFile.free;
end;
end;
function DoLink(Fichier : String):TErrors;
begin
IndexDebutBoucle:=-1;
PileFichiersTraites.Clear;
BoucleInfinie.Clear;
LinkedFile.Clear;
result:=ScanFile(Fichier);
end;
procedure SortLabelArray(var ALabelArray:array of TTLabel);
var i,j,N:integer;
temp:TTLabel;
begin
N:=High(ALabelArray);
for i:=0 to N-1 do //on fait varier i de 0 à N-2
for j:=N downto (i+1) do //on fait varier j de N-1 à (i+1)
if ALabelArray[j].name>ALabelArray[i].name then
begin
temp:=ALabelArray[i];
ALabelArray[i]:=ALabelArray[j];
ALabelArray[j]:=temp;
end;
end;
function KindOfFile:TFileContains;
var i:integer;
s,strimmed:string;
begin
for i:=0 to LinkedFile.Count-1 do
begin
s:=LinkedFile.Strings[i];
strimmed:=Trim(s);
if strimmed='' then Continue;
case strimmed[1] of
'0'..'9':include(Result,TLNumbered);
'$','/':begin
//
end
else include(Result,TLnotNumbered);
end;
end;
end;
procedure ProcessFiles(f1,f2:TStringList;Firstn,Stepn:integer);
var LabelArray:array of TTLabel;
i:integer;
CurrentLine,LabelIndex:integer;
s,strimmed:string;
temp:TTLabel;
begin
CurrentLine:=Firstn;
for i:=0 to f1.Count-1 do
begin
s:=f1.Strings[i];
strimmed:=Trim(s);
if strimmed='' then Continue;
case strimmed[1] of
'&':begin
SetLength(LabelArray,Length(LabelArray)+1);
LabelIndex:=High(labelArray);
LabelArray[LabelIndex].name:=strimmed;
LabelArray[LabelIndex].line:=trim(IntToStr(CurrentLine));
end
else begin
f2.Add(IntToStr(CurrentLine)+' '+s);
Inc(CurrentLine,Stepn);
end;
end;
end;
////Sort LabelArray
SortLabelArray(LabelArray);
//Second Pass : replace Labels by linenumbers
for i:=0 to High(LabelArray) do
begin
temp:=LabelArray[i];
f2.Text:=StringReplace(f2.Text,temp.name,temp.line,[rfReplaceAll,rfIgnoreCase]);
end;
end;
//Tips and Info display
procedure Tips;
begin
writeln('"Labelized BASIC" to Oric BASIC converter for OSDK v1.3');
writeln('Wilfrid AVRILLON - 2007');
writeln;
writeln('Syntax : ',ExtractFileName(ParamStr(0)),' <FromFile> <ToFile> <FirstLineNum> <Step> -v');
writeln;
writeln('<FromFile>.... : File to be processed - mandatory');
writeln('<ToFile>.... : Target file, result of the process - mandatory');
writeln('<FirstLineNum> : Index of the first Line');
writeln(' of the Basic Program.');
writeln(' default=1 - this field is optional');
writeln('<Step>........ : Step used for the numbering of the BASIC program.');
writeln(' default=1 - this field is optional');
writeln;
writeln('Example : ',ExtractFileName(ParamStr(0)),' myprogram.txt myprogram.bas 5 5');
writeln;
end;
procedure SetColor(fg: byte; bg: byte);
begin
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),fg + (bg * 16));
end;
procedure DisplayError;
begin
setcolor(12,0);
write('Error : ');
setcolor(7,0);
end;
function FileNameIsValid(FileName:string):boolean;
var F:file;
begin
result:=true;
if FileExists(FileName) then exit;
try
AssignFile(F,Filename);
Rewrite(F);
CloseFile(F);
DeleteFile(FileName);
except
on EInOutError do result:=false;
end;
end;
procedure ExecuteProgram;
var firstlinenum:integer;
firstlinnums:string;
stepnum:integer;
stepnums:string;
b1,b2:boolean;
PossibleErrors:TErrors;
i:integer;
FileContains:TFileContains;
begin
if ParamCount<2
then begin
if ParamCount=0
then begin
DisplayError;
writeln('Not enough parameters !')
end
else begin
if ((pos('?',ParamStr(1))=0) and (pos('HELP',uppercase(ParamStr(1)))=0))
then begin
DisplayError;
writeln('Not enough parameters !')
end;
end;
tips;
exit;
end;
if ParamCount>4
then begin
DisplayError;
writeln('Too many parameters !');
tips;
exit;
end;
//number of parameters between 2 and 4
FFile:=ParamStr(1);
TFile:=ParamStr(2);
firstlinnums:=ParamStr(3);
stepnums:=ParamStr(4);
b1:=FileExists(Ffile);
if not b1
then begin
DisplayError;
write('The file ');
setcolor(15,0);
write(FFile);
setcolor(7,0);
writeln(' does not exist...');
Tips;
exit;
end;
b2:=FileNameIsValid(Tfile);
if not b2
then begin
DisplayError;
write('The file name ');
setcolor(15,0);
write(TFile);
setcolor(7,0);
writeln(' is not valid...');
tips;
exit;
end;
PossibleErrors:=DoLink(Ffile);
if EFileMissing in PossibleErrors
then begin
DisplayError;
Writeln('File not found :');
for i:=0 to BoucleInfinie.Count-1 do
begin
Write(' ',BoucleInfinie.strings[i]);
if i<>(BoucleInfinie.Count-1)
then begin
if i=0
then writeln(' calls')
else writeln(' which calls')
end
else begin
setcolor(15,0);
writeln(' which does not exist !');
setcolor(7,0);
end;
end;
exit;
end;
if ECircularLink in PossibleErrors
then begin
DisplayError;
Writeln('Circular link found :');
//Afficher Le Contenu de BoucleInfinie
for i:=0 to BoucleInfinie.Count-1 do
begin
Write(' ',BoucleInfinie.strings[i]);
if i<>(BoucleInfinie.Count-1)
then begin
if i=0
then writeln(' calls')
else writeln(' which calls')
end;
end;
exit;
end;
//TODO : vérifier si le fichier est numéroté ou non
FileContains:=KindOfFile;
if FileContains=[TLNumbered]//regular Basic file
then begin
LinkedFile.SaveToFile(TFile);
exit;
end;
if FileContains=[TLNumbered,TLnotNumbered]
then begin
DisplayError;
Writeln('Error : you mixed regular BASIC with labelized Basic !');
exit;
end;
//All is OK, continue
firstlinenum:=StrToIntDef(firstlinnums,1);
stepnum:=StrToIntDef(stepnums,1);
f2:=TStringList.Create;
try
if FileExists(TFile) then DeleteFile(TFile);
f2.Clear;
f2.Add('#file '+ExtractFileName(FFile));
writeln('Building the program LABELS');
ProcessFiles(LinkedFile,f2,firstlinenum,stepnum);
f2.SaveToFile(TFile);
finally
f2.Free;
end;
end;
begin
PileFichiersTraites:=TStringList.Create;
BoucleInfinie:=TStringList.Create;
LinkedFile:=TStringList.Create;
try
try
ExecuteProgram;
except
//Gérer la condition d'erreur
Writeln('Error encountered, this program terminates...');
//Définit ExitCode <> 0 pour indiquer la condition d'erreur (par convention)
ExitCode := 1;
end;
finally
PileFichiersTraites.Free;
BoucleInfinie.Free;
LinkedFile.Free;
end;
end. |
Partager