Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Fix for #1203 Using the codepage with arduino.l4j.ini on win32 #1271

Merged
merged 6 commits into from
Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,16 @@ export function spawn(

let codepage = "65001";
if (os.platform() === "win32") {
try {
const chcp = child_process.execSync("chcp.com");
codepage = chcp.toString().split(":").pop().trim();
} catch (error) {
arduinoChannel.warning(`Defaulting to code page 850 because chcp.com failed.\
\rEnsure your path includes %SystemRoot%\\system32\r${error.message}`);
codepage = "850";
codepage = getArduinoL4jCodepage(command.replace(/.exe$/i, ".l4j.ini"));
if (!codepage) {
try {
const chcp = child_process.execSync("chcp.com");
codepage = chcp.toString().split(":").pop().trim();
} catch (error) {
arduinoChannel.warning(`Defaulting to code page 850 because chcp.com failed.\
\rEnsure your path includes %SystemRoot%\\system32\r${error.message}`);
codepage = "850";
}
}
}

Expand Down Expand Up @@ -253,6 +256,16 @@ export function spawn(
});
}

export function getArduinoL4jCodepage(filePath: string): string | undefined {
const encoding = parseConfigFile(filePath).get("-Dfile.encoding");
if (encoding === "UTF8") {
return "65001";
}
return Object.keys(encodingMapping).reduce((r, key) => {
return encodingMapping[key] === encoding ? key : r;
}, undefined);
}

export function decodeData(data: Buffer, codepage: string): string {
if (Object.prototype.hasOwnProperty.call(encodingMapping, codepage)) {
return iconv.decode(data, encodingMapping[codepage]);
Expand Down