378f44db191e2195e3e4657bfa09c0d1cee6cfee
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / util / file.js
1 sdump('D_TRACE','Loading file.js\n');
2
3 var dirService = Components.classes["@mozilla.org/file/directory_service;1"].
4         getService( Components.interfaces.nsIProperties );
5
6 //////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 function set_object_in_file(file,obj) {
9         try {
10                 if (typeof(file)=='string') file = get_file( file );
11                 var f = create_output_stream(file);
12                 var obj_json = js2JSON( obj );
13                 f.write( obj_json, obj_json.length ); f.close();
14         } catch(E) {
15                 sdump('D_FILE', 'set_object_in_file: ' + js2JSON(E) + '\n');
16                 throw(E);
17         }
18 }
19
20 function get_object_in_file(file) {
21         try {
22                 if (typeof(file)=='string') file = get_file( file );
23                 if (file.exists()) {
24                         var f = create_input_stream(file);
25                         var obj = JSON2js( f.read(-1) );
26                         f.close();
27                         return obj;
28                 } else {
29                         throw('File does not exist. ');
30                 }
31         } catch(E) {
32                 sdump('D_FILE', 'get_object_in_file: ' + js2JSON(E) + '\n');
33                 throw(E);
34         }
35 }
36
37 //////////////////////////////////////////////////////////////////////////////////////////////////////
38
39 function create_input_stream(file) {
40         try {
41                 if (typeof(file)=='string') file = get_file( file );
42                 var f = Components.classes["@mozilla.org/network/file-input-stream;1"]
43                         .createInstance(Components.interfaces.nsIFileInputStream);
44                 if (file && file.exists()) {
45                         f.init(file, 0x01, 0, 0);
46                         var s = Components.classes["@mozilla.org/scriptableinputstream;1"]
47                                 .createInstance(Components.interfaces.nsIScriptableInputStream);
48                         if (f) {
49                                 s.init(f);
50                                 return s;
51                         }
52                 }
53         } catch(E) {
54                 sdump('D_ERROR', E);
55         }
56         return null;
57 }
58
59 function create_output_stream(file) {
60         try {
61                 if (typeof(file)=='string') file = get_file( file );
62                 var f = Components.classes["@mozilla.org/network/file-output-stream;1"]
63                         .createInstance(Components.interfaces.nsIFileOutputStream);
64                 if (file) {
65                         if (! file.exists()) file.create( 0, 0640 );
66                         f.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
67                         return f;
68                 }
69         } catch(E) {
70                 sdump('D_ERROR', E);
71         }
72         return null;
73 }
74
75 function get_file( fname ) {
76         try {
77                 file = dirService.get( "AChrom",  Components.interfaces.nsIFile );
78                 file.append(mw.myPackageDir); file.append("content"); file.append("conf"); file.append(fname);
79                 sdump('D_FILE','get_file( ' + fname + ').path = ' + file.path + '\n');
80                 return file;
81         } catch(E) {
82                 sdump('D_ERROR', E);
83                 return null;
84         }
85 }