Skip to content

Commit 96113a2

Browse files
committed
mruby-io: define File.absolute_path method; ref #6482
1 parent 436269f commit 96113a2

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

mrbgems/mruby-io/src/file.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,12 @@ path_gethome(mrb_state *mrb, const char **pathp)
454454
}
455455

456456
static mrb_value
457-
path_expand(mrb_state *mrb, const char *path, const char *base)
457+
path_expand(mrb_state *mrb, const char *path, const char *base, mrb_bool tilda)
458458
{
459459
mrb_value ary;
460460

461461
// split path conponents as array and normalization
462-
if (path[0] == '~') {
462+
if (tilda && path[0] == '~') {
463463
base = path_gethome(mrb, &path);
464464
ary = path_split(mrb, path, base, NULL);
465465
}
@@ -468,7 +468,7 @@ path_expand(mrb_state *mrb, const char *path, const char *base)
468468
}
469469
else {
470470
const char *wd = NULL;
471-
if (base[0] == '~') {
471+
if (tilda && base[0] == '~') {
472472
wd = path_gethome(mrb, &base);
473473
}
474474
#ifndef _WIN32
@@ -537,7 +537,16 @@ mrb_file_expand_path(mrb_state *mrb, mrb_value self)
537537
const char *path;
538538
const char *default_dir = ".";
539539
mrb_get_args(mrb, "z|z", &path, &default_dir);
540-
return path_expand(mrb, path, default_dir);
540+
return path_expand(mrb, path, default_dir, TRUE);
541+
}
542+
543+
static mrb_value
544+
mrb_file_absolute_path(mrb_state *mrb, mrb_value self)
545+
{
546+
const char *path;
547+
const char *default_dir = ".";
548+
mrb_get_args(mrb, "z|z", &path, &default_dir);
549+
return path_expand(mrb, path, default_dir, FALSE);
541550
}
542551

543552
static mrb_value
@@ -796,6 +805,7 @@ mrb_init_file(mrb_state *mrb)
796805
mrb_define_class_method_id(mrb, file, MRB_SYM(dirname), mrb_file_dirname, MRB_ARGS_REQ(1));
797806
mrb_define_class_method_id(mrb, file, MRB_SYM(basename), mrb_file_basename, MRB_ARGS_REQ(1));
798807
mrb_define_class_method_id(mrb, file, MRB_SYM(realpath), mrb_file_realpath, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
808+
mrb_define_class_method_id(mrb, file, MRB_SYM(absolute_path), mrb_file_absolute_path, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
799809
mrb_define_class_method_id(mrb, file, MRB_SYM_Q(absolute_path), mrb_file_absolute_path_p, MRB_ARGS_REQ(1));
800810
mrb_define_class_method_id(mrb, file, MRB_SYM(expand_path), mrb_file_expand_path, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
801811

0 commit comments

Comments
 (0)