Amnon82
18th March 2006, 13:14
I used OpenC2pas 0.03 to translate D2VParse.c to D2VParse.pas.
It isn't complete and I need some help.
// ParseD2V by Donald A. Graft
// Released under the Gnu Public Licence (GPL)
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include 'global.h'
Integer parse_d2v(HWND hWnd, char *szInput)
begin
FILE *fp, *wfp;
char line[2048], *p;
Integer i, fill, val, prev_val := -1, ndx := 0, count := 0, fdom := -1;
Integer D2Vformat := 0;
Integer vob, cell;
unsigned Integer gop_field, field_operation;
char render[128], temp[20];
Integer type;
// Open the D2V file to be parsed.
fp := fopen(szInput, 'r');
if (fp = 0) then
begin
MessageBox(hWnd, 'Cannot open the D2V file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
// Mutate the file name to the output text file to receive the parsed data.
p := (* C2PAS: RefOrBit? *)&szInput[strlen(szInput)];
while (*p <> '.') do p--;
p[1] := 0;
StrCat(p, 'parse.txt');
// Open the output file.
wfp := fopen(szInput, 'w');
if (wfp = 0) then
begin
MessageBox(hWnd, 'Cannot create the parse output file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(wfp, 'D2V Parse Output'#10''#10'');
// Pick up the D2V format number
fgets(line, 2048, fp);
if (strncmp(line, 'DGIndexProjectFile', 18) <> 0) then
begin
MessageBox(hWnd, 'The file is not a DGIndex project file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 0;
end;
sscanf(line, 'DGIndexProjectFile%d', (* C2PAS: RefOrBit? *)&D2Vformat);
if (D2Vformat <> D2V_FILE_VERSION) then
begin
MessageBox(hWnd, 'Obsolete D2V file.'#10'Recreate it with this version of DGIndex.', nil, MB_OK or MB_ICONERROR);
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(wfp, 'Encoded Frame: Display Frames....Flags Byte (* means in 3:2 pattern)'#10'');
fprintf(wfp, '--------------------------------------------------------------------'#10''#10'');
// Get the field operation flag.
while (fgets(line, 2048, fp) <> 0) do
begin
if (strncmp(line, 'Field_Operation', 8) = 0) then break;
end;
sscanf(line, 'Field_Operation=%d', (* C2PAS: RefOrBit? *)&field_operation);
if (field_operation = 1) then
fprintf(wfp, '[Forced Film, ignoring flags]'#10'')
else if (field_operation = 2) then
fprintf(wfp, '[Raw Frames, ignoring flags]'#10'')
else
fprintf(wfp, '[Field Operation None, using flags]'#10'');
// Skip past the rest of the D2V header info to the data lines.
while (fgets(line, 2048, fp) <> 0) do
begin
if (strncmp(line, 'Location', 8) = 0) then break;
end;
while (fgets(line, 2048, fp) <> 0) do
begin
if (line[0] <> '\n') then break;
end;
// Process the data lines.
do
begin
// Skip to the TFF/RFF flags entries.
p := line;
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&gop_field);
if (gop_field (* C2PAS: RefOrBit? *)& 0x100) then
fprintf(wfp, '[GOP]'#10'');
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
sscanf(p, '%d', (* C2PAS: RefOrBit? *)&vob);
while (*p++ <> ' ') do ;
sscanf(p, '%d', (* C2PAS: RefOrBit? *)&cell);
while (*p++ <> ' ') do ;
// Process the flags entries.
while ((*p > := '0' and *p < := '9') or (*p > := 'a' and *p < := 'f')) do
begin
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&val);
if (val = 0xff) then
begin
fprintf(wfp, '[EOF]'#10'');
break;
end;
// Pick up the frame type.
type := (val (* C2PAS: RefOrBit? *)& 0x30) >> 4;
sprintf(temp, '%d [%s]', ndx, type = 1 ? 'I' : (type = 2 ? 'P' : 'B'));
// Isolate the TFF/RFF bits.
val (* C2PAS: RefOrBit? *)& := 0x3;
// Determine field dominance.
if (fdom = -1) then
begin
fdom := (val >> 1) (* C2PAS: RefOrBit? *)& 1;
fprintf(wfp, '[Clip is %s]'#10'', fdom ? 'TFF' : 'BFF');
end;
// Show encoded-to-display mapping.
if (field_operation = 0) then
begin
if ((count mod 2) and (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d,%d', temp, ndx + count/2, ndx + count/2 + 1, ndx + count/2 + 1);
end
else if ((count mod 2) and not (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d', temp, ndx + count/2, ndx + count/2 + 1);
end
else if ( not (count mod 2) and (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d,%d', temp, ndx + count/2, ndx + count/2, ndx + count/2 + 1);
end
else if ( not (count mod 2) and not (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d', temp, ndx + count/2, ndx + count/2);
end;
fill := 32 - strlen(render);
for (i := 0; i < fill; i++) StrCat(render, '.');
sprintf(temp, '%x', val);
fprintf(wfp, '%s%s', render, temp);
end
else
begin
sprintf(render, '%s: %d,%d', temp, ndx, ndx);
fill := 32 - strlen(render);
for (i := 0; i < fill; i++) StrCat(render, '.');
sprintf(temp, '%x', val);
fprintf(wfp, '%s%s', render, temp);
end;
// Show vob cell id.
// printf(" [vob/cell=%d/%d]", vob, cell);
// Print warnings for 3:2 breaks and field order transitions.
if ((prev_val > := 0) and ((val = 0 and prev_val = 3) or (val <> 0 and val = prev_val + 1))) then
begin
fprintf(wfp, ' *');
end;
if (prev_val > := 0) then
begin
if ((val = 2 and prev_val = 0) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 3 and prev_val = 0) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 0 and prev_val = 1) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 1 and prev_val = 1) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 0 and prev_val = 2) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 1 and prev_val = 2) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 2 and prev_val = 3) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 3 and prev_val = 3) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]');
end;
fprintf(wfp, ''#10'');
// Count the encoded frames.
ndx++;
// Count the number of pulled-down fields.
if (val = 1 or val = 3) then count++;
// Move to the next flags entry.
while (*p <> ' ' and *p <> '\n') do p++;
p++;
prev_val := val;
end;
end; while ((fgets(line, 2048, fp) <> 0) and
((line[0] > := '0' and line[0] < := '9') or (line[0] > := 'a' and line[0] < := 'f'))) do ;
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 1;
end;
Integer fix_d2v(HWND hWnd, char *Input, Integer silent)
begin
FILE *fp, *wfp, *dfp;
char line[2048], prev_line[2048], wfile[2048], logfile[2048], *p, *q;
Integer val, mval, prev_val, mprev_val, fix;
Boolean first, found;
Integer D2Vformat := 0;
fp := fopen(Input, 'r');
if (fp = 0) then
begin
MessageBox(hWnd, 'Cannot open the D2V file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
// Pick up the D2V format number
fgets(line, 1024, fp);
if (strncmp(line, 'DGIndexProjectFile', 18) <> 0) then
begin
MessageBox(hWnd, 'The file is not a DGIndex project file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
sscanf(line, 'DGIndexProjectFile%d', (* C2PAS: RefOrBit? *)&D2Vformat);
if (D2Vformat <> D2V_FILE_VERSION) then
begin
MessageBox(hWnd, 'Obsolete D2V file.'#10'Recreate it with this version of DGIndex.', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
StrCopy(wfile, Input);
StrCat(wfile,'.fixed');
wfp := fopen(wfile, 'w');
if (wfp = 0) then
begin
MessageBox(hWnd, 'Cannot create the fixed D2V file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
fputs(line, wfp);
// Mutate the file name to the output text file to receive processing status information.
StrCopy(logfile, Input);
p := (* C2PAS: RefOrBit? *)&logfile[strlen(logfile)];
while (*p <> '.') do p--;
p[1] := 0;
StrCat(p, 'fix.txt');
// Open the output file.
dfp := fopen(logfile, 'w');
if (dfp = 0) then
begin
fclose(fp);
fclose(wfp);
MessageBox(hWnd, 'Cannot create the info output file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(dfp, 'D2V Fix Output'#10''#10'');
while (fgets(line, 1024, fp) <> 0) do
begin
fputs(line, wfp);
if (strncmp(line, 'Location', 8) = 0) then break;
end;
fgets(line, 1024, fp);
fputs(line, wfp);
fgets(line, 1024, fp);
prev_line[0] := 0;
prev_val := -1;
mprev_val := -1;
found := false;
do
begin
p := line;
while (*p++ <> ' ') do;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
first := true;
while ((*p > := '0' and *p < := '9') or (*p > := 'a' and *p < := 'f')) do
begin
fix := -1;
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&val);
if (val = 0xff) then break;
// Isolate the TFF/RFF bits.
mval := val (* C2PAS: RefOrBit? *)& 0x3;
if (prev_val > := 0) then mprev_val := prev_val (* C2PAS: RefOrBit? *)& 0x3;
// Detect field order transitions.
if (mval = 2 and mprev_val = 0) then fix := 1
else if (mval = 3 and mprev_val = 0) then fix := 1
else if (mval = 0 and mprev_val = 1) then fix := 0
else if (mval = 1 and mprev_val = 1) then fix := 0
else if (mval = 0 and mprev_val = 2) then fix := 3
else if (mval = 1 and mprev_val = 2) then fix := 3
else if (mval = 2 and mprev_val = 3) then fix := 2
else if (mval = 3 and mprev_val = 3) then fix := 2;
// Correct the field order transition.
if (fix > := 0) then
begin
found := true;
fprintf(dfp, 'Field order transition: %x -> %x'#10'', mprev_val, mval);
fprintf(dfp, prev_line);
fprintf(dfp, line);
if (first = false) then
begin
q := p;
while (*q-- <> ' ') do ;
end
else
begin
q := prev_line;
while (*q <> '\n') do q++;
while ( not ((*q > := '0' and *q < := '9') or (*q > := 'a' and *q < := 'f'))) do q--;
end;
*q := (char) fix + '0';
fprintf(dfp, 'corrected...'#10'');
fprintf(dfp, prev_line);
fprintf(dfp, line);
fprintf(dfp, ''#10'');
end;
while (*p <> ' ' and *p <> '\n') do p++;
p++;
prev_val := val;
first := false;
end;
fputs(prev_line, wfp);
StrCopy(prev_line, line);
end; while ((fgets(line, 2048, fp) <> 0) and
((line[0] > := '0' and line[0] < := '9') or (line[0] > := 'a' and line[0] < := 'f'))) do ;
fputs(prev_line, wfp);
fputs(line, wfp);
while (fgets(line, 2048, fp) <> 0) do fputs(line, wfp);
fclose(fp);
fclose(wfp);
if (found = false) then
begin
if (silent) then
begin
fclose(dfp);
unlink(wfile);
unlink(logfile);
end
else
begin
fprintf(dfp, 'No errors found.'#10'');
fclose(dfp);
unlink(wfile);
MessageBox(hWnd, 'No errors found.', 'Fix D2V', MB_OK or MB_ICONINFORMATION);
end;
(* C2PAS: Exit *) Result := 0;
end
else
begin
FILE *bad, *good, *fixed;
char c;
fclose(dfp);
// Copy the D2V file to *.d2v.bad version.
good := fopen(Input, 'r');
if (good = 0) then
(* C2PAS: Exit *) Result := 1;
sprintf(line, '%s.bad', Input);
bad := fopen(line, 'w');
if (bad = 0) then
begin
fclose(good);
(* C2PAS: Exit *) Result := 1;
end;
while ((c := fgetc(good)) <> EOF) do fputc(c, bad);
fclose(good);
fclose(bad);
// Copy the *.d2v.fixed version to the D2V file.
good := fopen(Input, 'w');
if (good = 0) then
(* C2PAS: Exit *) Result := 1;
sprintf(line, '%s.fixed', Input);
fixed := fopen(line, 'r');
while ((c := fgetc(fixed)) <> EOF) do fputc(c, good);
fclose(good);
fclose(fixed);
// Ditch the *.d2v.fixed version.
unlink(line);
if ( not CLIActive) then
begin
MessageBox(hWnd, 'Field order corrected. The original version was'#10'saved with the extension ".bad".', 'Correct Field Order', MB_OK or MB_ICONINFORMATION);
if ( not silent) then
ShellExecute(hDlg, 'open', logfile, nil, nil, SW_SHOWNORMAL);
end;
end;
(* C2PAS: Exit *) Result := 1;
end;
I'll keep on translating it myself and post my further progress.
It isn't complete and I need some help.
// ParseD2V by Donald A. Graft
// Released under the Gnu Public Licence (GPL)
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include 'global.h'
Integer parse_d2v(HWND hWnd, char *szInput)
begin
FILE *fp, *wfp;
char line[2048], *p;
Integer i, fill, val, prev_val := -1, ndx := 0, count := 0, fdom := -1;
Integer D2Vformat := 0;
Integer vob, cell;
unsigned Integer gop_field, field_operation;
char render[128], temp[20];
Integer type;
// Open the D2V file to be parsed.
fp := fopen(szInput, 'r');
if (fp = 0) then
begin
MessageBox(hWnd, 'Cannot open the D2V file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
// Mutate the file name to the output text file to receive the parsed data.
p := (* C2PAS: RefOrBit? *)&szInput[strlen(szInput)];
while (*p <> '.') do p--;
p[1] := 0;
StrCat(p, 'parse.txt');
// Open the output file.
wfp := fopen(szInput, 'w');
if (wfp = 0) then
begin
MessageBox(hWnd, 'Cannot create the parse output file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(wfp, 'D2V Parse Output'#10''#10'');
// Pick up the D2V format number
fgets(line, 2048, fp);
if (strncmp(line, 'DGIndexProjectFile', 18) <> 0) then
begin
MessageBox(hWnd, 'The file is not a DGIndex project file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 0;
end;
sscanf(line, 'DGIndexProjectFile%d', (* C2PAS: RefOrBit? *)&D2Vformat);
if (D2Vformat <> D2V_FILE_VERSION) then
begin
MessageBox(hWnd, 'Obsolete D2V file.'#10'Recreate it with this version of DGIndex.', nil, MB_OK or MB_ICONERROR);
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(wfp, 'Encoded Frame: Display Frames....Flags Byte (* means in 3:2 pattern)'#10'');
fprintf(wfp, '--------------------------------------------------------------------'#10''#10'');
// Get the field operation flag.
while (fgets(line, 2048, fp) <> 0) do
begin
if (strncmp(line, 'Field_Operation', 8) = 0) then break;
end;
sscanf(line, 'Field_Operation=%d', (* C2PAS: RefOrBit? *)&field_operation);
if (field_operation = 1) then
fprintf(wfp, '[Forced Film, ignoring flags]'#10'')
else if (field_operation = 2) then
fprintf(wfp, '[Raw Frames, ignoring flags]'#10'')
else
fprintf(wfp, '[Field Operation None, using flags]'#10'');
// Skip past the rest of the D2V header info to the data lines.
while (fgets(line, 2048, fp) <> 0) do
begin
if (strncmp(line, 'Location', 8) = 0) then break;
end;
while (fgets(line, 2048, fp) <> 0) do
begin
if (line[0] <> '\n') then break;
end;
// Process the data lines.
do
begin
// Skip to the TFF/RFF flags entries.
p := line;
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&gop_field);
if (gop_field (* C2PAS: RefOrBit? *)& 0x100) then
fprintf(wfp, '[GOP]'#10'');
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
sscanf(p, '%d', (* C2PAS: RefOrBit? *)&vob);
while (*p++ <> ' ') do ;
sscanf(p, '%d', (* C2PAS: RefOrBit? *)&cell);
while (*p++ <> ' ') do ;
// Process the flags entries.
while ((*p > := '0' and *p < := '9') or (*p > := 'a' and *p < := 'f')) do
begin
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&val);
if (val = 0xff) then
begin
fprintf(wfp, '[EOF]'#10'');
break;
end;
// Pick up the frame type.
type := (val (* C2PAS: RefOrBit? *)& 0x30) >> 4;
sprintf(temp, '%d [%s]', ndx, type = 1 ? 'I' : (type = 2 ? 'P' : 'B'));
// Isolate the TFF/RFF bits.
val (* C2PAS: RefOrBit? *)& := 0x3;
// Determine field dominance.
if (fdom = -1) then
begin
fdom := (val >> 1) (* C2PAS: RefOrBit? *)& 1;
fprintf(wfp, '[Clip is %s]'#10'', fdom ? 'TFF' : 'BFF');
end;
// Show encoded-to-display mapping.
if (field_operation = 0) then
begin
if ((count mod 2) and (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d,%d', temp, ndx + count/2, ndx + count/2 + 1, ndx + count/2 + 1);
end
else if ((count mod 2) and not (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d', temp, ndx + count/2, ndx + count/2 + 1);
end
else if ( not (count mod 2) and (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d,%d', temp, ndx + count/2, ndx + count/2, ndx + count/2 + 1);
end
else if ( not (count mod 2) and not (val = 1 or val = 3)) then
begin
sprintf(render, '%s: %d,%d', temp, ndx + count/2, ndx + count/2);
end;
fill := 32 - strlen(render);
for (i := 0; i < fill; i++) StrCat(render, '.');
sprintf(temp, '%x', val);
fprintf(wfp, '%s%s', render, temp);
end
else
begin
sprintf(render, '%s: %d,%d', temp, ndx, ndx);
fill := 32 - strlen(render);
for (i := 0; i < fill; i++) StrCat(render, '.');
sprintf(temp, '%x', val);
fprintf(wfp, '%s%s', render, temp);
end;
// Show vob cell id.
// printf(" [vob/cell=%d/%d]", vob, cell);
// Print warnings for 3:2 breaks and field order transitions.
if ((prev_val > := 0) and ((val = 0 and prev_val = 3) or (val <> 0 and val = prev_val + 1))) then
begin
fprintf(wfp, ' *');
end;
if (prev_val > := 0) then
begin
if ((val = 2 and prev_val = 0) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 3 and prev_val = 0) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 0 and prev_val = 1) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 1 and prev_val = 1) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 0 and prev_val = 2) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 1 and prev_val = 2) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 2 and prev_val = 3) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]')
else if ((val = 3 and prev_val = 3) or (val = 2 and prev_val = 0)) then
fprintf(wfp, ' [FIELD ORDER TRANSITION!]');
end;
fprintf(wfp, ''#10'');
// Count the encoded frames.
ndx++;
// Count the number of pulled-down fields.
if (val = 1 or val = 3) then count++;
// Move to the next flags entry.
while (*p <> ' ' and *p <> '\n') do p++;
p++;
prev_val := val;
end;
end; while ((fgets(line, 2048, fp) <> 0) and
((line[0] > := '0' and line[0] < := '9') or (line[0] > := 'a' and line[0] < := 'f'))) do ;
fclose(fp);
fclose(wfp);
(* C2PAS: Exit *) Result := 1;
end;
Integer fix_d2v(HWND hWnd, char *Input, Integer silent)
begin
FILE *fp, *wfp, *dfp;
char line[2048], prev_line[2048], wfile[2048], logfile[2048], *p, *q;
Integer val, mval, prev_val, mprev_val, fix;
Boolean first, found;
Integer D2Vformat := 0;
fp := fopen(Input, 'r');
if (fp = 0) then
begin
MessageBox(hWnd, 'Cannot open the D2V file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
// Pick up the D2V format number
fgets(line, 1024, fp);
if (strncmp(line, 'DGIndexProjectFile', 18) <> 0) then
begin
MessageBox(hWnd, 'The file is not a DGIndex project file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
sscanf(line, 'DGIndexProjectFile%d', (* C2PAS: RefOrBit? *)&D2Vformat);
if (D2Vformat <> D2V_FILE_VERSION) then
begin
MessageBox(hWnd, 'Obsolete D2V file.'#10'Recreate it with this version of DGIndex.', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
StrCopy(wfile, Input);
StrCat(wfile,'.fixed');
wfp := fopen(wfile, 'w');
if (wfp = 0) then
begin
MessageBox(hWnd, 'Cannot create the fixed D2V file!', nil, MB_OK or MB_ICONERROR);
fclose(fp);
(* C2PAS: Exit *) Result := 0;
end;
fputs(line, wfp);
// Mutate the file name to the output text file to receive processing status information.
StrCopy(logfile, Input);
p := (* C2PAS: RefOrBit? *)&logfile[strlen(logfile)];
while (*p <> '.') do p--;
p[1] := 0;
StrCat(p, 'fix.txt');
// Open the output file.
dfp := fopen(logfile, 'w');
if (dfp = 0) then
begin
fclose(fp);
fclose(wfp);
MessageBox(hWnd, 'Cannot create the info output file!', nil, MB_OK or MB_ICONERROR);
(* C2PAS: Exit *) Result := 0;
end;
fprintf(dfp, 'D2V Fix Output'#10''#10'');
while (fgets(line, 1024, fp) <> 0) do
begin
fputs(line, wfp);
if (strncmp(line, 'Location', 8) = 0) then break;
end;
fgets(line, 1024, fp);
fputs(line, wfp);
fgets(line, 1024, fp);
prev_line[0] := 0;
prev_val := -1;
mprev_val := -1;
found := false;
do
begin
p := line;
while (*p++ <> ' ') do;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
while (*p++ <> ' ') do ;
first := true;
while ((*p > := '0' and *p < := '9') or (*p > := 'a' and *p < := 'f')) do
begin
fix := -1;
sscanf(p, '%x', (* C2PAS: RefOrBit? *)&val);
if (val = 0xff) then break;
// Isolate the TFF/RFF bits.
mval := val (* C2PAS: RefOrBit? *)& 0x3;
if (prev_val > := 0) then mprev_val := prev_val (* C2PAS: RefOrBit? *)& 0x3;
// Detect field order transitions.
if (mval = 2 and mprev_val = 0) then fix := 1
else if (mval = 3 and mprev_val = 0) then fix := 1
else if (mval = 0 and mprev_val = 1) then fix := 0
else if (mval = 1 and mprev_val = 1) then fix := 0
else if (mval = 0 and mprev_val = 2) then fix := 3
else if (mval = 1 and mprev_val = 2) then fix := 3
else if (mval = 2 and mprev_val = 3) then fix := 2
else if (mval = 3 and mprev_val = 3) then fix := 2;
// Correct the field order transition.
if (fix > := 0) then
begin
found := true;
fprintf(dfp, 'Field order transition: %x -> %x'#10'', mprev_val, mval);
fprintf(dfp, prev_line);
fprintf(dfp, line);
if (first = false) then
begin
q := p;
while (*q-- <> ' ') do ;
end
else
begin
q := prev_line;
while (*q <> '\n') do q++;
while ( not ((*q > := '0' and *q < := '9') or (*q > := 'a' and *q < := 'f'))) do q--;
end;
*q := (char) fix + '0';
fprintf(dfp, 'corrected...'#10'');
fprintf(dfp, prev_line);
fprintf(dfp, line);
fprintf(dfp, ''#10'');
end;
while (*p <> ' ' and *p <> '\n') do p++;
p++;
prev_val := val;
first := false;
end;
fputs(prev_line, wfp);
StrCopy(prev_line, line);
end; while ((fgets(line, 2048, fp) <> 0) and
((line[0] > := '0' and line[0] < := '9') or (line[0] > := 'a' and line[0] < := 'f'))) do ;
fputs(prev_line, wfp);
fputs(line, wfp);
while (fgets(line, 2048, fp) <> 0) do fputs(line, wfp);
fclose(fp);
fclose(wfp);
if (found = false) then
begin
if (silent) then
begin
fclose(dfp);
unlink(wfile);
unlink(logfile);
end
else
begin
fprintf(dfp, 'No errors found.'#10'');
fclose(dfp);
unlink(wfile);
MessageBox(hWnd, 'No errors found.', 'Fix D2V', MB_OK or MB_ICONINFORMATION);
end;
(* C2PAS: Exit *) Result := 0;
end
else
begin
FILE *bad, *good, *fixed;
char c;
fclose(dfp);
// Copy the D2V file to *.d2v.bad version.
good := fopen(Input, 'r');
if (good = 0) then
(* C2PAS: Exit *) Result := 1;
sprintf(line, '%s.bad', Input);
bad := fopen(line, 'w');
if (bad = 0) then
begin
fclose(good);
(* C2PAS: Exit *) Result := 1;
end;
while ((c := fgetc(good)) <> EOF) do fputc(c, bad);
fclose(good);
fclose(bad);
// Copy the *.d2v.fixed version to the D2V file.
good := fopen(Input, 'w');
if (good = 0) then
(* C2PAS: Exit *) Result := 1;
sprintf(line, '%s.fixed', Input);
fixed := fopen(line, 'r');
while ((c := fgetc(fixed)) <> EOF) do fputc(c, good);
fclose(good);
fclose(fixed);
// Ditch the *.d2v.fixed version.
unlink(line);
if ( not CLIActive) then
begin
MessageBox(hWnd, 'Field order corrected. The original version was'#10'saved with the extension ".bad".', 'Correct Field Order', MB_OK or MB_ICONINFORMATION);
if ( not silent) then
ShellExecute(hDlg, 'open', logfile, nil, nil, SW_SHOWNORMAL);
end;
end;
(* C2PAS: Exit *) Result := 1;
end;
I'll keep on translating it myself and post my further progress.