static VALUE fsdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone) { datum key, value; struct dbmdata *dbmp; DBM *dbm; ExportStringValue(keystr); key.dptr = RSTRING_PTR(keystr); key.dsize = RSTRING_LENINT(keystr); GetDBM2(obj, dbmp, dbm); value = sdbm_fetch(dbm, key); if (value.dptr == 0) { if (ifnone == Qnil && rb_block_given_p()) return rb_yield(rb_external_str_new(key.dptr, key.dsize)); return ifnone; } return rb_external_str_new(value.dptr, value.dsize); }
/* call-seq: load_image(io, required_components = COMPONENTS_DEFAULT) => [data, width, height, components] load_image(io, required_components = COMPONENTS_DEFAULT) { |info| ... } => obj Loads an image using stb_image and returns the resulting data and its width, height, and the number of components per pixel in the data. The returned data is a packed string of unsigned 8-bit integers (8 bits per component). The length of the string will always be width * height * components. In the second form, the info array is yielded to the block if the image is successfully loaded. Otherwise, the method returns nil. This is possibly more convenient than doing an <tt>if info ... end</tt> block to check if the image was successfully loaded. === IO Objects IO objects accepted for loading data with must implement at least IO#read(length) and either IO#eof or IO#eof?. In addition, they may also implement a skip(length) method that skips length bytes and does not return any value. If the IO object does not respond to #skip, #read will be called instead and its result will be discarded. If you want to avoid unnecessary allocations, it may be wise to implement a skip method. === Components If required_components is provided and not the default value, the image data returned will have as many components as are requested. In turn, the number of components returned via the array will be the same as required_components. Valid options for required_components are: [::COMPONENTS_DEFAULT] The default value, loads as many components as are provided by the image. [::COMPONENTS_GREY] Loads only one component. [::COMPONENTS_GREY_ALPHA] Loads two components. [::COMPONENTS_RGB] Loads three components (red, green, and blue). [::COMPONENTS_RGB_ALPHA] Loads four components (red, green, blue, and alpha). === Example open('image.png') { |io| STBI.load_image(io) { |data, width, height, components| format = case components when STBI::COMPONENTS_GREY then Gl::GL_RED when STBI::COMPONENTS_GREY_ALPHA then Gl::GL_RG when STBI_COMPONENTS_RGB then Gl::RGB when STBI_COMPONENTS_RGB_ALPHA then Gl::RGBA end Gl::glTexImage2D(Gl::GL_TEXTURE_2D, 0, format, width, height, 0, format, Gl::GL_UNSIGNED_BYTE, data) } } */ static VALUE sr_load_image(int argc, VALUE *argv, VALUE sr_self) { VALUE sr_callbacks; VALUE sr_req_comp; VALUE sr_image_data = Qnil; int x = 0; int y = 0; int components[2] = { STBI_default, 0 }; rb_scan_args(argc, argv, "11", &sr_callbacks, &sr_req_comp); if (NIL_P(sr_callbacks)) { rb_raise(rb_eArgError, "IO object cannot be nil"); return Qnil; } if (RTEST(sr_req_comp)) { components[0] = FIX2INT(sr_req_comp); } if (!rb_obj_respond_to(sr_callbacks, s_read_funcid, 0) || !(rb_obj_respond_to(sr_callbacks, s_eof_funcid, 0) || rb_obj_respond_to(sr_callbacks, s_eof_qm_funcid, 0))) { rb_raise(rb_eTypeError, "IO object does not respond to either read or eof/eof?"); } stbi_uc *data = stbi_load_from_callbacks(&s_st_callbacks, &sr_callbacks, &x, &y, &components[1], components[0]); if (data) { const long length = x * y * components[!components[0]]; sr_image_data = rb_ary_new3(4, rb_external_str_new((const char *)data, length), INT2FIX(x), INT2FIX(y), INT2FIX(components[!components[0]])); stbi_image_free(data); } if (!NIL_P(sr_image_data) && rb_block_given_p()) { return rb_yield_splat(sr_image_data); } else { return sr_image_data; } }
/* * call-seq: * sdbm.key(value) -> key * * Returns the +key+ associated with the given +value+. If more than one * +key+ corresponds to the given +value+, then the first key to be found * will be returned. If no keys are found, +nil+ will be returned. */ static VALUE fsdbm_key(VALUE obj, VALUE valstr) { datum key, val; struct dbmdata *dbmp; DBM *dbm; ExportStringValue(valstr); val.dptr = RSTRING_PTR(valstr); val.dsize = RSTRING_LENINT(valstr); GetDBM2(obj, dbmp, dbm); for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { val = sdbm_fetch(dbm, key); if (val.dsize == RSTRING_LEN(valstr) && memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) return rb_external_str_new(key.dptr, key.dsize); } return Qnil; }
/* call-seq: load_float_image(io, required_components = COMPONENTS_DEFAULT) => [data, width, height, components] load_float_image(io, required_components = COMPONENTS_DEFAULT) { |info| ... } => obj Similar to ::load_image, except the returned image data is a packaed string for an array of 32-bit floats (e.g., String#unpack('f*') will extract an array of floating point values representing the components of the image's pixels). In the second form, the info array is yielded to the block if the image is successfully loaded. Otherwise, the method returns nil. This is possibly more convenient than doing an <tt>if info ... end</tt> block to check if the image was successfully loaded. For further information on the IO object, the required_components argument, and so on, see the documentation for load_image. === Example open('image.png') { |io| STBI.load_float_image(io) { |data, width, height, components| format = case components when STBI::COMPONENTS_GREY then Gl::GL_RED when STBI::COMPONENTS_GREY_ALPHA then Gl::GL_RG when STBI_COMPONENTS_RGB then Gl::RGB when STBI_COMPONENTS_RGB_ALPHA then Gl::RGBA end Gl::glTexImage2D(Gl::GL_TEXTURE_2D, 0, format, width, height, 0, format, Gl::GL_FLOAT, data) } } */ static VALUE sr_load_float_image(int argc, VALUE *argv, VALUE sr_self) { VALUE sr_callbacks; VALUE sr_req_comp; VALUE sr_image_data = Qnil; int x = 0; int y = 0; int components[2] = { STBI_default, 0 }; rb_scan_args(argc, argv, "11", &sr_callbacks, &sr_req_comp); if (NIL_P(sr_callbacks)) { rb_raise(rb_eArgError, "IO object cannot be nil"); return Qnil; } if (RTEST(sr_req_comp)) { components[0] = FIX2INT(sr_req_comp); } float *data = stbi_loadf_from_callbacks(&s_st_callbacks, &sr_callbacks, &x, &y, &components[1], components[0]); if (data) { const long length = x * y * components[!components[0]] * sizeof(float); sr_image_data = rb_ary_new3(4, rb_external_str_new((const char *)data, length), INT2FIX(x), INT2FIX(y), INT2FIX(components[!components[0]])); stbi_image_free(data); } if (!NIL_P(sr_image_data) && rb_block_given_p()) { return rb_yield_splat(sr_image_data); } else { return sr_image_data; } }
/* * call-seq: * blob.text(max_lines = -1, encoding = Encoding.default_external) -> string * * Return up to +max_lines+ of text from a blob as a +String+. * If +max_lines+ is less than 0, the full string is returned. * * The string is created with the given +encoding+, defaulting to * Encoding.default_external. * * When limiting the size of the text with +max_lines+, the string is * expected to have an ASCII-compatible encoding, and is checked * for the newline +\n+ character. */ static VALUE rb_git_blob_text_GET(int argc, VALUE *argv, VALUE self) { git_blob *blob; size_t size; const char *content; VALUE rb_max_lines, rb_encoding; Data_Get_Struct(self, git_blob, blob); rb_scan_args(argc, argv, "02", &rb_max_lines, &rb_encoding); content = git_blob_rawcontent(blob); size = git_blob_rawsize(blob); if (!NIL_P(rb_max_lines)) { size_t i = 0; int lines = 0, maxlines; Check_Type(rb_max_lines, T_FIXNUM); maxlines = FIX2INT(rb_max_lines); if (maxlines >= 0) { while (i < size && lines < maxlines) { if (content[i++] == '\n') lines++; } size = (size_t)i; } } if (!NIL_P(rb_encoding)) { return rb_enc_str_new(content, size, rb_to_encoding(rb_encoding)); } return rb_external_str_new(content, size); }
VALUE string_spec_rb_external_str_new(VALUE self, VALUE str) { return rb_external_str_new(RSTRING_PTR(str), RSTRING_LEN(str)); }