I need to read file content as string (binary file) to variable, then iterate over each character in that string, convert each character to its hexadecimal representation, use comma (or any other distinguishable character) as character separator and embed the result in my actual LaTeX file where I'm doing all that stuff.
In python I have something like this (4 lines!):
fileContent = ""
with open("binary.so", "rb") as f:
fileContent = f.read()
print( ",".join( map(lambda x: str(ord(x)), fileContent) ) )
Now the hard part:
- no extra packages = only tex primitives
- defined as tex macro:
\readfile{ input_file_here }
- must work with pdflatex
- file might be empty
- newlines and other special characters must be preserved (it is binary!)
- I need a check if it is really a file and not a directory and if I'm allowed to read such files
- should work on Linux/Unix and Windows (actually I need only for Linux, but more general solution would be nice)
- no extra definitions in preamble
- no
luatex
, noimmediate
, so no shell and no extra tools
Because this is a binary file (I need to treat all files like this) we have character values between 0 (0x00) and 255 (0xFF). Numbers might be HEX or just decimal. base64 encoding would be OK too, but without external tools and extra flags like --shell-escape
.
\readfile{binary.so} % will return something like "D,E,A,D,B,E,E,F,1,3,3,7" or "0x0D,0x0E,..." or "13,14,..."
I tried something like this and it didn't work: (example taken from here)
\newread\makerfile
\openin\makerfile=binary.so
\ifeof\makerfile
\else
% it looks promising, but it shouldn't read by newlines
\read\makerfile to\makerline
\closein\makerfile
% iterate over each character
% ...
\fi
E.g. this works, but I cannot iterate over characters, because I don't know how
\makeatletter
\newcommand\saferead[1] {
\bgroup
\let\do\@makeother
\dospecials
\catcode`\ =10 % spaces
\catcode`\^^M=\active % newlines
\input{#1}
\egroup
}
\makeatother
I googled like crazy and found only some useful bits but I'm still far away from what I want.
\immediate
? Do you mean the commands to be executed only when TeX ships a page out?$PATH
,$$
etc. (invoked without--shell-escape
flag for security reasons). I'm able to read arbitrary ascii files, and also write arbitrary tex in temp. There are multiple injection points in the middle of the document.