/gio2/ext/gio2/rbgiodatainputstream.c

https://github.com/masaakiaoyagi/ruby-gnome2 · C · 309 lines · 240 code · 49 blank · 20 comment · 24 complexity · e743a67e34899207ec046bfc247e4a60 MD5 · raw file

  1. /* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
  2. /*
  3. * Copyright (C) 2011 Ruby-GNOME2 Project Team
  4. * Copyright (C) 2008-2009 Ruby-GNOME2 Project Team
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "rbgio2private.h"
  22. #define RG_TARGET_NAMESPACE cDataInputStream
  23. #define _SELF(value) RVAL2GDATAINPUTSTREAM(value)
  24. static VALUE
  25. rg_initialize(int argc, VALUE *argv, VALUE self)
  26. {
  27. VALUE base_stream, byte_order, newline_type;
  28. rb_scan_args(argc, argv, "12", &base_stream, &byte_order, &newline_type);
  29. G_INITIALIZE(self, g_data_input_stream_new(RVAL2GINPUTSTREAM(base_stream)));
  30. if (!NIL_P(byte_order))
  31. g_data_input_stream_set_byte_order(_SELF(self),
  32. RVAL2GDATASTREAMBYTEORDER(byte_order));
  33. if (!NIL_P(newline_type))
  34. g_data_input_stream_set_newline_type(_SELF(self),
  35. RVAL2GDATASTREAMNEWLINETYPE(newline_type));
  36. return Qnil;
  37. }
  38. static VALUE
  39. rg_read_byte(int argc, VALUE *argv, VALUE self)
  40. {
  41. VALUE cancellable;
  42. GError *error = NULL;
  43. guchar value;
  44. rb_scan_args(argc, argv, "01", &cancellable);
  45. value = g_data_input_stream_read_byte(_SELF(self),
  46. RVAL2GCANCELLABLE(cancellable),
  47. &error);
  48. if (error != NULL)
  49. rbgio_raise_error(error);
  50. return GUCHAR2RVAL(value);
  51. }
  52. static VALUE
  53. rg_read_int16(int argc, VALUE *argv, VALUE self)
  54. {
  55. VALUE cancellable;
  56. GError *error = NULL;
  57. gint16 value;
  58. rb_scan_args(argc, argv, "01", &cancellable);
  59. value = g_data_input_stream_read_int16(_SELF(self),
  60. RVAL2GCANCELLABLE(cancellable),
  61. &error);
  62. if (error != NULL)
  63. rbgio_raise_error(error);
  64. return GINT162RVAL(value);
  65. }
  66. static VALUE
  67. rg_read_uint16(int argc, VALUE *argv, VALUE self)
  68. {
  69. VALUE cancellable;
  70. GError *error = NULL;
  71. guint16 value;
  72. rb_scan_args(argc, argv, "01", &cancellable);
  73. value = g_data_input_stream_read_uint16(_SELF(self),
  74. RVAL2GCANCELLABLE(cancellable),
  75. &error);
  76. if (error != NULL)
  77. rbgio_raise_error(error);
  78. return GUINT162RVAL(value);
  79. }
  80. static VALUE
  81. rg_read_int32(int argc, VALUE *argv, VALUE self)
  82. {
  83. VALUE cancellable;
  84. GError *error = NULL;
  85. gint32 value;
  86. rb_scan_args(argc, argv, "01", &cancellable);
  87. value = g_data_input_stream_read_int32(_SELF(self),
  88. RVAL2GCANCELLABLE(cancellable),
  89. &error);
  90. if (error != NULL)
  91. rbgio_raise_error(error);
  92. return GINT322RVAL(value);
  93. }
  94. static VALUE
  95. rg_read_uint32(int argc, VALUE *argv, VALUE self)
  96. {
  97. VALUE cancellable;
  98. GError *error = NULL;
  99. guint32 value;
  100. rb_scan_args(argc, argv, "01", &cancellable);
  101. value = g_data_input_stream_read_uint32(_SELF(self),
  102. RVAL2GCANCELLABLE(cancellable),
  103. &error);
  104. if (error != NULL)
  105. rbgio_raise_error(error);
  106. return GUINT322RVAL(value);
  107. }
  108. static VALUE
  109. rg_read_int64(int argc, VALUE *argv, VALUE self)
  110. {
  111. VALUE cancellable;
  112. GError *error = NULL;
  113. gint64 value;
  114. rb_scan_args(argc, argv, "01", &cancellable);
  115. value = g_data_input_stream_read_int64(_SELF(self),
  116. RVAL2GCANCELLABLE(cancellable),
  117. &error);
  118. if (error != NULL)
  119. rbgio_raise_error(error);
  120. return GINT642RVAL(value);
  121. }
  122. static VALUE
  123. rg_read_uint64(int argc, VALUE *argv, VALUE self)
  124. {
  125. VALUE cancellable;
  126. GError *error = NULL;
  127. guint64 value;
  128. rb_scan_args(argc, argv, "01", &cancellable);
  129. value = g_data_input_stream_read_uint64(_SELF(self),
  130. RVAL2GCANCELLABLE(cancellable),
  131. &error);
  132. if (error != NULL)
  133. rbgio_raise_error(error);
  134. return GUINT642RVAL(value);
  135. }
  136. static VALUE
  137. rg_read_line(int argc, VALUE *argv, VALUE self)
  138. {
  139. VALUE cancellable;
  140. gsize length;
  141. GError *error;
  142. char *line;
  143. rb_scan_args(argc, argv, "01", &cancellable);
  144. line = g_data_input_stream_read_line(_SELF(self),
  145. &length,
  146. RVAL2GCANCELLABLE(cancellable),
  147. &error);
  148. if (error != NULL)
  149. rbgio_raise_error(error);
  150. return CSTR2RVAL_TAINTED_FREE(line, length);
  151. }
  152. static VALUE
  153. rg_read_line_async(int argc, VALUE *argv, VALUE self)
  154. {
  155. VALUE rbio_priority, rbcancellable, block;
  156. int io_priority;
  157. GCancellable *cancellable;
  158. rb_scan_args(argc, argv, "02&", &rbio_priority, &rbcancellable, &block);
  159. io_priority = RVAL2IOPRIORITYDEFAULT(rbio_priority);
  160. cancellable = RVAL2GCANCELLABLE(rbcancellable);
  161. SAVE_BLOCK(block);
  162. g_data_input_stream_read_line_async(_SELF(self),
  163. io_priority,
  164. cancellable,
  165. rbgio_async_ready_callback,
  166. (gpointer)block);
  167. return self;
  168. }
  169. static VALUE
  170. rg_read_line_finish(VALUE self, VALUE result)
  171. {
  172. GError *error = NULL;
  173. gsize length;
  174. char *line;
  175. line = g_data_input_stream_read_line_finish(_SELF(self),
  176. RVAL2GASYNCRESULT(result),
  177. &length,
  178. &error);
  179. if (error != NULL)
  180. rbgio_raise_error(error);
  181. return CSTR2RVAL_TAINTED_FREE(line, length);
  182. }
  183. #if GLIB_CHECK_VERSION(2, 26, 0)
  184. static VALUE
  185. rg_read_upto(int argc, VALUE *argv, VALUE self)
  186. {
  187. VALUE rbstop_chars, cancellable;
  188. const char *stop_chars;
  189. gsize length;
  190. GError *error;
  191. char *string;
  192. rb_scan_args(argc, argv, "11", &rbstop_chars, &cancellable);
  193. stop_chars = RVAL2CSTR(rbstop_chars);
  194. string = g_data_input_stream_read_upto(_SELF(self),
  195. stop_chars,
  196. RSTRING_LEN(rbstop_chars),
  197. &length,
  198. RVAL2GCANCELLABLE(cancellable),
  199. &error);
  200. if (error != NULL)
  201. rbgio_raise_error(error);
  202. return CSTR2RVAL_TAINTED_FREE(string, length);
  203. }
  204. static VALUE
  205. rg_read_upto_async(int argc, VALUE *argv, VALUE self)
  206. {
  207. VALUE rbstop_chars, rbcancellable, rbio_priority, block;
  208. const char *stop_chars;
  209. int io_priority;
  210. GCancellable *cancellable;
  211. rb_scan_args(argc, argv, "12&", &rbstop_chars, &rbio_priority, &rbcancellable, &block);
  212. stop_chars = RVAL2CSTR(rbstop_chars);
  213. io_priority = RVAL2IOPRIORITYDEFAULT(rbio_priority);
  214. cancellable = RVAL2GCANCELLABLE(rbcancellable);
  215. SAVE_BLOCK(block);
  216. g_data_input_stream_read_upto_async(_SELF(self),
  217. stop_chars,
  218. RSTRING_LEN(rbstop_chars),
  219. io_priority,
  220. cancellable,
  221. rbgio_async_ready_callback,
  222. (gpointer)block);
  223. return self;
  224. }
  225. static VALUE
  226. rg_read_upto_finish(VALUE self, VALUE result)
  227. {
  228. GError *error = NULL;
  229. gsize length;
  230. char *string;
  231. string = g_data_input_stream_read_upto_finish(_SELF(self),
  232. RVAL2GASYNCRESULT(result),
  233. &length,
  234. &error);
  235. if (error != NULL)
  236. rbgio_raise_error(error);
  237. return CSTR2RVAL_TAINTED_FREE(string, length);
  238. }
  239. #endif
  240. void
  241. Init_gdatainputstream(VALUE mGio)
  242. {
  243. VALUE RG_TARGET_NAMESPACE = G_DEF_CLASS(G_TYPE_DATA_INPUT_STREAM, "DataInputStream", mGio);
  244. RG_DEF_METHOD(initialize, -1);
  245. RG_DEF_METHOD(read_byte, -1);
  246. RG_DEF_METHOD(read_int16, -1);
  247. RG_DEF_METHOD(read_uint16, -1);
  248. RG_DEF_METHOD(read_int32, -1);
  249. RG_DEF_METHOD(read_uint32, -1);
  250. RG_DEF_METHOD(read_int64, -1);
  251. RG_DEF_METHOD(read_uint64, -1);
  252. RG_DEF_METHOD(read_line, -1);
  253. RG_DEF_METHOD(read_line_async, -1);
  254. RG_DEF_METHOD(read_line_finish, 1);
  255. #if GLIB_CHECK_VERSION(2, 26, 0)
  256. RG_DEF_METHOD(read_upto, -1);
  257. RG_DEF_METHOD(read_upto_async, -1);
  258. RG_DEF_METHOD(read_upto_finish, 1);
  259. #endif
  260. }