-
1. Re: Check if File Exists
Helge Blischke Aug 4, 2012 8:08 AM (in response to Eric Thorsteinson)Look into the PLRM for the status operator. If the file exists, it returns - among a couple of properties - true and false
otherwise (see the PLRM for details)
Helge
-
2. Re: Check if File Exists
Eric Thorsteinson Aug 7, 2012 7:09 AM (in response to Helge Blischke)I think I am making progress. I was able to handle the missing file names with a custom undefinedfilename handler.
For example
errordict begin
/*undefinedfilename /undefinedfilename load def
/undefinedfilename { %def
(DEFAULT.EPS) run
} bind def
end
This seems to work fine for me although this will not be helpfull if any other eps is missing, as any missing eps will get the default one printed. (in my case the report would be un usable if any other eps was missing.
I would like to build better error handling in to my report so I will keep playing with this, although I think I am on the right track.
-
3. Re: Check if File Exists
Mr. Horton Aug 8, 2012 4:55 PM (in response to Eric Thorsteinson)How about taking Helge's suggestion and writing something like this:
(queried.eps) dup status true {run %runs queried.eps file if it exists
}
{pop %pops the string (queried.eps) off the operand stack and then goes on to the next part of your PS program
}ifelse
-
4. Re: Check if File Exists
Eric Thorsteinson Aug 9, 2012 6:10 AM (in response to Mr. Horton)I will try that out today.
-
5. Re: Check if File Exists
Eric Thorsteinson Aug 13, 2012 7:52 AM (in response to Eric Thorsteinson)I have tried that out. and If I do this:
/BrokerLogo
{
(VALIDLOGOFILE.EPS) dup status true {(VALIDLOGOFILE.EPS) run
}{pop}ifelse} def
The logo prints.
If I try a file name that does not exist, I still tet undefunedfilename.
The Operandstack lists as follows on the error page:
===top of stack===
(INVALIDLOGOFILE.EPS)
false
(INVALIDLOGOFILE.EPS)
I hope to get more time to work on this today.
-
6. Re: Check if File Exists
Mr. Horton Aug 13, 2012 9:23 AM (in response to Eric Thorsteinson)Eric,
I removed the "true" after the "status" code as this always forces the filename to be "run" whether it exists or not, and that is why you got the undefinedfilename error.
The "dup" in the procedure I posted duplicates the file name string - you should not code another file name string in front of the "run" command because it already exists on the operand stack.
Unless you have to, for some specific reason, don't 'hard code" the file name string in your procedure, instead do something like this:
/Logo
{
dup status {run 4{pop}repeat}{pop}ifelse %The inserted "4{pop}repeat" pops the unused status information off of the operand stack if the file exists.
}def
%To run the procedure put the file name string on the operand stack and then call the "Logo" procedure like this:
(VALIDLOGOFILE.EPS) Logo
%This will make the Logo procedure useable for a variety of file names, instead of defining a new procedure for each unique file name.
-
7. Re: Check if File Exists
Eric Thorsteinson Aug 13, 2012 10:33 AM (in response to Mr. Horton)That mostly works for me.
if I do it exactly as you have above, if there is no valid logo, I do not get the error page. If there is a valid logo I get a typecheck error.
ERROR: typecheck
OFFENDING COMMAND: run COMMAND TYPE: operatortype
OPERANDSTACK: (5 total entries)
===top of stack===
1326208983
1344846436
117861
128
(VALIDEPS.EPS)
It should also be noted, I did a little test where I added the valid logo name (VALIDLOGO.EPS) before the run - "{(VALIDLOGO.EPS) run 4{pop}..."
That works fine for a good logo and a bad one.
In that command what is the "4" and the repeat? {run 4{pop}repeat}{pop}ifelse
-
8. Re: Check if File Exists
Mr. Horton Aug 13, 2012 10:51 AM (in response to Eric Thorsteinson)Eric,
put the "run" after the "4{pop}repeat".
Sorry about that.
-
9. Re: Check if File Exists
Eric Thorsteinson Aug 13, 2012 11:00 AM (in response to Mr. Horton)Ok. That works perfect for both valid and invalid images! Thank you so much for your help. Now, I just need to make sure that I understand what all is going on here. I understand everything except for 4{pop}repeat
Am I right in thinking that we are poping the last 4 values off the stack. In this case, the stack would be:
1326208983
1344846436
117861
128
(VALIDEPS.EPS)
This should leave just the logo, which we run in the next command? Asside from looking at the stack how would I know that I need to pop 4 items from there?
This is what I am running now, and it work good!
/Logo
{
dup status {4{pop}repeat run}{pop}ifelse
}def
/BrokerLogo
{
420 711 translate
(LOGOFILE.EPS) Logo
-420 -711 translate} def
-
10. Re: Check if File Exists
Mr. Horton Aug 13, 2012 11:34 AM (in response to Eric Thorsteinson)The PostScript Language Reference Manual (PLRM) gives the results of running the "status" command and that's how you know that it puts 4 items on the operand stack (ie. pages, bytes, referenced, created).
Another suggestion is that you encapsulate the "Logo" procedure like this:
/Logo
{
/Logoobj save def
dup status(4{pop}repeat run)(pop)ifelse
Logoobj restore
}def
%This will save the state of the interpreter before you attempt to find/run the logo eps file and then restore the state afterwards.
%Now you can call the procedure like this:
/BrokerLogo
{
420 711 translate
(LOGOFILE.EPS) Logo
}def
%because the Logo procedure "encapsulates" its function, your current positioning is still where the "420 711 translate" statement left it.
Since you are returning to a certain position it appears that you may not be done rendering the page and unless you have already dealt with temporarily redefining the showpage operator or your eps files don't have it then you can edit the Logo procedure so that any "showpage" in the file to run will be ignored like this:
/Logo
{
/*showpage /showpage load def %store a copy of the original showpage procedure.
/showpage {}def %redefine showpage to do nothing.
/Logoobj save def
dup status(4{pop}repeat run)(pop)ifelse
Logoobj restore
/showpage /*showpage load def %resets showpage back to its original definition
}def
-
11. Re: Check if File Exists
Eric Thorsteinson Aug 13, 2012 1:45 PM (in response to Mr. Horton)You are correct. I am not done with the page, and I had created a function to handle the showpage problem. I ran into that last week. Some of the EPS files I was printing were fine, others would mess up my print by printing. The PostScript Language Program Design book has a very good example on how to handle this.
I will play around with this a bit more.
Thanks again for all the help. This Postscript stuff has been forced on me, and it is a bit of a switch from teh C# and Java I normaly work with.
-
12. Re: Check if File Exists
jdeubert Aug 21, 2012 6:02 AM (in response to Eric Thorsteinson)Hi, Eric -
I'd take a different approach to this problem; rather than test for the existence of the file, I'd wrap the "run" call in a "stopped" invocation:
/ExtraLogo
{
/mysaveobj save def % Do our bookkeeping
mark % Push a mark on the stack
{ (6661.EPS) run } stopped % Try to execute the file
{ (File failed) = } if % If it fails, emit a simple error message
cleartomark % Clean up the stack
mysaveobj restore % Reclaim VM
} def
Note that the code in the "if" procedure body can be anything you wish, including an alternative version of the logo.
In the interest of robustness, you should arguably always execute an external file in a stopped context. I presume you are also doing all the other good practices, such as doing a save and restore around the external call.
The nice thing about this approach is that it will catch anything that goes wrong with the execution of the file: permission problems, errors in the PS code, etc.
If you're interested, there's an article on using EPS files in handwritten PS code in the November 2004 issue (#36) of the Acumen Journal (www.acumentraining.com/acumenjournal.html).
- John
-------
John Deubert
Acumen Training
PostScript & PDF consulting and training
www.acumentraining.com

