1 dump('entering util/file.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.file = function (mw,G,fname) {
6 this.mw = mw; this.G = G;
8 this.dirService = Components.classes["@mozilla.org/file/directory_service;1"].
9 getService( Components.interfaces.nsIProperties );
11 if (fname) this.get(fname);
16 util.file.prototype = {
20 '_input_stream' : null,
21 '_output_stream' : null,
23 'get' : function( fname ) {
25 if (!fname) { fname = this.name; } else { this.name = fname; }
26 if (!fname) throw('Must specify a filename.');
28 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
29 this._file.append(this.mw.myPackageDir);
30 this._file.append("content");
31 this._file.append("conf");
32 this._file.append(fname);
34 this.G.error.sdump('D_FILE',this._file.path);
39 this.G.error.sdump('D_ERROR','util.file.get(): ' + E);
44 'close' : function() {
46 if (!this._file) throw('Must .get() a file first.');
47 if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
48 if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
51 this.G.error.sdump('D_ERROR','util.file.close(): ' + E);
56 'set_object' : function(obj) {
58 if (!this._file) throw('Must .get() a file first.');
59 if (this._input_stream || this._output_stream) throw('Must .close() first.');
60 if (!obj) throw('Must specify an object.');
63 try { obj_json = js2JSON( obj ); } catch(E) { throw('Could not JSONify the object: ' + E); }
65 this._create_output_stream();
66 this._output_stream.write( obj_json, String( obj_json ).length );
70 this.G.error.sdump('D_ERROR','util.file.set_object(): ' + E);
75 'get_object' : function() {
77 if (!this._file) throw('Must .get() a file first.');
78 if (!this._file.exists()) throw('File does not exist.');
79 if (this._input_stream || this._output_stream) throw('Must .close() first.');
81 this._create_input_stream();
82 var data = this._input_stream(-1);
85 var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
90 this.G.error.sdump('D_ERROR','util.file.get_object(): ' + E);
95 '_create_input_stream' : function() {
97 if (!this._file) throw('Must .get() a file first.');
98 if (this._input_stream || this._output_stream) throw('Must .close() first.');
99 if (!this._file.exists()) throw('File does not exist.');
101 var f = Components.classes["@mozilla.org/network/file-input-stream;1"]
102 .createInstance(Components.interfaces.nsIFileInputStream);
103 f.init(file, 0x01, 0, 0);
104 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
105 .createInstance(Components.interfaces.nsIScriptableInputStream);
107 this._input_stream.init(f);
108 return this._input_stream;
110 throw('Could not instantiate input stream.');
114 this.G.error.sdump('D_ERROR','util.file._create_input_stream(): ' + E);
119 '_create_output_stream' : function() {
121 if (!this._file) throw('Must .get() a file first.');
122 if (this._input_stream || this._output_stream) throw('Must .close() first.');
124 if (! this._file.exists()) this._file.create( 0, 0640 );
126 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
127 .createInstance(Components.interfaces.nsIFileOutputStream);
128 this._output_stream.init(this._file, 0x02 | 0x08 | 0x20, 0644, 0);
130 return this._output_stream;
133 this.G.error.sdump('D_ERROR','util.file._create_output_stream(): ' + E);
140 dump('exiting util/file.js\n');