<commit_before>/* inchi_input.cc
 * Copyright 2014 Cubane Canada, Inc.
 *
 * Released under the MIT license -- see MIT-LICENSE for details
 */
#include "node.h"
#include "v8.h"
#include "uv.h"
#include "nan.h"

#include "./inchi_input.h"

#include "./using_v8.h"

/**
 * Wrapper for INCHI_Input structure
 *
 * @module InChILib
 * @class  InchiInput
 */

/* local (non-API) functions */
static void populate_input(Handle<Value> val, inchi_Input* in);
static void populate_atom(const Handle<Object> atom, inchi_Atom* target);
static void populate_ret(Handle<Object> ret, const inchi_Output& out, int result);
static void addstring(Handle<Object> ret, const char * name, const char * value);



/**
 * Create an InchiInput structure from a partially or fully specified
 * JavaScript object
 *
 * @method Create
 * @param {Handle<Object>} input Object handle that parallels an inchi_Input
 * @param
 */
InchiInput * InchiInput::Create(Handle<Value> val) {
  // validation

  // create an empty InchiInput
  InchiInput * input = new InchiInput;

  // populate it
  populate_input(val, &(input->in_));

  // return it
  return input;
}

int InchiInput::GetInchi() {
  result_ = GetINCHI(&(this->in_), &(this->out_));

  return result_;
}

Handle<Object> InchiInput::GetResult() {
  Local<Object> ret = Object::New();

  populate_ret(ret, this->out_, result_);

  return ret;
}

void populate_atom(const Handle<Object> atom, inchi_Atom* target) {
  // initialize atom
  memset(target, 0, sizeof(*target));

  // atom.name is required
  Handle<String> elname = atom->Get(NanSymbol("elname"))->ToString();

  NanCString(elname, 0, target->elname, ATOM_EL_LEN);
  elname->WriteAscii(target->elname, 0, ATOM_EL_LEN);
  target->elname[ATOM_EL_LEN-1] = '\0';  // ensure termination

  // populate neighbor array
  Handle<Array> neighbor =
    Handle<Array>::Cast(atom->Get(NanSymbol("neighbor")));

  target->num_bonds = neighbor->Length();
  for (int i = 0; i < target->num_bonds; ++i) {
    target->neighbor[i]    = neighbor->Get(i)->ToNumber()->Value();

    // TODO(SOM): bond types & stero too
    target->bond_type[i]   = INCHI_BOND_TYPE_SINGLE;
    target->bond_stereo[i] = INCHI_BOND_STEREO_NONE;
  }

  // default fill atoms with default isotopes
  target->num_iso_H[0] = -1;
}

static void populate_input(Handle<Value> val, inchi_Input* in) {
  // TODO(SOM): support validation, possibly return error code

  // expect args[0] to be an Object, call it 'mol'
  // expect mol.atom to be an Array<commit_msg>remove direct call to WriteAscii()<commit_after>/* inchi_input.cc
 * Copyright 2014 Cubane Canada, Inc.
 *
 * Released under the MIT license -- see MIT-LICENSE for details
 */
#include "node.h"
#include "v8.h"
#include "uv.h"
#include "nan.h"

#include "./inchi_input.h"

#include "./using_v8.h"

/**
 * Wrapper for INCHI_Input structure
 *
 * @module InChILib
 * @class  InchiInput
 */

/* local (non-API) functions */
static void populate_input(Handle<Value> val, inchi_Input* in);
static void populate_atom(const Handle<Object> atom, inchi_Atom* target);
static void populate_ret(Handle<Object> ret, const inchi_Output& out, int result);
static void addstring(Handle<Object> ret, const char * name, const char * value);



/**
 * Create an InchiInput structure from a partially or fully specified
 * JavaScript object
 *
 * @method Create
 * @param {Handle<Object>} input Object handle that parallels an inchi_Input
 * @param
 */
InchiInput * InchiInput::Create(Handle<Value> val) {
  // validation

  // create an empty InchiInput
  InchiInput * input = new InchiInput;

  // populate it
  populate_input(val, &(input->in_));

  // return it
  return input;
}

int InchiInput::GetInchi() {
  result_ = GetINCHI(&(this->in_), &(this->out_));

  return result_;
}

Handle<Object> InchiInput::GetResult() {
  Local<Object> ret = Object::New();

  populate_ret(ret, this->out_, result_);

  return ret;
}

void populate_atom(const Handle<Object> atom, inchi_Atom* target) {
  // initialize atom
  memset(target, 0, sizeof(*target));

  // atom.name is required
  Handle<String> elname = atom->Get(NanSymbol("elname"))->ToString();

  NanCString(elname, 0, target->elname, ATOM_EL_LEN);
  target->elname[ATOM_EL_LEN-1] = '\0';  // ensure termination

  // populate neighbor array
  Handle<Array> neighbor =
    Handle<Array>::Cast(atom->Get(NanSymbol("neighbor")));

  target->num_bonds = neighbor->Length();
  for (int i = 0; i < target->num_bonds; ++i) {
    target->neighbor[i]    = neighbor->Get(i)->ToNumber()->Value();

    // TODO(SOM): bond types & stero too
    target->bond_type[i]   = INCHI_BOND_TYPE_SINGLE;
    target->bond_stereo[i] = INCHI_BOND_STEREO_NONE;
  }

  // default fill atoms with default isotopes
  target->num_iso_H[0] = -1;
}

static void populate_input(Handle<Value> val, inchi_Input* in) {
  // TODO(SOM): support validation, possibly return error code

  // expect args[0] to be an Object, call it 'mol'
  // expect mol.atom to be an Array
{'fraction_non_alphanumeric': 0.14377639251960586, 'fraction_numerical': 0.005831490046249749, 'mean_word_length': 4.1384297520661155, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 69, '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': '3469138', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1676, 'n_words': 444}