<commit_before>/********************************
   Copyright 2008 Google Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 ********************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <crush/general.h>
#include <crush/ffutils.h>
#include <crush/bstree.h>
#include <crush/dbfr.h>
#include "filterkeys_main.h"
#include "filterkeys.h"

char default_delim[2] = { 0xfe, 0x00 };
char *delim;
struct fkeys_conf fk_conf;

/* lookup the fields to be used as filter */
static int configure_filterkeys(struct fkeys_conf *conf, struct cmdargs *args,
                                dbfr_t *ffile_reader, const char *delim) {
  char *t_keybuf;
  int i, acum_len;

  memset(conf, 0x0, sizeof(struct fkeys_conf));

  /* parse header for keys */
  dbfr_getline(ffile_reader);
  if (args->keys) {
    conf->key_count = expand_nums(args->keys, &conf->indexes, &conf->size);
  } else if (args->key_labels) {
    conf->key_count = expand_label_list(args->key_labels,
                                        ffile_reader->current_line,
                                        delim, &conf->indexes, &conf->size);
  }
  for (i = 0; i < conf->key_count; i++)
    conf->indexes[i]--;
  if (conf->key_count < 1)
    return conf->key_count;

  /* load filters */
  bst_init(&conf->ftree, (int (*)(const void*, const void*))strcmp, free);
  while (dbfr_getline(ffile_reader) > 0) {

    t_keybuf = (char *) xmalloc(ffile_reader->current_line_sz);
    conf->key_buffer_sz = (conf->key_buffer_sz < ffile_reader->current_line_sz ?
                           ffile_reader->current_line_sz : conf->key_buffer_sz);

    for (acum_len = 0, i = 0; i < conf->key_count; i++)
      acum_len += get_line_field(t_keybuf + acum_len,
                                 ffile_reader->current_line,
                                 ffile_reader->current_line_sz - acum_len,
                                 conf->indexes[i], delim);
    if (acum_len > 0) {
      bst_insert(&conf->ftree, t_keybuf);
    }
  }

  return 0;
}

/** @brief
 *
 * @param args contains the parsed cmd-line options & arguments.
 * @param argc number of cmd-line arguments.
 * @param argv list of cmd-line arguments<commit_msg>filterkeys: Assing buffer size once.<commit_after>/********************************
   Copyright 2008 Google Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 ********************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <crush/general.h>
#include <crush/ffutils.h>
#include <crush/bstree.h>
#include <crush/dbfr.h>
#include "filterkeys_main.h"
#include "filterkeys.h"

char default_delim[2] = { 0xfe, 0x00 };
char *delim;
struct fkeys_conf fk_conf;

/* lookup the fields to be used as filter */
static int configure_filterkeys(struct fkeys_conf *conf, struct cmdargs *args,
                                dbfr_t *ffile_reader, const char *delim) {
  char *t_keybuf;
  int i, acum_len;

  memset(conf, 0x0, sizeof(struct fkeys_conf));

  /* parse header for keys */
  dbfr_getline(ffile_reader);
  if (args->keys) {
    conf->key_count = expand_nums(args->keys, &conf->indexes, &conf->size);
  } else if (args->key_labels) {
    conf->key_count = expand_label_list(args->key_labels,
                                        ffile_reader->current_line,
                                        delim, &conf->indexes, &conf->size);
  }
  for (i = 0; i < conf->key_count; i++)
    conf->indexes[i]--;
  if (conf->key_count < 1)
    return conf->key_count;

  /* load filters */
  bst_init(&conf->ftree, (int (*)(const void*, const void*))strcmp, free);
  while (dbfr_getline(ffile_reader) > 0) {

    t_keybuf = (char *) xmalloc(ffile_reader->current_line_sz);
    for (acum_len = 0, i = 0; i < conf->key_count; i++)
      acum_len += get_line_field(t_keybuf + acum_len,
                                 ffile_reader->current_line,
                                 ffile_reader->current_line_sz - acum_len,
                                 conf->indexes[i], delim);
    if (acum_len > 0) {
      bst_insert(&conf->ftree, t_keybuf);
    }
  }
  conf->key_buffer_sz = ffile_reader->current_line_sz;

  return 0;
}

/** @brief
 *
 * @param args contains the parsed cmd-line options & arguments.
 * @param argc number of cmd-line arguments.
 * @param argv list of cmd-line arguments
{'fraction_non_alphanumeric': 0.1361168320539225, 'fraction_numerical': 0.008238157648380453, 'mean_word_length': 2.796730632551528, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3192266', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1662, 'n_words': 472}