1 /* ***** BEGIN LICENSE BLOCK *****
  2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3  *
  4  * The contents of this file are subject to the Mozilla Public License Version
  5  * 1.1 (the "License"); you may not use this file except in compliance with
  6  * the License. You may obtain a copy of the License at
  7  * http://www.mozilla.org/MPL/
  8  *
  9  * Software distributed under the License is distributed on an "AS IS" basis,
 10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11  * for the specific language governing rights and limitations under the
 12  * License.
 13  *
 14  * The Initial Developer of the Original Code is Fireinput Inc.
 15  *
 16  * Portions created by the Initial Developer are Copyright (C) 2007
 17  * the Initial Developer. All Rights Reserved.
 18  *
 19  * Contributor(s):
 20  *     Olly Ja <ollyja@gmail.com>
 21  *
 22  * Alternatively, the contents of this file may be used under the terms of
 23  * either the GNU General Public License Version 2 or later (the "GPL"), or
 24  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 25  * in which case the provisions of the GPL or the LGPL are applicable instead
 26  * of those above. If you wish to allow use of your version of this file only
 27  * under the terms of either the GPL or the LGPL, and not to allow others to
 28  * use your version of this file under the terms of the MPL, indicate your
 29  * decision by deleting the provisions above and replace them with the notice
 30  * and other provisions required by the GPL or the LGPL. If you do not delete
 31  * the provisions above, a recipient may use your version of this file under
 32  * the terms of any one of the MPL, the GPL or the LGPL.
 33  *
 34  * ***** END LICENSE BLOCK ***** 
 35  */
 36 
 37 // Constants
 38 
 39 const IOService = Components.classes["@mozilla.org/network/io-service;1"];
 40 const StreamLoader = Components.classes["@mozilla.org/network/stream-loader;1"];
 41 const ScriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"];
 42 
 43 var FireinputStream = 
 44 {
 45     loadData: function(url)
 46     {
 47        var lines = this.readLine(url);
 48        return lines; 
 49     },
 50 
 51     readLine: function(url)
 52     {
 53        var ioService = IOService.getService(Components.interfaces.nsIIOService);
 54 
 55        var channel;
 56        var stream;
 57        try
 58        {
 59             var uri = ioService.newFileURI(url);
 60             channel = ioService.newChannelFromURI(uri);
 61             stream = channel.open();
 62        }
 63        catch (exc)
 64        {
 65             return null;
 66        }
 67 
 68        try
 69        {
 70           var sis = ScriptableInputStream.getService(Components.interfaces.nsIScriptableInputStream); 
 71           sis.init(stream);
 72 
 73           var segments = [];
 74           for (var count = stream.available(); count; count = stream.available())
 75           {
 76              var segment = sis.read(count);
 77              segments.push(segment);
 78           }
 79           sis.close();
 80           var text = segments.join("");
 81 
 82           // var data = FireinputUnicode.getUnicodeString(text); 
 83           var lines = text.split(/\r\n|\r|\n/);
 84           return lines;
 85         }
 86         catch (exc)
 87         {
 88             stream.close();
 89         }
 90 
 91         return null; 
 92     }, 
 93 
 94     loadDataAsync: function(url, user)
 95     {
 96        var ioService = IOService.getService(Components.interfaces.nsIIOService);
 97 
 98        var uri = ioService.newFileURI(url);
 99        var channel = ioService.newChannelFromURI(uri);
100        function processCallback(line)
101        {
102             var caller = user.caller; 
103             var func =  user.onavailable; 
104             func.call(caller, line); 
105        }
106 
107        function completeCallback()
108        {
109             var caller = user.caller; 
110             var func =  user.oncomplete; 
111             if(func)
112                func.call(caller);
113        }
114        var observer = new StreamObserver(processCallback, completeCallback);
115        channel.asyncOpen(observer, null);
116     }
117 
118 };
119 
120 // ************************************************************************************************
121 
122 function StreamObserver(processCB, completeCB)
123 {
124     this.processCB = processCB; 
125     this.completeCB = completeCB; 
126     this.data = [];
127 }   
128 
129 StreamObserver.prototype =
130 {
131     onStartRequest: function(request, context)
132     {
133     },
134 
135     onStopRequest: function(request, context, status)
136     {
137        this.completeCB(); 
138     },
139 
140     processData: function(str)
141     {
142        var lines = str.split(/\r\n|\r|\n/);
143        this.data = []; 
144        if(str.lastIndexOf("\n") != 0)
145        {
146           this.data.push(lines.pop()); 
147        }
148    
149        for(var i=lines.length-1; i>=0; i--)
150        {
151        //      var data = FireinputUnicode.getUnicodeString(lines[i]);
152              this.processCB(lines[i]);
153        }
154     },
155 
156     onDataAvailable: function(request, context, inStr, sourceOffset, count)
157     {
158        var sis = ScriptableInputStream.createInstance(Components.interfaces.nsIScriptableInputStream);
159        sis.init(inStr);
160        this.processData(this.data.join("") + sis.read(count));
161     }
162 };
163  


syntax highlighted by Code2HTML, v. 0.9.1