VOOZH about

URL: https://qiita.com/kjunichi/items/74e079d6770f3cc17c2d

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


👁 Image
3

Go to list of users who liked

2

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@kjunichi(Junichi)in👁 Image
ジャパンシステム株式会社

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

3
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

Go to list of users who liked

2
0

Go to list of comments

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

Go to list of users who liked

2