2010-07-17 00:50:19 -04:00
|
|
|
#include "ruby.h"
|
|
|
|
|
|
|
|
static VALUE rb_cPathname;
|
2010-07-17 11:03:31 -04:00
|
|
|
static ID id_at_path, id_to_path;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
get_strpath(VALUE obj)
|
|
|
|
{
|
2010-07-19 05:35:31 -04:00
|
|
|
VALUE strpath;
|
|
|
|
strpath = rb_ivar_get(obj, id_at_path);
|
|
|
|
if (TYPE(strpath) != T_STRING)
|
|
|
|
rb_raise(rb_eTypeError, "unexpected @path");
|
|
|
|
return strpath;
|
2010-07-17 11:03:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_strpath(VALUE obj, VALUE val)
|
|
|
|
{
|
|
|
|
rb_ivar_set(obj, id_at_path, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a Pathname object from the given String (or String-like object).
|
|
|
|
* If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
path_initialize(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE str;
|
2010-07-19 05:35:31 -04:00
|
|
|
if (TYPE(arg) == T_STRING) {
|
2010-07-17 11:03:31 -04:00
|
|
|
str = arg;
|
2010-07-19 05:35:31 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = rb_check_funcall(arg, id_to_path, 0, NULL);
|
|
|
|
if (str == Qundef)
|
|
|
|
str = arg;
|
|
|
|
StringValue(str);
|
|
|
|
}
|
2010-07-17 11:03:31 -04:00
|
|
|
if (memchr(RSTRING_PTR(str), '\0', RSTRING_LEN(str)))
|
|
|
|
rb_raise(rb_eArgError, "pathname contains null byte");
|
|
|
|
str = rb_obj_dup(str);
|
|
|
|
|
|
|
|
set_strpath(self, str);
|
|
|
|
OBJ_INFECT(self, str);
|
2010-07-17 12:10:19 -04:00
|
|
|
return self;
|
2010-07-17 11:03:31 -04:00
|
|
|
}
|
2010-07-17 00:50:19 -04:00
|
|
|
|
2010-07-19 05:35:31 -04:00
|
|
|
static VALUE
|
|
|
|
path_freeze(VALUE self)
|
|
|
|
{
|
|
|
|
rb_call_super(0, 0);
|
|
|
|
rb_str_freeze(get_strpath(self));
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-07-26 09:35:18 -04:00
|
|
|
static VALUE
|
|
|
|
path_taint(VALUE self)
|
|
|
|
{
|
|
|
|
rb_call_super(0, 0);
|
2010-07-27 10:14:14 -04:00
|
|
|
rb_obj_taint(get_strpath(self));
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
path_untaint(VALUE self)
|
|
|
|
{
|
|
|
|
rb_call_super(0, 0);
|
|
|
|
rb_obj_untaint(get_strpath(self));
|
2010-07-26 09:35:18 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-07-28 09:24:55 -04:00
|
|
|
/*
|
|
|
|
* Compare this pathname with +other+. The comparison is string-based.
|
|
|
|
* Be aware that two different paths (<tt>foo.txt</tt> and <tt>./foo.txt</tt>)
|
|
|
|
* can refer to the same file.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
path_eq(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
if (!rb_obj_is_kind_of(other, rb_cPathname))
|
|
|
|
return Qfalse;
|
|
|
|
return rb_str_equal(get_strpath(self), get_strpath(other));
|
|
|
|
}
|
|
|
|
|
2010-07-17 00:50:19 -04:00
|
|
|
void
|
|
|
|
Init_pathname()
|
|
|
|
{
|
2010-07-17 11:03:31 -04:00
|
|
|
id_at_path = rb_intern("@path");
|
|
|
|
id_to_path = rb_intern("to_path");
|
|
|
|
|
2010-07-17 00:50:19 -04:00
|
|
|
rb_cPathname = rb_define_class("Pathname", rb_cObject);
|
2010-07-17 11:03:31 -04:00
|
|
|
rb_define_method(rb_cPathname, "initialize", path_initialize, 1);
|
2010-07-19 05:35:31 -04:00
|
|
|
rb_define_method(rb_cPathname, "freeze", path_freeze, 0);
|
2010-07-26 09:35:18 -04:00
|
|
|
rb_define_method(rb_cPathname, "taint", path_taint, 0);
|
2010-07-27 10:14:14 -04:00
|
|
|
rb_define_method(rb_cPathname, "untaint", path_untaint, 0);
|
2010-07-28 09:24:55 -04:00
|
|
|
rb_define_method(rb_cPathname, "==", path_eq, 1);
|
|
|
|
rb_define_method(rb_cPathname, "===", path_eq, 1);
|
|
|
|
rb_define_method(rb_cPathname, "eql?", path_eq, 1);
|
2010-07-17 00:50:19 -04:00
|
|
|
}
|