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

Commit b5f1c2e

Browse files
authored
Refine codepage mapping. (#369)
1 parent 1da8030 commit b5f1c2e

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

misc/codepageMapping.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"936": "GBK",
3+
"932": "Shift_JIS"
4+
}

src/common/util.ts

+12-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as properties from "properties";
1010
import * as vscode from "vscode";
1111
import * as WinReg from "winreg";
1212

13+
const encodingMapping: object = JSON.parse(fs.readFileSync(path.join(__dirname, "../../../misc", "codepageMapping.json"), "utf8"));
14+
1315
/**
1416
* This function will return the VSCode C/C++ extesnion compatible platform literals.
1517
* @function getCppConfigPlatform
@@ -211,30 +213,10 @@ export function spawn(command: string, outputChannel: vscode.OutputChannel, args
211213

212214
if (outputChannel) {
213215
child.stdout.on("data", (data: Buffer) => {
214-
switch (codepage) {
215-
case "932":
216-
outputChannel.append(iconv.decode(data, "Shift_JIS"));
217-
break;
218-
case "936":
219-
outputChannel.append(iconv.decode(data, "GBK"));
220-
break;
221-
default:
222-
outputChannel.append(data.toString());
223-
break;
224-
}
216+
outputChannel.append(decodeData(data, codepage));
225217
});
226218
child.stderr.on("data", (data: Buffer) => {
227-
switch (codepage) {
228-
case "932":
229-
outputChannel.append(iconv.decode(data, "Shift_JIS"));
230-
break;
231-
case "936":
232-
outputChannel.append(iconv.decode(data, "GBK"));
233-
break;
234-
default:
235-
outputChannel.append(data.toString());
236-
break;
237-
}
219+
outputChannel.append(decodeData(data, codepage));
238220
});
239221
}
240222

@@ -250,6 +232,13 @@ export function spawn(command: string, outputChannel: vscode.OutputChannel, args
250232
});
251233
}
252234

235+
export function decodeData(data: Buffer, codepage: string): string {
236+
if (encodingMapping.hasOwnProperty(codepage)) {
237+
return iconv.decode(data, encodingMapping[codepage]);
238+
}
239+
return data.toString();
240+
}
241+
253242
export function tryParseJSON(jsonString: string) {
254243
try {
255244
const jsonObj = JSON.parse(jsonString);
@@ -397,5 +386,5 @@ export function getRegistryValues(hive: string, key: string, name: string): Prom
397386
}
398387

399388
export function convertToHex(number, width = 0) {
400-
return padStart(number.toString(16), width, "0");
389+
return padStart(number.toString(16), width, "0");
401390
}

0 commit comments

Comments
 (0)