comparison dwt/browser/InputStream.d @ 298:eec6ddb07873

More xpcom/mozilla port
author John Reimer<terminal.node@gmail.com>
date Sun, 10 Aug 2008 22:25:43 -0700
parents 44258e0b6687
children 942da4b6558a
comparison
equal deleted inserted replaced
297:2f204a4aebc6 298:eec6ddb07873
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.browser.InputStream; 13 module dwt.browser.InputStream;
12 14
15 import Math = tango.math.Math;
13 import dwt.dwthelper.utils; 16 import dwt.dwthelper.utils;
14 17
15 import dwt.internal.C; 18 //import dwt.internal.C;
16 import dwt.internal.mozilla.XPCOM; 19 import dwt.internal.mozilla.XPCOM;
17 import dwt.internal.mozilla.XPCOMObject; 20 //import dwt.internal.mozilla.XPCOMObject;
18 import dwt.internal.mozilla.nsID; 21 import dwt.internal.mozilla.nsID;
19 import dwt.internal.mozilla.nsIInputStream; 22 import dwt.internal.mozilla.nsIInputStream;
20 import dwt.internal.mozilla.nsISupports; 23 import dwt.internal.mozilla.nsISupports;
21 24
22 class InputStream { 25 class InputStream : nsIInputStream {
23 XPCOMObject inputStream; 26 //XPCOMObject inputStream;
24 int refCount = 0; 27 int refCount = 0;
25 28
26 byte[] buffer; 29 byte[] buffer;
27 int index = 0; 30 int index = 0;
28 31
29 InputStream (byte[] buffer) { 32 this (byte[] buffer) {
30 this.buffer = buffer; 33 this.buffer = buffer;
31 index = 0; 34 index = 0;
32 createCOMInterfaces (); 35 //createCOMInterfaces ();
33 } 36 }
34 37
35 int AddRef () { 38 nsrefcnt AddRef () {
36 refCount++; 39 refCount++;
37 return refCount; 40 return refCount;
38 } 41 }
39 42 /+
40 void createCOMInterfaces () { 43 void createCOMInterfaces () {
41 /* Create each of the interfaces that this object implements */ 44 /* Create each of the interfaces that this object implements */
42 inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) { 45 inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) {
43 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} 46 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
44 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} 47 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
59 } 62 }
60 63
61 int /*long*/ getAddress () { 64 int /*long*/ getAddress () {
62 return inputStream.getAddress (); 65 return inputStream.getAddress ();
63 } 66 }
64 67 +/
65 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) { 68 nsresult QueryInterface (nsID* riid, void** ppvObject) {
66 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE; 69 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
67 nsID guid = new nsID (); 70 //nsID guid = new nsID ();
68 XPCOM.memmove (guid, riid, nsID.sizeof); 71 //XPCOM.memmove (guid, riid, nsID.sizeof);
69 72
70 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { 73 if (*riid == nsISupports.IID) {
71 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF); 74 *ppvObject = cast(void*)cast(nsISupports)this;
72 AddRef (); 75 AddRef ();
73 return XPCOM.NS_OK; 76 return XPCOM.NS_OK;
74 } 77 }
75 if (guid.Equals (nsIInputStream.NS_IINPUTSTREAM_IID)) { 78 if (*riid == nsIInputStream.IID) {
76 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF); 79 *ppvObject = cast(void*)cast(nsIInputStream)this;
77 AddRef (); 80 AddRef ();
78 return XPCOM.NS_OK; 81 return XPCOM.NS_OK;
79 } 82 }
80 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); 83 *ppvObject = null;
81 return XPCOM.NS_ERROR_NO_INTERFACE; 84 return XPCOM.NS_ERROR_NO_INTERFACE;
82 } 85 }
83 86
84 int Release () { 87 nsrefcnt Release () {
85 refCount--; 88 refCount--;
86 if (refCount is 0) disposeCOMInterfaces (); 89 //if (refCount is 0) disposeCOMInterfaces ();
87 return refCount; 90 return refCount;
88 } 91 }
89 92
90 /* nsIInputStream implementation */ 93 /* nsIInputStream implementation */
91 94
92 int Close () { 95 nsresult Close () {
93 buffer = null; 96 buffer = null;
94 index = 0; 97 index = 0;
95 return XPCOM.NS_OK; 98 return XPCOM.NS_OK;
96 } 99 }
97 100
98 int Available (int /*long*/ _retval) { 101 nsresult Available (PRUint32* _retval) {
99 int available = buffer is null ? 0 : buffer.length - index; 102 PRUint32 available = buffer is null ? 0 : buffer.length - index;
100 XPCOM.memmove (_retval, new int[] {available}, 4); 103 *_retval = available;
104 //XPCOM.memmove (_retval, new int[] {available}, 4);
101 return XPCOM.NS_OK; 105 return XPCOM.NS_OK;
102 } 106 }
103 107
104 int Read(int /*long*/ aBuf, int aCount, int /*long*/ _retval) { 108 nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* _retval) {
105 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index); 109 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
110 if (aBuf is null)
111 assert(0);
106 if (max > 0) { 112 if (max > 0) {
107 byte[] src = new byte[max]; 113 //byte[] src = new byte[max];
108 System.arraycopy (buffer, index, src, 0, max); 114 //System.arraycopy (buffer, index, src, 0, max);
109 XPCOM.memmove (aBuf, src, max); 115 //XPCOM.memmove (aBuf, src, max);
116 aBuf[0..max] = buffer[index..$];
110 index += max; 117 index += max;
111 } 118 }
112 XPCOM.memmove(_retval, new int[] {max}, 4); 119 *_retval = max;
113 return XPCOM.NS_OK; 120 return XPCOM.NS_OK;
114 } 121 }
115 122
116 int ReadSegments (int /*long*/ aWriter, int /*long*/ aClosure, int aCount, int /*long*/ _retval) { 123 nsresult ReadSegments (nsWriteSegFun aWriter, void* aClosure, PRUint32 aCount, PRUint32* _retval) {
117 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index); 124 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
118 int cnt = max; 125 PRUint32 cnt = max;
119 while (cnt > 0) { 126 while (cnt > 0) {
120 int[] aWriteCount = new int[1]; 127 PRUint32 aWriteCount;
121 int /*long*/ rc = XPCOM.Call (aWriter, getAddress (), aClosure, buffer, index, cnt, aWriteCount); 128 nsresult rc = aWrite (cast(nsIInputStream)this, aClosure, buffer.ptr, index, cnt, &aWriteCount);
122 if (rc !is XPCOM.NS_OK) break; 129 if (rc !is XPCOM.NS_OK) break;
123 index += aWriteCount[0]; 130 index += aWriteCount;
124 cnt -= aWriteCount[0]; 131 cnt -= aWriteCount;
125 } 132 }
126 XPCOM.memmove (_retval, new int[] {max - cnt}, 4); 133 //XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
134 *_retval = (max - cnt);
127 return XPCOM.NS_OK; 135 return XPCOM.NS_OK;
128 } 136 }
129 137
130 int IsNonBlocking (int /*long*/ _retval) { 138 nsresult IsNonBlocking (PRUint32* _retval) {
131 /* blocking */ 139 /* blocking */
132 XPCOM.memmove (_retval, new int[] {0}, 4); 140 *_retval = 0;
133 return XPCOM.NS_OK; 141 return XPCOM.NS_OK;
134 } 142 }
135 } 143 }