<commit_before>/*
 * Duck: a Daisy 2.02 parser API.
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * This file is part of libduck.
 *
 * libduck is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libduck is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libduck; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>

#include "duck.h"
#include "duck_internals.h"
#include "parser.h"
#include "utils.h"
#include "logs.h"


unsigned int
libduck_version (void)
{
  return LIBDUCK_VERSION_INT;
}

void
duck_verbosity (duck_verb_t level)
{
  dd_log_verb (level);
}

void
duck_uninit (duck_t *handle)
{
  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle)
    return;

  dd_daisydata_free (handle->data);
  free (handle);
}

duck_t *
duck_init (duck_init_param_t *param)
{
  duck_t *handle;
  duck_init_param_t p;
  const duck_init_param_t *pp = &p;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (param)
    pp = param;
  else
    memset (&p, 0, sizeof (p));

  handle = calloc (1, sizeof (duck_t));
  if (!handle)
    return NULL;

  return handle;
}

int
duck_getpos (duck_t *handle, int *smilpos, int *nodepos)
{
  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data)
    return -1;

  if (smilpos)
  {
    if (!handle->data->smil_pos)
      return -1;
    *smilpos = handle->data->smil_pos->item_id;
  }

  if (nodepos)
  {
    if (!handle->data->node_pos)
      return -1;
    *nodepos = handle->data->node_pos->item_id;
  }

  return 0;
}

int
duck_getheading (duck_t *handle, duck_hx_t *hx)
{
  const smilnode_t *smil_n;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data || !hx)
    return -1;

  memset (hx, 0, sizeof (*hx));

  smil_n = handle->data->smil_head;
  for (; smil_n; smil_n = smil_n->next)
  {
    if (smil_n->type != DUCK_SMILNODE_HEADING)
      continue;

    switch (smil_n->level)
    {
    case 1:
      hx->h1++;
      hx->h2 = hx->h3 = hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 2:
      hx->h2++;
      hx->h3 = hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 3:
      hx->h3++;
      hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 4:
      hx->h4++;
      hx->h5 = hx->h6 = 0;
      break;

    case 5:
      hx->h5++;
      hx->h6 = 0;
      break;

    case 6:
      hx->h6++;
      break;

    default:
      return -1;
    }

    if (smil_n == handle->data->smil_pos)
      break;
  }

  return 0;
}

int
duck_walk_smil (duck_t *handle, int smilpos)
{
  int rc;
  node_t *node_n;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data)
    return -1;

  rc = duck_walk (handle, smilpos, 1);
  if (rc)
    return rc;

  node_n = handle->data->node_head;
  if (!node_n || !handle->data->smil_pos->fragment_identifier)
    return 0;

  for (; node_n; node_n = node_n->next)
    if (node_n->fragment_identifier
        && !strcmp (handle->data->smil_pos->fragment_identifier,
                    node_n->fragment_identifier))
      break;

  if (!node_n)
    return 0;

  handle->data->node_pos = node_n;
  return 0;
}

int
duck_walk_time (duck_t *handle, int smilpos, int time)
{
  int i;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);
  
  if (!handle || !handle->data)
    return -1;
  
  for (i = 0;; ++i)
  {
    if (duck_walk (handle, smilpos, i))
      return -1;
   
    node_t *node_n = handle->data->node_pos;
    if (!node_n)
      return -1;
    
    if (   time >= node_n->audio_pos_start
        && time <  node_n->audio_pos_stop)<commit_msg>the fragment identifier of the smil can never be the same that the fragment identifier of the node; then we remove it<commit_after>/*
 * Duck: a Daisy 2.02 parser API.
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * This file is part of libduck.
 *
 * libduck is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libduck is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libduck; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>

#include "duck.h"
#include "duck_internals.h"
#include "parser.h"
#include "utils.h"
#include "logs.h"


unsigned int
libduck_version (void)
{
  return LIBDUCK_VERSION_INT;
}

void
duck_verbosity (duck_verb_t level)
{
  dd_log_verb (level);
}

void
duck_uninit (duck_t *handle)
{
  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle)
    return;

  dd_daisydata_free (handle->data);
  free (handle);
}

duck_t *
duck_init (duck_init_param_t *param)
{
  duck_t *handle;
  duck_init_param_t p;
  const duck_init_param_t *pp = &p;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (param)
    pp = param;
  else
    memset (&p, 0, sizeof (p));

  handle = calloc (1, sizeof (duck_t));
  if (!handle)
    return NULL;

  return handle;
}

int
duck_getpos (duck_t *handle, int *smilpos, int *nodepos)
{
  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data)
    return -1;

  if (smilpos)
  {
    if (!handle->data->smil_pos)
      return -1;
    *smilpos = handle->data->smil_pos->item_id;
  }

  if (nodepos)
  {
    if (!handle->data->node_pos)
      return -1;
    *nodepos = handle->data->node_pos->item_id;
  }

  return 0;
}

int
duck_getheading (duck_t *handle, duck_hx_t *hx)
{
  const smilnode_t *smil_n;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data || !hx)
    return -1;

  memset (hx, 0, sizeof (*hx));

  smil_n = handle->data->smil_head;
  for (; smil_n; smil_n = smil_n->next)
  {
    if (smil_n->type != DUCK_SMILNODE_HEADING)
      continue;

    switch (smil_n->level)
    {
    case 1:
      hx->h1++;
      hx->h2 = hx->h3 = hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 2:
      hx->h2++;
      hx->h3 = hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 3:
      hx->h3++;
      hx->h4 = hx->h5 = hx->h6 = 0;
      break;

    case 4:
      hx->h4++;
      hx->h5 = hx->h6 = 0;
      break;

    case 5:
      hx->h5++;
      hx->h6 = 0;
      break;

    case 6:
      hx->h6++;
      break;

    default:
      return -1;
    }

    if (smil_n == handle->data->smil_pos)
      break;
  }

  return 0;
}

int
duck_walk_smil (duck_t *handle, int smilpos)
{
  int rc;
  node_t *node_n;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);

  if (!handle || !handle->data)
    return -1;

  rc = duck_walk (handle, smilpos, 1);
  if (rc)
    return rc;

  node_n = handle->data->node_head;
  if (!node_n)
    return 0;

  handle->data->node_pos = node_n;
  return 0;
}

int
duck_walk_time (duck_t *handle, int smilpos, int time)
{
  int i;

  dd_log (DUCK_MSG_VERBOSE, __FUNCTION__);
  
  if (!handle || !handle->data)
    return -1;
  
  for (i = 0;; ++i)
  {
    if (duck_walk (handle, smilpos, i))
      return -1;
   
    node_t *node_n = handle->data->node_pos;
    if (!node_n)
      return -1;
    
    if (   time >= node_n->audio_pos_start
        && time <  node_n->audio_pos_stop)
{'fraction_non_alphanumeric': 0.13200654994331779, 'fraction_numerical': 0.01776042322710669, 'mean_word_length': 2.7347130761994354, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 126, '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': '1003855', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3114, 'n_words': 849}