Function Entry: CLAL-FILE-ENCODING

← Prev | ↑ Chapter | Next → | Index | Symbols

Function Entry: CLAL-FILE-ENCODING

Name

clal-file-encoding

Class

clautolisp Extension Function

Syntax

(clal-file-encoding path)

Arguments and Values

  • path – AutoLISP string. Filename to sniff; resolved against the AutoLISP search path the same way LOAD would. Need not be a .lsp source file; any file is accepted.

Description

Open path, read up to the first four bytes, and return the canonical clautolisp encoding implied by the byte-order mark:

BOM bytes Returned string
EF BB BF "UTF-8-BOM"
FF FE "UTF-16-LE"
FE FF "UTF-16-BE"
FF FE 00 00 "UTF-32-LE"
00 00 FE FF "UTF-32-BE"
no BOM "ANSI"

UTF-32-LE and UTF-16-LE share their first two bytes; the sniffer checks UTF-32 first so the two-byte UTF-16 prefix doesn't mask a four-byte UTF-32 BOM.

clal-file-encoding is the clautolisp counterpart to BricsCAD's vle-file-encoding. Differences:

  • Returns canonical clautolisp strings (e.g. "UTF-16-LE") rather than vendor strings ("UTF-16LE").
  • Preserves the UTF-8-BOM / plain-UTF-8 distinction. BricsCAD reports plain "UTF-8" in both cases; clautolisp reports "UTF-8-BOM" when the marker is present, letting user code decide between rewriting the BOM and dropping it.
  • Returns "ANSI" rather than the bare empty string when no BOM is detected — the file might be ANSI, UTF-8 without BOM, or something else; without a marker the encoding is undetermined and falls back to the host code page.

Return Values

  • AutoLISP string when the file exists and is readable.
  • nil when the file cannot be resolved; also sets ERRNO to 73 (same convention as LOAD's missing-file path).

Side Effects

  • Opens path for reading and immediately closes it.
  • Sets ERRNO to 73 on resolution failure, 0 on success.

Affected By

  • The AutoLISP search-path used by LOAD.

Exceptional Situations

  • Returns nil rather than signalling for the common "file not found" case; user code can chain clal-file-encoding into a presence check.

Examples

;; UTF-8 source with a BOM.
(clal-file-encoding "lib/with-bom.lsp")
=> "UTF-8-BOM"

;; BOMless source (could be anything from US-ASCII to ANSI to
;; UTF-8-without-BOM; we don't speculate).
(clal-file-encoding "lib/plain.lsp")
=> "ANSI"

;; Nonexistent path -> nil + ERRNO 73.
(clal-file-encoding "/no/such/file") => nil
(getvar "ERRNO")                     => 73

Availability

  • clautolisp: documented (this entry); implemented since clautolisp 1.0.83.
  • AutoCAD: NOT present.
  • BricsCAD: vle-file-encoding is the analogue but does not preserve the UTF-8-BOM distinction and uses vendor strings.

Source Notes

  • clautolisp-only extension. See encoding-dispatch.issue for the cross-dialect translation table this helper's vocabulary matches.