/* vi:set sw=4:

    rbstrbuf.c

    Copyright (c) 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>

    This program is free software.
    You can distribute/modify this program under the terms of
    the GNU Lesser General Public License version 2 or later.

*/


#include <stdio.h>
#ifdef __STDC__
# include <stdlib.h>
#endif

#include "ruby.h"
#include "re.h"


#include "sftextbuf.h"

VALUE StringBuffer;
VALUE StringBufferMark;


/* buffer type dependent definitions */

#define IS_STRING_BUFFER

#include "sftextbuf.c"


/* ruby interface */

#define BUFFER_CLASS StringBuffer
#define MARK_CLASS StringBufferMark

#define GET_BUF(v,var) Data_Get_Struct(v, struct sf_strbuf, var)
#define GET_MARK(v, var) Data_Get_Struct(v, struct sf_strbufmark, var)
#define GET_MARK_CHK(v, var) GET_MARK(v, var); alive_check(var)

#define WRAP_BUF(p, c) Data_Wrap_Struct(c, buffer_mark, buffer_free, p)
#define WRAP_MARK(p, c) Data_Wrap_Struct(c, bufmark_mark, bufmark_free, p)

#include "rbbufcommon.c"


/*
========================================================================
                                init
========================================================================
*/

void
Init_string_buffer()
{
    StringBuffer = rb_define_class("StringBuffer", rb_cObject);
    rb_define_singleton_method(StringBuffer, "new", buffer_s_new, 0);
    rb_define_method(StringBuffer, "new_mark",     buffer_new_mark,     -1);
    rb_define_method(StringBuffer, "head",         buffer_head,          0);
    rb_define_method(StringBuffer, "tail",         buffer_tail,          0);
    rb_define_method(StringBuffer, "size",         buffer_clen,          0);
    rb_define_method(StringBuffer, "length",       buffer_clen,          0);
    rb_define_method(StringBuffer, "byte_size",    buffer_len,           0);
    rb_define_method(StringBuffer, "byte_length",  buffer_len,           0);
    rb_define_method(StringBuffer, "empty?",       buffer_empty_p,       0);
    rb_define_method(StringBuffer, "substr",       buffer_substr,        2);
    rb_define_method(StringBuffer, "[]",           buffer_substr,        2);
    rb_define_method(StringBuffer, "each",         buffer_each_line,     0);
    rb_define_method(StringBuffer, "each_line",    buffer_each_line,     0);
    rb_define_method(StringBuffer, "reverse_each", buffer_rev_each_line, 0);
    rb_define_method(StringBuffer, "string",       buffer_string,        0);
    rb_define_method(StringBuffer, "to_s",         buffer_string,        0);
    rb_define_method(StringBuffer, "concat",       buffer_concat,        1);
    rb_define_method(StringBuffer, "clear",        buffer_clear,         0);
    rb_define_method(StringBuffer, "<<",           buffer_concat,        1);
    rb_define_method(StringBuffer, "index",        buffer_index,        -1);
    rb_define_method(StringBuffer, "rindex",       buffer_rindex,       -1);
    rb_define_method(StringBuffer, "match_to",     buffer_match_to,     -1);
    rb_define_method(StringBuffer, "=~",           buffer_match_to,     -1);
    rb_define_method(StringBuffer, "inspect",      buffer_inspect,       0);

    StringBufferMark = rb_define_class("StringBufferMark", rb_cData);
    rb_define_method(StringBufferMark, "buffer",       bufmark_buffer,    0);
    rb_define_method(StringBufferMark, "dup",          bufmark_dup,      -1);
    rb_define_method(StringBufferMark, "clone",        bufmark_clone,     0);
    rb_define_method(StringBufferMark, "alive?",       bufmark_alive_p,   0);
    rb_define_method(StringBufferMark, "discard",      bufmark_discard,   0);
    rb_define_method(StringBufferMark, "insert",       bufmark_insert,    1);
/* rb_define_method(StringBufferMark, "copy_of",      bufmark_copyof,    2); */
    rb_define_method(StringBufferMark, "delete",       bufmark_delete,    1);
    rb_define_method(StringBufferMark, "[]",           bufmark_substr,    1);
    rb_define_method(StringBufferMark, "substr",       bufmark_substr,    1);
    rb_define_method(StringBufferMark, "substr_ro",    bufmark_rosubstr,  1);
    rb_define_method(StringBufferMark, "getline",      bufmark_getline,   0);
    rb_define_method(StringBufferMark, "each_line_ro", bufmark_roeach,    0);
    rb_define_method(StringBufferMark, "index",        bufmark_char_index,0);
    rb_define_method(StringBufferMark, "to_i",         bufmark_char_index,0);
    rb_define_method(StringBufferMark, "byte_index",   bufmark_byte_index,0);
    rb_define_method(StringBufferMark, "index=",       bufmark_setidx,    1);
    rb_define_method(StringBufferMark, "move_to",      bufmark_setidx,    1);
    rb_define_method(StringBufferMark, "move",         bufmark_fwd,       1);
    rb_define_method(StringBufferMark, "forward",      bufmark_fwd,       1);
    rb_define_method(StringBufferMark, "back",         bufmark_back,      1);
    rb_define_method(StringBufferMark, "skip",         bufmark_skip,     -1);
    rb_define_method(StringBufferMark, "head",         bufmark_go_beg,    0);
    rb_define_method(StringBufferMark, "tail",         bufmark_go_end,    0);
    rb_define_method(StringBufferMark, "+",            bufmark_plus,      1);
    rb_define_method(StringBufferMark, "-",            bufmark_minus,     1);
    rb_define_method(StringBufferMark, "column",       bufmark_col,       0);
    rb_define_method(StringBufferMark, "x",            bufmark_col,       0);
    rb_define_method(StringBufferMark, "byte_column",  bufmark_bytecol,   0);
    rb_define_method(StringBufferMark, "column=",      bufmark_setcol,    1);
    rb_define_method(StringBufferMark, "x=",           bufmark_setcol,    1);
    rb_define_method(StringBufferMark, "next_line",    bufmark_next_line, 0);
    rb_define_method(StringBufferMark, "prev_line",    bufmark_prev_line, 0);
    rb_define_method(StringBufferMark, "line_head",    bufmark_line_head, 0);
    rb_define_method(StringBufferMark, "line_tail",    bufmark_line_tail, 0);
    rb_define_method(StringBufferMark, "inspect",      bufmark_inspect,   0);


    rb_define_method(StringBuffer, "debug_on", debugon, 0);
    rb_define_method(StringBuffer, "debug_off", debugoff, 0);
}
