Class: Bsm::GeneratorInternal

Inherits:
Object
  • Object
show all
Defined in:
ext/bsm/internal/bsm.c

Instance Method Summary collapse

Instance Method Details

#generate_raw(input) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/bsm/internal/bsm.c', line 52

static VALUE generator_generate_raw(VALUE self, VALUE input)
{
  struct generator_wrapper* w;
  TypedData_Get_Struct(self, struct generator_wrapper, &generator_data_type, w);

  input = rb_String(input);

  /* Iterate over lines, feeding each to bsm2_convert_line, concatenating
   * the returned bytes into one binary String. */
  VALUE result = rb_str_new_literal("");
  rb_enc_associate(result, rb_ascii8bit_encoding());

  char* cstr = StringValuePtr(input);
  long total = RSTRING_LEN(input);
  long line_start = 0;

  for (long i = 0; i <= total; i++)
  {
    if (i == total || cstr[i] == '\n')
    {
      long line_len = i - line_start;
      const char* line = cstr + line_start;

      size_t out_len = 0;
      bsm2_error* err = NULL;
      char* out = bsm2_convert_line(w->proc, line, (size_t)line_len,
                                    &out_len, &err);
      if (!out)
      {
        if (err)
        {
          VALUE msg = rb_str_new_cstr(bsm2_error_message(err));
          bsm2_error_free(err);
          rb_raise(eInvalidInput, "%" PRIsVALUE, msg);
        }
        rb_raise(eInvalidInput, "bsm2_convert_line failed");
      }

      if (out_len)
      {
        rb_str_cat(result, out, (long)out_len);
      }
      free(out);

      line_start = i + 1;
    }
  }

  return result;
}