<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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 "stdafx.h"

#include <cstdio>
#include <memory.h>
#include <string>
#include <iostream>

#include <libxml/xmlmemory.h>

#include <zorba/store_consts.h>

#include "store_defs.h"
#include "string_pool.h"
#include "qname_pool.h"
#include "simple_store.h"
#include "atomic_items.h"
#include "node_items.h"
#include "dataguide.h"
#include "nsbindings.h"
#include "loader.h"
#include "simple_item_factory.h"
#include "node_factory.h"

#include "zorbatypes/datetime.h"
#include "zorbatypes/URI.h"

#include "diagnostics/xquery_diagnostics.h"
#include "diagnostics/assert.h"


namespace zorba {
namespace simplestore {

#ifndef NDEBUG

#define LOADER_TRACE(level, msg)             \
{                                            \
  if (level <= loader.theTraceLevel)         \
    std::cout << msg << std::endl;           \
}

#define LOADER_TRACE1(msg) LOADER_TRACE(1, msg);
#define LOADER_TRACE2(msg) LOADER_TRACE(2, msg);
#define LOADER_TRACE3(msg) LOADER_TRACE(3, msg);

#else
#define LOADER_TRACE(msg)
#define LOADER_TRACE1(msg)
#define LOADER_TRACE2(msg)
#define LOADER_TRACE3(msg)
#endif

#define ZORBA_LOADER_CHECK_ERROR(loader) \
  do { \
    if (!loader.theXQueryDiagnostics->errors().empty()) \
      return; \
  } while (0);


/*******************************************************************************

********************************************************************************/
XmlLoader::XmlLoader(
    store::ItemFactory* factory,
    XQueryDiagnostics* xqueryDiagnostics,
    const store::LoadProperties& loadProperties,
    bool dataguide)
  :
  theLoadProperties(loadProperties),
  ctxt(NULL),
  theFactory(static_cast<BasicItemFactory*>(factory)),
  theXQueryDiagnostics(xqueryDiagnostics),
  theTraceLevel(0),
  theBuildDataGuide(dataguide)
{
}


XmlLoader::~XmlLoader() 
{
}


/*******************************************************************************

********************************************************************************/
void XmlLoader::error(void *ctx, xmlErrorPtr error)
{
  if ( error->level == XML_ERR_NONE )
    return;

  ztd::itoa_buf_type itoa_buf;

  zstring libxml_dict_key_4( ZED_PREFIX "libxml_" );
  libxml_dict_key_4 += error->level == XML_ERR_WARNING ? "WAR_" : "ERR_";
  libxml_dict_key_4 += ztd::itoa( error->code, itoa_buf );

  zstring str1_5, str2_6, str3_7, int1_8, message_9( error->message );
  if ( error->str1 )
    str1_5 = error->str1;
  if ( error->str2 )
    str2_6 = error->str2;
  if ( error->str3 )
    str3_7 = error->str3;

  if ( error->int1 ) {                  // assume valid only if > 0
    switch ( error->code ) {
      case XML_ERR_ENTITY_CHAR_ERROR:
      case XML_ERR_INVALID_CHAR:
      case XML_ERR_SEPARATOR_REQUIRED:
        // For these error codes, int1 is a char.
        int1_8 = static_cast<char>( error->int1 );
        break;
      case XML_DTD_ID_SUBSET:
      case XML_DTD_UNKNOWN_ID:
      case XML_ERR_ELEMCONTENT_NOT_FINISHED:
      case XML_ERR_INTERNAL_ERROR:
      case XML_ERR_TAG_NOT_FINISHED:
        // For these error codes, int1 is an int.
        int1_8 = ztd::itoa( error->int1, itoa_buf );
        break;
      default:
        // For an unaccounted-for error code, use a heuristic to guess whether
        // int1 is a char or an int.
        if ( ascii::is_print( error->int1 ) )
          int1_8 = static_cast<char>( error->int1 );
        else
          int1_8 = ztd::itoa( error->int1, itoa_buf );
    } // switch
  } // if

  XmlLoader *const loader = static_cast<FastXmlLoader*>(ctx);
  switch ( error->level ) {
    case XML_ERR_ERROR:
    case XML_ERR_FATAL:
      loader->theXQueryDiagnostics->add_error(
        NEW_XQUERY_EXCEPTION(
          zerr::ZSTR0021_LOADER_PARSING_ERROR,
          ERROR_PARAMS(
            error->file, error->line, error->int2 /* column */,
            libxml_dict_key_4, str1_5, str2_6, str3_7, int1_8, message_9
          )
        )
      );
      break;
    case XML_ERR_WARNING:
      loader->theXQueryDiagnostics->add_warning(
        NEW_XQUERY_WARNING(
          zwarn::ZWST0007_LOADER_PARSING_WARNING,
          WARN_PARAMS(
            error->file, error->line, error->int2 /* column */,
            libxml_dict_key_4, str1_5, str2_6, str3_7, int1_8, message_9
          )
        )
      );
      break;
    default:
      ZORBA_ASSERT( false );
  } // switch
}
<commit_msg>Now using char const*; fixed static_cast.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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 "stdafx.h"

#include <cstdio>
#include <memory.h>
#include <string>
#include <iostream>

#include <libxml/xmlmemory.h>

#include <zorba/store_consts.h>

#include "store_defs.h"
#include "string_pool.h"
#include "qname_pool.h"
#include "simple_store.h"
#include "atomic_items.h"
#include "node_items.h"
#include "dataguide.h"
#include "nsbindings.h"
#include "loader.h"
#include "simple_item_factory.h"
#include "node_factory.h"

#include "zorbatypes/datetime.h"
#include "zorbatypes/URI.h"

#include "diagnostics/xquery_diagnostics.h"
#include "diagnostics/assert.h"


namespace zorba {
namespace simplestore {

#ifndef NDEBUG

#define LOADER_TRACE(level, msg)             \
{                                            \
  if (level <= loader.theTraceLevel)         \
    std::cout << msg << std::endl;           \
}

#define LOADER_TRACE1(msg) LOADER_TRACE(1, msg);
#define LOADER_TRACE2(msg) LOADER_TRACE(2, msg);
#define LOADER_TRACE3(msg) LOADER_TRACE(3, msg);

#else
#define LOADER_TRACE(msg)
#define LOADER_TRACE1(msg)
#define LOADER_TRACE2(msg)
#define LOADER_TRACE3(msg)
#endif

#define ZORBA_LOADER_CHECK_ERROR(loader) \
  do { \
    if (!loader.theXQueryDiagnostics->errors().empty()) \
      return; \
  } while (0);


/*******************************************************************************

********************************************************************************/
XmlLoader::XmlLoader(
    store::ItemFactory* factory,
    XQueryDiagnostics* xqueryDiagnostics,
    const store::LoadProperties& loadProperties,
    bool dataguide)
  :
  theLoadProperties(loadProperties),
  ctxt(NULL),
  theFactory(static_cast<BasicItemFactory*>(factory)),
  theXQueryDiagnostics(xqueryDiagnostics),
  theTraceLevel(0),
  theBuildDataGuide(dataguide)
{
}


XmlLoader::~XmlLoader() 
{
}


/*******************************************************************************

********************************************************************************/
void XmlLoader::error(void *ctx, xmlErrorPtr error)
{
  if ( error->level == XML_ERR_NONE )
    return;

  ztd::itoa_buf_type itoa_buf;

  zstring libxml_dict_key_4( ZED_PREFIX "libxml_" );
  libxml_dict_key_4 += error->level == XML_ERR_WARNING ? "WAR_" : "ERR_";
  libxml_dict_key_4 += ztd::itoa( error->code, itoa_buf );

  char const *const str1_5 = error->str1 ? error->str1 : "";
  char const *const str2_6 = error->str2 ? error->str2 : "";
  char const *const str3_7 = error->str3 ? error->str3 : "";
  zstring int1_8;
  char const *const message_9 = error->message ? error->message : "";

  if ( error->int1 ) {                  // assume valid only if > 0
    switch ( error->code ) {
      case XML_ERR_ENTITY_CHAR_ERROR:
      case XML_ERR_INVALID_CHAR:
      case XML_ERR_SEPARATOR_REQUIRED:
        // For these error codes, int1 is a char.
        int1_8 = static_cast<char>( error->int1 );
        break;
      case XML_DTD_ID_SUBSET:
      case XML_DTD_UNKNOWN_ID:
      case XML_ERR_ELEMCONTENT_NOT_FINISHED:
      case XML_ERR_INTERNAL_ERROR:
      case XML_ERR_TAG_NOT_FINISHED:
        // For these error codes, int1 is an int.
        int1_8 = ztd::itoa( error->int1, itoa_buf );
        break;
      default:
        // For an unaccounted-for error code, use a heuristic to guess whether
        // int1 is a char or an int.
        if ( ascii::is_print( error->int1 ) )
          int1_8 = static_cast<char>( error->int1 );
        else
          int1_8 = ztd::itoa( error->int1, itoa_buf );
    } // switch
  } // if

  XmlLoader *const loader = static_cast<XmlLoader*>( ctx );
  switch ( error->level ) {
    case XML_ERR_ERROR:
    case XML_ERR_FATAL:
      loader->theXQueryDiagnostics->add_error(
        NEW_XQUERY_EXCEPTION(
          zerr::ZSTR0021_LOADER_PARSING_ERROR,
          ERROR_PARAMS(
            error->file, error->line, error->int2 /* column */,
            libxml_dict_key_4, str1_5, str2_6, str3_7, int1_8, message_9
          )
        )
      );
      break;
    case XML_ERR_WARNING:
      loader->theXQueryDiagnostics->add_warning(
        NEW_XQUERY_WARNING(
          zwarn::ZWST0007_LOADER_PARSING_WARNING,
          WARN_PARAMS(
            error->file, error->line, error->int2 /* column */,
            libxml_dict_key_4, str1_5, str2_6, str3_7, int1_8, message_9
          )
        )
      );
      break;
    default:
      ZORBA_ASSERT( false );
  } // switch
}

{'fraction_non_alphanumeric': 0.17024035105215918, 'fraction_numerical': 0.01835045377480802, 'mean_word_length': 3.294646680942184, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4417344', 'n_tokens_mistral': 3323, 'n_tokens_neox': 3258, 'n_words': 735}