예제 #1
0
파일: native.c 프로젝트: trmmy/bson-ruby
/**
 * Converts the milliseconds time to the raw BSON bytes. We need to
 * explicitly convert using 64 bit here.
 *
 * @example Convert the milliseconds value to BSON bytes.
 *    rb_time_to_bson(time, 2124132340000, encoded);
 *
 * @param [ Time ] self The Ruby Time object.
 * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
{
  double t = NUM2DBL(rb_funcall(self, rb_intern("to_f"), 0));
  int64_t milliseconds = (int64_t)(t * 1000);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  return int64_t_to_bson(milliseconds, encoded);
}
예제 #2
0
파일: native.c 프로젝트: trmmy/bson-ruby
/**
 * Append the ruby float as 8-byte double value to buffer.
 *
 * @example Convert float to double and append.
 *    rb_float_to_bson(..., 1.2311);
 *
 * @param [ String] encoded Optional string buffer, default provided by rb_str_encoded_binary
 * @param [ Float ] self The ruby float value.
 *
 * @return [ String ] The encoded bytes with double value appended.
 *
 * @since 2.0.0
 */
static VALUE rb_float_to_bson(int argc, VALUE *argv, VALUE self)
{
  const double v = NUM2DBL(self);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  rb_str_cat(encoded, (char*) &v, 8);
  return encoded;
}
예제 #3
0
파일: native.c 프로젝트: jblight/bson-ruby
/**
 * Converts the milliseconds time to the raw BSON bytes. We need to
 * explicitly convert using 64 bit here.
 *
 * @example Convert the milliseconds value to BSON bytes.
 *    rb_time_to_bson(time, 2124132340000, encoded);
 *
 * @param [ Time ] self The Ruby Time object.
 * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
{
  int64_t t = NUM2INT64(rb_funcall(self, rb_intern("to_i"), 0));
  int64_t milliseconds = (int64_t)(t * 1000);
  int32_t micro = NUM2INT(rb_funcall(self, rb_intern("usec"), 0));
  int64_t time = milliseconds + (micro / 1000);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  return int64_t_to_bson(time, encoded);
}
예제 #4
0
파일: native.c 프로젝트: trmmy/bson-ruby
/**
 * Convert the Ruby integer into a character string and append with nullchar to encoded BSON.
 *
 * @example Convert the integer to string and append with nullchar.
 *    rb_integer_to_bson_key(128, encoded);
 *
 * @param [ Integer ] self The Ruby integer.
 * @param [ String ] encoded The Ruby binary string to append to.
 *
 * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
 *
 * @since 2.0.0
 */
static VALUE rb_integer_to_bson_key(int argc, VALUE *argv, VALUE self)
{
  char bytes[INTEGER_CHAR_SIZE];
  const int64_t v = NUM2INT64(self);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  int length;
  if (v < BSON_INDEX_SIZE)
    return rb_str_cat(encoded, rb_bson_array_indexes[v], strlen(rb_bson_array_indexes[v]) + 1);
  length = snprintf(bytes, INTEGER_CHAR_SIZE, "%ld", (long)v);
  return rb_str_cat(encoded, bytes, length + 1);
}
예제 #5
0
파일: native.c 프로젝트: jblight/bson-ruby
/**
 * Append the ruby float as 8-byte double value to buffer.
 *
 * @example Convert float to double and append.
 *    rb_float_to_bson(..., 1.2311);
 *
 * @param [ String] encoded Optional string buffer, default provided by rb_str_encoded_binary
 * @param [ Float ] self The ruby float value.
 *
 * @return [ String ] The encoded bytes with double value appended.
 *
 * @since 2.0.0
 */
static VALUE rb_float_to_bson(int argc, VALUE *argv, VALUE self)
{
  const double v = NUM2DBL(self);
  VALUE encoded = rb_get_default_encoded(argc, argv);
  # if __BYTE_ORDER == __LITTLE_ENDIAN 
  rb_str_cat(encoded, (char*) &v, 8);
  #elif __BYTE_ORDER == __BIG_ENDIAN
  doublebytet swap;
  unsigned char b;
  swap.d = v;
  for (int i=0; i < sizeof(double)/2; i++) {
       b=swap.b[i];
       swap.b[i] = swap.b[((sizeof(double)-1)-i)];
       swap.b[((sizeof(double)-1)-i)]=b;
  }
  rb_str_cat(encoded, (char*)&swap.d, 8);
  #endif 
  return encoded;
}
예제 #6
0
파일: native.c 프로젝트: trmmy/bson-ruby
/**
 * Encode a true value to bson.
 *
 * @example Encode the true value.
 *    rb_true_class_to_bson(0, true);
 *
 * @param [ int ] argc The number or arguments.
 * @param [ Array<Object> ] argv The arguments.
 * @param [ TrueClass ] self The true value.
 *
 * @return [ String ] The encoded string.
 *
 * @since 2.0.0
 */
static VALUE rb_true_class_to_bson(int argc, VALUE *argv, VALUE self)
{
  VALUE encoded = rb_get_default_encoded(argc, argv);
  rb_str_cat(encoded, &rb_bson_true_byte, 1);
  return encoded;
}