2005-04-30 08:50:34 -04:00
|
|
|
/* -*- indent-tabs-mode: nil -*- */
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
|
|
|
* rubyext.c
|
|
|
|
*
|
|
|
|
* $Author$
|
|
|
|
* $Date$
|
|
|
|
*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Copyright (C) 2003-2005 why the lucky stiff
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
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
|
2005-09-12 23:58:33 -04:00
|
|
|
struct {
|
|
|
|
unsigned long flags; /* always 0 for freed obj */
|
|
|
|
struct RVALUE *next;
|
|
|
|
} free;
|
2004-05-14 23:11:28 -04:00
|
|
|
#endif
|
2005-09-12 23:58:33 -04:00
|
|
|
struct RBasic basic;
|
|
|
|
struct RObject object;
|
|
|
|
struct RClass klass;
|
|
|
|
/*struct RFloat flonum;*/
|
|
|
|
/*struct RString string;*/
|
|
|
|
struct RArray array;
|
|
|
|
/*struct RRegexp regexp;*/
|
|
|
|
struct RHash hash;
|
|
|
|
/*struct RData data;*/
|
|
|
|
struct RStruct rstruct;
|
|
|
|
/*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
|
|
|
|
*/
|
2005-09-12 23:58:33 -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_haskey, s_match, s_keys, s_unpack, s_tr_bang, s_default_set, s_tag_read_class, s_tag_subclasses, s_resolver, s_push, s_emitter, s_level, s_detect_implicit, s_node_import, s_out, s_input, s_intern, s_transform, s_yaml_new, s_yaml_initialize, s_node_export, s_to_yaml, s_write, s_set_resolver;
|
|
|
|
static ID s_tags, s_domain, s_kind, s_name, s_options, s_type_id, s_type_id_set, s_style, s_style_set, s_value, s_value_set;
|
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;
|
2005-09-12 23:58:33 -04:00
|
|
|
static VALUE sym_1quote, sym_2quote, sym_fold, sym_literal, sym_plain, sym_inline;
|
|
|
|
static VALUE cDate, cNode, cMap, cSeq, cScalar, cOut, cParser, cResolver, cPrivateType, cDomainType, cYObject, cBadAlias, cDefaultKey, cMergeKey, cEmitter;
|
|
|
|
static VALUE oDefaultResolver, oGenericResolver;
|
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_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));
|
2005-09-12 23:58:33 -04:00
|
|
|
void rb_syck_emitter_handler _((SyckEmitter *, st_data_t));
|
2003-12-10 21:39:59 -05:00
|
|
|
int syck_parser_assign_io _((SyckParser *, VALUE));
|
2005-09-27 18:57:53 -04:00
|
|
|
VALUE syck_scalar_alloc _((VALUE class));
|
|
|
|
VALUE syck_seq_alloc _((VALUE class));
|
|
|
|
VALUE syck_map_alloc _((VALUE class));
|
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;
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE resolver;
|
2003-08-19 12:38:02 -04:00
|
|
|
int taint;
|
2003-06-05 01:40:03 -04:00
|
|
|
};
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
struct emitter_xtra {
|
|
|
|
VALUE oid;
|
|
|
|
VALUE data;
|
|
|
|
VALUE port;
|
|
|
|
};
|
|
|
|
|
2003-10-17 16:53:39 -04:00
|
|
|
/*
|
|
|
|
* Convert YAML to bytecode
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_syck_compile(self, port)
|
2004-12-23 12:23:02 -05:00
|
|
|
VALUE self, port;
|
2003-10-17 16:53:39 -04:00
|
|
|
{
|
|
|
|
SYMID oid;
|
|
|
|
int taint;
|
|
|
|
char *ret;
|
|
|
|
VALUE bc;
|
2005-09-12 23:58:33 -04:00
|
|
|
bytestring_t *sav;
|
2003-10-17 16:53:39 -04:00
|
|
|
|
|
|
|
SyckParser *parser = syck_new_parser();
|
2004-12-23 12:23:02 -05: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
|
|
|
{
|
2004-12-15 11:01:11 -05:00
|
|
|
StringValue(str2);
|
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-12-23 12:23:02 -05:00
|
|
|
SyckParser *parser;
|
|
|
|
VALUE port;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2003-08-19 12:38:02 -04:00
|
|
|
int taint = Qtrue;
|
2004-12-23 12:23:02 -05:00
|
|
|
VALUE tmp;
|
|
|
|
if (!NIL_P(tmp = rb_check_string_type(port))) {
|
|
|
|
taint = OBJ_TAINTED(port); /* original taintedness */
|
|
|
|
port = tmp;
|
|
|
|
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 );
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( NIL_P( val ) )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
|
|
|
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
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_syck_mktime(str, len)
|
2003-05-09 17:25:50 -04:00
|
|
|
char *str;
|
2005-09-12 23:58:33 -04:00
|
|
|
long len;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
|
|
|
VALUE time;
|
|
|
|
char *ptr = str;
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE year = INT2FIX(0);
|
|
|
|
VALUE mon = INT2FIX(0);
|
|
|
|
VALUE day = INT2FIX(0);
|
|
|
|
VALUE hour = INT2FIX(0);
|
|
|
|
VALUE min = INT2FIX(0);
|
|
|
|
VALUE sec = INT2FIX(0);
|
2004-01-28 11:59:00 -05:00
|
|
|
long usec;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Year*/
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > 0 ) {
|
|
|
|
year = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Month*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 4;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > ptr - str ) {
|
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
|
|
|
mon = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Day*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > ptr - str ) {
|
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
|
|
|
day = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Hour*/
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > ptr - str ) {
|
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
|
|
|
hour = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Minute */
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > ptr - str ) {
|
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
|
|
|
min = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Second */
|
2003-05-09 17:25:50 -04:00
|
|
|
ptr += 2;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ptr[0] != '\0' && len > ptr - str ) {
|
|
|
|
while ( !ISDIGIT( *ptr ) ) ptr++;
|
|
|
|
sec = INT2FIX(strtol(ptr, NULL, 10));
|
|
|
|
}
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/* Millisecond */
|
2003-05-15 03:38:10 -04:00
|
|
|
ptr += 2;
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( len > ptr - str && *ptr == '.' )
|
2003-05-30 19:09:54 -04:00
|
|
|
{
|
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);
|
2005-09-12 23:58:33 -04:00
|
|
|
S_FREE(padded);
|
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*/
|
2005-09-12 23:58:33 -04:00
|
|
|
while ( len > ptr - str && *ptr != 'Z' && *ptr != '+' && *ptr != '-' && *ptr != '\0' ) ptr++;
|
|
|
|
if ( len > ptr - str && ( *ptr == '-' || *ptr == '+' ) )
|
2003-05-15 03:38:10 -04:00
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
time_t tz_offset = strtol(ptr, NULL, 10) * 3600;
|
2004-01-28 11:59:00 -05:00
|
|
|
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-12-23 12:23:02 -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-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE tmp;
|
|
|
|
if ( !NIL_P(tmp = rb_check_convert_type(entry, T_HASH, "Hash", "to_hash")) )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
entry = tmp;
|
2004-12-23 12:23:02 -05:00
|
|
|
rb_funcall( hsh, s_update, 1, entry );
|
|
|
|
}
|
2003-06-17 21:06:00 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-08-21 17:16:58 -04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( type_id != NULL && strncmp( type_id, "tag:yaml.org,2002:", 18 ) == 0 )
|
|
|
|
{
|
|
|
|
type_id += 18;
|
|
|
|
}
|
|
|
|
|
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--;
|
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( colon >= ptr && *colon == ':' ) *colon = '\0';
|
2004-01-12 17:55:09 -05:00
|
|
|
|
|
|
|
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--;
|
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( colon >= ptr && *colon == ':' ) *colon = '\0';
|
2004-08-07 11:34:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_syck_mktime( n->data.str->ptr, n->data.str->len );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
else if ( strcmp( type_id, "timestamp#spaced" ) == 0 )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_syck_mktime( n->data.str->ptr, n->data.str->len );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
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
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_syck_mktime( n->data.str->ptr, n->data.str->len );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2004-12-23 12:23:02 -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 &&
|
2005-09-12 23:58:33 -04:00
|
|
|
n->data.str->len > 1 &&
|
2004-05-14 23:11:28 -04:00
|
|
|
strncmp( n->data.str->ptr, ":", 1 ) == 0 )
|
2004-01-07 18:46:18 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_funcall( oDefaultResolver, s_transfer, 2,
|
|
|
|
rb_str_new2( "tag:ruby.yaml.org,2002:sym" ),
|
2004-05-06 02:29:56 -04:00
|
|
|
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-12-23 12:23:02 -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 ) )
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE tmp;
|
|
|
|
if ( !NIL_P(tmp = rb_check_convert_type(v, T_HASH, "Hash", "to_hash")) )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE dup = rb_funcall( tmp, s_dup, 0 );
|
2004-12-23 12:23:02 -05:00
|
|
|
rb_funcall( dup, s_update, 1, obj );
|
|
|
|
obj = dup;
|
|
|
|
skip_aset = 1;
|
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
else if ( !NIL_P(tmp = rb_check_array_type(v)) )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE end = rb_ary_pop( tmp );
|
|
|
|
VALUE tmph = rb_check_convert_type(end, T_HASH, "Hash", "to_hash");
|
|
|
|
if ( !NIL_P(tmph) )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE dup = rb_funcall( tmph, s_dup, 0 );
|
|
|
|
tmp = rb_ary_reverse( tmp );
|
|
|
|
rb_ary_push( tmp, obj );
|
|
|
|
rb_iterate( rb_each, tmp, syck_merge_i, dup );
|
2004-12-23 12:23:02 -05:00
|
|
|
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-12-23 12:23:02 -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;
|
2005-09-12 23:58:33 -04:00
|
|
|
struct parser_xtra *bonus = (struct parser_xtra *)p->bonus;
|
|
|
|
VALUE resolver = bonus->resolver;
|
|
|
|
if ( NIL_P( resolver ) )
|
|
|
|
{
|
|
|
|
resolver = oDefaultResolver;
|
|
|
|
}
|
2003-08-21 17:16:58 -04:00
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Create node,
|
2003-08-21 17:16:58 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_funcall( resolver, s_node_import, 1, Data_Wrap_Struct( cNode, NULL, NULL, n ) );
|
2003-08-21 17:16:58 -04:00
|
|
|
|
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-08-19 12:38:02 -04:00
|
|
|
if ( bonus->taint) OBJ_TAINT( obj );
|
2004-12-23 12:23:02 -05: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'",
|
2005-09-12 23:58:33 -04:00
|
|
|
msg,
|
|
|
|
p->linect,
|
|
|
|
p->cursor - p->lineptr,
|
|
|
|
p->lineptr);
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
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
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_set_model( p, input, model )
|
|
|
|
VALUE p, input, model;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
SyckParser *parser;
|
|
|
|
Data_Get_Struct(p, SyckParser, parser);
|
|
|
|
syck_parser_handler( parser, rb_syck_load_handler );
|
|
|
|
/* WARN: gonna be obsoleted soon!! */
|
2004-12-23 12:23:02 -05:00
|
|
|
if ( model == sym_generic )
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_funcall( p, s_set_resolver, 1, oGenericResolver );
|
2004-12-23 12:23:02 -05:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_parser_implicit_typing( parser, 1 );
|
|
|
|
syck_parser_taguri_expansion( parser, 1 );
|
|
|
|
|
|
|
|
if ( NIL_P( input ) )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
input = rb_ivar_get( p, s_input );
|
2004-12-23 12:23:02 -05:00
|
|
|
}
|
2003-10-14 19:15:19 -04:00
|
|
|
if ( input == sym_bytecode )
|
|
|
|
{
|
|
|
|
syck_parser_set_input_type( parser, syck_bytecode_utf8 );
|
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
syck_parser_set_input_type( parser, syck_yaml_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;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
struct parser_xtra *bonus;
|
2003-06-05 00:43:05 -04:00
|
|
|
rb_gc_mark(parser->root);
|
|
|
|
rb_gc_mark(parser->root_on_error);
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( parser->bonus != NULL )
|
|
|
|
{
|
|
|
|
bonus = (struct parser_xtra *)parser->bonus;
|
|
|
|
rb_gc_mark( bonus->data );
|
|
|
|
rb_gc_mark( bonus->proc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the parser and any bonus attachment.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rb_syck_free_parser(p)
|
|
|
|
SyckParser *p;
|
|
|
|
{
|
|
|
|
struct parser_xtra *bonus = (struct parser_xtra *)p->bonus;
|
|
|
|
if ( bonus != NULL ) S_FREE( bonus );
|
|
|
|
syck_free_parser(p);
|
2003-06-05 00:43:05 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Parser.allocate
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2005-02-22 09:51:53 -05:00
|
|
|
VALUE syck_parser_s_alloc _((VALUE));
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE
|
2004-12-23 12:23:02 -05:00
|
|
|
syck_parser_s_alloc(class)
|
|
|
|
VALUE class;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
VALUE pobj;
|
2003-05-09 17:25:50 -04:00
|
|
|
SyckParser *parser = syck_new_parser();
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
pobj = Data_Wrap_Struct( class, syck_mark_parser, rb_syck_free_parser, parser );
|
|
|
|
|
2003-05-15 17:46:47 -04:00
|
|
|
syck_parser_set_root_on_error( parser, Qnil );
|
|
|
|
|
2004-12-23 12:23:02 -05:00
|
|
|
return pobj;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Parser.initialize( resolver, options )
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-09 17:25:50 -04:00
|
|
|
static VALUE
|
2004-12-23 12:23:02 -05:00
|
|
|
syck_parser_initialize(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
VALUE options;
|
|
|
|
if (rb_scan_args(argc, argv, "01", &options) == 0)
|
|
|
|
{
|
|
|
|
options = rb_hash_new();
|
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
else
|
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
Check_Type(options, T_HASH);
|
|
|
|
}
|
2004-03-03 18:08:45 -05:00
|
|
|
rb_ivar_set(self, s_options, options);
|
2004-12-23 12:23:02 -05: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-12-23 12:23:02 -05:00
|
|
|
SyckParser *parser;
|
2003-11-25 14:51:38 -05:00
|
|
|
|
|
|
|
if ( rb_respond_to( size, s_to_i ) ) {
|
2005-01-20 10:39:22 -05:00
|
|
|
int n = NUM2INT(rb_funcall(size, s_to_i, 0));
|
2004-12-23 12:23:02 -05:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2005-01-20 10:39:22 -05:00
|
|
|
parser->bufsize = n;
|
2003-11-25 14:51:38 -05:00
|
|
|
}
|
2004-12-23 12:23:02 -05: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-12-23 12:23:02 -05:00
|
|
|
SyckParser *parser;
|
2003-11-25 14:51:38 -05:00
|
|
|
|
2004-12-23 12:23:02 -05: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-12-23 12:23:02 -05: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-12-23 12:23:02 -05:00
|
|
|
SyckParser *parser;
|
2005-09-12 23:58:33 -04:00
|
|
|
struct parser_xtra *bonus = S_ALLOC_N( struct parser_xtra, 1 );
|
|
|
|
volatile VALUE hash; /* protect from GC */
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &port, &proc);
|
|
|
|
|
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-12-23 12:23:02 -05:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_set_model( self, input, model );
|
2004-12-23 12:23:02 -05:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
bonus->taint = syck_parser_assign_io(parser, port);
|
|
|
|
bonus->data = hash = rb_hash_new();
|
|
|
|
bonus->resolver = rb_attr_get( self, s_resolver );
|
|
|
|
if ( NIL_P( proc ) ) bonus->proc = 0;
|
|
|
|
else bonus->proc = proc;
|
|
|
|
|
|
|
|
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-12-23 12:23:02 -05: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-12-23 12:23:02 -05:00
|
|
|
SyckParser *parser;
|
2005-09-12 23:58:33 -04:00
|
|
|
struct parser_xtra *bonus = S_ALLOC_N( struct parser_xtra, 1 );
|
2003-06-05 01:40:03 -04:00
|
|
|
volatile VALUE hash;
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1&", &port, &proc);
|
|
|
|
|
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-12-23 12:23:02 -05:00
|
|
|
Data_Get_Struct(self, SyckParser, parser);
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_set_model( self, input, model );
|
|
|
|
|
|
|
|
bonus->taint = syck_parser_assign_io(parser, port);
|
|
|
|
bonus->resolver = rb_attr_get( self, s_resolver );
|
|
|
|
bonus->proc = 0;
|
|
|
|
parser->bonus = (void *)bonus;
|
2004-12-23 12:23:02 -05:00
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
while ( 1 )
|
2004-12-23 12:23:02 -05:00
|
|
|
{
|
2003-06-05 01:40:03 -04:00
|
|
|
/* Reset hash for tracking nodes */
|
2005-09-12 23:58:33 -04:00
|
|
|
bonus->data = hash = rb_hash_new();
|
2003-06-05 01:40:03 -04:00
|
|
|
|
|
|
|
/* Parse a document */
|
2004-12-23 12:23:02 -05: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-12-23 12:23:02 -05: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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Parser#set_resolver
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE
|
|
|
|
syck_parser_set_resolver( self, resolver )
|
|
|
|
VALUE self, resolver;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set( self, s_resolver, resolver );
|
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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver.initialize
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
static VALUE
|
|
|
|
syck_resolver_initialize( self )
|
|
|
|
VALUE self;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE tags = rb_hash_new();
|
|
|
|
rb_ivar_set(self, s_tags, rb_hash_new());
|
|
|
|
return self;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#add_type
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_resolver_add_type( self, taguri, cls )
|
|
|
|
VALUE self, taguri, cls;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE tags = rb_attr_get(self, s_tags);
|
|
|
|
rb_hash_aset( tags, taguri, cls );
|
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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#use_types_at
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_resolver_use_types_at( self, hsh )
|
|
|
|
VALUE self, hsh;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set( self, s_tags, hsh );
|
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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#detect_implicit
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_resolver_detect_implicit( self, val )
|
|
|
|
VALUE self, val;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
char *type_id;
|
|
|
|
return rb_str_new2( "" );
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#node_import
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_resolver_node_import( self, node )
|
|
|
|
VALUE self, node;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
SyckNode *n;
|
|
|
|
VALUE obj;
|
|
|
|
int i = 0;
|
|
|
|
Data_Get_Struct(node, SyckNode, n);
|
|
|
|
|
|
|
|
switch (n->kind)
|
|
|
|
{
|
|
|
|
case syck_str_kind:
|
|
|
|
obj = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
break;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
case syck_seq_kind:
|
|
|
|
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;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
case syck_map_kind:
|
|
|
|
obj = rb_hash_new();
|
|
|
|
for ( i = 0; i < n->data.pairs->idx; i++ )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( rb_obj_is_kind_of( k, cDefaultKey ) )
|
|
|
|
{
|
|
|
|
rb_funcall( obj, s_default_set, 1, v );
|
|
|
|
skip_aset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! skip_aset )
|
|
|
|
{
|
|
|
|
rb_hash_aset( obj, k, v );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( n->type_id != NULL )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( self, s_transfer, 2, rb_str_new2( n->type_id ), obj );
|
|
|
|
}
|
|
|
|
return obj;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Set instance variables
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_set_ivars( vars, obj )
|
|
|
|
VALUE vars, obj;
|
2003-05-13 02:34:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE ivname = rb_ary_entry( vars, 0 );
|
|
|
|
char *ivn;
|
|
|
|
StringValue( ivname );
|
|
|
|
ivn = S_ALLOC_N( char, RSTRING(ivname)->len + 2 );
|
|
|
|
ivn[0] = '@';
|
|
|
|
ivn[1] = '\0';
|
|
|
|
strncat( ivn, RSTRING(ivname)->ptr, RSTRING(ivname)->len );
|
|
|
|
rb_iv_set( obj, ivn, rb_ary_entry( vars, 1 ) );
|
|
|
|
S_FREE( ivn );
|
|
|
|
return Qnil;
|
2003-05-13 02:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#const_find
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE
|
|
|
|
syck_const_find( const_name )
|
|
|
|
VALUE const_name;
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE tclass = rb_cObject;
|
|
|
|
VALUE tparts = rb_str_split( const_name, "::" );
|
|
|
|
int i = 0;
|
|
|
|
for ( i = 0; i < RARRAY(tparts)->len; i++ ) {
|
|
|
|
VALUE tpart = rb_to_id( rb_ary_entry( tparts, i ) );
|
|
|
|
if ( !rb_const_defined( tclass, tpart ) ) return Qnil;
|
|
|
|
tclass = rb_const_get( tclass, tpart );
|
2004-12-23 12:23:02 -05:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
return tclass;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Resolver#transfer
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_resolver_transfer( self, type, val )
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE self, type, val;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
if (NIL_P(type) || RSTRING(StringValue(type))->len == 0)
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
type = rb_funcall( self, s_detect_implicit, 1, val );
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( ! (NIL_P(type) || RSTRING(StringValue(type))->len == 0) )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE str_xprivate = rb_str_new2( "x-private" );
|
|
|
|
VALUE colon = rb_str_new2( ":" );
|
|
|
|
VALUE tags = rb_attr_get(self, s_tags);
|
|
|
|
VALUE target_class = rb_hash_aref( tags, type );
|
|
|
|
VALUE subclass = target_class;
|
|
|
|
VALUE obj = Qnil;
|
2003-05-10 15:55:18 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* Should no tag match exactly, check for subclass format
|
|
|
|
*/
|
|
|
|
if ( NIL_P( target_class ) )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE subclass_parts = rb_ary_new();
|
|
|
|
VALUE parts = rb_str_split( type, ":" );
|
|
|
|
|
|
|
|
while ( RARRAY(parts)->len > 1 )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE partial;
|
|
|
|
rb_ary_unshift( subclass_parts, rb_ary_pop( parts ) );
|
|
|
|
partial = rb_ary_join( parts, colon );
|
|
|
|
target_class = rb_hash_aref( tags, partial );
|
|
|
|
if ( NIL_P( target_class ) )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_str_append( partial, colon );
|
|
|
|
target_class = rb_hash_aref( tags, partial );
|
2003-08-21 17:16:58 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* Possible subclass found, see if it supports subclassing
|
|
|
|
*/
|
|
|
|
if ( ! NIL_P( target_class ) )
|
|
|
|
{
|
|
|
|
subclass = target_class;
|
|
|
|
if ( RARRAY(subclass_parts)->len > 0 && rb_respond_to( target_class, s_tag_subclasses ) &&
|
|
|
|
RTEST( rb_funcall( target_class, s_tag_subclasses, 0 ) ) )
|
|
|
|
{
|
|
|
|
VALUE subclass_v;
|
|
|
|
subclass = rb_ary_join( subclass_parts, colon );
|
|
|
|
subclass = rb_funcall( target_class, s_tag_read_class, 1, subclass );
|
|
|
|
subclass_v = syck_const_find( subclass );
|
|
|
|
|
|
|
|
if ( subclass_v != Qnil )
|
|
|
|
{
|
|
|
|
subclass = subclass_v;
|
|
|
|
}
|
|
|
|
else if ( rb_cObject == target_class && subclass_v == Qnil )
|
|
|
|
{
|
|
|
|
target_class = cYObject;
|
|
|
|
type = subclass;
|
|
|
|
subclass = cYObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/* rb_raise(rb_eTypeError, "invalid typing scheme: %s given",
|
|
|
|
* scheme);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( rb_respond_to( target_class, s_call ) )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( target_class, s_call, 2, type, val );
|
|
|
|
}
|
|
|
|
else
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( rb_respond_to( target_class, s_yaml_new ) )
|
2003-05-10 15:55:18 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_funcall( target_class, s_yaml_new, 3, subclass, type, val );
|
|
|
|
}
|
|
|
|
else if ( !NIL_P( target_class ) )
|
|
|
|
{
|
|
|
|
obj = rb_obj_alloc( subclass );
|
|
|
|
if ( rb_respond_to( obj, s_yaml_initialize ) )
|
|
|
|
{
|
|
|
|
rb_funcall( obj, s_yaml_initialize, 2, type, val );
|
|
|
|
}
|
|
|
|
else if ( !NIL_P( obj ) && rb_obj_is_instance_of( val, rb_cHash ) )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_iterate( rb_each, val, syck_set_ivars, obj );
|
2003-08-21 17:16:58 -04:00
|
|
|
}
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE parts = rb_str_split( type, ":" );
|
|
|
|
VALUE scheme = rb_ary_shift( parts );
|
|
|
|
if ( rb_str_cmp( scheme, str_xprivate ) == 0 )
|
|
|
|
{
|
|
|
|
VALUE name = rb_ary_join( parts, colon );
|
|
|
|
obj = rb_funcall( cPrivateType, s_new, 2, name, val );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE domain = rb_ary_shift( parts );
|
|
|
|
VALUE name = rb_ary_join( parts, colon );
|
|
|
|
obj = rb_funcall( cDomainType, s_new, 3, domain, name, val );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2003-05-10 15:55:18 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Resolver#tagurize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_resolver_tagurize( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
VALUE tmp = rb_check_string_type(val);
|
|
|
|
|
|
|
|
if ( !NIL_P(tmp) )
|
|
|
|
{
|
|
|
|
char *taguri;
|
|
|
|
val = tmp;
|
|
|
|
taguri = syck_type_id_to_uri( RSTRING(val)->ptr );
|
|
|
|
return rb_str_new2( taguri );
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::DefaultResolver#detect_implicit
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_defaultresolver_detect_implicit( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
char *type_id;
|
|
|
|
VALUE tmp = rb_check_string_type(val);
|
|
|
|
|
|
|
|
if ( !NIL_P(tmp) )
|
|
|
|
{
|
|
|
|
val = tmp;
|
|
|
|
type_id = syck_match_implicit( RSTRING(val)->ptr, RSTRING(val)->len );
|
|
|
|
return rb_str_new2( type_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_str_new2( "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::DefaultResolver#node_import
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_defaultresolver_node_import( self, node )
|
|
|
|
VALUE self, node;
|
|
|
|
{
|
|
|
|
SyckNode *n;
|
|
|
|
VALUE obj;
|
|
|
|
Data_Get_Struct( node, SyckNode, n );
|
|
|
|
if ( !yaml_org_handler( n, &obj ) )
|
|
|
|
{
|
|
|
|
obj = rb_funcall( self, s_transfer, 2, rb_str_new2( n->type_id ), obj );
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::GenericResolver#node_import
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_genericresolver_node_import( self, node )
|
|
|
|
VALUE self, node;
|
|
|
|
{
|
|
|
|
SyckNode *n;
|
|
|
|
int i = 0;
|
|
|
|
VALUE t = Qnil, obj = Qnil, v = Qnil, style = Qnil;
|
|
|
|
Data_Get_Struct(node, SyckNode, n);
|
|
|
|
|
|
|
|
if ( n->type_id != NULL )
|
|
|
|
{
|
|
|
|
t = rb_str_new2(n->type_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (n->kind)
|
|
|
|
{
|
|
|
|
case syck_str_kind:
|
|
|
|
{
|
|
|
|
v = rb_str_new( n->data.str->ptr, n->data.str->len );
|
|
|
|
if ( n->data.str->style == scalar_1quote )
|
|
|
|
{
|
|
|
|
style = sym_1quote;
|
|
|
|
}
|
|
|
|
else if ( n->data.str->style == scalar_2quote )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
style = sym_2quote;
|
|
|
|
}
|
|
|
|
else if ( n->data.str->style == scalar_fold )
|
|
|
|
{
|
|
|
|
style = sym_fold;
|
|
|
|
}
|
|
|
|
else if ( n->data.str->style == scalar_literal )
|
|
|
|
{
|
|
|
|
style = sym_literal;
|
|
|
|
}
|
|
|
|
else if ( n->data.str->style == scalar_plain )
|
|
|
|
{
|
|
|
|
style = sym_plain;
|
2003-08-21 17:16:58 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
obj = rb_funcall( cScalar, s_new, 3, t, v, style );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
|
|
|
rb_iv_set(obj, "@kind", sym_seq);
|
|
|
|
v = rb_ary_new2( syck_seq_count( n ) );
|
|
|
|
for ( i = 0; i < syck_seq_count( n ); i++ )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ary_store( v, i, syck_seq_read( n, i ) );
|
2003-08-21 17:16:58 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( n->data.list->style == seq_inline )
|
2003-08-21 17:16:58 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
style = sym_inline;
|
|
|
|
}
|
|
|
|
obj = rb_funcall( cSeq, s_new, 3, t, v, style );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_map_kind:
|
|
|
|
rb_iv_set(obj, "@kind", sym_map);
|
|
|
|
v = rb_hash_new();
|
|
|
|
for ( i = 0; i < syck_map_count( n ); i++ )
|
|
|
|
{
|
|
|
|
rb_hash_aset( v, syck_map_read( n, map_key, i ), syck_map_read( n, map_value, i ) );
|
2003-08-21 17:16:58 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( n->data.pairs->style == map_inline )
|
|
|
|
{
|
|
|
|
style = sym_inline;
|
|
|
|
}
|
|
|
|
obj = rb_funcall( cMap, s_new, 3, t, v, style );
|
|
|
|
break;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
return obj;
|
2003-05-10 15:55:18 -04:00
|
|
|
}
|
|
|
|
|
2003-06-17 12:55:54 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::BadAlias.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_badalias_initialize( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_iv_set( self, "@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;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE str1 = rb_ivar_get( alias1, s_name );
|
|
|
|
VALUE str2 = rb_ivar_get( alias2, s_name );
|
2004-05-06 02:29:56 -04:00
|
|
|
VALUE val = rb_funcall( str1, s_cmp, 1, str2 );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-06-17 12:55:54 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::DomainType.initialize
|
2003-06-17 12:55:54 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_domaintype_initialize( self, domain, type_id, val )
|
2005-10-13 10:30:54 -04:00
|
|
|
VALUE self, domain, type_id, val;
|
2003-06-17 12:55:54 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_iv_set( self, "@domain", domain );
|
|
|
|
rb_iv_set( self, "@type_id", type_id );
|
|
|
|
rb_iv_set( self, "@value", val );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Object.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_yobject_initialize( self, klass, ivars )
|
|
|
|
VALUE self, klass, ivars;
|
|
|
|
{
|
|
|
|
rb_iv_set( self, "@class", klass );
|
|
|
|
rb_iv_set( self, "@ivars", ivars );
|
2003-06-17 12:55:54 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::PrivateType.initialize
|
2003-06-17 12:55:54 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_privatetype_initialize( self, type_id, val )
|
|
|
|
VALUE self, type_id, val;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_iv_set( self, "@type_id", type_id );
|
|
|
|
rb_iv_set( self, "@value", val );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark node contents.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
syck_node_mark( n )
|
|
|
|
SyckNode *n;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
switch ( n->kind )
|
|
|
|
{
|
|
|
|
case syck_seq_kind:
|
|
|
|
for ( i = 0; i < n->data.list->idx; i++ )
|
|
|
|
{
|
|
|
|
rb_gc_mark( syck_seq_read( n, i ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_map_kind:
|
|
|
|
for ( i = 0; i < n->data.pairs->idx; i++ )
|
|
|
|
{
|
|
|
|
rb_gc_mark( syck_map_read( n, map_key, i ) );
|
|
|
|
rb_gc_mark( syck_map_read( n, map_value, i ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't free Ruby data, Ruby will do that
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rb_syck_free_node( SyckNode *n )
|
|
|
|
{
|
|
|
|
switch ( n->kind )
|
|
|
|
{
|
|
|
|
case syck_str_kind:
|
|
|
|
S_FREE( n->data.str );
|
|
|
|
n->data.str = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
|
|
|
if ( n->data.list != NULL )
|
|
|
|
{
|
|
|
|
S_FREE( n->data.list->items );
|
|
|
|
S_FREE( n->data.list );
|
|
|
|
n->data.list = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_map_kind:
|
|
|
|
if ( n->data.pairs != NULL )
|
|
|
|
{
|
|
|
|
S_FREE( n->data.pairs->keys );
|
|
|
|
S_FREE( n->data.pairs->values );
|
|
|
|
S_FREE( n->data.pairs );
|
|
|
|
n->data.pairs = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_FREE( n );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Scalar.allocate
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_scalar_alloc( class )
|
|
|
|
VALUE class;
|
|
|
|
{
|
|
|
|
SyckNode *node = syck_alloc_str();
|
|
|
|
VALUE obj = Data_Wrap_Struct( class, syck_node_mark, rb_syck_free_node, node );
|
|
|
|
node->id = obj;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Scalar.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_scalar_initialize( self, type_id, val, style )
|
|
|
|
VALUE self, type_id, val, style;
|
|
|
|
{
|
|
|
|
rb_iv_set( self, "@kind", sym_scalar );
|
|
|
|
rb_funcall( self, s_type_id_set, 1, type_id );
|
|
|
|
rb_funcall( self, s_value_set, 1, val );
|
|
|
|
rb_funcall( self, s_style_set, 1, style );
|
2003-06-17 12:55:54 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-05-13 02:34:18 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Scalar.style=
|
2003-05-13 02:34:18 -04:00
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_scalar_style_set( self, style )
|
|
|
|
VALUE self, style;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( NIL_P( style ) )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_none;
|
|
|
|
}
|
|
|
|
else if ( style == sym_1quote )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_1quote;
|
|
|
|
}
|
|
|
|
else if ( style == sym_2quote )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_2quote;
|
|
|
|
}
|
|
|
|
else if ( style == sym_fold )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_fold;
|
|
|
|
}
|
|
|
|
else if ( style == sym_literal )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_literal;
|
|
|
|
}
|
|
|
|
else if ( style == sym_plain )
|
|
|
|
{
|
|
|
|
node->data.str->style = scalar_plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@style", style );
|
2003-05-13 02:34:18 -04:00
|
|
|
return self;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Scalar.value=
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_scalar_value_set( self, val )
|
|
|
|
VALUE self, val;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
StringValue( val );
|
|
|
|
node->data.str->ptr = RSTRING(val)->ptr;
|
|
|
|
node->data.str->len = RSTRING(val)->len;
|
|
|
|
node->data.str->style = scalar_none;
|
|
|
|
|
|
|
|
rb_iv_set( self, "@value", val );
|
|
|
|
return val;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Seq.allocate
|
|
|
|
*/
|
2003-05-10 15:55:18 -04:00
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_seq_alloc( class )
|
|
|
|
VALUE class;
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
SyckNode *node;
|
|
|
|
VALUE obj;
|
|
|
|
node = syck_alloc_seq();
|
|
|
|
obj = Data_Wrap_Struct( class, syck_node_mark, rb_syck_free_node, node );
|
|
|
|
node->id = obj;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Seq.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_seq_initialize( self, type_id, val, style )
|
|
|
|
VALUE self, type_id, val, style;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
rb_iv_set( self, "@kind", sym_seq );
|
|
|
|
rb_funcall( self, s_type_id_set, 1, type_id );
|
|
|
|
rb_funcall( self, s_value_set, 1, val );
|
|
|
|
rb_funcall( self, s_style_set, 1, style );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Seq.value=
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_seq_value_set( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
val = rb_check_array_type( val );
|
|
|
|
if ( !NIL_P( val ) ) {
|
|
|
|
int i;
|
|
|
|
syck_seq_empty( node );
|
|
|
|
for ( i = 0; i < RARRAY( val )->len; i++ )
|
|
|
|
{
|
|
|
|
syck_seq_add( node, rb_ary_entry(val, i) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@value", val );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Seq.add
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_seq_add_m( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
VALUE emitter = rb_ivar_get( self, s_emitter );
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( rb_respond_to( emitter, s_node_export ) ) {
|
|
|
|
val = rb_funcall( emitter, s_node_export, 1, val );
|
|
|
|
}
|
|
|
|
syck_seq_add( node, val );
|
|
|
|
rb_ary_push( rb_ivar_get( self, s_value ), val );
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Seq.style=
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_seq_style_set( self, style )
|
|
|
|
VALUE self, style;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( style == sym_inline )
|
|
|
|
{
|
|
|
|
node->data.list->style = seq_inline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->data.list->style = seq_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@style", style );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Map.allocate
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_map_alloc( class )
|
|
|
|
VALUE class;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
VALUE obj;
|
|
|
|
node = syck_alloc_map();
|
|
|
|
obj = Data_Wrap_Struct( class, syck_node_mark, rb_syck_free_node, node );
|
|
|
|
node->id = obj;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Map.initialize
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_map_initialize( self, type_id, val, style )
|
|
|
|
VALUE self, type_id, val, style;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( !NIL_P( val ) )
|
|
|
|
{
|
|
|
|
VALUE hsh = rb_check_convert_type(val, T_HASH, "Hash", "to_hash");
|
|
|
|
VALUE keys;
|
|
|
|
int i;
|
|
|
|
if ( NIL_P(hsh) )
|
|
|
|
{
|
|
|
|
rb_raise( rb_eTypeError, "wrong argument type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = rb_funcall( hsh, s_keys, 0 );
|
|
|
|
for ( i = 0; i < RARRAY(keys)->len; i++ )
|
|
|
|
{
|
|
|
|
VALUE key = rb_ary_entry(keys, i);
|
|
|
|
syck_map_add( node, key, rb_hash_aref(hsh, key) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@kind", sym_seq );
|
|
|
|
rb_funcall( self, s_type_id_set, 1, type_id );
|
|
|
|
rb_funcall( self, s_value_set, 1, val );
|
|
|
|
rb_funcall( self, s_style_set, 1, style );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Map.value=
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_map_value_set( self, val )
|
|
|
|
VALUE self, val;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( !NIL_P( val ) )
|
|
|
|
{
|
|
|
|
VALUE hsh = rb_check_convert_type(val, T_HASH, "Hash", "to_hash");
|
|
|
|
VALUE keys;
|
|
|
|
int i;
|
|
|
|
if ( NIL_P(hsh) )
|
|
|
|
{
|
|
|
|
rb_raise( rb_eTypeError, "wrong argument type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
syck_map_empty( node );
|
|
|
|
keys = rb_funcall( hsh, s_keys, 0 );
|
|
|
|
for ( i = 0; i < RARRAY(keys)->len; i++ )
|
|
|
|
{
|
|
|
|
VALUE key = rb_ary_entry(keys, i);
|
|
|
|
syck_map_add( node, key, rb_hash_aref(hsh, key) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@value", val );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Map.add
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_map_add_m( self, key, val )
|
|
|
|
VALUE self, key, val;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
VALUE emitter = rb_ivar_get( self, s_emitter );
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( rb_respond_to( emitter, s_node_export ) ) {
|
|
|
|
key = rb_funcall( emitter, s_node_export, 1, key );
|
|
|
|
val = rb_funcall( emitter, s_node_export, 1, val );
|
|
|
|
}
|
|
|
|
syck_map_add( node, key, val );
|
|
|
|
rb_hash_aset( rb_ivar_get( self, s_value ), key, val );
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Map.style=
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_map_style_set( self, style )
|
|
|
|
VALUE self, style;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( style == sym_inline )
|
|
|
|
{
|
|
|
|
node->data.pairs->style = map_inline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->data.pairs->style = map_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@style", style );
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cloning method for all node types
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_node_init_copy( copy, orig )
|
|
|
|
VALUE copy, orig;
|
|
|
|
{
|
|
|
|
SyckNode *copy_n;
|
|
|
|
SyckNode *orig_n;
|
|
|
|
|
|
|
|
if ( copy == orig )
|
|
|
|
return copy;
|
|
|
|
|
|
|
|
if ( TYPE( orig ) != T_DATA ||
|
|
|
|
RDATA( orig )->dfree != ( RUBY_DATA_FUNC )rb_syck_free_node )
|
|
|
|
{
|
|
|
|
rb_raise( rb_eTypeError, "wrong argument type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
Data_Get_Struct( orig, SyckNode, orig_n );
|
|
|
|
Data_Get_Struct( copy, SyckNode, copy_n );
|
|
|
|
MEMCPY( copy_n, orig_n, SyckNode, 1 );
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Node#type_id=
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_node_type_id_set( self, type_id )
|
|
|
|
VALUE self, type_id;
|
|
|
|
{
|
|
|
|
SyckNode *node;
|
|
|
|
Data_Get_Struct( self, SyckNode, node );
|
|
|
|
|
|
|
|
if ( node->type_id != NULL ) S_FREE( node->type_id );
|
|
|
|
|
|
|
|
if ( NIL_P( type_id ) ) {
|
|
|
|
node->type_id = NULL;
|
|
|
|
} else {
|
|
|
|
node->type_id = StringValuePtr( type_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_iv_set( self, "@type_id", type_id );
|
|
|
|
return type_id;
|
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;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE t;
|
|
|
|
SyckNode *n;
|
|
|
|
SyckNode *orig_n;
|
|
|
|
Data_Get_Struct(self, SyckNode, orig_n);
|
|
|
|
|
|
|
|
switch (orig_n->kind)
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
case syck_map_kind:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
n = syck_alloc_map();
|
|
|
|
for ( i = 0; i < orig_n->data.pairs->idx; i++ )
|
|
|
|
{
|
|
|
|
syck_map_add( n, rb_funcall( syck_map_read( orig_n, map_key, i ), s_transform, 0 ),
|
|
|
|
rb_funcall( syck_map_read( orig_n, map_value, i ), s_transform, 0 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
n = syck_alloc_seq();
|
|
|
|
for ( i = 0; i < orig_n->data.list->idx; i++ )
|
|
|
|
{
|
|
|
|
syck_seq_add( n, rb_funcall( syck_seq_read( orig_n, i ), s_transform, 0 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_str_kind:
|
|
|
|
n = syck_new_str2( orig_n->data.str->ptr, orig_n->data.str->len, orig_n->data.str->style );
|
|
|
|
break;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
|
|
|
|
if ( orig_n->type_id != NULL )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
n->type_id = syck_strndup( orig_n->type_id, strlen( orig_n->type_id ) );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
if ( orig_n->anchor != NULL )
|
2003-05-09 17:25:50 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
n->anchor = syck_strndup( orig_n->anchor, strlen( orig_n->anchor ) );
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
t = Data_Wrap_Struct( cNode, NULL, NULL, n );
|
|
|
|
n->id = t;
|
|
|
|
t = rb_funcall( oDefaultResolver, s_node_import, 1, t );
|
|
|
|
syck_free_node( n );
|
|
|
|
return t;
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Emitter callback: assembles YAML document events from
|
|
|
|
* Ruby symbols. This is a brilliant way to do it.
|
|
|
|
* No one could possibly object.
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
2004-12-23 12:23:02 -05:00
|
|
|
void
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_syck_emitter_handler(e, data)
|
|
|
|
SyckEmitter *e;
|
|
|
|
st_data_t data;
|
|
|
|
{
|
|
|
|
SyckNode *n;
|
|
|
|
Data_Get_Struct((VALUE)data, SyckNode, n);
|
|
|
|
|
|
|
|
switch (n->kind)
|
|
|
|
{
|
|
|
|
case syck_map_kind:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
syck_emit_map( e, n->type_id, n->data.pairs->style );
|
|
|
|
for ( i = 0; i < n->data.pairs->idx; i++ )
|
|
|
|
{
|
|
|
|
syck_emit_item( e, syck_map_read( n, map_key, i ) );
|
|
|
|
syck_emit_item( e, syck_map_read( n, map_value, i ) );
|
|
|
|
}
|
|
|
|
syck_emit_end( e );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_seq_kind:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
syck_emit_seq( e, n->type_id, n->data.list->style );
|
|
|
|
for ( i = 0; i < n->data.list->idx; i++ )
|
|
|
|
{
|
|
|
|
syck_emit_item( e, syck_seq_read( n, i ) );
|
|
|
|
}
|
|
|
|
syck_emit_end( e );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case syck_str_kind:
|
|
|
|
{
|
|
|
|
syck_emit_scalar( e, n->type_id, n->data.str->style, 0, 0, 0, n->data.str->ptr, n->data.str->len );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle output from the emitter
|
|
|
|
*/
|
|
|
|
void
|
2003-07-11 18:52:14 -04:00
|
|
|
rb_syck_output_handler( emitter, str, len )
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
char *str;
|
|
|
|
long len;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
struct emitter_xtra *bonus = (struct emitter_xtra *)emitter->bonus;
|
|
|
|
VALUE dest = bonus->port;
|
2004-12-23 12:23:02 -05:00
|
|
|
if (TYPE(dest) == T_STRING) {
|
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
|
|
|
}
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* Helper function for marking nodes in the anchor
|
|
|
|
* symbol table.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
syck_out_mark( emitter, node )
|
|
|
|
VALUE emitter, node;
|
|
|
|
{
|
|
|
|
SyckEmitter *emitterPtr;
|
|
|
|
struct emitter_xtra *bonus;
|
|
|
|
Data_Get_Struct(emitter, SyckEmitter, emitterPtr);
|
|
|
|
bonus = (struct emitter_xtra *)emitterPtr->bonus;
|
|
|
|
rb_ivar_set( node, s_emitter, emitter );
|
|
|
|
/* syck_emitter_mark_node( emitterPtr, (st_data_t)node ); */
|
|
|
|
if ( !NIL_P( bonus->oid ) ) {
|
|
|
|
rb_hash_aset( bonus->data, bonus->oid, node );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Mark emitter values.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
syck_mark_emitter(emitter)
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
struct emitter_xtra *bonus;
|
2003-07-14 16:35:07 -04:00
|
|
|
if ( emitter->bonus != NULL )
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
bonus = (struct emitter_xtra *)emitter->bonus;
|
|
|
|
rb_gc_mark( bonus->data );
|
|
|
|
rb_gc_mark( bonus->port );
|
2003-07-14 16:35:07 -04:00
|
|
|
}
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Free the emitter and any bonus attachment.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rb_syck_free_emitter(e)
|
|
|
|
SyckEmitter *e;
|
|
|
|
{
|
|
|
|
struct emitter_xtra *bonus = (struct emitter_xtra *)e->bonus;
|
|
|
|
if ( bonus != NULL ) S_FREE( bonus );
|
|
|
|
syck_free_emitter(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* YAML::Syck::Emitter.allocate
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
2005-02-22 09:51:53 -05:00
|
|
|
VALUE syck_emitter_s_alloc _((VALUE));
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE
|
2004-12-23 12:23:02 -05:00
|
|
|
syck_emitter_s_alloc(class)
|
|
|
|
VALUE class;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
VALUE pobj;
|
2003-07-11 18:52:14 -04:00
|
|
|
SyckEmitter *emitter = syck_new_emitter();
|
2004-06-29 13:34:08 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
pobj = Data_Wrap_Struct( class, syck_mark_emitter, rb_syck_free_emitter, emitter );
|
|
|
|
syck_emitter_handler( emitter, rb_syck_emitter_handler );
|
|
|
|
syck_output_handler( emitter, rb_syck_output_handler );
|
2003-07-11 18:52:14 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set( pobj, s_out, rb_funcall( cOut, s_new, 1, pobj ) );
|
2004-12-23 12:23:02 -05:00
|
|
|
return pobj;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Emitter.reset( options )
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE
|
|
|
|
syck_emitter_reset( argc, argv, self )
|
2004-12-23 12:23:02 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE options, tmp;
|
|
|
|
SyckEmitter *emitter;
|
|
|
|
struct emitter_xtra *bonus;
|
|
|
|
volatile VALUE hash; /* protect from GC */
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
|
|
|
bonus = (struct emitter_xtra *)emitter->bonus;
|
|
|
|
if ( bonus != NULL ) S_FREE( bonus );
|
|
|
|
|
|
|
|
bonus = S_ALLOC_N( struct emitter_xtra, 1 );
|
|
|
|
bonus->port = rb_str_new2( "" );
|
|
|
|
bonus->data = hash = rb_hash_new();
|
2004-12-23 12:23:02 -05:00
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &options) == 0)
|
|
|
|
{
|
|
|
|
options = rb_hash_new();
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set(self, s_options, options);
|
2004-12-23 12:23:02 -05:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
else if ( !NIL_P(tmp = rb_check_string_type(options)) )
|
|
|
|
{
|
|
|
|
bonus->port = tmp;
|
|
|
|
}
|
|
|
|
else if ( rb_respond_to( options, s_write ) )
|
|
|
|
{
|
|
|
|
bonus->port = options;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-12-23 12:23:02 -05:00
|
|
|
Check_Type(options, T_HASH);
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set(self, s_options, options);
|
2004-12-23 12:23:02 -05:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
|
2005-09-17 13:22:49 -04:00
|
|
|
emitter->headless = 1;
|
2005-09-12 23:58:33 -04:00
|
|
|
emitter->bonus = (void *)bonus;
|
|
|
|
rb_ivar_set(self, s_level, INT2FIX(0));
|
|
|
|
rb_ivar_set(self, s_resolver, Qnil);
|
2004-12-23 12:23:02 -05:00
|
|
|
return self;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Emitter.emit( object_id ) { |out| ... }
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_emitter_emit( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2003-07-11 18:52:14 -04:00
|
|
|
VALUE self;
|
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE oid, proc;
|
|
|
|
char *anchor_name;
|
2003-07-11 18:52:14 -04:00
|
|
|
SyckEmitter *emitter;
|
2005-09-12 23:58:33 -04:00
|
|
|
struct emitter_xtra *bonus;
|
|
|
|
SYMID symple;
|
|
|
|
int level = FIX2INT(rb_ivar_get(self, s_level)) + 1;
|
|
|
|
rb_ivar_set(self, s_level, INT2FIX(level));
|
2003-07-11 18:52:14 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_scan_args(argc, argv, "1&", &oid, &proc);
|
2004-12-23 12:23:02 -05:00
|
|
|
Data_Get_Struct(self, SyckEmitter, emitter);
|
2005-09-12 23:58:33 -04:00
|
|
|
bonus = (struct emitter_xtra *)emitter->bonus;
|
|
|
|
|
|
|
|
/* Calculate anchors, normalize nodes, build a simpler symbol table */
|
|
|
|
bonus->oid = oid;
|
|
|
|
if ( !NIL_P( oid ) && RTEST( rb_funcall( bonus->data, s_haskey, 1, oid ) ) ) {
|
|
|
|
symple = rb_hash_aref( bonus->data, oid );
|
|
|
|
} else {
|
|
|
|
symple = rb_funcall( proc, s_call, 1, rb_ivar_get( self, s_out ) );
|
|
|
|
}
|
|
|
|
syck_emitter_mark_node( emitter, (st_data_t)symple );
|
|
|
|
|
|
|
|
/* Second pass, build emitted string */
|
|
|
|
level -= 1;
|
|
|
|
rb_ivar_set(self, s_level, INT2FIX(level));
|
|
|
|
if ( level == 0 )
|
|
|
|
{
|
|
|
|
syck_emit(emitter, (st_data_t)symple);
|
|
|
|
syck_emitter_flush(emitter, 0);
|
|
|
|
|
|
|
|
return bonus->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return symple;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Emitter#node_export
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_emitter_node_export( self, node )
|
|
|
|
VALUE self, node;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
return rb_funcall( node, s_to_yaml, 1, self );
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Emitter#set_resolver
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_emitter_set_resolver( self, resolver )
|
|
|
|
VALUE self, resolver;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set( self, s_resolver, resolver );
|
2003-07-11 18:52:14 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Out::initialize
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_out_initialize( self, emitter )
|
|
|
|
VALUE self, emitter;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_ivar_set( self, s_emitter, emitter );
|
2003-07-11 18:52:14 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Out::map
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_out_map( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE type_id, style, map;
|
|
|
|
if (rb_scan_args(argc, argv, "11", &type_id, &style) == 1) {
|
|
|
|
style = Qnil;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
map = rb_funcall( cMap, s_new, 3, type_id, rb_hash_new(), style );
|
|
|
|
syck_out_mark( rb_ivar_get( self, s_emitter ), map );
|
|
|
|
rb_yield( map );
|
|
|
|
return map;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* YAML::Syck::Out::seq
|
2003-07-11 18:52:14 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
2005-09-12 23:58:33 -04:00
|
|
|
syck_out_seq( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2004-02-16 20:51:39 -05:00
|
|
|
VALUE self;
|
2003-07-11 18:52:14 -04:00
|
|
|
{
|
2005-09-12 23:58:33 -04:00
|
|
|
VALUE type_id, style, seq;
|
|
|
|
if (rb_scan_args(argc, argv, "11", &type_id, &style) == 1) {
|
|
|
|
style = Qnil;
|
|
|
|
}
|
|
|
|
seq = rb_funcall( cSeq, s_new, 3, type_id, rb_ary_new(), style );
|
|
|
|
syck_out_mark( rb_ivar_get( self, s_emitter ), seq );
|
|
|
|
rb_yield( seq );
|
|
|
|
return seq;
|
|
|
|
}
|
2003-07-11 18:52:14 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* YAML::Syck::Out::scalar
|
|
|
|
syck_out_scalar( self, type_id, str, style )
|
|
|
|
VALUE self, type_id, str, style;
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
syck_out_scalar( argc, argv, self )
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE type_id, str, style, scalar;
|
|
|
|
if (rb_scan_args(argc, argv, "21", &type_id, &str, &style) == 2) {
|
|
|
|
style = Qnil;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
2005-09-12 23:58:33 -04:00
|
|
|
scalar = rb_funcall( cScalar, s_new, 3, type_id, str, style );
|
|
|
|
syck_out_mark( rb_ivar_get( self, s_emitter ), scalar );
|
|
|
|
return scalar;
|
2003-07-11 18:52:14 -04:00
|
|
|
}
|
|
|
|
|
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-12-23 12:23:02 -05: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");
|
|
|
|
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("<=>");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_intern = rb_intern("intern");
|
2004-12-23 12:23:02 -05:00
|
|
|
s_update = rb_intern("update");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_detect_implicit = rb_intern("detect_implicit");
|
2004-12-23 12:23:02 -05:00
|
|
|
s_dup = rb_intern("dup");
|
2003-10-03 15:51:25 -04:00
|
|
|
s_default_set = rb_intern("default=");
|
2004-12-23 12:23:02 -05:00
|
|
|
s_match = rb_intern("match");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_push = rb_intern("push");
|
|
|
|
s_haskey = rb_intern("has_key?");
|
2004-12-23 12:23:02 -05:00
|
|
|
s_keys = rb_intern("keys");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_node_import = rb_intern("node_import");
|
2004-12-23 12:23:02 -05:00
|
|
|
s_tr_bang = rb_intern("tr!");
|
2003-08-21 17:16:58 -04:00
|
|
|
s_unpack = rb_intern("unpack");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_write = rb_intern("write");
|
|
|
|
s_tag_read_class = rb_intern( "yaml_tag_read_class" );
|
|
|
|
s_tag_subclasses = rb_intern( "yaml_tag_subclasses?" );
|
|
|
|
s_emitter = rb_intern( "emitter" );
|
|
|
|
s_set_resolver = rb_intern( "set_resolver" );
|
|
|
|
s_node_export = rb_intern( "node_export" );
|
|
|
|
s_to_yaml = rb_intern( "to_yaml" );
|
|
|
|
s_transform = rb_intern( "transform" );
|
|
|
|
s_yaml_new = rb_intern("yaml_new");
|
|
|
|
s_yaml_initialize = rb_intern("yaml_initialize");
|
|
|
|
|
|
|
|
s_tags = rb_intern("@tags");
|
2004-03-03 18:08:45 -05:00
|
|
|
s_name = rb_intern("@name");
|
|
|
|
s_options = rb_intern("@options");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_kind = rb_intern("@kind");
|
2004-03-03 18:08:45 -05:00
|
|
|
s_type_id = rb_intern("@type_id");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_type_id_set = rb_intern("type_id=");
|
|
|
|
s_resolver = rb_intern("@resolver");
|
|
|
|
s_level = rb_intern( "@level" );
|
|
|
|
s_style = rb_intern("@style");
|
|
|
|
s_style_set = rb_intern("style=");
|
2004-03-03 18:08:45 -05:00
|
|
|
s_value = rb_intern("@value");
|
2005-09-12 23:58:33 -04:00
|
|
|
s_value_set = rb_intern("value=");
|
|
|
|
s_out = rb_intern("@out");
|
|
|
|
s_input = rb_intern("@input");
|
2004-03-03 18:08:45 -05:00
|
|
|
|
2004-12-23 12:23:02 -05:00
|
|
|
sym_model = ID2SYM(rb_intern("Model"));
|
|
|
|
sym_generic = ID2SYM(rb_intern("Generic"));
|
2005-09-12 23:58:33 -04:00
|
|
|
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"));
|
2005-09-12 23:58:33 -04:00
|
|
|
sym_1quote = ID2SYM(rb_intern("quote1"));
|
|
|
|
sym_2quote = ID2SYM(rb_intern("quote2"));
|
|
|
|
sym_fold = ID2SYM(rb_intern("fold"));
|
|
|
|
sym_literal = ID2SYM(rb_intern("literal"));
|
|
|
|
sym_plain = ID2SYM(rb_intern("plain"));
|
|
|
|
sym_inline = ID2SYM(rb_intern("inline"));
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2003-07-04 08:51:42 -04:00
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Define YAML::Syck::Resolver class
|
2003-07-04 08:51:42 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
cResolver = rb_define_class_under( rb_syck, "Resolver", rb_cObject );
|
|
|
|
rb_define_attr( cResolver, "tags", 1, 1 );
|
|
|
|
rb_define_method( cResolver, "initialize", syck_resolver_initialize, 0 );
|
|
|
|
rb_define_method( cResolver, "add_type", syck_resolver_add_type, 2 );
|
|
|
|
rb_define_method( cResolver, "use_types_at", syck_resolver_use_types_at, 1 );
|
|
|
|
rb_define_method( cResolver, "detect_implicit", syck_resolver_detect_implicit, 1 );
|
|
|
|
rb_define_method( cResolver, "transfer", syck_resolver_transfer, 2 );
|
|
|
|
rb_define_method( cResolver, "node_import", syck_resolver_node_import, 1 );
|
|
|
|
rb_define_method( cResolver, "tagurize", syck_resolver_tagurize, 1 );
|
|
|
|
|
|
|
|
oDefaultResolver = rb_funcall( cResolver, rb_intern( "new" ), 0 );
|
|
|
|
rb_define_singleton_method( oDefaultResolver, "node_import", syck_defaultresolver_node_import, 1 );
|
|
|
|
rb_define_singleton_method( oDefaultResolver, "detect_implicit", syck_defaultresolver_detect_implicit, 1 );
|
|
|
|
rb_define_const( rb_syck, "DefaultResolver", oDefaultResolver );
|
|
|
|
oGenericResolver = rb_funcall( cResolver, rb_intern( "new" ), 0 );
|
|
|
|
rb_define_singleton_method( oGenericResolver, "node_import", syck_genericresolver_node_import, 1 );
|
|
|
|
rb_define_const( rb_syck, "GenericResolver", oGenericResolver );
|
2003-05-10 15:55:18 -04:00
|
|
|
|
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 );
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_attr( cParser, "resolver", 1, 1 );
|
|
|
|
rb_define_attr( cParser, "input", 1, 1 );
|
2004-12-23 12:23:02 -05:00
|
|
|
rb_define_alloc_func( cParser, syck_parser_s_alloc );
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_method(cParser, "initialize", syck_parser_initialize, -1 );
|
|
|
|
rb_define_method(cParser, "bufsize=", syck_parser_bufsize_set, 1 );
|
|
|
|
rb_define_method(cParser, "bufsize", syck_parser_bufsize_get, 0 );
|
2003-05-09 17:25:50 -04:00
|
|
|
rb_define_method(cParser, "load", syck_parser_load, -1);
|
|
|
|
rb_define_method(cParser, "load_documents", syck_parser_load_documents, -1);
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_method(cParser, "set_resolver", syck_parser_set_resolver, 1);
|
2003-05-09 17:25:50 -04:00
|
|
|
|
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 );
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_method( cNode, "initialize_copy", syck_node_init_copy, 1 );
|
|
|
|
rb_define_attr( cNode, "emitter", 1, 1 );
|
|
|
|
rb_define_attr( cNode, "resolver", 1, 1 );
|
|
|
|
rb_define_attr( cNode, "kind", 1, 0 );
|
|
|
|
rb_define_attr( cNode, "type_id", 1, 0 );
|
|
|
|
rb_define_attr( cNode, "value", 1, 0 );
|
|
|
|
rb_define_method( cNode, "type_id=", syck_node_type_id_set, 1 );
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Define YAML::Syck::Scalar, YAML::Syck::Seq, YAML::Syck::Map --
|
|
|
|
* all are the publicly usable variants of YAML::Syck::Node
|
|
|
|
*/
|
|
|
|
cScalar = rb_define_class_under( rb_syck, "Scalar", cNode );
|
|
|
|
rb_define_alloc_func( cScalar, syck_scalar_alloc );
|
|
|
|
rb_define_attr( cNode, "value", 1, 0 );
|
|
|
|
rb_define_method( cScalar, "initialize", syck_scalar_initialize, 3 );
|
|
|
|
rb_define_method( cScalar, "value=", syck_scalar_value_set, 1 );
|
|
|
|
rb_define_method( cScalar, "style=", syck_scalar_style_set, 1 );
|
|
|
|
cSeq = rb_define_class_under( rb_syck, "Seq", cNode );
|
|
|
|
rb_define_alloc_func( cSeq, syck_seq_alloc );
|
|
|
|
rb_define_method( cSeq, "initialize", syck_seq_initialize, 3 );
|
|
|
|
rb_define_method( cSeq, "value=", syck_seq_value_set, 1 );
|
|
|
|
rb_define_method( cSeq, "add", syck_seq_add_m, 1 );
|
|
|
|
rb_define_method( cSeq, "style=", syck_seq_style_set, 1 );
|
|
|
|
cMap = rb_define_class_under( rb_syck, "Map", cNode );
|
|
|
|
rb_define_alloc_func( cMap, syck_map_alloc );
|
|
|
|
rb_define_method( cMap, "initialize", syck_map_initialize, 3 );
|
|
|
|
rb_define_method( cMap, "value=", syck_map_value_set, 1 );
|
|
|
|
rb_define_method( cMap, "add", syck_map_add_m, 2 );
|
|
|
|
rb_define_method( cMap, "style=", syck_map_style_set, 1 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define YAML::PrivateType class
|
2003-07-04 08:51:42 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
cPrivateType = rb_define_class_under( rb_yaml, "PrivateType", rb_cObject );
|
2003-06-17 12:55:54 -04:00
|
|
|
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
|
|
|
/*
|
2005-09-12 23:58:33 -04:00
|
|
|
* Define YAML::DomainType class
|
2003-07-04 08:51:42 -04:00
|
|
|
*/
|
2005-09-12 23:58:33 -04:00
|
|
|
cDomainType = rb_define_class_under( rb_yaml, "DomainType", rb_cObject );
|
2003-06-17 12:55:54 -04:00
|
|
|
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);
|
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Object class
|
|
|
|
*/
|
|
|
|
cYObject = rb_define_class_under( rb_yaml, "Object", rb_cObject );
|
|
|
|
rb_define_attr( cYObject, "class", 1, 1 );
|
|
|
|
rb_define_attr( cYObject, "ivars", 1, 1 );
|
|
|
|
rb_define_method( cYObject, "initialize", syck_yobject_initialize, 2);
|
|
|
|
rb_define_method( cYObject, "yaml_initialize", syck_yobject_initialize, 2);
|
|
|
|
|
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-12-23 12:23:02 -05: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-12-23 12:23:02 -05:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::DefaultKey class
|
|
|
|
*/
|
|
|
|
cDefaultKey = rb_define_class_under( rb_syck, "DefaultKey", rb_cObject );
|
2003-10-03 15:51:25 -04:00
|
|
|
|
2005-09-12 23:58:33 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Out classes
|
|
|
|
*/
|
|
|
|
cOut = rb_define_class_under( rb_syck, "Out", rb_cObject );
|
|
|
|
rb_define_attr( cOut, "emitter", 1, 1 );
|
|
|
|
rb_define_method( cOut, "initialize", syck_out_initialize, 1 );
|
|
|
|
rb_define_method( cOut, "map", syck_out_map, -1 );
|
|
|
|
rb_define_method( cOut, "seq", syck_out_seq, -1 );
|
|
|
|
rb_define_method( cOut, "scalar", syck_out_scalar, -1 );
|
|
|
|
|
2003-07-11 18:52:14 -04:00
|
|
|
/*
|
|
|
|
* Define YAML::Syck::Emitter class
|
|
|
|
*/
|
|
|
|
cEmitter = rb_define_class_under( rb_syck, "Emitter", rb_cObject );
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_attr( cEmitter, "level", 1, 1 );
|
2004-12-23 12:23:02 -05:00
|
|
|
rb_define_alloc_func( cEmitter, syck_emitter_s_alloc );
|
2005-09-12 23:58:33 -04:00
|
|
|
rb_define_method( cEmitter, "initialize", syck_emitter_reset, -1 );
|
|
|
|
rb_define_method( cEmitter, "reset", syck_emitter_reset, -1 );
|
|
|
|
rb_define_method( cEmitter, "emit", syck_emitter_emit, -1 );
|
|
|
|
rb_define_method( cEmitter, "set_resolver", syck_emitter_set_resolver, 1);
|
|
|
|
rb_define_method( cEmitter, "node_export", syck_emitter_node_export, 1);
|
2003-05-09 17:25:50 -04:00
|
|
|
}
|
|
|
|
|