Tweaks to get UTF-8 reading/writing correct on Windows.

This commit is contained in:
Michael Mathews 2011-06-19 10:47:24 +01:00
parent d5a0d3a3d9
commit a8cd6b59de
5 changed files with 41 additions and 40 deletions

View File

@ -1,29 +1,7 @@
function readFileSync(filename, encoding, callback) { function readFileSync(filename, encoding) {
if (typeof arguments[1] === 'function') { encoding = encoding || 'utf-8';
encoding = null;
callback = arguments[1]; return readFile(filename, encoding);
}
// TODO: support other encodings
var lines = [],
reader,
input,
s;
// see http://download.oracle.com/javase/1,5.0/docs/api/java/nio/charset/Charset.html
try {
reader = new java.io.InputStreamReader(new java.io.FileInputStream(filename), 'UTF-8');
input = new java.io.BufferedReader(reader);
while( (s = input.readLine()) != null) {
lines.push( new java.lang.String(s.getBytes('UTF-8')) );
}
return lines.join('\n');
}
catch (e) {
throw('Cannot read module file '+filename+', '+e);
}
} }
function readdirSync(path) { function readdirSync(path) {
@ -149,7 +127,7 @@ function copyFile(inFile, outDir, fileName) {
var bos = new Packages.java.io.BufferedOutputStream(new Packages.java.io.FileOutputStream(outFile), 4096); var bos = new Packages.java.io.BufferedOutputStream(new Packages.java.io.FileOutputStream(outFile), 4096);
var theChar; var theChar;
while ((theChar = bis.read()) != -1) { while ((theChar = bis.read()) != -1) {
bos.write(theChar); bos.write(theChar);
} }
bos.close(); bos.close();
bis.close(); bis.close();
@ -160,26 +138,33 @@ function toFile(path) {
return parts.pop(); return parts.pop();
} }
function write(path, content, encoding) { function writeFileSync(filename, data, encoding) {
var output = new java.io.BufferedWriter(new java.io.FileWriter(path)); encoding = encoding || 'utf-8';
var out = new Packages.java.io.PrintWriter(
new Packages.java.io.OutputStreamWriter(
new Packages.java.io.FileOutputStream(filename),
encoding
)
);
try { try {
//FileWriter always assumes default encoding is OK! out.write(data);
output.write( content );
} }
finally { finally {
output.close(); out.flush();
out.close();
} }
} }
module.exports = { module.exports = {
readFileSync: readFileSync, readFileSync: readFileSync,
writeFileSync: writeFileSync,
readdirSync: readdirSync, readdirSync: readdirSync,
stat: stat, stat: stat,
ls: ls, ls: ls,
mkPath: mkPath, mkPath: mkPath,
toDir: toDir, toDir: toDir,
copyFile: copyFile, copyFile: copyFile
write: write
}; };

View File

@ -283,7 +283,7 @@
var path = outdir + '/' + filename, var path = outdir + '/' + filename,
html = containerTemplate.call(data, data); html = containerTemplate.call(data, data);
fs.write(path, html, 'UTF-8') fs.writeFileSync(path, html)
} }
} }

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html>
<head> <head>
<meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSDoc: <?js= title ?></title> <title>JSDoc: <?js= title ?></title>
<script src="http://shjs.sourceforge.net/sh_main.min.js"> </script> <script src="http://shjs.sourceforge.net/sh_main.min.js"> </script>

View File

@ -1,5 +1,6 @@
/** /**
* @desc テスト * @constructor
* @desc Τεκμηρίωση είναι η επικοινωνία!
*/ */
test = function() { Test = function() {
}; };

View File

@ -145,5 +145,20 @@ testFile('test/t/cases/variations.js');
testFile('test/t/cases/versiontag.js'); testFile('test/t/cases/versiontag.js');
report(); var os = java.lang.System.getProperty('os.name'),
isWin = !!os.startsWith('Windows');
/** Add codes to display string in color (red) on the console (if OS supports). */
function red(str) {
if (isWin) { return str; }
else { return '\033[031m' + str + '\033[0m'; }
}
/** Add codes to display string in color (red) on the console (if OS supports). */
function green(str) {
if (isWin) { return str; }
else { return '\033[032m' + str + '\033[0m'; }
}
report();