Module: Rbxl::Native

Defined in:
ext/rbxl_native/native.c

Class Method Summary collapse

Class Method Details

.generate_sheet(rows) ⇒ Object

Rbxl::Native.generate_sheet(rows) → XML string

rows: Array of Arrays, each inner array is a row of cell values



727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
# File 'ext/rbxl_native/native.c', line 727

static VALUE rb_native_generate(VALUE self, VALUE rows)
{
    (void)self;
    Check_Type(rows, T_ARRAY);

    VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));
    VALUE cWriteOnlyCell = rb_const_get(mRbxl, rb_intern("WriteOnlyCell"));

    long num_rows = RARRAY_LEN(rows);

    /* Find max columns for dimension ref */
    int max_cols = 1;
    for (long i = 0; i < num_rows; i++) {
        VALUE row = rb_ary_entry(rows, i);
        int len = (int)RARRAY_LEN(row);
        if (len > max_cols) max_cols = len;
    }

    dynbuf buf;
    dynbuf_init(&buf);

    #define W(s) dynbuf_append(&buf, s, sizeof(s) - 1)

    W("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
      "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">\n"
      "  <dimension ref=\"A1:");
    write_column_name(&buf, max_cols);
    write_int(&buf, num_rows);
    W("\"/>\n  <sheetData>");

    for (long i = 0; i < num_rows; i++) {
        VALUE row = rb_ary_entry(rows, i);
        Check_Type(row, T_ARRAY);
        long row_num = i + 1;
        long ncols = RARRAY_LEN(row);

        W("<row r=\"");
        write_int(&buf, row_num);
        W("\">");

        for (long j = 0; j < ncols; j++) {
            write_cell(&buf, (int)(j + 1), (int)row_num, rb_ary_entry(row, j), cWriteOnlyCell);
        }

        W("</row>");
    }

    W("</sheetData>\n</worksheet>");

    #undef W

    VALUE result = make_utf8_str(buf.data, (long)buf.len);
    dynbuf_free(&buf);
    return result;
}

.parse_sheet(xml_str, shared_strings) ⇒ Object




496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'ext/rbxl_native/native.c', line 496

static VALUE rb_native_parse(VALUE self, VALUE xml_str, VALUE shared_strings)
{
    (void)self;
    Check_Type(xml_str, T_STRING);
    Check_Type(shared_strings, T_ARRAY);

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 0;
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);

    return run_parse(&ctx, xml_str);
}

.parse_sheet_full(xml_str, shared_strings) ⇒ Object




518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'ext/rbxl_native/native.c', line 518

static VALUE rb_native_parse_full(VALUE self, VALUE xml_str, VALUE shared_strings)
{
    (void)self;
    Check_Type(xml_str, T_STRING);
    Check_Type(shared_strings, T_ARRAY);

    VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 1;
    ctx.cReadOnlyCell      = rb_const_get(mRbxl, rb_intern("ReadOnlyCell"));
    ctx.cRow               = rb_const_get(mRbxl, rb_intern("Row"));
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);
    dynbuf_init(&ctx.cell_ref);

    return run_parse(&ctx, xml_str);
}

.parse_sheet_full_io(io, shared_strings, max_bytes) ⇒ Object




570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'ext/rbxl_native/native.c', line 570

static VALUE rb_native_parse_full_io(VALUE self, VALUE io, VALUE shared_strings, VALUE max_bytes)
{
    (void)self;
    Check_Type(shared_strings, T_ARRAY);

    long max = NIL_P(max_bytes) ? 0 : NUM2LONG(max_bytes);

    VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 1;
    ctx.cReadOnlyCell      = rb_const_get(mRbxl, rb_intern("ReadOnlyCell"));
    ctx.cRow               = rb_const_get(mRbxl, rb_intern("Row"));
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);
    dynbuf_init(&ctx.cell_ref);

    return run_parse_io(&ctx, io, max);
}

.parse_sheet_io(io, shared_strings, max_bytes) ⇒ Object




547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'ext/rbxl_native/native.c', line 547

static VALUE rb_native_parse_io(VALUE self, VALUE io, VALUE shared_strings, VALUE max_bytes)
{
    (void)self;
    Check_Type(shared_strings, T_ARRAY);

    long max = NIL_P(max_bytes) ? 0 : NUM2LONG(max_bytes);

    parse_ctx ctx;
    memset(&ctx, 0, sizeof(ctx));
    ctx.shared_strings     = shared_strings;
    ctx.shared_strings_len = RARRAY_LEN(shared_strings);
    ctx.current_row        = Qnil;
    ctx.full_mode          = 0;
    dynbuf_init(&ctx.text_buf);
    dynbuf_init(&ctx.raw_buf);

    return run_parse_io(&ctx, io, max);
}