1 dump('entering util/file.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.file = function (fname) {
6 JSAN.use('util.error'); this.error = new util.error();
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 = {
18 'myPackageDir' : 'evergreen',
22 '_input_stream' : null,
23 '_output_stream' : null,
25 'get' : function( fname ) {
27 if (!fname) { fname = this.name; } else { this.name = fname; }
28 if (!fname) throw('Must specify a filename.');
30 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
31 this._file.append(myPackageDir);
32 this._file.append("content");
33 this._file.append("conf");
34 this._file.append(fname);
36 this.error.sdump('D_FILE',this._file.path);
41 this.error.sdump('D_ERROR','util.file.get(): ' + E);
46 'close' : function() {
48 if (!this._file) throw('Must .get() a file first.');
49 if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
50 if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
53 this.error.sdump('D_ERROR','util.file.close(): ' + E);
58 'set_object' : function(obj) {
60 if (!this._file) throw('Must .get() a file first.');
61 if (this._input_stream || this._output_stream) throw('Must .close() first.');
62 if (!obj) throw('Must specify an object.');
65 try { obj_json = js2JSON( obj ); } catch(E) { throw('Could not JSONify the object: ' + E); }
67 this._create_output_stream();
68 this._output_stream.write( obj_json, String( obj_json ).length );
72 this.error.sdump('D_ERROR','util.file.set_object(): ' + E);
77 'get_object' : function() {
79 if (!this._file) throw('Must .get() a file first.');
80 if (!this._file.exists()) throw('File does not exist.');
81 if (this._input_stream || this._output_stream) throw('Must .close() first.');
83 this._create_input_stream();
84 var data = this._input_stream.read(-1);
87 var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
92 this.error.sdump('D_ERROR','util.file.get_object(): ' + E);
97 '_create_input_stream' : function() {
99 if (!this._file) throw('Must .get() a file first.');
100 if (this._input_stream || this._output_stream) throw('Must .close() first.');
101 if (!this._file.exists()) throw('File does not exist.');
103 var f = Components.classes["@mozilla.org/network/file-input-stream;1"]
104 .createInstance(Components.interfaces.nsIFileInputStream);
105 f.init(this._file, 0x01, 0, 0);
106 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
107 .createInstance(Components.interfaces.nsIScriptableInputStream);
109 this._input_stream.init(f);
110 return this._input_stream;
112 throw('Could not instantiate input stream.');
116 this.error.sdump('D_ERROR','util.file._create_input_stream(): ' + E);
121 '_create_output_stream' : function() {
123 if (!this._file) throw('Must .get() a file first.');
124 if (this._input_stream || this._output_stream) throw('Must .close() first.');
126 if (! this._file.exists()) this._file.create( 0, 0640 );
128 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
129 .createInstance(Components.interfaces.nsIFileOutputStream);
130 this._output_stream.init(this._file, 0x02 | 0x08 | 0x20, 0644, 0);
132 return this._output_stream;
135 this.error.sdump('D_ERROR','util.file._create_output_stream(): ' + E);
142 dump('exiting util/file.js\n');