LoginSignup
3
2

More than 5 years have passed since last update.

c言語でmrubyを使ってフルパスを取得する

Last updated at Posted at 2015-03-04

前提

mruby-ioが組み込まれたmrubyである事が必要です。

Rubyだと

puts File.realpath("./foo.txt")

やってみた

mrb_module_getとmrb_class_getの使い分けが実行時に怒られるか怒られないかで
体で覚えてる段階です。

#include <stdio.h>
#include "mruby.h"
#include "mruby/compile.h"
#include "mruby/string.h"

void main() {
mrb_state *mrb;
mrb_value val;
char *filepath = "./foo.txt";

mrb = mrb_open();

val = mrb_funcall(mrb, mrb_obj_value(mrb_class_get(mrb, "File")), "realpath", 1,
mrb_str_new_cstr(mrb, filepath));

char *fullpath = mrb_string_value_ptr(mrb, val);
printf("%s\n", fullpath);
}

必要になったmrubyのAPI

  • mrb_open
  • mrb_funcall
  • mrb_obj_value
  • mrb_class_get
  • mrb_str_new_cstr
  • mrb_string_value_ptr

ビルド

mrubyにもpkg-config風のmruby-configがあるので、意外と簡単。

gcc mrubyFile.c `mruby/bin/mruby-config --cflags --ldflags --libs --ldflags-before-libs`   

関連投稿

関連記事

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2