1
- function file_put_contents ( aFile , data , flags , context ) {
2
- // http://kevin.vanzonneveld.net
3
- // + original by: Brett Zamir (http://brett-zamir.me)
4
- // % note 1: Only works at present in Mozilla (and unfinished too); might also allow context to determine
5
- // % note 1: whether for Mozilla, for HTTP PUT or POST requests, etc.
6
- // * example 1: file_put_contents('file://Users/Kevin/someFile.txt', 'hello');
7
- // * returns 1: 5
8
-
9
-
10
- // Fix: allow file to be placed outside of profile directory; fix: add PHP-style flags, etc.
11
- var opts = 0 , i = 0 ;
12
-
13
- // Initialize binary arguments. Both the string & integer (constant) input is
14
- // allowed
15
- var OPTS = {
16
- FILE_USE_INCLUDE_PATH : 1 ,
17
- LOCK_EX : 2 ,
18
- FILE_APPEND : 8 ,
19
- FILE_TEXT : 32 ,
20
- FILE_BINARY : 64
21
- } ;
22
- if ( typeof flags === 'number' ) {
23
- opts = flags ;
24
- }
25
- else { // Allow for a single string or an array of string flags
26
- flags = [ ] . concat ( flags ) ;
27
- for ( i = 0 ; i < flags . length ; i ++ ) {
28
- if ( OPTS [ flags [ i ] ] ) {
29
- opts = opts | OPTS [ flags [ i ] ] ;
30
- } else {
31
-
32
- }
33
- }
34
- }
35
- var append = opts & OPTS . FILE_APPEND ;
36
-
37
-
38
- var charset = 'UTF-8' ; // Can be any character encoding name that Mozilla supports
39
- // Setting earlier, but even with a different setting, it still seems to save as UTF-8
40
-
41
- // var em = Cc['@mozilla.org/extensions/manager;1'].
42
- // getService(Ci.nsIExtensionManager);
43
- // the path may use forward slash ('/') as the delimiter
44
- // var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, 'content/'+filename);
45
-
46
- var file ;
47
- if ( ( / ^ f i l e : \/ \/ / ) . test ( aFile ) ) {
48
- // absolute file URL
49
- var ios = Components . classes [ "@mozilla.org/network/io-service;1" ] .
50
- getService ( Components . interfaces . nsIIOService ) ;
51
- var URL = ios . newURI ( aFile , null , null ) ;
52
- file = URL . QueryInterface ( Components . interfaces . nsIFileURL ) . file ; // becomes nsIFile
53
- }
54
- else {
55
- //native
56
-
57
- file = Components . classes [ "@mozilla.org/file/local;1" ] .
58
- createInstance ( Components . interfaces . nsILocalFile ) ;
59
- file . initWithPath ( aFile ) ; // e.g., "/home"
60
- }
61
- /**
62
- //tmp
63
- var file = Components.classes["@mozilla.org/file/directory_service;1"].
64
- getService(Components.interfaces.nsIProperties).
65
- get("TmpD", Components.interfaces.nsIFile);
66
- file.append("suggestedName.tmp");
67
- file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
68
- //
69
- var file = Components.classes["@mozilla.org/file/directory_service;1"].
70
- getService(Components.interfaces.nsIProperties).
71
- get("ProfD", Components.interfaces.nsIFile);
72
- //*/
73
-
74
-
75
- if ( typeof file === 'string' ) {
76
- var tempfilename = file ;
77
- file = Cc [ '@mozilla.org/file/directory_service;1' ] . getService ( Ci . nsIProperties ) . get ( 'ProfD' , Ci . nsILocalFile ) ;
78
- file . append ( tempfilename ) ;
79
- }
80
-
81
- if ( ! file . exists ( ) ) { // if it doesn't exist, create // || !file.isDirectory()
82
- file . create ( Ci . nsIFile . NORMAL_FILE_TYPE , 0777 ) ; // DIRECTORY_TYPE
83
- }
84
- else if ( ! append ) {
85
- file . remove ( false ) ; // I needed to do this to avoid some apparent race condition (the effect of junk getting added to the end)
86
- file . create ( Ci . nsIFile . NORMAL_FILE_TYPE , 0777 ) ; // DIRECTORY_TYPE
87
- }
88
- // file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0664 ); // for temporary
89
-
90
- var foStream = Cc [ '@mozilla.org/network/file-output-stream;1' ] . createInstance ( Ci . nsIFileOutputStream ) ;
91
- var fileFlags = append ? ( 0x02 | 0x10 ) : 0x02 ; // use 0x02 | 0x10 to open file for appending.
92
- foStream . init ( file , fileFlags , 0664 , 0 ) ;
93
- // foStream.init(file, 0x02 | 0x08 | 0x20, 0664 , 0); // write, create, truncate
94
-
95
- var os = Cc [ '@mozilla.org/intl/converter-output-stream;1' ] . createInstance ( Ci . nsIConverterOutputStream ) ;
96
- // This assumes that foStream is the nsIOutputStream you want to write to
97
- // 0x0000 instead of '?' will produce an exception: http://www.xulplanet.com/references/xpcomref/ifaces/nsIConverterOutputStream.html
98
- // If charset in xsl:output is ISO-8859-1, the file won't open--if it is GB2312, it will output as UTF-8--seems buggy or?
99
- os . init ( foStream , charset , 0 , 0x0000 ) ;
100
- os . writeString ( data ) ;
101
- os . close ( ) ;
102
- foStream . close ( ) ;
103
- // todo: return number of bytes written or false on failure
1
+ function file_put_contents ( aFile , data , flags , context ) {
2
+ // http://kevin.vanzonneveld.net
3
+ // + original by: Brett Zamir (http://brett-zamir.me)
4
+ // % note 1: Only works at present in Mozilla (and unfinished too); might also allow context to determine
5
+ // % note 1: whether for Mozilla, for HTTP PUT or POST requests, etc.
6
+ // * example 1: file_put_contents('file://Users/Kevin/someFile.txt', 'hello');
7
+ // * returns 1: 5
8
+
9
+
10
+ // Fix: allow file to be placed outside of profile directory; fix: add PHP-style flags, etc.
11
+ var opts = 0 , i = 0 ;
12
+
13
+ // Initialize binary arguments. Both the string & integer (constant) input is
14
+ // allowed
15
+ var OPTS = {
16
+ FILE_USE_INCLUDE_PATH : 1 ,
17
+ LOCK_EX : 2 ,
18
+ FILE_APPEND : 8 ,
19
+ FILE_TEXT : 32 ,
20
+ FILE_BINARY : 64
21
+ } ;
22
+ if ( typeof flags === 'number' ) {
23
+ opts = flags ;
24
+ }
25
+ else { // Allow for a single string or an array of string flags
26
+ flags = [ ] . concat ( flags ) ;
27
+ for ( i = 0 ; i < flags . length ; i ++ ) {
28
+ if ( OPTS [ flags [ i ] ] ) {
29
+ opts = opts | OPTS [ flags [ i ] ] ;
30
+ } else {
31
+
32
+ }
33
+ }
34
+ }
35
+ var append = opts & OPTS . FILE_APPEND ;
36
+
37
+
38
+ var charset = 'UTF-8' ; // Can be any character encoding name that Mozilla supports
39
+ // Setting earlier, but even with a different setting, it still seems to save as UTF-8
40
+
41
+ // var em = Cc['@mozilla.org/extensions/manager;1'].
42
+ // getService(Ci.nsIExtensionManager);
43
+ // the path may use forward slash ('/') as the delimiter
44
+ // var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, 'content/'+filename);
45
+
46
+ var file ;
47
+ if ( ( / ^ f i l e : \/ \/ / ) . test ( aFile ) ) {
48
+ // absolute file URL
49
+ var ios = Components . classes [ "@mozilla.org/network/io-service;1" ] .
50
+ getService ( Components . interfaces . nsIIOService ) ;
51
+ var URL = ios . newURI ( aFile , null , null ) ;
52
+ file = URL . QueryInterface ( Components . interfaces . nsIFileURL ) . file ; // becomes nsIFile
53
+ }
54
+ else {
55
+ //native
56
+
57
+ file = Components . classes [ "@mozilla.org/file/local;1" ] .
58
+ createInstance ( Components . interfaces . nsILocalFile ) ;
59
+ file . initWithPath ( aFile ) ; // e.g., "/home"
60
+ }
61
+ /**
62
+ //tmp
63
+ var file = Components.classes["@mozilla.org/file/directory_service;1"].
64
+ getService(Components.interfaces.nsIProperties).
65
+ get("TmpD", Components.interfaces.nsIFile);
66
+ file.append("suggestedName.tmp");
67
+ file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 438); // 0666
68
+ //
69
+ var file = Components.classes["@mozilla.org/file/directory_service;1"].
70
+ getService(Components.interfaces.nsIProperties).
71
+ get("ProfD", Components.interfaces.nsIFile);
72
+ //*/
73
+
74
+
75
+ if ( typeof file === 'string' ) {
76
+ var tempfilename = file ;
77
+ file = Cc [ '@mozilla.org/file/directory_service;1' ] . getService ( Ci . nsIProperties ) . get ( 'ProfD' , Ci . nsILocalFile ) ;
78
+ file . append ( tempfilename ) ;
79
+ }
80
+
81
+ if ( ! file . exists ( ) ) { // if it doesn't exist, create // || !file.isDirectory()
82
+ file . create ( Ci . nsIFile . NORMAL_FILE_TYPE , 511 ) ; // DIRECTORY_TYPE (0777)
83
+ }
84
+ else if ( ! append ) {
85
+ file . remove ( false ) ; // I needed to do this to avoid some apparent race condition (the effect of junk getting added to the end)
86
+ file . create ( Ci . nsIFile . NORMAL_FILE_TYPE , 511 ) ; // DIRECTORY_TYPE (0777)
87
+ }
88
+ // file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 436 ); // for temporary (0664)
89
+
90
+ var foStream = Cc [ '@mozilla.org/network/file-output-stream;1' ] . createInstance ( Ci . nsIFileOutputStream ) ;
91
+ var fileFlags = append ? ( 0x02 | 0x10 ) : 0x02 ; // use 0x02 | 0x10 to open file for appending.
92
+ foStream . init ( file , fileFlags , 436 , 0 ) ; // 436 is 0664
93
+ // foStream.init(file, 0x02 | 0x08 | 0x20, 436 , 0); // write, create, truncate (436 is 0664)
94
+
95
+ var os = Cc [ '@mozilla.org/intl/converter-output-stream;1' ] . createInstance ( Ci . nsIConverterOutputStream ) ;
96
+ // This assumes that foStream is the nsIOutputStream you want to write to
97
+ // 0x0000 instead of '?' will produce an exception: http://www.xulplanet.com/references/xpcomref/ifaces/nsIConverterOutputStream.html
98
+ // If charset in xsl:output is ISO-8859-1, the file won't open--if it is GB2312, it will output as UTF-8--seems buggy or?
99
+ os . init ( foStream , charset , 0 , 0x0000 ) ;
100
+ os . writeString ( data ) ;
101
+ os . close ( ) ;
102
+ foStream . close ( ) ;
103
+ // todo: return number of bytes written or false on failure
104
104
}
0 commit comments