2004-01-07 18:46:18 -05:00
|
|
|
/* -*- indent-tabs-mode: nil -*-
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* rubyext.c
|
|
|
|
*
|
|
|
|
* $Author$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 why the lucky stiff
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "syck.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2003-06-17 12:55:54 -04:00
|
|
|
typedef struct RVALUE {
|
|
|
|
union {
|
2004-05-14 23:11:28 -04:00
|
|
|
#if 0
|
2004-05-06 02:29:56 -04:00
|
|
|
struct {
|
|
|
|
unsigned long flags; /* always 0 for freed obj */
|
|
|
|
struct RVALUE *next;
|
|
|
|
} free;
|
2004-05-14 23:11:28 -04:00
|
|
|
#endif
|
2004-05-06 02:29:56 -04:00
|
|
|
struct RBasic basic;
|
|
|
|
struct RObject object;
|
|
|
|
struct RClass klass;
|
2004-05-14 23:11:28 -04:00
|
|
|
/*struct RFloat flonum;*/
|
|
|
|
/*struct RString string;*/
|
2004-05-06 02:29:56 -04:00
|
|
|
struct RArray array;
|
2004-05-14 23:11:28 -04:00
|
|
|
/*struct RRegexp regexp;*/
|
2004-05-06 02:29:56 -04:00
|
|
|
struct RHash hash;
|
2004-05-14 23:11:28 -04:00
|
|
|
/*struct RData data;*/
|
2004-05-06 02:29:56 -04:00
|
|
|
struct RStruct rstruct;
|
2004-05-14 23:11:28 -04:00
|
|
|
/*struct RBignum bignum;*/
|
|
|
|
/*struct RFile file;*/
|
2003-06-17 12:55:54 -04:00
|
|
|
} as;
|
|
|
|
} RVALUE;
|
|
|
|
|
2003-10-17 16:53:39 -04:00
|
|
|
typedef struct {
|
|
|
|
long hash;
|
|
|
|
char *buffer;
|
|
|
|
long length;
|
|
|
|
long remaining;
|
|
|
|
int printed;
|
|
|
|
} bytestring_t;
|
|
|
|
|
2003-05-10 15:55:18 -04:00
|
|
|
#define RUBY_DOMAIN "ruby.yaml.org,2002"
|
|
|
|
|
2003-07-28 15:27:42 -04:00
|
|
|
/*
|
|
|
|
* symbols and constants
|
|
|
|
*/
|
2004-05-06 02:29:56 -04:00
|
|
|
static ID s_new, s_utc, s_at, s_to_f, s_to_i, s_read, s_binmode, s_call, s_cmp, s_transfer, s_update, s_dup, s_match, s_keys, s_to_str, s_unpack, s_tr_bang, s_anchors, s_default_set;
|
2004-03-03 18:08:45 -05:00
|
|
|
static ID s_anchors, s_domain, s_families, s_kind, s_name, s_options, s_private_types, s_type_id, s_value;
|
2003-10-14 19:15:19 -04:00
|
|
|
static VALUE sym_model, sym_generic, sym_input, sym_bytecode;
|
2003-05-09 17:25:50 -04:00
|
|
|
static VALUE sym_scalar, sym_seq, sym_map;
|
2003-10-03 15:51:25 -04:00
|
|
|
VALUE cDate, cParser, cLoader, cNode, cPrivateType, cDomainType, cBadAlias, cDefaultKey, cMergeKey, cEmitter;
|
2003-06-17 21:06:00 -04:00
|
|
|
VALUE oDefaultLoader;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* my private collection of numerical oddities.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
static double S_zero() { return 0.0; }
|
|
|
|
static double S_one() { return 1.0; }
|
|
|
|
static double S_inf() { return S_one() / S_zero(); }
|
|
|
|
static double S_nan() { return S_zero() / S_zero(); }
|
|
|
|
|
|
|
|
static VALUE syck_node_transform( VALUE );
|
|
|
|
|
2003-07-28 15:27:42 -04:00
|
|
|
/*
|
|
|
|
* handler prototypes
|
|
|
|
*/
|
2003-05-14 06:57:42 -04:00
|
|
|
SYMID rb_syck_parse_handler _((SyckParser *, SyckNode *));
|
|
|
|
SYMID rb_syck_load_handler _((SyckParser *, SyckNode *));
|
|
|
|
void rb_syck_err_handler _((SyckParser *, char *));
|
2003-06-17 12:55:54 -04:00
|
|
|
SyckNode * rb_syck_bad_anchor_handler _((SyckParser *, char *));
|
2003-07-11 18:52:14 -04:00
|
|
|
void rb_syck_output_handler _((SyckEmitter *, char *, long));
|
2003-12-10 21:39:59 -05:00
|
|
|
int syck_parser_assign_io _((SyckParser *, VALUE));
|
2003-05-14 06:57:42 -04:00
|
|
|
|
2003-06-05 01:40:03 -04:00
|
|
|
struct parser_xtra {
|
2003-07-04 08:51:42 -04:00
|
|
|
VALUE data; /* Borrowed this idea from marshal.c to fix [ruby-core:8067] problem */
|
2003-06-05 01:40:03 -04:00
|
|
|
VALUE proc;
|
2003-08-19 12:38:02 -04:00
|
|
|
int taint;
|
2003-06-05 01:40:03 -04:00
|
|
|
};
|
|
|
|
|
2003-10-17 16:53:39 -04:00
|
|
|
/*
|
|
|
|
* Convert YAML to bytecode
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_syck_compile(self, port)
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE self, port;
|
2003-10-17 16:53:39 -04:00
|
|
|
{
|
|
|
|
SYMID oid;
|
|
|
|
int taint;
|
|
|
|
char *ret;
|
|
|
|
VALUE bc;
|
|
|
|
bytestring_t *sav;
|
|
|
|
|
|
|
|
SyckParser *parser = syck_new_parser();
|
2004-05-14 23:11:28 -04:00
|
|
|
taint = syck_parser_assign_io(parser, port);
|
2003-10-17 16:53:39 -04:00
|
|
|
syck_parser_handler( parser, syck_yaml2byte_handler );
|
|
|
|
syck_parser_error_handler( parser, NULL );
|
|
|
|
syck_parser_implicit_typing( parser, 0 );
|
|
|
|
syck_parser_taguri_expansion( parser, 0 );
|
|
|
|
oid = syck_parse( parser );
|
|
|
|
syck_lookup_sym( parser, oid, (char **)&sav );
|
|
|
|
|
|
|
|
ret = S_ALLOC_N( char, strlen( sav->buffer ) + 3 );
|
|
|
|
ret[0] = '\0';
|
|
|
|
strcat( ret, "D\n" );
|
|
|
|
strcat( ret, sav->buffer );
|
|
|
|
|
|
|
|
syck_free_parser( parser );
|
|
|
|
|
|
|
|
bc = rb_str_new2( ret );
|
|
|
|
if ( taint ) OBJ_TAINT( bc );
|
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* read from io.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
long
|
|
|
|
rb_syck_io_str_read( char *buf, SyckIoStr *str, long max_size, long skip )
|
|
|
|
{
|
|
|
|
long len = 0;
|
|
|
|
|
|
|
|
ASSERT( str != NULL );
|
|
|
|
max_size -= skip;
|
|
|
|
|
2003-11-25 14:51:38 -05:00
|
|
|
if ( max_size <= 0 ) max_size = 0;
|
|
|
|
else
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* call io#read.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
VALUE src = (VALUE)str->ptr;
|
|
|
|
VALUE n = LONG2NUM(max_size);
|
2003-11-25 14:51:38 -05:00
|
|
|
VALUE str2 = rb_funcall2(src, s_read, 1, &n);
|
|
|
|
if (!NIL_P(str2))
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-11-25 14:51:38 -05:00
|
|
|
len = RSTRING(str2)->len;
|
|
|
|
memcpy( buf + skip, RSTRING(str2)->ptr, len );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
len += skip;
|
|
|
|
buf[len] = '\0';
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* determine: are we reading from a string or io?
|
2003-08-19 12:38:02 -04:00
|
|
|
* (returns tainted? boolean)
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-08-19 12:38:02 -04:00
|
|
|
int
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_parser_assign_io(parser, port)
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
|
|
|
VALUE port;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-08-19 12:38:02 -04:00
|
|
|
int taint = Qtrue;
|
2003-08-21 17:16:58 -04:00
|
|
|
if (rb_respond_to(port, s_to_str)) {
|
2004-05-14 23:11:28 -04:00
|
|
|
taint = OBJ_TAINTED(port); /* original taintedness */
|
|
|
|
StringValue(port); /* possible conversion */
|
|
|
|
syck_parser_str( parser, RSTRING(port)->ptr, RSTRING(port)->len, NULL );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
else if (rb_respond_to(port, s_read)) {
|
|
|
|
if (rb_respond_to(port, s_binmode)) {
|
|
|
|
rb_funcall2(port, s_binmode, 0, 0);
|
|
|
|
}
|
2003-07-04 08:51:42 -04:00
|
|
|
syck_parser_str( parser, (char *)port, 0, rb_syck_io_str_read );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
|
|
|
}
|
2003-08-19 12:38:02 -04:00
|
|
|
return taint;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Get value in hash by key, forcing an empty hash if nil.
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_get_hash_aref(hsh, key)
|
|
|
|
VALUE hsh, key;
|
|
|
|
{
|
|
|
|
VALUE val = rb_hash_aref( hsh, key );
|
|
|
|
if ( NIL_P( val ) )
|
|
|
|
{
|
|
|
|
val = rb_hash_new();
|
|
|
|
rb_hash_aset(hsh, key, val);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* creating timestamps
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
SYMID
|
|
|
|
rb_syck_mktime(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
VALUE time;
|
|
|
|
char *ptr = str;
|
2004-01-28 11:59:00 -05:00
|
|
|
VALUE year, mon, day, hour, min, sec;
|
|
|
|
long usec;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Year*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr[4] = '\0';
|
|
|
|
year = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Month*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 4;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-05-09 17:25:50 -04:00
|
|
|
mon = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Day*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-05-09 17:25:50 -04:00
|
|
|
day = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Hour*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-05-09 17:25:50 -04:00
|
|
|
hour = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Minute */
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-05-09 17:25:50 -04:00
|
|
|
min = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Second */
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-05-09 17:25:50 -04:00
|
|
|
sec = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Millisecond */
|
2003-05-15 03:38:10 -04:00
|
|
|
ptr += 2;
|
2003-05-30 19:09:54 -04:00
|
|
|
if ( *ptr == '.' )
|
|
|
|
{
|
2004-01-28 11:59:00 -05:00
|
|
|
char *padded = syck_strndup( "000000", 6 );
|
|
|
|
char *end = ptr + 1;
|
|
|
|
while ( isdigit( *end ) ) end++;
|
|
|
|
MEMCPY(padded, ptr + 1, char, end - (ptr + 1));
|
|
|
|
usec = strtol(padded, NULL, 10);
|
2003-05-30 19:09:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-28 11:59:00 -05:00
|
|
|
usec = 0;
|
2003-05-30 19:09:54 -04:00
|
|
|
}
|
2003-05-15 03:38:10 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Time Zone*/
|
2003-05-15 03:38:10 -04:00
|
|
|
while ( *ptr != 'Z' && *ptr != '+' && *ptr != '-' && *ptr != '\0' ) ptr++;
|
|
|
|
if ( *ptr == '-' || *ptr == '+' )
|
|
|
|
{
|
2004-01-28 11:59:00 -05:00
|
|
|
time_t tz_offset = strtol(ptr, NULL, 10) * 3600;
|
|
|
|
time_t tmp;
|
2003-05-15 03:38:10 -04:00
|
|
|
|
|
|
|
while ( *ptr != ':' && *ptr != '\0' ) ptr++;
|
|
|
|
if ( *ptr == ':' )
|
|
|
|
{
|
|
|
|
ptr += 1;
|
|
|
|
if ( tz_offset < 0 )
|
|
|
|
{
|
2004-01-28 11:59:00 -05:00
|
|
|
tz_offset -= strtol(ptr, NULL, 10) * 60;
|
2003-05-15 03:38:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-28 11:59:00 -05:00
|
|
|
tz_offset += strtol(ptr, NULL, 10) * 60;
|
2003-05-15 03:38:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Make TZ time*/
|
2004-01-28 11:59:00 -05:00
|
|
|
time = rb_funcall(rb_cTime, s_utc, 6, year, mon, day, hour, min, sec);
|
|
|
|
tmp = NUM2LONG(rb_funcall(time, s_to_i, 0)) - tz_offset;
|
|
|
|
return rb_funcall(rb_cTime, s_at, 2, LONG2NUM(tmp), LONG2NUM(usec));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make UTC time*/
|
|
|
|
return rb_funcall(rb_cTime, s_utc, 7, year, mon, day, hour, min, sec, LONG2NUM(usec));
|
2003-05-15 03:38:10 -04:00
|
|
|
|
2004-01-28 11:59:00 -05:00
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* {generic mode} node handler
|
|
|
|
* - Loads data into Node classes
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
SYMID
|
|
|
|
rb_syck_parse_handler(p, n)
|
|
|
|
SyckParser *p;
|
|
|
|
SyckNode *n;
|
|
|
|
{
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE t, obj, v = Qnil;
|
2003-05-09 17:25:50 -04:00
|
|
|
int i;
|
2003-06-05 01:40:03 -04:00
|
|
|
struct parser_xtra *bonus;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
obj = rb_obj_alloc(cNode);
|
|
|
|
if ( n->type_id != NULL )
|
|
|
|
{
|
|
|
|
t = rb_str_new2(n->type_id);
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(obj, s_type_id, t);
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (n->kind)
|
|
|
|
{
|
|
|
|
case syck_str_kind:
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(obj, s_kind, sym_scalar);
|
2003-05-09 17:25:50 -04:00
|
|
|
v = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(obj, s_kind, sym_seq);
|
2003-05-09 17:25:50 -04:00
|
|
|
v = rb_ary_new2( n->data.list->idx );
|
|
|
|
for ( i = 0; i < n->data.list->idx; i++ )
|
|
|
|
{
|
|
|
|
rb_ary_store( v, i, syck_seq_read( n, i ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_map_kind:
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(obj, s_kind, sym_map);
|
2003-05-09 17:25:50 -04:00
|
|
|
v = rb_hash_new();
|
|
|
|
for ( i = 0; i < n->data.pairs->idx; i++ )
|
|
|
|
{
|
|
|
|
VALUE key = syck_node_transform( syck_map_read( n, map_key, i ) );
|
|
|
|
VALUE val = rb_ary_new();
|
|
|
|
rb_ary_push(val, syck_map_read( n, map_key, i ));
|
|
|
|
rb_ary_push(val, syck_map_read( n, map_value, i ));
|
|
|
|
|
|
|
|
rb_hash_aset( v, key, val );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-06-05 01:40:03 -04:00
|
|
|
bonus = (struct parser_xtra *)p->bonus;
|
2003-08-19 12:38:02 -04:00
|
|
|
if ( bonus->taint) OBJ_TAINT( obj );
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( bonus->proc != 0 ) rb_funcall(bonus->proc, s_call, 1, v);
|
2003-06-05 01:40:03 -04:00
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(obj, s_value, v);
|
2003-06-05 01:40:03 -04:00
|
|
|
rb_hash_aset(bonus->data, INT2FIX(RHASH(bonus->data)->tbl->num_entries), obj);
|
2003-05-09 17:25:50 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2003-06-17 21:06:00 -04:00
|
|
|
/*
|
|
|
|
* handles merging of an array of hashes
|
|
|
|
* (see http://www.yaml.org/type/merge/)
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_merge_i( entry, hsh )
|
|
|
|
VALUE entry, hsh;
|
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( rb_obj_is_kind_of( entry, rb_cHash ) )
|
|
|
|
{
|
|
|
|
rb_funcall( hsh, s_update, 1, entry );
|
|
|
|
}
|
2003-06-17 21:06:00 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2003-08-21 17:16:58 -04:00
|
|
|
* build a syck node from a Ruby VALUE
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-08-21 17:16:58 -04:00
|
|
|
SyckNode *
|
|
|
|
rb_new_syck_node( obj, type_id )
|
|
|
|
VALUE obj, type_id;
|
|
|
|
{
|
|
|
|
long i = 0;
|
|
|
|
SyckNode *n = NULL;
|
|
|
|
|
|
|
|
if (rb_respond_to(obj, s_to_str))
|
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
StringValue(obj); /* possible conversion */
|
2003-08-21 17:16:58 -04:00
|
|
|
n = syck_alloc_str();
|
|
|
|
n->data.str->ptr = RSTRING(obj)->ptr;
|
|
|
|
n->data.str->len = RSTRING(obj)->len;
|
|
|
|
}
|
2004-05-14 23:11:28 -04:00
|
|
|
else if ( rb_obj_is_kind_of( obj, rb_cArray ) )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
|
|
|
n = syck_alloc_seq();
|
|
|
|
for ( i = 0; i < RARRAY(obj)->len; i++ )
|
|
|
|
{
|
|
|
|
syck_seq_add(n, rb_ary_entry(obj, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( rb_obj_is_kind_of( obj, rb_cHash ) )
|
|
|
|
{
|
|
|
|
VALUE keys;
|
|
|
|
n = syck_alloc_map();
|
|
|
|
keys = rb_funcall( obj, s_keys, 0 );
|
|
|
|
for ( i = 0; i < RARRAY(keys)->len; i++ )
|
|
|
|
{
|
|
|
|
VALUE key = rb_ary_entry(keys, i);
|
|
|
|
syck_map_add(n, key, rb_hash_aref(obj, key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( n!= NULL && rb_respond_to( type_id, s_to_str ) )
|
|
|
|
{
|
|
|
|
StringValue(type_id);
|
|
|
|
n->type_id = syck_strndup( RSTRING(type_id)->ptr, RSTRING(type_id)->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* default handler for ruby.yaml.org types
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
yaml_org_handler( n, ref )
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckNode *n;
|
2003-08-21 17:16:58 -04:00
|
|
|
VALUE *ref;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
char *type_id = n->type_id;
|
|
|
|
int transferred = 0;
|
|
|
|
long i = 0;
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE obj = Qnil;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
switch (n->kind)
|
|
|
|
{
|
|
|
|
case syck_str_kind:
|
2003-08-21 17:16:58 -04:00
|
|
|
transferred = 1;
|
2004-01-12 17:55:09 -05:00
|
|
|
if ( type_id == NULL )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "null" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = Qnil;
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "binary" ) == 0 )
|
|
|
|
{
|
|
|
|
VALUE arr;
|
|
|
|
obj = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
rb_funcall( obj, s_tr_bang, 2, rb_str_new2( "\n\t " ), rb_str_new2( "" ) );
|
|
|
|
arr = rb_funcall( obj, s_unpack, 1, rb_str_new2( "m" ) );
|
|
|
|
obj = rb_ary_shift( arr );
|
|
|
|
}
|
|
|
|
else if ( strcmp( type_id, "bool#yes" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = Qtrue;
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "bool#no" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = Qfalse;
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "int#hex" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-01-12 17:55:09 -05:00
|
|
|
syck_str_blow_away_commas( n );
|
2003-05-09 17:25:50 -04:00
|
|
|
obj = rb_cstr2inum( n->data.str->ptr, 16 );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "int#oct" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-01-12 17:55:09 -05:00
|
|
|
syck_str_blow_away_commas( n );
|
2003-05-09 17:25:50 -04:00
|
|
|
obj = rb_cstr2inum( n->data.str->ptr, 8 );
|
|
|
|
}
|
2004-01-12 17:55:09 -05:00
|
|
|
else if ( strcmp( type_id, "int#base60" ) == 0 )
|
|
|
|
{
|
|
|
|
char *ptr, *end;
|
|
|
|
long sixty = 1;
|
|
|
|
long total = 0;
|
|
|
|
syck_str_blow_away_commas( n );
|
|
|
|
ptr = n->data.str->ptr;
|
|
|
|
end = n->data.str->ptr + n->data.str->len;
|
|
|
|
while ( end > ptr )
|
|
|
|
{
|
|
|
|
long bnum = 0;
|
|
|
|
char *colon = end - 1;
|
|
|
|
while ( colon >= ptr && *colon != ':' )
|
|
|
|
{
|
|
|
|
colon--;
|
|
|
|
}
|
|
|
|
if ( *colon == ':' ) *colon = '\0';
|
|
|
|
|
|
|
|
bnum = strtol( colon + 1, NULL, 10 );
|
|
|
|
total += bnum * sixty;
|
|
|
|
sixty *= 60;
|
|
|
|
end = colon;
|
|
|
|
}
|
|
|
|
obj = INT2FIX(total);
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strncmp( type_id, "int", 3 ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
syck_str_blow_away_commas( n );
|
|
|
|
obj = rb_cstr2inum( n->data.str->ptr, 10 );
|
|
|
|
}
|
2004-08-07 11:34:39 -04:00
|
|
|
else if ( strcmp( type_id, "float#base60" ) == 0 )
|
|
|
|
{
|
|
|
|
char *ptr, *end;
|
|
|
|
long sixty = 1;
|
|
|
|
double total = 0.0;
|
|
|
|
syck_str_blow_away_commas( n );
|
|
|
|
ptr = n->data.str->ptr;
|
|
|
|
end = n->data.str->ptr + n->data.str->len;
|
|
|
|
while ( end > ptr )
|
|
|
|
{
|
|
|
|
double bnum = 0;
|
|
|
|
char *colon = end - 1;
|
|
|
|
while ( colon >= ptr && *colon != ':' )
|
|
|
|
{
|
|
|
|
colon--;
|
|
|
|
}
|
|
|
|
if ( *colon == ':' ) *colon = '\0';
|
|
|
|
|
|
|
|
bnum = strtod( colon + 1, NULL );
|
|
|
|
total += bnum * sixty;
|
|
|
|
sixty *= 60;
|
|
|
|
end = colon;
|
|
|
|
}
|
|
|
|
obj = rb_float_new( total );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "float#nan" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_float_new( S_nan() );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "float#inf" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_float_new( S_inf() );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "float#neginf" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_float_new( -S_inf() );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strncmp( type_id, "float", 5 ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
double f;
|
|
|
|
syck_str_blow_away_commas( n );
|
|
|
|
f = strtod( n->data.str->ptr, NULL );
|
|
|
|
obj = rb_float_new( f );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "timestamp#iso8601" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_syck_mktime( n->data.str->ptr );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "timestamp#spaced" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_syck_mktime( n->data.str->ptr );
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "timestamp#ymd" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-06-17 12:55:54 -04:00
|
|
|
char *ptr = n->data.str->ptr;
|
|
|
|
VALUE year, mon, day;
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Year*/
|
2003-06-17 12:55:54 -04:00
|
|
|
ptr[4] = '\0';
|
|
|
|
year = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Month*/
|
2003-06-17 12:55:54 -04:00
|
|
|
ptr += 4;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-06-17 12:55:54 -04:00
|
|
|
mon = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Day*/
|
2003-06-17 12:55:54 -04:00
|
|
|
ptr += 2;
|
2003-12-10 21:39:59 -05:00
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
2003-06-17 12:55:54 -04:00
|
|
|
day = INT2FIX(strtol(ptr, NULL, 10));
|
2004-05-14 23:11:28 -04:00
|
|
|
|
2004-01-13 02:57:33 -05:00
|
|
|
if ( !cDate ) {
|
|
|
|
/*
|
|
|
|
* Load Date module
|
|
|
|
*/
|
|
|
|
rb_require( "date" );
|
|
|
|
cDate = rb_const_get( rb_cObject, rb_intern("Date") );
|
|
|
|
}
|
2003-06-17 12:55:54 -04:00
|
|
|
|
|
|
|
obj = rb_funcall( cDate, s_new, 3, year, mon, day );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strncmp( type_id, "timestamp", 9 ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
obj = rb_syck_mktime( n->data.str->ptr );
|
|
|
|
}
|
2004-01-12 17:55:09 -05:00
|
|
|
else if ( strncmp( type_id, "merge", 5 ) == 0 )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( cMergeKey, s_new, 0 );
|
|
|
|
}
|
|
|
|
else if ( strncmp( type_id, "default", 7 ) == 0 )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( cDefaultKey, s_new, 0 );
|
|
|
|
}
|
2004-05-14 23:11:28 -04:00
|
|
|
else if ( n->data.str->style == scalar_plain &&
|
|
|
|
n->data.str->len > 1 &&
|
|
|
|
strncmp( n->data.str->ptr, ":", 1 ) == 0 )
|
2004-01-07 18:46:18 -05:00
|
|
|
{
|
2004-05-06 02:29:56 -04:00
|
|
|
obj = rb_funcall( oDefaultLoader, s_transfer, 2,
|
|
|
|
rb_str_new2( "ruby/sym" ),
|
|
|
|
rb_str_new( n->data.str->ptr + 1, n->data.str->len - 1 ) );
|
2004-01-07 18:46:18 -05:00
|
|
|
}
|
2004-01-12 17:55:09 -05:00
|
|
|
else if ( strcmp( type_id, "str" ) == 0 )
|
2004-01-07 18:46:18 -05:00
|
|
|
{
|
2004-01-12 17:55:09 -05:00
|
|
|
obj = rb_str_new( n->data.str->ptr, n->data.str->len );
|
2004-01-07 18:46:18 -05:00
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
else
|
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
transferred = 0;
|
2003-05-09 17:25:50 -04:00
|
|
|
obj = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
2003-10-14 19:15:19 -04:00
|
|
|
if ( type_id == NULL || strcmp( type_id, "seq" ) == 0 )
|
|
|
|
{
|
|
|
|
transferred = 1;
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
obj = rb_ary_new2( n->data.list->idx );
|
|
|
|
for ( i = 0; i < n->data.list->idx; i++ )
|
|
|
|
{
|
|
|
|
rb_ary_store( obj, i, syck_seq_read( n, i ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_map_kind:
|
2003-10-14 19:15:19 -04:00
|
|
|
if ( type_id == NULL || strcmp( type_id, "map" ) == 0 )
|
|
|
|
{
|
|
|
|
transferred = 1;
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
obj = rb_hash_new();
|
|
|
|
for ( i = 0; i < n->data.pairs->idx; i++ )
|
|
|
|
{
|
2004-01-12 17:55:09 -05:00
|
|
|
VALUE k = syck_map_read( n, map_key, i );
|
|
|
|
VALUE v = syck_map_read( n, map_value, i );
|
|
|
|
int skip_aset = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle merge keys
|
|
|
|
*/
|
|
|
|
if ( rb_obj_is_kind_of( k, cMergeKey ) )
|
|
|
|
{
|
|
|
|
if ( rb_obj_is_kind_of( v, rb_cHash ) )
|
|
|
|
{
|
|
|
|
VALUE dup = rb_funcall( v, s_dup, 0 );
|
|
|
|
rb_funcall( dup, s_update, 1, obj );
|
|
|
|
obj = dup;
|
|
|
|
skip_aset = 1;
|
|
|
|
}
|
|
|
|
else if ( rb_obj_is_kind_of( v, rb_cArray ) )
|
|
|
|
{
|
|
|
|
VALUE end = rb_ary_pop( v );
|
|
|
|
if ( rb_obj_is_kind_of( end, rb_cHash ) )
|
|
|
|
{
|
|
|
|
VALUE dup = rb_funcall( end, s_dup, 0 );
|
|
|
|
v = rb_ary_reverse( v );
|
|
|
|
rb_ary_push( v, obj );
|
|
|
|
rb_iterate( rb_each, v, syck_merge_i, dup );
|
|
|
|
obj = dup;
|
|
|
|
skip_aset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-03 15:51:25 -04:00
|
|
|
else if ( rb_obj_is_kind_of( k, cDefaultKey ) )
|
|
|
|
{
|
|
|
|
rb_funcall( obj, s_default_set, 1, v );
|
|
|
|
skip_aset = 1;
|
|
|
|
}
|
2003-06-17 21:06:00 -04:00
|
|
|
|
2004-01-12 17:55:09 -05:00
|
|
|
if ( ! skip_aset )
|
|
|
|
{
|
|
|
|
rb_hash_aset( obj, k, v );
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-05-11 02:32:13 -04:00
|
|
|
|
2003-08-21 17:16:58 -04:00
|
|
|
*ref = obj;
|
|
|
|
return transferred;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* {native mode} node handler
|
|
|
|
* - Converts data into native Ruby types
|
|
|
|
*/
|
|
|
|
SYMID
|
|
|
|
rb_syck_load_handler(p, n)
|
|
|
|
SyckParser *p;
|
|
|
|
SyckNode *n;
|
|
|
|
{
|
|
|
|
VALUE obj = Qnil;
|
|
|
|
struct parser_xtra *bonus;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt common transfers
|
|
|
|
*/
|
|
|
|
int transferred = yaml_org_handler(n, &obj);
|
|
|
|
if ( transferred == 0 && n->type_id != NULL )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( oDefaultLoader, s_transfer, 2, rb_str_new2( n->type_id ), obj );
|
|
|
|
}
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* ID already set, let's alter the symbol table to accept the new object
|
|
|
|
*/
|
2004-05-06 02:29:56 -04:00
|
|
|
if (n->id > 0 && !NIL_P(obj))
|
2003-06-17 12:55:54 -04:00
|
|
|
{
|
|
|
|
MEMCPY((void *)n->id, (void *)obj, RVALUE, 1);
|
|
|
|
MEMZERO((void *)obj, RVALUE, 1);
|
|
|
|
obj = n->id;
|
|
|
|
}
|
|
|
|
|
2003-06-05 01:40:03 -04:00
|
|
|
bonus = (struct parser_xtra *)p->bonus;
|
2003-08-19 12:38:02 -04:00
|
|
|
if ( bonus->taint) OBJ_TAINT( obj );
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( bonus->proc != 0 ) rb_funcall(bonus->proc, s_call, 1, obj);
|
2003-05-11 02:32:13 -04:00
|
|
|
|
2003-06-05 01:40:03 -04:00
|
|
|
rb_hash_aset(bonus->data, INT2FIX(RHASH(bonus->data)->tbl->num_entries), obj);
|
2003-05-09 17:25:50 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* friendly errors.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
void
|
|
|
|
rb_syck_err_handler(p, msg)
|
|
|
|
SyckParser *p;
|
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
char *endl = p->cursor;
|
|
|
|
|
|
|
|
while ( *endl != '\0' && *endl != '\n' )
|
|
|
|
endl++;
|
|
|
|
|
|
|
|
endl[0] = '\0';
|
2003-05-22 02:14:17 -04:00
|
|
|
rb_raise(rb_eArgError, "%s on line %d, col %d: `%s'",
|
2003-05-09 17:25:50 -04:00
|
|
|
msg,
|
|
|
|
p->linect,
|
|
|
|
p->cursor - p->lineptr,
|
|
|
|
p->lineptr);
|
|
|
|
}
|
|
|
|
|
2003-07-28 15:27:42 -04:00
|
|
|
/*
|
|
|
|
* provide bad anchor object to the parser.
|
|
|
|
*/
|
2003-06-17 12:55:54 -04:00
|
|
|
SyckNode *
|
|
|
|
rb_syck_bad_anchor_handler(p, a)
|
|
|
|
SyckParser *p;
|
|
|
|
char *a;
|
|
|
|
{
|
2003-11-25 14:51:38 -05:00
|
|
|
VALUE anchor_name = rb_str_new2( a );
|
|
|
|
SyckNode *badanc = syck_new_map( rb_str_new2( "name" ), anchor_name );
|
2003-10-14 19:15:19 -04:00
|
|
|
badanc->type_id = syck_strndup( "tag:ruby.yaml.org,2002:object:YAML::Syck::BadAlias", 53 );
|
2003-06-17 12:55:54 -04:00
|
|
|
return badanc;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* data loaded based on the model requested.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
void
|
2003-10-14 19:15:19 -04:00
|
|
|
syck_set_model( parser, input, model )
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
|
|
|
VALUE input, model;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( model == sym_generic )
|
|
|
|
{
|
|
|
|
syck_parser_handler( parser, rb_syck_parse_handler );
|
|
|
|
syck_parser_implicit_typing( parser, 1 );
|
|
|
|
syck_parser_taguri_expansion( parser, 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syck_parser_handler( parser, rb_syck_load_handler );
|
|
|
|
syck_parser_implicit_typing( parser, 1 );
|
|
|
|
syck_parser_taguri_expansion( parser, 0 );
|
|
|
|
}
|
2003-10-14 19:15:19 -04:00
|
|
|
if ( input == sym_bytecode )
|
|
|
|
{
|
|
|
|
syck_parser_set_input_type( parser, syck_bytecode_utf8 );
|
|
|
|
}
|
2003-06-17 12:55:54 -04:00
|
|
|
syck_parser_error_handler( parser, rb_syck_err_handler );
|
|
|
|
syck_parser_bad_anchor_handler( parser, rb_syck_bad_anchor_handler );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2003-07-28 15:27:42 -04:00
|
|
|
* mark parser nodes
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-06-05 00:43:05 -04:00
|
|
|
static void
|
|
|
|
syck_mark_parser(parser)
|
|
|
|
SyckParser *parser;
|
|
|
|
{
|
|
|
|
rb_gc_mark(parser->root);
|
|
|
|
rb_gc_mark(parser->root_on_error);
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.new
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
VALUE
|
|
|
|
syck_parser_new(argc, argv, class)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE class;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE pobj, options, init_argv[1];
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckParser *parser = syck_new_parser();
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &options);
|
2004-05-14 23:11:28 -04:00
|
|
|
pobj = Data_Wrap_Struct( class, syck_mark_parser, syck_free_parser, parser );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-05-15 17:46:47 -04:00
|
|
|
syck_parser_set_root_on_error( parser, Qnil );
|
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
if ( ! rb_obj_is_instance_of( options, rb_cHash ) )
|
|
|
|
{
|
|
|
|
options = rb_hash_new();
|
|
|
|
}
|
2004-05-14 23:11:28 -04:00
|
|
|
init_argv[0] = options;
|
|
|
|
rb_obj_call_init(pobj, 1, init_argv);
|
|
|
|
return pobj;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.initialize( options )
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
static VALUE
|
|
|
|
syck_parser_initialize( self, options )
|
|
|
|
VALUE self, options;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(self, s_options, options);
|
2004-05-14 23:11:28 -04:00
|
|
|
return self;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-11-25 14:51:38 -05:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.bufsize = Integer
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
syck_parser_bufsize_set( self, size )
|
|
|
|
VALUE self, size;
|
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
2003-11-25 14:51:38 -05:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2003-11-25 14:51:38 -05:00
|
|
|
if ( rb_respond_to( size, s_to_i ) ) {
|
|
|
|
parser->bufsize = NUM2INT(rb_funcall(size, s_to_i, 0));
|
|
|
|
}
|
2004-05-14 23:11:28 -04:00
|
|
|
return self;
|
2003-11-25 14:51:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.bufsize => Integer
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
syck_parser_bufsize_get( self )
|
|
|
|
VALUE self;
|
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
2003-11-25 14:51:38 -05:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
|
|
|
return INT2FIX( parser->bufsize );
|
2003-11-25 14:51:38 -05:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.load( IO or String )
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
VALUE
|
|
|
|
syck_parser_load(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE self;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-10-14 19:15:19 -04:00
|
|
|
VALUE port, proc, model, input;
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
2003-06-05 01:40:03 -04:00
|
|
|
struct parser_xtra bonus;
|
|
|
|
volatile VALUE hash; /* protect from GC */
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &port, &proc);
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
input = rb_hash_aref( rb_attr_get( self, s_options ), sym_input );
|
|
|
|
model = rb_hash_aref( rb_attr_get( self, s_options ), sym_model );
|
2004-05-14 23:11:28 -04:00
|
|
|
syck_set_model( parser, input, model );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
bonus.taint = syck_parser_assign_io(parser, port);
|
2003-06-05 01:40:03 -04:00
|
|
|
bonus.data = hash = rb_hash_new();
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( NIL_P( proc ) ) bonus.proc = 0;
|
2003-06-05 01:40:03 -04:00
|
|
|
else bonus.proc = proc;
|
2004-05-14 23:11:28 -04:00
|
|
|
|
|
|
|
parser->bonus = (void *)&bonus;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-05-22 01:41:06 -04:00
|
|
|
return syck_parse( parser );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Parser.load_documents( IO or String ) { |doc| }
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
VALUE
|
|
|
|
syck_parser_load_documents(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE self;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-10-14 19:15:19 -04:00
|
|
|
VALUE port, proc, v, input, model;
|
2004-05-14 23:11:28 -04:00
|
|
|
SyckParser *parser;
|
2003-06-05 01:40:03 -04:00
|
|
|
struct parser_xtra bonus;
|
|
|
|
volatile VALUE hash;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1&", &port, &proc);
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
input = rb_hash_aref( rb_attr_get( self, s_options ), sym_input );
|
|
|
|
model = rb_hash_aref( rb_attr_get( self, s_options ), sym_model );
|
2004-05-14 23:11:28 -04:00
|
|
|
syck_set_model( parser, input, model );
|
2003-06-05 01:40:03 -04:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
bonus.taint = syck_parser_assign_io(parser, port);
|
2003-05-09 17:25:50 -04:00
|
|
|
while ( 1 )
|
2004-05-14 23:11:28 -04:00
|
|
|
{
|
2003-06-05 01:40:03 -04:00
|
|
|
/* Reset hash for tracking nodes */
|
|
|
|
bonus.data = hash = rb_hash_new();
|
|
|
|
bonus.proc = 0;
|
|
|
|
parser->bonus = (void *)&bonus;
|
|
|
|
|
|
|
|
/* Parse a document */
|
2004-05-14 23:11:28 -04:00
|
|
|
v = syck_parse( parser );
|
2003-05-09 17:25:50 -04:00
|
|
|
if ( parser->eof == 1 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-06-05 01:40:03 -04:00
|
|
|
|
|
|
|
/* Pass document to block */
|
2004-05-14 23:11:28 -04:00
|
|
|
rb_funcall( proc, s_call, 1, v );
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader.initialize
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
static VALUE
|
|
|
|
syck_loader_initialize( self )
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE families;
|
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
families = rb_hash_new();
|
|
|
|
rb_ivar_set(self, s_families, families);
|
|
|
|
rb_ivar_set(self, s_private_types, rb_hash_new());
|
|
|
|
rb_ivar_set(self, s_anchors, rb_hash_new());
|
2003-05-10 15:55:18 -04:00
|
|
|
|
|
|
|
rb_hash_aset(families, rb_str_new2( YAML_DOMAIN ), rb_hash_new());
|
|
|
|
rb_hash_aset(families, rb_str_new2( RUBY_DOMAIN ), rb_hash_new());
|
|
|
|
|
2004-06-29 13:34:08 -04:00
|
|
|
return self;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Add type family, used by add_*_type methods.
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_add_type_family( self, domain, type_re, proc )
|
|
|
|
VALUE self, domain, type_re, proc;
|
|
|
|
{
|
|
|
|
VALUE families, domain_types;
|
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
families = rb_attr_get(self, s_families);
|
2003-05-10 15:55:18 -04:00
|
|
|
domain_types = syck_get_hash_aref(families, domain);
|
|
|
|
rb_hash_aset( domain_types, type_re, proc );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader.add_domain_type
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_add_domain_type( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE domain, type_re, proc;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "2&", &domain, &type_re, &proc);
|
|
|
|
syck_loader_add_type_family( self, domain, type_re, proc );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader.add_builtin_type
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_add_builtin_type( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE type_re, proc;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1&", &type_re, &proc);
|
|
|
|
syck_loader_add_type_family( self, rb_str_new2( YAML_DOMAIN ), type_re, proc );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader.add_ruby_type
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_add_ruby_type( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE type_re, proc;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1&", &type_re, &proc);
|
|
|
|
syck_loader_add_type_family( self, rb_str_new2( RUBY_DOMAIN ), type_re, proc );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader.add_private_type
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_add_private_type( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE type_re, proc, priv_types;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1&", &type_re, &proc);
|
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
priv_types = rb_attr_get(self, s_private_types);
|
2003-05-10 15:55:18 -04:00
|
|
|
rb_hash_aset( priv_types, type_re, proc );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader#detect
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_loader_detect_implicit( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
char *type_id;
|
|
|
|
|
|
|
|
if ( TYPE(val) == T_STRING )
|
|
|
|
{
|
|
|
|
type_id = syck_match_implicit( RSTRING(val)->ptr, RSTRING(val)->len );
|
|
|
|
return rb_str_new2( type_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_str_new2( "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iterator to search a type hash for a match.
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
static VALUE
|
2003-05-10 15:55:18 -04:00
|
|
|
transfer_find_i(entry, col)
|
|
|
|
VALUE entry, col;
|
|
|
|
{
|
|
|
|
VALUE key = rb_ary_entry( entry, 0 );
|
|
|
|
VALUE tid = rb_ary_entry( col, 0 );
|
2004-05-14 23:11:28 -04:00
|
|
|
if ( rb_respond_to( key, s_match ) )
|
|
|
|
{
|
|
|
|
VALUE match = rb_funcall( key, rb_intern("match"), 1, tid );
|
|
|
|
if ( ! NIL_P( match ) )
|
|
|
|
{
|
|
|
|
rb_ary_push( col, rb_ary_entry( entry, 1 ) );
|
|
|
|
rb_iter_break();
|
|
|
|
}
|
|
|
|
}
|
2003-05-10 15:55:18 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Loader#transfer
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
|
|
|
syck_loader_transfer( self, type, val )
|
|
|
|
VALUE self, type, val;
|
|
|
|
{
|
|
|
|
char *taguri = NULL;
|
|
|
|
|
|
|
|
if (NIL_P(type) || !RSTRING(type)->ptr || RSTRING(type)->len == 0)
|
|
|
|
{
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Empty transfer, detect type
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
if ( TYPE(val) == T_STRING )
|
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
StringValue(val);
|
2003-05-10 15:55:18 -04:00
|
|
|
taguri = syck_match_implicit( RSTRING(val)->ptr, RSTRING(val)->len );
|
|
|
|
taguri = syck_taguri( YAML_DOMAIN, taguri, strlen( taguri ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
taguri = syck_type_id_to_uri( RSTRING(type)->ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( taguri != NULL )
|
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
int transferred = 0;
|
2003-07-28 15:27:42 -04:00
|
|
|
VALUE scheme, name, type_hash, domain = Qnil, type_proc = Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE type_uri = rb_str_new2( taguri );
|
2003-10-14 19:15:19 -04:00
|
|
|
VALUE str_taguri = rb_str_new2("tag");
|
2003-05-11 02:32:13 -04:00
|
|
|
VALUE str_xprivate = rb_str_new2("x-private");
|
2003-08-21 17:16:58 -04:00
|
|
|
VALUE str_yaml_domain = rb_str_new2(YAML_DOMAIN);
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE parts = rb_str_split( type_uri, ":" );
|
|
|
|
|
|
|
|
scheme = rb_ary_shift( parts );
|
|
|
|
|
|
|
|
if ( rb_str_cmp( scheme, str_xprivate ) == 0 )
|
|
|
|
{
|
|
|
|
name = rb_ary_join( parts, rb_str_new2( ":" ) );
|
2004-03-03 18:08:45 -05:00
|
|
|
type_hash = rb_attr_get(self, s_private_types);
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
else if ( rb_str_cmp( scheme, str_taguri ) == 0 )
|
|
|
|
{
|
2003-06-17 12:55:54 -04:00
|
|
|
domain = rb_ary_shift( parts );
|
2003-05-10 15:55:18 -04:00
|
|
|
name = rb_ary_join( parts, rb_str_new2( ":" ) );
|
2004-03-03 18:08:45 -05:00
|
|
|
type_hash = rb_attr_get(self, s_families);
|
2003-05-10 15:55:18 -04:00
|
|
|
type_hash = rb_hash_aref(type_hash, domain);
|
2003-08-21 17:16:58 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Route yaml.org types through the transfer
|
|
|
|
* method here in this extension
|
|
|
|
*/
|
|
|
|
if ( rb_str_cmp( domain, str_yaml_domain ) == 0 )
|
|
|
|
{
|
|
|
|
SyckNode *n = rb_new_syck_node(val, name);
|
|
|
|
if ( n != NULL )
|
|
|
|
{
|
|
|
|
transferred = yaml_org_handler(n, &val);
|
|
|
|
S_FREE( n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "invalid typing scheme: %s given",
|
|
|
|
scheme);
|
|
|
|
}
|
|
|
|
|
2003-08-21 17:16:58 -04:00
|
|
|
if ( ! transferred )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
if ( rb_obj_is_instance_of( type_hash, rb_cHash ) )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2003-08-21 17:16:58 -04:00
|
|
|
type_proc = rb_hash_aref( type_hash, name );
|
|
|
|
if ( NIL_P( type_proc ) )
|
|
|
|
{
|
|
|
|
VALUE col = rb_ary_new();
|
|
|
|
rb_ary_push( col, name );
|
|
|
|
rb_iterate(rb_each, type_hash, transfer_find_i, col );
|
|
|
|
name = rb_ary_shift( col );
|
|
|
|
type_proc = rb_ary_shift( col );
|
|
|
|
}
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-08-21 17:16:58 -04:00
|
|
|
if ( rb_respond_to( type_proc, s_call ) )
|
|
|
|
{
|
|
|
|
val = rb_funcall(type_proc, s_call, 2, type_uri, val);
|
|
|
|
}
|
|
|
|
else if ( rb_str_cmp( scheme, str_xprivate ) == 0 )
|
|
|
|
{
|
|
|
|
val = rb_funcall(cPrivateType, s_new, 2, name, val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = rb_funcall(cDomainType, s_new, 3, domain, name, val);
|
|
|
|
}
|
|
|
|
transferred = 1;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-06-17 12:55:54 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::BadAlias.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_badalias_initialize( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set( self, s_name, val );
|
2003-06-17 12:55:54 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2004-05-06 02:29:56 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::BadAlias.<=>
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_badalias_cmp( alias1, alias2 )
|
|
|
|
VALUE alias1, alias2;
|
|
|
|
{
|
|
|
|
VALUE str1 = rb_ivar_get( alias1, s_name );
|
|
|
|
VALUE str2 = rb_ivar_get( alias2, s_name );
|
|
|
|
VALUE val = rb_funcall( str1, s_cmp, 1, str2 );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-06-17 12:55:54 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::DomainType.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_domaintype_initialize( self, domain, type_id, val )
|
|
|
|
VALUE self, type_id, val;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set( self, s_domain, domain );
|
|
|
|
rb_ivar_set( self, s_type_id, type_id );
|
|
|
|
rb_ivar_set( self, s_value, val );
|
2003-06-17 12:55:54 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::PrivateType.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_privatetype_initialize( self, type_id, val )
|
|
|
|
VALUE self, type_id, val;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set( self, s_type_id, type_id );
|
|
|
|
rb_ivar_set( self, s_value, val );
|
2003-06-17 12:55:54 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Node.initialize
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_node_initialize( self, type_id, val )
|
|
|
|
VALUE self, type_id, val;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set( self, s_type_id, type_id );
|
|
|
|
rb_ivar_set( self, s_value, val );
|
2003-05-13 02:34:18 -04:00
|
|
|
return self;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_node_thash( entry, t )
|
|
|
|
VALUE entry, t;
|
|
|
|
{
|
|
|
|
VALUE key, val;
|
|
|
|
key = rb_ary_entry( entry, 0 );
|
|
|
|
val = syck_node_transform( rb_ary_entry( rb_ary_entry( entry, 1 ), 1 ) );
|
|
|
|
rb_hash_aset( t, key, val );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_node_ahash( entry, t )
|
|
|
|
VALUE entry, t;
|
|
|
|
{
|
|
|
|
VALUE val = syck_node_transform( entry );
|
|
|
|
rb_ary_push( t, val );
|
2003-05-13 02:34:18 -04:00
|
|
|
return Qnil;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Node.transform
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2003-05-09 17:25:50 -04:00
|
|
|
syck_node_transform( self )
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE t = Qnil;
|
2004-03-03 18:08:45 -05:00
|
|
|
VALUE type_id = rb_attr_get( self, s_type_id );
|
|
|
|
VALUE val = rb_attr_get( self, s_value );
|
2003-05-09 17:25:50 -04:00
|
|
|
if ( rb_obj_is_instance_of( val, rb_cHash ) )
|
|
|
|
{
|
|
|
|
t = rb_hash_new();
|
|
|
|
rb_iterate( rb_each, val, syck_node_thash, t );
|
|
|
|
}
|
|
|
|
else if ( rb_obj_is_instance_of( val, rb_cArray ) )
|
|
|
|
{
|
|
|
|
t = rb_ary_new();
|
|
|
|
rb_iterate( rb_each, val, syck_node_ahash, t );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t = val;
|
|
|
|
}
|
2003-06-17 12:55:54 -04:00
|
|
|
return rb_funcall( oDefaultLoader, s_transfer, 2, type_id, t );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Handle output from the emitter
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rb_syck_output_handler( emitter, str, len )
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
char *str;
|
|
|
|
long len;
|
|
|
|
{
|
2003-07-24 12:30:43 -04:00
|
|
|
VALUE dest = (VALUE)emitter->bonus;
|
2003-08-21 17:16:58 -04:00
|
|
|
if ( rb_respond_to( dest, s_to_str ) ) {
|
2003-07-24 12:30:43 -04:00
|
|
|
rb_str_cat( dest, str, len );
|
|
|
|
} else {
|
|
|
|
rb_io_write( dest, rb_str_new( str, len ) );
|
|
|
|
}
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark emitter values.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
syck_mark_emitter(emitter)
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
{
|
2003-07-24 12:30:43 -04:00
|
|
|
rb_gc_mark(emitter->ignore_id);
|
2003-07-14 16:35:07 -04:00
|
|
|
if ( emitter->bonus != NULL )
|
|
|
|
{
|
|
|
|
rb_gc_mark( (VALUE)emitter->bonus );
|
|
|
|
}
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.new
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_new(argc, argv, class)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE class;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2004-05-14 23:11:28 -04:00
|
|
|
VALUE pobj, options, init_argv[1];
|
2003-07-11 18:52:14 -04:00
|
|
|
SyckEmitter *emitter = syck_new_emitter();
|
2004-06-29 13:34:08 -04:00
|
|
|
rb_scan_args(argc, argv, "01", &options);
|
|
|
|
|
|
|
|
pobj = Data_Wrap_Struct( class, syck_mark_emitter, syck_free_emitter, emitter );
|
2003-07-11 18:52:14 -04:00
|
|
|
syck_emitter_ignore_id( emitter, Qnil );
|
|
|
|
syck_emitter_handler( emitter, rb_syck_output_handler );
|
|
|
|
emitter->bonus = (void *)rb_str_new2( "" );
|
|
|
|
|
|
|
|
if ( ! rb_obj_is_instance_of( options, rb_cHash ) )
|
|
|
|
{
|
|
|
|
options = rb_hash_new();
|
|
|
|
}
|
2004-05-14 23:11:28 -04:00
|
|
|
init_argv[0] = options;
|
|
|
|
rb_obj_call_init(pobj, 1, init_argv);
|
|
|
|
return pobj;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.initialize( options )
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
syck_emitter_initialize( self, options )
|
|
|
|
VALUE self, options;
|
|
|
|
{
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(self, s_options, options);
|
2004-05-14 23:11:28 -04:00
|
|
|
return self;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.level
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_level_m( self )
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2003-07-11 18:52:14 -04:00
|
|
|
return LONG2NUM( emitter->level );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.flush
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_flush_m( self )
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2003-07-24 12:30:43 -04:00
|
|
|
syck_emitter_flush( emitter, 0 );
|
2003-07-11 18:52:14 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.write( str )
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_write_m( self, str )
|
2003-08-01 06:57:32 -04:00
|
|
|
VALUE self, str;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2004-04-26 19:24:46 -04:00
|
|
|
StringValue(str);
|
2003-07-11 18:52:14 -04:00
|
|
|
syck_emitter_write( emitter, RSTRING(str)->ptr, RSTRING(str)->len );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.simple( str )
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_simple_write( self, str )
|
2003-08-01 06:57:32 -04:00
|
|
|
VALUE self, str;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2004-04-26 19:24:46 -04:00
|
|
|
StringValue(str);
|
2003-07-11 18:52:14 -04:00
|
|
|
syck_emitter_simple( emitter, RSTRING(str)->ptr, RSTRING(str)->len );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.start_object( object_id )
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_emitter_start_object( self, oid )
|
|
|
|
VALUE self, oid;
|
|
|
|
{
|
|
|
|
char *anchor_name;
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2003-07-11 18:52:14 -04:00
|
|
|
anchor_name = syck_emitter_start_obj( emitter, oid );
|
|
|
|
|
|
|
|
if ( anchor_name == NULL )
|
|
|
|
{
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_str_new2( anchor_name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-16 20:51:40 -05:00
|
|
|
* YAML::Syck::Emitter.end_object
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2004-02-16 20:51:39 -05:00
|
|
|
syck_emitter_end_object( self )
|
|
|
|
VALUE self;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2003-07-11 18:52:14 -04:00
|
|
|
syck_emitter_end_obj( emitter );
|
|
|
|
|
|
|
|
if ( emitter->level < 0 )
|
|
|
|
{
|
2003-07-24 12:30:43 -04:00
|
|
|
syck_emitter_flush( emitter, 0 );
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
return (VALUE)emitter->bonus;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* Initialize Syck extension
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
void
|
|
|
|
Init_syck()
|
|
|
|
{
|
|
|
|
VALUE rb_yaml = rb_define_module( "YAML" );
|
|
|
|
VALUE rb_syck = rb_define_module_under( rb_yaml, "Syck" );
|
|
|
|
rb_define_const( rb_syck, "VERSION", rb_str_new2( SYCK_VERSION ) );
|
2003-10-17 16:53:39 -04:00
|
|
|
rb_define_module_function( rb_syck, "compile", rb_syck_compile, 1 );
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
/*
|
|
|
|
* Global symbols
|
|
|
|
*/
|
2003-06-17 12:55:54 -04:00
|
|
|
s_new = rb_intern("new");
|
2003-05-09 17:25:50 -04:00
|
|
|
s_utc = rb_intern("utc");
|
2003-05-15 03:38:10 -04:00
|
|
|
s_at = rb_intern("at");
|
|
|
|
s_to_f = rb_intern("to_f");
|
2003-11-25 14:51:38 -05:00
|
|
|
s_to_i = rb_intern("to_i");
|
2003-05-09 17:25:50 -04:00
|
|
|
s_read = rb_intern("read");
|
2003-10-03 15:51:25 -04:00
|
|
|
s_anchors = rb_intern("anchors");
|
2003-05-09 17:25:50 -04:00
|
|
|
s_binmode = rb_intern("binmode");
|
2003-06-17 12:55:54 -04:00
|
|
|
s_transfer = rb_intern("transfer");
|
|
|
|
s_call = rb_intern("call");
|
2004-05-06 02:29:56 -04:00
|
|
|
s_cmp = rb_intern("<=>");
|
2004-05-14 23:11:28 -04:00
|
|
|
s_update = rb_intern("update");
|
|
|
|
s_dup = rb_intern("dup");
|
2003-10-03 15:51:25 -04:00
|
|
|
s_default_set = rb_intern("default=");
|
2004-05-14 23:11:28 -04:00
|
|
|
s_match = rb_intern("match");
|
|
|
|
s_keys = rb_intern("keys");
|
|
|
|
s_to_str = rb_intern("to_str");
|
|
|
|
s_tr_bang = rb_intern("tr!");
|
2003-08-21 17:16:58 -04:00
|
|
|
s_unpack = rb_intern("unpack");
|
2003-06-17 21:06:00 -04:00
|
|
|
|
2004-03-03 18:08:45 -05:00
|
|
|
s_anchors = rb_intern("@anchors");
|
|
|
|
s_domain = rb_intern("@domain");
|
|
|
|
s_families = rb_intern("@families");
|
|
|
|
s_kind = rb_intern("@kind");
|
|
|
|
s_name = rb_intern("@name");
|
|
|
|
s_options = rb_intern("@options");
|
|
|
|
s_private_types = rb_intern("@private_types");
|
|
|
|
s_type_id = rb_intern("@type_id");
|
|
|
|
s_value = rb_intern("@value");
|
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
sym_model = ID2SYM(rb_intern("Model"));
|
|
|
|
sym_generic = ID2SYM(rb_intern("Generic"));
|
|
|
|
sym_input = ID2SYM(rb_intern("Input"));
|
|
|
|
sym_bytecode = ID2SYM(rb_intern("Bytecode"));
|
2003-05-09 17:25:50 -04:00
|
|
|
sym_map = ID2SYM(rb_intern("map"));
|
|
|
|
sym_scalar = ID2SYM(rb_intern("scalar"));
|
|
|
|
sym_seq = ID2SYM(rb_intern("seq"));
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Loader class
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
cLoader = rb_define_class_under( rb_syck, "Loader", rb_cObject );
|
|
|
|
rb_define_attr( cLoader, "families", 1, 1 );
|
|
|
|
rb_define_attr( cLoader, "private_types", 1, 1 );
|
2003-10-03 15:51:25 -04:00
|
|
|
rb_define_attr( cLoader, "anchors", 1, 1 );
|
2003-05-10 15:55:18 -04:00
|
|
|
rb_define_method( cLoader, "initialize", syck_loader_initialize, 0 );
|
|
|
|
rb_define_method( cLoader, "add_domain_type", syck_loader_add_domain_type, -1 );
|
|
|
|
rb_define_method( cLoader, "add_builtin_type", syck_loader_add_builtin_type, -1 );
|
|
|
|
rb_define_method( cLoader, "add_ruby_type", syck_loader_add_ruby_type, -1 );
|
|
|
|
rb_define_method( cLoader, "add_private_type", syck_loader_add_private_type, -1 );
|
2003-11-25 14:51:38 -05:00
|
|
|
rb_define_method( cLoader, "bufsize=", syck_parser_bufsize_set, 1 );
|
|
|
|
rb_define_method( cLoader, "bufsize", syck_parser_bufsize_get, 0 );
|
2003-05-13 02:34:18 -04:00
|
|
|
rb_define_method( cLoader, "detect_implicit", syck_loader_detect_implicit, 1 );
|
2003-05-10 15:55:18 -04:00
|
|
|
rb_define_method( cLoader, "transfer", syck_loader_transfer, 2 );
|
|
|
|
|
|
|
|
oDefaultLoader = rb_funcall( cLoader, rb_intern( "new" ), 0 );
|
|
|
|
rb_define_const( rb_syck, "DefaultLoader", oDefaultLoader );
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Parser class
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
cParser = rb_define_class_under( rb_syck, "Parser", rb_cObject );
|
2003-05-09 17:25:50 -04:00
|
|
|
rb_define_attr( cParser, "options", 1, 1 );
|
2004-05-14 23:11:28 -04:00
|
|
|
rb_define_singleton_method( cParser, "new", syck_parser_new, -1 );
|
2003-05-09 17:25:50 -04:00
|
|
|
rb_define_method(cParser, "initialize", syck_parser_initialize, 1);
|
|
|
|
rb_define_method(cParser, "load", syck_parser_load, -1);
|
|
|
|
rb_define_method(cParser, "load_documents", syck_parser_load_documents, -1);
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Node class
|
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
cNode = rb_define_class_under( rb_syck, "Node", rb_cObject );
|
|
|
|
rb_define_attr( cNode, "kind", 1, 1 );
|
|
|
|
rb_define_attr( cNode, "type_id", 1, 1 );
|
|
|
|
rb_define_attr( cNode, "value", 1, 1 );
|
|
|
|
rb_define_attr( cNode, "anchor", 1, 1 );
|
|
|
|
rb_define_method( cNode, "initialize", syck_node_initialize, 2);
|
|
|
|
rb_define_method( cNode, "transform", syck_node_transform, 0);
|
2003-06-17 12:55:54 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::PrivateType class
|
|
|
|
*/
|
2003-06-17 12:55:54 -04:00
|
|
|
cPrivateType = rb_define_class_under( rb_syck, "PrivateType", rb_cObject );
|
|
|
|
rb_define_attr( cPrivateType, "type_id", 1, 1 );
|
|
|
|
rb_define_attr( cPrivateType, "value", 1, 1 );
|
|
|
|
rb_define_method( cPrivateType, "initialize", syck_privatetype_initialize, 2);
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::DomainType class
|
|
|
|
*/
|
2003-06-17 12:55:54 -04:00
|
|
|
cDomainType = rb_define_class_under( rb_syck, "DomainType", rb_cObject );
|
|
|
|
rb_define_attr( cDomainType, "domain", 1, 1 );
|
|
|
|
rb_define_attr( cDomainType, "type_id", 1, 1 );
|
|
|
|
rb_define_attr( cDomainType, "value", 1, 1 );
|
|
|
|
rb_define_method( cDomainType, "initialize", syck_domaintype_initialize, 3);
|
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::BadAlias class
|
|
|
|
*/
|
2003-06-17 12:55:54 -04:00
|
|
|
cBadAlias = rb_define_class_under( rb_syck, "BadAlias", rb_cObject );
|
|
|
|
rb_define_attr( cBadAlias, "name", 1, 1 );
|
|
|
|
rb_define_method( cBadAlias, "initialize", syck_badalias_initialize, 1);
|
2004-05-06 02:29:56 -04:00
|
|
|
rb_define_method( cBadAlias, "<=>", syck_badalias_cmp, 1);
|
|
|
|
rb_include_module( cBadAlias, rb_const_get( rb_cObject, rb_intern("Comparable") ) );
|
2003-06-17 21:06:00 -04:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::MergeKey class
|
|
|
|
*/
|
|
|
|
cMergeKey = rb_define_class_under( rb_syck, "MergeKey", rb_cObject );
|
2003-07-11 18:52:14 -04:00
|
|
|
|
2004-05-14 23:11:28 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::DefaultKey class
|
|
|
|
*/
|
|
|
|
cDefaultKey = rb_define_class_under( rb_syck, "DefaultKey", rb_cObject );
|
2003-10-03 15:51:25 -04:00
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Emitter class
|
|
|
|
*/
|
|
|
|
cEmitter = rb_define_class_under( rb_syck, "Emitter", rb_cObject );
|
2004-05-14 23:11:28 -04:00
|
|
|
rb_define_singleton_method( cEmitter, "new", syck_emitter_new, -1 );
|
2003-07-11 18:52:14 -04:00
|
|
|
rb_define_method( cEmitter, "initialize", syck_emitter_initialize, 1 );
|
|
|
|
rb_define_method( cEmitter, "level", syck_emitter_level_m, 0 );
|
|
|
|
rb_define_method( cEmitter, "write", syck_emitter_write_m, 1 );
|
|
|
|
rb_define_method( cEmitter, "<<", syck_emitter_write_m, 1 );
|
|
|
|
rb_define_method( cEmitter, "simple", syck_emitter_simple_write, 1 );
|
|
|
|
rb_define_method( cEmitter, "flush", syck_emitter_flush_m, 0 );
|
|
|
|
rb_define_method( cEmitter, "start_object", syck_emitter_start_object, 1 );
|
|
|
|
rb_define_method( cEmitter, "end_object", syck_emitter_end_object, 0 );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|