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