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

#include "mruby.h"
#include "mruby/data.h"
#include "mrb_geoip.h"
#include <GeoIP.h>
#include <GeoIPCity.h>

#define DONE mrb_gc_arena_restore(mrb, 0);

typedef struct {
  GeoIP *gi;
  GeoIPRecord *gir;
  char *db;
  char *host;
} mrb_geoip_data;

static const char *MK_STR(const char *str){
    return str ? str : "N/A";
}

static void mrb_geoip_free(mrb_state *mrb, void *p)
{
  mrb_geoip_data *data = p;

  GeoIPRecord_delete(data->gir);
  GeoIP_delete(data->gi);
}

static const struct mrb_data_type mrb_geoip_data_type = {
  "mrb_geoip_data", mrb_geoip_free,
};

static mrb_value mrb_geoip_init(mrb_state *mrb, mrb_value self)
{
  mrb_geoip_data *data;
  char *db;

  data = (mrb_geoip_data *)DATA_PTR(self);
  if (data) {
    mrb_free(mrb, data);
  }
  DATA_TYPE(self) = &mrb_geoip_data_type;
  DATA_PTR(self) = NULL;

  mrb_get_args(mrb, "z", &db);
  data = (mrb_geoip_data *)mrb_malloc(mrb, sizeof(mrb_geoip_data));
  data->db = db;
  data->host = NULL;
  data->gi = GeoIP_open(db, GEOIP_INDEX_CACHE);
  if (data->gi == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "GeoIP database open fail");
  }
  data->gir = NULL;
  DATA_PTR(self) = data;

  return self;
}

static mrb_value mrb_geoip_record_by_name(mrb_state *mrb, mrb_value self)
{
  mrb_geoip_data *data = DATA_PTR(self);
  char *host;

  if (data->gir != NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "GeoIP record already exits");
  }
  mrb_get_args(mrb, "z", &host);
  data->host = host;
  data->gir = GeoIP_record_by_name(data->gi, data->host);
  if (data->gir == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "not found GeoIP record for hostanme");<commit_msg>GeoIPRecord struct was initialized when already exists<commit_after>/*
** mrb_geoip.c - GeoIP class
**
** Copyright (c) <NAME> 2014
**
** See Copyright Notice in LICENSE
*/

#include "mruby.h"
#include "mruby/data.h"
#include "mrb_geoip.h"
#include <GeoIP.h>
#include <GeoIPCity.h>

#define DONE mrb_gc_arena_restore(mrb, 0);

typedef struct {
  GeoIP *gi;
  GeoIPRecord *gir;
  char *db;
  char *host;
} mrb_geoip_data;

static const char *MK_STR(const char *str){
    return str ? str : "N/A";
}

static void mrb_geoip_free(mrb_state *mrb, void *p)
{
  mrb_geoip_data *data = p;

  GeoIPRecord_delete(data->gir);
  GeoIP_delete(data->gi);
}

static const struct mrb_data_type mrb_geoip_data_type = {
  "mrb_geoip_data", mrb_geoip_free,
};

static mrb_value mrb_geoip_init(mrb_state *mrb, mrb_value self)
{
  mrb_geoip_data *data;
  char *db;

  data = (mrb_geoip_data *)DATA_PTR(self);
  if (data) {
    mrb_free(mrb, data);
  }
  DATA_TYPE(self) = &mrb_geoip_data_type;
  DATA_PTR(self) = NULL;

  mrb_get_args(mrb, "z", &db);
  data = (mrb_geoip_data *)mrb_malloc(mrb, sizeof(mrb_geoip_data));
  data->db = db;
  data->host = NULL;
  data->gi = GeoIP_open(db, GEOIP_INDEX_CACHE);
  if (data->gi == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "GeoIP database open fail");
  }
  data->gir = NULL;
  DATA_PTR(self) = data;

  return self;
}

static mrb_value mrb_geoip_record_by_name(mrb_state *mrb, mrb_value self)
{
  mrb_geoip_data *data = DATA_PTR(self);
  char *host;

  if (data->gir != NULL) {
    GeoIPRecord_delete(data->gir);
    data->gir = NULL;
  }
  mrb_get_args(mrb, "z", &host);
  data->host = host;
  data->gir = GeoIP_record_by_name(data->gi, data->host);
  if (data->gir == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "not found GeoIP record for hostanme");
{'fraction_non_alphanumeric': 0.14533068407607153, 'fraction_numerical': 0.002838489923360772, 'mean_word_length': 4.41321044546851, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '3987255', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1541, 'n_words': 312}