Files
Betriebsf-hrungssoftware/src/domain/kodierung.ts
Claude e748adc76f
Some checks failed
Prüfung / tests (push) Has been cancelled
Prüfung / container (push) Has been cancelled
Stammdaten: Leistungen mit Ordnern und Vorkalkulation, GAEB- und DATANORM-Import, Material mit Rabatten, Angebot mit Reitern Leistungen/Material und KI-Vorschlag (0.18.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
2026-10-21 16:02:01 +00:00

39 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Zeichensatz von Importdateien erkennen: GAEB-XML nennt ihn in der ersten Zeile, DATANORM ist meist im alten
// DOS-Zeichensatz (Codepage 850), manchmal Windows-1252 oder UTF-8. Codepage 850 kennt TextDecoder nicht.
const CP850 =
"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø×ƒáíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´­±‗¾¶§÷¸°¨·¹³²■ ";
export function cp850(bytes: Uint8Array): string {
let s = "";
for (let i = 0; i < bytes.length; i += 8192) {
const teil = bytes.subarray(i, i + 8192);
s += Array.from(teil, (b) => (b < 128 ? String.fromCharCode(b) : CP850[b - 128]!)).join("");
}
return s;
}
function utf8(bytes: Uint8Array): string | null {
try { return new TextDecoder("utf-8", { fatal: true }).decode(bytes); } catch { return null; }
}
/** Text einer Datei: UTF-8, sonst Codepage 850 oder Windows-1252, je nachdem, wo Umlaute plausibler sind */
export function dateiText(bytes: Uint8Array): string {
// GAEB-XML: Kodierung aus der XML-Deklaration
const anfang = new TextDecoder("latin1").decode(bytes.subarray(0, 200));
const dekl = /<\?xml[^>]*encoding=["']([^"']+)["']/i.exec(anfang);
if (dekl) {
const k = dekl[1]!.toLowerCase();
if (k === "utf-8" || k === "utf8") return utf8(bytes) ?? new TextDecoder("windows-1252").decode(bytes);
try { return new TextDecoder(k).decode(bytes); } catch { /* unbekannt: raten */ }
}
const u = utf8(bytes);
if (u !== null) return u.replace(/^/, "");
let dos = 0, win = 0;
for (const b of bytes) {
if (b === 0x81 || b === 0x84 || b === 0x94 || b === 0x8e || b === 0x99 || b === 0x9a || b === 0xe1) dos++;
else if (b === 0xe4 || b === 0xf6 || b === 0xfc || b === 0xc4 || b === 0xd6 || b === 0xdc || b === 0xdf) win++;
}
return dos > win ? cp850(bytes) : new TextDecoder("windows-1252").decode(bytes);
}