- Timestamp:
- 05/31/06 05:38:41 (3 years ago)
- Location:
- trunk/sagot/src
- Files:
-
- 3 modified
-
sagot_glib_filechannel.c (modified) (1 diff)
-
sagot_glib_iochannel.c (modified) (9 diffs)
-
sagot_glib_iochannel.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sagot/src/sagot_glib_filechannel.c
r23 r24 26 26 {"read", sagot_iochannel_read, 1,0,0 }, 27 27 {"getc", sagot_iochannel_getc, 0,0,0 }, 28 {"close", sagot_iochannel_close, 0,0,0 }, 28 29 }; 29 30 -
trunk/sagot/src/sagot_glib_iochannel.c
r23 r24 27 27 {"read", sagot_iochannel_read, 1,0,0 }, 28 28 {"getc", sagot_iochannel_getc, 0,0,0 }, 29 {"close", sagot_iochannel_close, 0,0,0 }, 30 {"watch", sagot_iochannel_watch, 1,0,0 }, 29 31 }; 30 32 … … 35 37 JSFunctionSpec iochannel_methods_static[] = { 36 38 }; 39 40 41 JSPropertySpec iochannel_properties_static[] = { 42 {"READ", G_IO_IN, JSPROP_READONLY | JSPROP_PERMANENT, 43 sagot_iochannel_gettype, NULL }, 44 {"WRITE", G_IO_OUT, JSPROP_READONLY | JSPROP_PERMANENT, 45 sagot_iochannel_gettype, NULL }, 46 {"READ_PRIORTY", G_IO_PRI, JSPROP_READONLY | JSPROP_PERMANENT, 47 sagot_iochannel_gettype, NULL }, 48 {"ERROR", G_IO_ERR, JSPROP_READONLY | JSPROP_PERMANENT, 49 sagot_iochannel_gettype, NULL }, 50 {"HANGUP", G_IO_HUP, JSPROP_READONLY | JSPROP_PERMANENT, 51 sagot_iochannel_gettype, NULL }, 52 {"INVALID", G_IO_NVAL, JSPROP_READONLY | JSPROP_PERMANENT, 53 sagot_iochannel_gettype, NULL }, 54 }; 55 37 56 /** 38 57 * Create a glib unix channel. … … 67 86 } 68 87 88 void sagot_iochannel_error (JSContext *cx, char *function, GError **error) { 89 char *pf = "%s unable to read stream: %s"; 90 size_t msglen = strlen(function) + strlen(pf) + strlen(error[0]->message); 91 char *message = g_malloc(msglen); 92 93 snprintf(message, msglen, pf, function, error[0]->message); 94 JS_ReportError(cx, message); 95 g_free(message); 96 } 97 69 98 bool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id, 70 99 jsval *vp) { … … 72 101 * and returning the already stored version. But I don't expect it to be 73 102 * called often. */ 74 returnJS_NewNumberValue(cx,103 *vp = JS_NewNumberValue(cx, 75 104 g_io_channel_unix_get_fd( JS_GetPrivate(cx, object) ), vp); 105 106 return true; 107 } 108 109 bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 110 jsval *vp) { 111 /* This is kind of a hack...but it works... ehh ;-) */ 112 *vp = id; 113 return true; 76 114 } 77 115 … … 81 119 82 120 if (JSVAL_IS_NUMBER(argv[0])) { 83 size_t buflen = (size_t) JSVAL_TO_ DOUBLE(argv[0]);121 size_t buflen = (size_t) JSVAL_TO_INT(argv[0]); 84 122 size_t bytesread; 85 123 char *buffer = g_malloc(buflen); … … 100 138 101 139 JSString *rdata = JS_NewStringCopyN(cx, buffer, bytesread); 102 rval = STRING_TO_JSVAL(rdata);140 *rval = STRING_TO_JSVAL(rdata); 103 141 g_free(buffer); 104 142 … … 112 150 113 151 GError **error = NULL; 114 gchar *line; 115 g_io_channel_read_line(channel, &line, NULL, NULL, error); 152 size_t bytesread; 153 size_t tpos; 154 char *line; 155 g_io_channel_read_line(channel, &line, &bytesread, &tpos, error); 116 156 117 157 if (!error) { 118 rval = STRING_TO_JSVAL( JS_NewString(cx, line, strlen(line)) );158 *rval = STRING_TO_JSVAL( JS_NewStringCopyN(cx, line, tpos) ); 119 159 g_free(line); 120 160 return true; 121 161 } 122 162 else { 123 sagot_iochannel_error( "readline()", error);163 sagot_iochannel_error(cx, "readline()", error); 124 164 return false; 125 165 } … … 135 175 if (!error) { 136 176 jschar jsunichars[] = { in, 0 }; 137 rval = STRING_TO_JSVAL( JS_NewUCStringCopyN(cx, jsunichars, 1) );177 *rval = STRING_TO_JSVAL( JS_NewUCStringCopyN(cx, jsunichars, 1) ); 138 178 return true; 139 179 } 140 180 else { 141 sagot_iochannel_error( "getc()", error);181 sagot_iochannel_error(cx, "getc()", error); 142 182 return false; 183 } 184 } 185 186 bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 187 jsval *argv, jsval *rval) { 188 GIOChannel *channel = JS_GetPrivate(cx, self); 189 GError **error = NULL; 190 g_io_channel_shutdown(channel, true, error); 191 192 if (error) { 193 sagot_iochannel_error(cx, "close()", error); 194 } 195 } 196 197 bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 198 jsval *argv, jsval *rval) { 199 GIOChannel *channel = JS_GetPrivate(cx, self); 200 GError **error = NULL; 201 g_io_channel_shutdown(channel, true, error); 202 203 if (error) { 204 sagot_iochannel_error(cx, "close()", error); 143 205 } 144 206 } … … 146 208 JSObject * sagot_InitIOChannelClass (JSContext *cx, JSObject *object) { 147 209 JSObject *proto = JS_InitClass(cx, object, NULL, &iochannel_class, 148 IOChannel, 2, NULL, iochannel_methods, NULL,149 iochannel_ methods_static);210 IOChannel, 2, iochannel_properties, iochannel_methods, 211 iochannel_properties_static, iochannel_methods_static); 150 212 151 213 return !proto ? NULL : proto; -
trunk/sagot/src/sagot_glib_iochannel.h
r23 r24 26 26 jsval *vp); 27 27 28 extern bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 29 jsval *vp); 30 28 31 extern bool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc, 29 32 jsval *argv, jsval *rval); … … 35 38 jsval *argv, jsval *rval); 36 39 40 extern bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 41 jsval *argv, jsval *rval); 42 43 extern bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 44 jsval *argv, jsval *rval); 45 37 46 JSObject * sagot_InitIOChannelClass (JSContext *cx, JSObject *object);