home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / fil2B8DF52B887F0BBE99452EBFA2BCF73F < prev    next >
Text File  |  2010-07-12  |  10KB  |  267 lines

  1. function SysUtils() {
  2.     this._uconv = this._Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(
  3.         this._Ci.nsIScriptableUnicodeConverter);
  4.     this._uconv.charset = "UTF-8";
  5. }
  6.  
  7. SysUtils.prototype = {
  8.     readTextFile: function SysUtils_readTextFile(file) {
  9.         var fileContents = "";
  10.         
  11.         var is = this._Cc["@mozilla.org/network/file-input-stream;1"].createInstance(this._Ci.nsIFileInputStream);
  12.         is.init(file, 0x01, 0, is.CLOSE_ON_EOF);
  13.         try {
  14.             var sis = this._Cc["@mozilla.org/scriptableinputstream;1"].createInstance(this._Ci.nsIScriptableInputStream);
  15.             sis.init(is);
  16.             try {
  17.                 while(sis.available() > 0)
  18.                     fileContents += this._uconv.ConvertToUnicode(sis.read(sis.available()));
  19.             } finally {
  20.                 sis.close();
  21.             }
  22.         } finally {
  23.             is.close();
  24.         }
  25.         
  26.         return fileContents;
  27.     },
  28.     
  29.     writeTextFile: function SysUtils_writeTextFile(file, text, accessRights) {
  30.         var fileOutStream = this._Cc["@mozilla.org/network/file-output-stream;1"]
  31.             .createInstance(this._Ci.nsIFileOutputStream);
  32.         let accessRights = accessRights || 0644;
  33.         fileOutStream.init(file, 0x02 | 0x08 | 0x20, accessRights, 0); 
  34.         
  35.         var converter = this._Cc["@mozilla.org/intl/converter-output-stream;1"]
  36.             .createInstance(this._Ci.nsIConverterOutputStream);
  37.         converter.init(fileOutStream, "UTF-8", 0, 0);
  38.         converter.writeString(text);
  39.         converter.close();
  40.     },
  41.     
  42.     writeStreamToFile: function SysUtils_writeStreamToFile(inputStream, destFile, accessRights, processWatcher) {
  43.         if ( !(inputStream instanceof this._Ci.nsIInputStream) )
  44.             throw new TypeError("First argument must be nsIInputStream");
  45.         if ( !(destFile instanceof this._Ci.nsIFile) )
  46.             throw new TypeError("Second argument must be nsIFile");
  47.         if ( (accessRights !== undefined) && !this.isNumber(accessRights) )
  48.             throw new TypeError("Third argument must be Number or undefined");
  49.         
  50.         var fileOutStream = this._Cc["@mozilla.org/network/file-output-stream;1"].createInstance(this._Ci.nsIFileOutputStream);
  51.         try {
  52.             let accessRights = accessRights || 0644;
  53.             fileOutStream.init(destFile, 0x02 | 0x08 | 0x20, accessRights, 0);
  54.             
  55.             var binInputStream = this._Cc["@mozilla.org/binaryinputstream;1"].createInstance(this._Ci.nsIBinaryInputStream);
  56.             binInputStream.setInputStream(inputStream);
  57.             try {
  58.                 var size;
  59.                 var buf;
  60.                 while (size = binInputStream.available()) {
  61.                     buf = binInputStream.readBytes(size);
  62.                     fileOutStream.write(buf, size);
  63.                     
  64.                     if (processWatcher) {
  65.                         processWatcher.onWrite(size);
  66.                         if (processWatcher.shouldStop())
  67.                             break;
  68.                     }
  69.                 }
  70.             } finally {
  71.                 binInputStream.close();
  72.             }
  73.         }
  74.         finally {
  75.             fileOutStream.close();
  76.         }
  77.     },
  78.     
  79.     extractZipArchive: function SysUtils_extractZipArchive(archiveFile, destDirFile) {
  80.         if ( !(archiveFile instanceof this._Ci.nsIFile) || !(destDirFile instanceof this._Ci.nsIFile) )
  81.             throw new TypeError("nsIFile required");
  82.         
  83.         function getItemFile(filePath) {
  84.             var itemLocation = destDirFile.clone();
  85.             var parts = filePath.split("/");
  86.             for (var i = 0; i < parts.length; ++i)
  87.                 itemLocation.append(parts[i]);
  88.             return itemLocation;
  89.         }
  90.         
  91.         var zReader = this._Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(this._Ci.nsIZipReader);
  92.         zReader.open(archiveFile);
  93.         try {
  94.             let entries = zReader.findEntries("*/");
  95.             
  96.             while (entries.hasMore()) {
  97.                 var entryName = entries.getNext();
  98.                 var target = getItemFile(entryName);
  99.                 if (!target.exists()) {
  100.                     target.create(Ci.nsILocalFile.DIRECTORY_TYPE, destDirFile.permissions);
  101.                 }
  102.             }
  103.             
  104.             entries = zReader.findEntries(null);
  105.             while (entries.hasMore()) {
  106.                 var entryName = entries.getNext();
  107.                 target = getItemFile(entryName);
  108.                 if (target.exists())
  109.                     continue;
  110.                 target.create(Ci.nsILocalFile.DIRECTORY_TYPE, destDirFile.permissions);
  111.                 zReader.extract(entryName, target);
  112.             }
  113.         }
  114.         finally {
  115.             zReader.close();
  116.         }
  117.     },
  118.     
  119.     xmlDocFromFile: function SysUtils_xmlDocFromFile(file) {
  120.         file = file.QueryInterface(this._Ci.nsILocalFile);
  121.         var inStream = this._Cc["@mozilla.org/network/file-input-stream;1"].createInstance(this._Ci.nsIFileInputStream);
  122.         inStream.init(file, 0x01, 0600, 0);
  123.         return this.xmlDocFromStream(inStream, null, null);
  124.     },
  125.     
  126.     xmlDocFromStream: function SysUtils_xmlDocFromStream(docStream, docURI, baseURI) {
  127.         docStream = docStream.QueryInterface(this._Ci.nsIInputStream);
  128.         try {
  129.             var domParser = XB._Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(this._Ci.nsIDOMParser);
  130.             if (docURI || baseURI)
  131.                 domParser.init(null, docURI, baseURI);
  132.             var XMLDoc = domParser.parseFromStream(docStream, null, docStream.available(), "text/xml");
  133.             if (XMLDoc.documentElement.localName == "parsererror")
  134.                 throw new Error(XMLDoc.documentElement.textContent);
  135.             return XMLDoc;
  136.         }
  137.         finally {
  138.             docStream.close();
  139.         }
  140.     },
  141.     
  142.     createFixupURIFromString: function SysUtils_createFixupURIFromString(aString) {
  143.         let uri = null;
  144.         if (aString) {
  145.             let URIFixup = this._Cc["@mozilla.org/docshell/urifixup;1"].getService(this._Ci.nsIURIFixup);
  146.             try {
  147.                 uri = URIFixup.createFixupURI(aString, URIFixup.FIXUP_FLAG_NONE);
  148.             } catch(e) {}
  149.         }
  150.         
  151.         return uri;
  152.     },
  153.     
  154.     insertBreaksInString: function(s) {
  155.         return ("" + s || "").replace(/^\s+/, '')
  156.                              .replace(/\s+$/, '')
  157.                              .replace(/([\/\-&\?\.])/g, "$1\u200B")
  158.                              .replace(/(\S{5})(\S{5})/g, "$1\u200B$2");
  159.     },
  160.     
  161.     normalizeString: function(s) {
  162.         return s ? s.replace(/^[\u0020\n\r\t]*/, "").replace(/[\u0020\n\r\t]+/g, " ").replace(/[\u0020\n\r\t]*$/, "") : "";
  163.     },
  164.     
  165.     trimSpaces: function SysUtils_trimSpaces(s) {
  166.         return s ? s.replace(/^[\u0020\n\r\t]*/, "").replace(/[\u0020\n\r\t]*$/, "") : "";
  167.     },
  168.     
  169.     trimAllSpaces: function SysUtils_trimAllSpaces(s) {
  170.         return s ? s.replace(/^\s\s*/, "").replace(/\s\s*$/, "") : "";
  171.     },
  172.     
  173.     strcmp: function SysUtils_strcmp(str1, str2) {
  174.         if ( !(this.isString(str1) && this.isString(str2)) )
  175.             throw new TypeError("Strings expected");
  176.         return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
  177.     },
  178.     
  179.     strdup: function SysUtils_strdup(str, num) {
  180.         var result = "";
  181.         for (; num > 0; num--)
  182.             result += str;
  183.         return result;
  184.     },
  185.     
  186.     isEmptyObject: function SysUtils_isEmptyObject(obj) {
  187.         return this.objectsAreEqual(obj, this._emptyObj);
  188.     },
  189.     
  190.     objectsAreEqual: function SysUtils_objectsAreEqual(obj1, obj2) {
  191.         if (typeof obj1 != "object" || typeof obj2 != "object")
  192.             throw new TypeError("Objects expected");
  193.         
  194.         var props1 = [], 
  195.             props2 = [];
  196.         
  197.         for (let propName in obj1)
  198.             props1.push(propName);
  199.         
  200.         for (let propName in obj2)
  201.             props2.push(propName);
  202.         
  203.         if (props1.length != props2.length)
  204.             return false;
  205.         
  206.         var prop1, prop2;
  207.         for each (let propName in props1) {
  208.             if ( !(propName in obj2) )
  209.                 return false;
  210.             
  211.             prop1 = obj1[propName];
  212.             prop2 = obj2[propName];
  213.             
  214.             if ( (typeof prop1 == "object") && (typeof prop2 == "object") ) {
  215.                 if ( !this.objectsAreEqual(prop1, prop2) )
  216.                     return false;
  217.             }
  218.             else {
  219.                 if (prop1 !== prop2)
  220.                     return false;
  221.             }
  222.         }
  223.         
  224.         return true;
  225.     },
  226.     
  227.     copyObj: function SysUtils_copyObj(src, deep) {
  228.         if ( !(src instanceof Object) )
  229.             throw new TypeError("Object required");
  230.         var result = new src.constructor(src);
  231.         for (let propName in src) {
  232.             let prop = src[propName];
  233.             result[propName] = (deep && (prop instanceof Object))? this.copyObj(prop): prop;
  234.         }
  235.         return result;
  236.     },
  237.     
  238.     isNumber: function SysUtils_isNumber(x) {
  239.         return (typeof x == "number");
  240.     },
  241.     
  242.     isString: function SysUtils_isString(x) {
  243.         return (typeof x == "string");
  244.     },
  245.     
  246.     isBool: function SysUtils_isBool(x) {
  247.         return (typeof x == "boolean");
  248.     },
  249.     
  250.     RegExp: {
  251.         _escapeRe: /([.*+?^=!:${}()|[\]\/\\])/g,
  252.         
  253.         escape: function(aString) {
  254.             return String(aString).replace(this._escapeRe, '\\$1');
  255.         },
  256.         
  257.         createFromString: function(aString, aFlags) {
  258.             return new RegExp(this.escape(aString), aFlags);
  259.         }
  260.     },
  261.     
  262.     _emptyObj: {},
  263.     _Ci: Components.interfaces,
  264.     _Cc: Components.classes
  265. };
  266.  
  267. var sysutils = new SysUtils();