Below is an example of simple ESS script which loads any file as binary, an converts any byte, which has most signficant bit set, into C hex-escaped character. Other bytes get copied to output stream without changes.
It means, that if we feed non-BOM utf-8 file to this script, an output will be fully single-byte-compatible.
UTF-8 to C escaped hex. Click to unfold...
function utf8_2c_esc(inFileName)
var inFile = new EsFile(),
outFile = new EsFile(),
path,
inbuff, inb,
inbPrev,
out = B"";
{
inFile$flags = EsFileFlag$$Read;
outFile$flags = EsFileFlag$$Write;
inFile$name = inFileName;
path = EsPath::createFromFilePath(inFileName);
if( "in" == path$fileExt )
path$fileExt = "";
else
path$fileExt = path$fileExt + ".processed";
outFile$name = path.pathGet(
EsPathFlag$$Default,
""
);
inFile.open();
inbuff = inFile.readAllAsBinBuffer();
inFile.close();
foreach(inb in inbuff)
{
if( inb & 0x80 )
out += EsStr::toByteString(
EsStr::format(
"\\x%0.2X",
inb
)
);
else
{
// Previous was an escape sequence, check if this char may be interpreted as part of hex
// - insert noop string break to interrupt hex sequence: ""
if(
!inbPrev#isEmpty() &&
(inbPrev & 0x80) &&
(inb in [[B'0' .. B'9'], [B'A' .. B'F'], [B'a' .. B'f']])
)
{
out += B'"';
out += B'"';
}
out += inb;
}
inbPrev = inb;
}
outFile.open();
outFile.writeAllAsBinBuffer(out);
}
In addition, you may want to execute this script from command line using ess-console executable, for instance, the following batch will d othe job:
Sample batch for running UTF-8 to C escaped hex. Click to unfold...
echo OFF
rem Find scripting console
set CURPATH=%cd%
for /r "%ProgramFiles(x86)%" %%a in (*) do (
if "%%~nxa"=="ess-console.exe" (
set ESSCON2=%%~dpnxa
goto essc_found
)
)
for /r "%ProgramFiles%" %%a in (*) do (
if "%%~nxa"=="ess-console.exe" (
set ESSCON2=%%~dpnxa
goto essc_found
)
)
echo Could not find ESS scripting console executable
goto exit
:essc_found
if exist "%ESSCON1%" (
set ESSCON="%ESSCON1%"
goto main
)
if exist "%ESSCON2%" (
set ESSCON="%ESSCON2%"
goto main
)
:main
echo ON
rem Check if compiled binary present, and if not, compile it
set CESSE=c:%HOMEPATH%\ECO-E\cesse
if not exist "%CESSE%\utf8_2c_esc.cesse" (
%ESSCON% -f scripts-core\misc\utf8_2c_esc.ess -o "%CESSE%" -c -x
)
set SCRIPT=%CESSE%\utf8_2c_esc.cesse
rem execute utf8 string escaper _1 - input *.strings.cc.in file
%ESSCON% -f "%SCRIPT%" -r -e utf8_2c_esc;"%1" -x
:exit