Sha256: 8dc0fb6ed380d4d9a1d45afb23149343e74f3fcb624b10ebf527ce46ae32eae2

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

#include "adapter_io.h"

AdapterIO::AdapterIO(VALUE s) {
  stream = s;
}

std::string& AdapterIO::read() {
  VALUE response = rb_funcall(stream, rb_intern("read"), 0);
  if (response == Qnil) {
    return empty;
  }
  else {
    // Attempt TO_S first before complaining?
    if (TYPE(response) != T_STRING) {
      rb_raise(
        CONST_GET(rb_mKernel, "ArgumentError"),
        "Write can only process string data. You need to stringify values returned in the callback."
      );
    }
    stringdata = std::string(RSTRING_PTR(response), RSTRING_LEN(response));
    return stringdata;
  }
}

uint32_t AdapterIO::read(char *buffer, uint32_t length) {
  VALUE response = rb_funcall(stream, rb_intern("read"), 1, INT2NUM(length));
  if (response == Qnil) {
    return 0;
  }
  else {
    length = length < RSTRING_LEN(response) ? length : RSTRING_LEN(response);
    memcpy(buffer, RSTRING_PTR(response), length);
    return length;
  }
}

void AdapterIO::write(const char *str) {
  rb_funcall(stream, rb_intern("write"), 1, rb_str_new2(str));
}

void AdapterIO::write(const char *str, uint64_t l) {
  rb_funcall(stream, rb_intern("write"), 1, rb_str_new(str, l));
}

bool AdapterIO::readline(std::string &line) {
  VALUE response = rb_funcall(stream, rb_intern("gets"), 0);
  if (response == Qnil) {
    return false;
  }
  else {
    line = std::string(RSTRING_PTR(response), RSTRING_LEN(response));
    return true;
  }
}

char* AdapterIO::readline() {
  return readline(stringdata) ? (char*)stringdata.c_str() : 0;
}

void AdapterIO::truncate() {
  rb_funcall(stream, rb_intern("truncate"), 0);
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
swift-0.14.0 ext/adapter_io.cc
swift-0.13.0 ext/adapter_io.cc
swift-0.12.0 ext/adapter_io.cc
swift-0.11.0 ext/adapter_io.cc
swift-0.10.0 ext/adapter_io.cc
swift-0.9.1 ext/adapter_io.cc
swift-0.9.0 ext/adapter_io.cc
swift-0.8.1 ext/adapter_io.cc
swift-0.8.0 ext/adapter_io.cc