bug 315672 - Manually unfold lines so as to not split UTF-8 character. patch by mvl, r=dmose

This commit is contained in:
mattwillis%gmail.com 2006-09-14 22:31:59 +00:00
Родитель 29ea29dae6
Коммит bb06b07439
1 изменённых файлов: 22 добавлений и 8 удалений

Просмотреть файл

@ -66,17 +66,31 @@ calIcsImporter.prototype.importFromStream =
function ics_importFromStream(aStream, aCount) {
var items = new Array();
// Read in the string. Note that it isn't a real string at this point, because
// likely, the file is utf8. The multibyte chars show up as multiple 'chars'
// in this string. So call it an array of octets for now.
var octetArray = "";
var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
scriptableInputStream.init(aStream);
octetArray = scriptableInputStream.read(-1);
// Some other apps (most notably, sunbird 0.2) happily splits an utf8 character
// between the octets, and adds a newline and space between them, for ics
// folding. Unfold manually before parsing the file as utf8.
// This is utf8 safe, because octets with the first bit 0 are always one-octet
// characters. So the space or the newline never can be part of a multi-byte
// char.
octetArray = octetArray.replace(/\n /, "");
// Interpret the byte-array as an utf8 string, and convert into a
// javascript string.
var convStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
.getService(Components.interfaces.nsIConverterInputStream);
convStream.init(aStream, 'UTF-8', 0, 0x0000);
var unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
// ics files are always utf8
unicodeConverter.charset = "UTF-8";
var str = unicodeConverter.ConvertFromUnicode(octetArray);
var tmpStr = {};
var str = "";
while (convStream.readString(-1, tmpStr)) {
str += tmpStr.value;
}
icssrv = Components.classes["@mozilla.org/calendar/ics-service;1"]
.getService(Components.interfaces.calIICSService);