<commit_before>/*
** mrb_uname.c - Uname class
**
** Copyright (c) <NAME> 2014
**
** See Copyright Notice in LICENSE
*/

#include "mruby.h"
#include "mruby/variable.h"
#include "mruby/class.h"
#include "mruby/data.h"
#include "mrb_uname.h"
#include <sys/utsname.h>

#define DONE mrb_gc_arena_restore(mrb, 0);

typedef struct {
  struct utsname uname_b;
} mrb_uname_data;

static const struct mrb_data_type mrb_uname_data_type = {
  "mrb_uname_data", mrb_free,
};

static mrb_value mrb_uname_wrap(mrb_state *mrb, struct RClass *uname,
    mrb_uname_data *data)
{
    return mrb_obj_value(Data_Wrap_Struct(mrb, uname, &mrb_uname_data_type,
          data));
}

static mrb_uname_data *mrb_uname_get_uname_data(mrb_state *mrb, mrb_value self)
{
  struct utsname uname_b;
  mrb_uname_data *data = NULL;
  mrb_sym uname_sym = mrb_intern_lit(mrb, "@@uname");

  if (mrb_cv_defined(mrb, self, uname_sym)) {
    Data_Get_Struct(mrb, mrb_cv_get(mrb, self, uname_sym),
          &mrb_uname_data_type, data);
  } else {
    data = (mrb_uname_data *)mrb_malloc(mrb, sizeof(mrb_uname_data));
    if (uname(&uname_b) != 0) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "uname failed");
    }
    data->uname_b = uname_b;
    mrb_cv_set(mrb, self, uname_sym,
        mrb_uname_wrap(mrb, mrb_class_ptr(self), data));
  }
  return data;
}

static mrb_value mrb_uname_sysname(mrb_state *mrb, mrb_value self)
{
  mrb_uname_data *data = mrb_uname_get_uname_data(mrb, self);
  return mrb_str_new_cstr(mrb, data->uname_b.sysname);
}

static mrb_value mrb_uname_machine(mrb_state *mrb, mrb_value self)
{
  mrb_uname_data *data = mrb_uname_get_uname_data(mrb, self);
  return mrb_str_new_cstr(mrb, data->uname_b.machine);
}

static mrb_value mrb_uname_nodename(mrb_state *mrb, mrb_value self)
{
  mrb_uname_data *data = mrb_uname_get_uname_data(mrb, self);
  return mrb_str_new_cstr(mrb, data->uname_b.nodename);
}

void mrb_mruby_uname_gem_init(mrb_state *mrb)
{
    struct RClass *uname;

    uname = mrb_define_class(mrb, "Uname", mrb->object_class);
    mrb_define_class_method(mrb, uname, "sysname", mrb_uname_sysname, MRB_ARGS_NONE());
    mrb_define_class_method(mrb, uname, "machine", mrb_uname_machine, MRB_ARGS_NONE());
    mrb_define_class_method(mrb, uname, "nodename", mrb_uname_nodename, MRB_ARGS_NONE());
    DONE;
}

void mrb_mruby_uname_gem_final(mrb_state *mrb)<commit_msg>Use macro for getting uname value<commit_after>/*
** mrb_uname.c - Uname class
**
** Copyright (c) <NAME> 2014
**
** See Copyright Notice in LICENSE
*/

#include "mruby.h"
#include "mruby/variable.h"
#include "mruby/class.h"
#include "mruby/data.h"
#include "mrb_uname.h"
#include <sys/utsname.h>

#define DONE mrb_gc_arena_restore(mrb, 0);

typedef struct {
  struct utsname uname_b;
} mrb_uname_data;

static const struct mrb_data_type mrb_uname_data_type = {
  "mrb_uname_data", mrb_free,
};

static mrb_value mrb_uname_wrap(mrb_state *mrb, struct RClass *uname,
    mrb_uname_data *data)
{
    return mrb_obj_value(Data_Wrap_Struct(mrb, uname, &mrb_uname_data_type,
          data));
}

static mrb_uname_data *mrb_uname_get_uname_data(mrb_state *mrb, mrb_value self)
{
  struct utsname uname_b;
  mrb_uname_data *data = NULL;
  mrb_sym uname_sym = mrb_intern_lit(mrb, "@@uname");

  if (mrb_cv_defined(mrb, self, uname_sym)) {
    Data_Get_Struct(mrb, mrb_cv_get(mrb, self, uname_sym),
          &mrb_uname_data_type, data);
  } else {
    data = (mrb_uname_data *)mrb_malloc(mrb, sizeof(mrb_uname_data));
    if (uname(&uname_b) != 0) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "uname failed");
    }
    data->uname_b = uname_b;
    mrb_cv_set(mrb, self, uname_sym,
        mrb_uname_wrap(mrb, mrb_class_ptr(self), data));
  }
  return data;
}

#define MRB_UNAME_GET_VALUE(mrb, self, name) \
  mrb_str_new_cstr(mrb, (mrb_uname_get_uname_data(mrb, self))->uname_b.name)

static mrb_value mrb_uname_sysname(mrb_state *mrb, mrb_value self)
{
  return MRB_UNAME_GET_VALUE(mrb, self, sysname);
}

static mrb_value mrb_uname_machine(mrb_state *mrb, mrb_value self)
{
  return MRB_UNAME_GET_VALUE(mrb, self, machine);
}

static mrb_value mrb_uname_nodename(mrb_state *mrb, mrb_value self)
{
  return MRB_UNAME_GET_VALUE(mrb, self, nodename);
}

void mrb_mruby_uname_gem_init(mrb_state *mrb)
{
    struct RClass *uname;

    uname = mrb_define_class(mrb, "Uname", mrb->object_class);
    mrb_define_class_method(mrb, uname, "sysname", mrb_uname_sysname, MRB_ARGS_NONE());
    mrb_define_class_method(mrb, uname, "machine", mrb_uname_machine, MRB_ARGS_NONE());
    mrb_define_class_method(mrb, uname, "nodename", mrb_uname_nodename, MRB_ARGS_NONE());
    DONE;
}

void mrb_mruby_uname_gem_final(mrb_state *mrb)
{'fraction_non_alphanumeric': 0.11899656946826759, 'fraction_numerical': 0.002572898799313894, 'mean_word_length': 5.425619834710743, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '58843', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2064, 'n_words': 346}