pucko
asked on
Help Convert C to Pascal
Can someone help me to convert this small program to delphi ?
http://www.let.rug.nl/~kleiweg/postscript/pssplit.c
http://www.let.rug.nl/~kleiweg/postscript/pssplit.c
here is a sample translation , using www.torry.net translation tools
unit test_c_txt;
{*************************************************************************
-> Converted with C to Pascal Converter 1.0
-> Release: 1.5.11.2008
-> Email: al_gun@ncable.net.au
-> Updates: http://cc.codegear.com/Author/302259
-> Copyright © 2005-2008 Ural Gunaydin, All rights reserved.
*************************************************************************}
interface
uses
Windows, Messages, SysUtils, Classes;
(*
* cFile: pssplit.c
*
* (c) Peter Kleiweg 1998
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
*)
//include <stdarg.h>
//include <stdio.h>
//include <stdlib.h>
//include <cString.h>
//include <errno.h>
{$ifdef __MSDOS__}
const strcasecmp(A, = B) (stricmp(A, B));
{$endif}
const BUFSIZE = 2048;
Char
buffer [BUFSIZE + 1],
*programname;
v1: procedure;
syntax (),
errit (Char const *format, Args: array of const);
function main (argc: integer; : Parray of argv): integer;
begin
cFile
*fp;
integer
page = 0,
prpage = 0,
doclevel = 0,
trailer = 0,
even = 0,
odd = 0,
beginpage,
endpage,
retval := 1;
programname := argv [0];
if (argc <> 5) then
syntax ();
beginpage := StrToInt (argv [2]);
endpage := StrToInt (argv [3]);
if (beginpage < 1 or endpage < beginpage) then
syntax ();
if ( not strcasecmp (argv [4], 'all')) then begin
even := 1;
odd := 1;
end; else if ( not strcasecmp (argv [4], 'even')) then
even := 1;
else if ( not strcasecmp (argv [4], 'odd')) then
odd := 1;
else
syntax ();
fp := FileOpen (argv [1], 'r');
if ( not fp) then
errit ('Opening cFile ' mod s': mod s', argv [1], strerror (errno));
while (fgets (buffer, BUFSIZE, fp)) begin
if ( not CompareMem (buffer, ' mod mod Page:', 7)) then begin
if ( not doclevel) then begin
page:= mod + 1;
prpage = (
page >= beginpage and page <= endpage
and ((odd and (page mod 2)) or (even and not (page mod 2)))
);
if (prpage) then begin
retval := 0;
fputs (buffer, stderr);
end;
end;
end; else if ( not CompareMem (buffer, ' mod mod BeginDocument', 15)) then
doclevel:= mod + 1;
else if ( not CompareMem (buffer, ' mod mod EndDocument', 13)) then
doclevel:= mod - 1;
else if ( not CompareMem (buffer, ' mod mod Trailer', 9)) then begin
if ( not doclevel) then
trailer := 1;
end;
if ((page = 0) or prpage or trailer) then
fputs (buffer, stdout);
end;
FileClose (fp);
if (retval) then
fprintf (stderr, ' mod s: No pages found', programname);
result:= retval;
end;
procedure errit (var format: const; v1: Args: array of const);
begin
va_list
list;
fprintf (stderr, 'Error mod s: ', programname);
va_start (list, format);
vfprintf (stderr, format, list);
fprintf (stderr, '');
exit (2);
end;
procedure syntax ();
begin
fprintf (
stderr,
'Usage: mod s cFile.ps beginpage endpage all or even or odd'
'Return value:'
'0 - ok'
'1 - no pages found'
'2 - error',
programname
);
exit (2);
end;
implementation
end.
ASKER
Still to much for me to get this to work
ASKER
the parts that I don't know what to replace with is for example:
cFile
*fp;
.....
fp := FileOpen (argv [1], 'r');
and this part:
cFile
*fp;
.....
fp := FileOpen (argv [1], 'r');
and this part:
fp := FileOpen (argv [1], 'r');
if ( not fp) then
errit ('Opening cFile ' mod s': mod s', argv [1], strerror (errno));
while (fgets (buffer, BUFSIZE, fp)) begin
if ( not CompareMem (buffer, ' mod mod Page:', 7)) then begin
if ( not doclevel) then begin
page:= mod + 1;
prpage = (
page >= beginpage and page <= endpage
and ((odd and (page mod 2)) or (even and not (page mod 2)))
);
if (prpage) then begin
retval := 0;
fputs (buffer, stderr);
end;
end;
end; else if ( not CompareMem (buffer, ' mod mod BeginDocument', 15)) then
doclevel:= mod + 1;
else if ( not CompareMem (buffer, ' mod mod EndDocument', 13)) then
doclevel:= mod - 1;
else if ( not CompareMem (buffer, ' mod mod Trailer', 9)) then begin
if ( not doclevel) then
trailer := 1;
end;
if ((page = 0) or prpage or trailer) then
fputs (buffer, stdout);
end;
FileClose (fp);
if (retval) then
fprintf (stderr, ' mod s: No pages found', programname);
result:= retval;
var
F: TextFile;
begin
AssignFile(F, ReportFileName);
ReWrite(F);
writeln(f, 'your text ');
closeFile(f);
end;
F: TextFile;
begin
AssignFile(F, ReportFileName);
ReWrite(F);
writeln(f, 'your text ');
closeFile(f);
end;
ASKER
How to write a text to a file I still understand, But not how to do the parsing with MemCompare etc.
That sure is a piss-poor C-to-Pascal translation. I wonder how many syntax errors that has in it. Looks to me like every single line has a syntax error. Multiple errors per line. The translator couldn't even put semi-colons in the right places, and this is even a simple rule to implement: never put a semi-colon in front of an 'else.'
The translation didn't even compensate for C's ability to use integers as booleans directly. So it assigns 0 or 1 to "odd" and "even" (which are built in Delphi functions by the way) then uses them as booleans later. i.e.: if odd then blah blah.
the StdErr output wasn't even properly defined to write to "ErrOutput" and this should've been an easy conversion. Similar for fgets, fputs, fprintf etc.
So, if I get this right, BdLm's help was this: here's a file with a few pascal keywords in it and identifiers copied from the C file. Now, rewrite every line because it doesn't even approach being a working translation.
The translation didn't even compensate for C's ability to use integers as booleans directly. So it assigns 0 or 1 to "odd" and "even" (which are built in Delphi functions by the way) then uses them as booleans later. i.e.: if odd then blah blah.
the StdErr output wasn't even properly defined to write to "ErrOutput" and this should've been an easy conversion. Similar for fgets, fputs, fprintf etc.
So, if I get this right, BdLm's help was this: here's a file with a few pascal keywords in it and identifiers copied from the C file. Now, rewrite every line because it doesn't even approach being a working translation.
ASKER
I agree.
Anyway, I think the right approach for me might be to use Ghostscript and the gsapi instead to extract the specified pages from a postscript file instead of converting this c code,
Unfortanly it seems like I only have two options there.
1.
The problem there was that -dFirstPage= xx and -dLastPage=yy only works for a pdf so my option there is to convert postscript to pdf, then extract the wanted pages into a new pdf and convert it back top postcript.
2. Extract to one postscript file per page with
the options -q -dBatch -dNOPAUSE-sDEVICE=pswrite -sOutputFile=c:\file.%003d .ps -f input.ps
Then through away the pages I don't want and then merge the wanted pages to a new ps file.
I think I will go for the first aproach and give up the idea of converting the c code. I guess ghostscript is more stable.
Anyway, I think the right approach for me might be to use Ghostscript and the gsapi instead to extract the specified pages from a postscript file instead of converting this c code,
Unfortanly it seems like I only have two options there.
1.
The problem there was that -dFirstPage= xx and -dLastPage=yy only works for a pdf so my option there is to convert postscript to pdf, then extract the wanted pages into a new pdf and convert it back top postcript.
2. Extract to one postscript file per page with
the options -q -dBatch -dNOPAUSE-sDEVICE=pswrite -sOutputFile=c:\file.%003d
Then through away the pages I don't want and then merge the wanted pages to a new ps file.
I think I will go for the first aproach and give up the idea of converting the c code. I guess ghostscript is more stable.
Well, GhostScript is not inherently more stable; any software is only as stable as the developer makes it. IMHO :)
I can't help with GhostScript in any way. And converting even that small C program to Delphi looks to me - while very do-able - like an all-day task. With a healthy dose of debugging, because invariably there is some tiny detail that screws everything up.
If I had to do it for myself then I wouldn't mind spending all day on this. The C program actually looks somewhat simple: it just reads a file into memory (2K bytes at a time) and then looks for certain phrases ('Page:', 'EndDocument', 'Trailer', etc.) on certain 'pages.' For some reason, being an odd or even page seems to matter.
The 2048 byte buffer is probably there only because of 1998's memory limitations (costs). Today, I don't think even disk cluster sizes are that small. So, I would probably modify the program to simply read in the entire file to memory, at once, and then scan through that looking for the targets.
Someone really proficient in C and Delphi might be able to whip this out in less time than a day though. I just know that I couldn't
Good luck!
I can't help with GhostScript in any way. And converting even that small C program to Delphi looks to me - while very do-able - like an all-day task. With a healthy dose of debugging, because invariably there is some tiny detail that screws everything up.
If I had to do it for myself then I wouldn't mind spending all day on this. The C program actually looks somewhat simple: it just reads a file into memory (2K bytes at a time) and then looks for certain phrases ('Page:', 'EndDocument', 'Trailer', etc.) on certain 'pages.' For some reason, being an odd or even page seems to matter.
The 2048 byte buffer is probably there only because of 1998's memory limitations (costs). Today, I don't think even disk cluster sizes are that small. So, I would probably modify the program to simply read in the entire file to memory, at once, and then scan through that looking for the targets.
Someone really proficient in C and Delphi might be able to whip this out in less time than a day though. I just know that I couldn't
Good luck!
ASKER
The ghostscript way is no problem for me. I currently use it for pdf conversion.
try #2 > convert from http://www.planet-source-c ode.com/vb /scripts/S howCode.as p?txtCodeI d=331&lngW Id=7
unit tesc;
interface
(*
* File: pssplit.c
*
* (c) Peter Kleiweg 1998
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2,
* ) or ((at your option) any later version.
*
*)
#ifdef __MSDOS__
#define strcasecmp(A, B) (strcomp(A, B))
#endif
#define BUFSIZE 2048
char
buffer [BUFSIZE + 1],
*programname;
procedure
syntax (procedure),
errit (char const *format, ...);
smallint main (smallint argc, char *argv [])
begin
FILE
*fp;
smallint
page := 0,
prpage := 0,
doclevel := 0,
trailer := 0,
even := 0,
odd := 0,
beginpage,
endpage,
retval := 1;
programname := argv [0];
if (argc <> 5)
syntax ;
beginpage := atoi (argv [2]);
endpage := atoi (argv [3]);
if (beginpage < 1 )) or (( endpage < beginpage)
syntax ;
if ( not strcasecmp (argv [4], 'all')) then begin
even := 1;
odd := 1;
end else if ( not strcasecmp (argv [4], 'even'))
even := 1;
else if ( not strcasecmp (argv [4], 'odd'))
odd := 1;
else
syntax ;
fp := fopen (argv [1], 'r');
if ( not fp)
errit ('Opening file \'%s\': %s', argv [1], strerror (errno));
while (fgets (buffer, BUFSIZE, fp)) then begin
if ( not memcmp (buffer, '%%Page:', 7)) then begin
if ( not doclevel) then begin
page++;
prpage := (
page >:= beginpage )) and (( page <:= endpage
)) and (( ((odd )) and (( (page % 2)) )) or (( (even )) and (( not (page % 2)))
);
if (prpage) then begin
retval := 0;
fputs (buffer, stderr);
end
end
end else if ( not memcmp (buffer, '%%BeginDocument', 15))
doclevel++;
else if ( not memcmp (buffer, '%%EndDocument', 13))
doclevel--;
else if ( not memcmp (buffer, '%%Trailer', 9)) then begin
if ( not doclevel)
trailer := 1;
end
if ((page := 0) )) or (( prpage )) or (( trailer)
fputs (buffer, stdout);
end
fclose (fp);
if (retval)
fprsmallintf (stderr, '\n%s: No pages found\n\n', programname);
return retval;
end
procedure errit (char const *format, ...)
begin
va_list
list;
fprsmallintf (stderr, '\nError %s: ', programname);
va_start (list, format);
vfprsmallintf (stderr, format, list);
fprsmallintf (stderr, '\n\n');
exit (2);
end
procedure syntax
begin
fprsmallintf (
stderr,
'\nUsage: %s file.ps beginpage endpage all|even|odd\n'
'Return value:\n'
'\t0 - ok\n'
'\t1 - no pages found\n'
'\t2 - error\n',
programname
);
exit (2);
end
the best result by reading the pascal file is cad kas c2delphi.exe converter, not for free, you may translate and type the code by hand to the delphi ide
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
ASKER
Must be something that differs though in the way it works. Get different result and corrupted postscripts that are much bigger with the delphi code abowe
Well if you give me a file to use it with, what is supposed to be outputted I could help you with it further.
http://sourceforge.net/project/showfiles.php?group_id=50975&package_id=44698&release_id=86188