Article View: comp.text
Article #687Re: String functions in TeX: instr and extract
From: Chris Torek
Date: Thu, 31 Aug 1989 06:16
Date: Thu, 31 Aug 1989 06:16
46 lines
2582 bytes
2582 bytes
>>I would like to redefine the \input command so that an alternate file
>>extension is used when the file is not found.
In article <19344@mimsy.UUCP> I wrote:
>You cannot achieve this. The only way to test for a file's existence
>is to open it; and open always succeeds. If the file cannot be opened,
>TeX interacts with its operator to get a different name---and it will
>not take `no name' for an answer! (I consider this a bug.)
This is not quite true (as ken@cs.rochester.edu pointed out). You
can first open it with `\openin', and then use `\ifeof' to check for
immediate EOF, which `appears' true for nonexistent files (and, after
testing, it seems it appears false for empty files!). Then, if and
only if it exists, you can \input it. Something like this (which I
may start using myself...):
\catcode`@=11 % plain TeX only, not in LaTeX style files.
% Give #1 as an error, with help #2. Stolen from LaTeX.
\def\@errhelp#1#2{\edef\@tempc{#2}\expandafter\errhelp\expandafter{\@tempc}
\errmessage{#1}}
\let\@oldinput=\input
\newread\test@existence
% Is the `\immediate\closein' below unnecessary?
\def\@inerr#1{\immediate\closein\test@existence
\@errhelp{Cannot read #1.tex}{The file you named does not exist,
or cannot be read.^^JIf you want to use a different file,
type \space I \string\input{file} <return> \space here.}}
\def\@inok#1{\immediate\closein\test@existence\@oldinput #1}
\def\input#1{\immediate\openin\test@existence=#1
\ifeof\test@existence\let\next\@inerr\else\let\next\@inok\fi\next{#1}}
\catcode`@=12 % plain TeX only again.
Incidentally, the reason for using \next and \@inerr and \@inok is that
otherwise TeX's error-interaction lets one delete tokens from the
expansion of \input. (My first version, before I experimented, used
\ifeof...\errmessage{Cannot read #1}\else...\fi; this let you delete
the \else...\fi part.)
(Now it is up to the original poster to decide how to look for
extensions in the argument to \input.)
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
Technical Details
Message-ID:
<19345@mimsy.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watmath!iuvax!uxc.cso.uiuc.edu!tank!mimsy!chrisReferences:
<1989Aug30.133802.15579@cs.dal.ca> <19344@mimsy.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watmath!iuvax!uxc.cso.uiuc.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.text Subject: Re: String functions in TeX: instr and extract Message-ID: <19345@mimsy.UUCP> Date: 31 Aug 89 06:16:17 GMT References: <1989Aug30.133802.15579@cs.dal.ca> <19344@mimsy.UUCP> Reply-To: chris@mimsy.umd.edu Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 46