<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 <libxml/parser.h>
#include "common/common.h"

#ifndef ZORBA_NO_UNICODE
#include <unicode/uclean.h>
#endif//#ifndef ZORBA_NO_UNICODE

#include "common/common.h"

#ifdef ZORBA_WITH_REST
#include <curl/curl.h>
#endif

#ifndef ZORBA_NO_BIGNUMBERS
#include "zorbatypes/m_apm.h"
#endif

#include "globalenv.h"
#include "types/root_typemanager.h"
#include "context/root_static_context.h"
#include "functions/library.h"
#include "store/current_store_headers.h"
#include "compiler/api/compiler_api.h"
#include "types/schema/schema.h"
#ifndef ZORBA_NO_UNICODE
#include <unicode/utypes.h>
#include <unicode/udata.h>
#endif//#ifndef ZORBA_NO_UNICODE
#include "store/api/collection.h"

#ifdef ZORBA_XBROWSER
#include "DOMStoreSingelton.h"
#endif

using namespace zorba;

GlobalEnvironment * GlobalEnvironment::m_globalEnv = 0;


GlobalEnvironment& GlobalEnvironment::getInstance()
{
  assert(m_globalEnv);

  if (!m_globalEnv) {
    GlobalEnvironment::init();
  }
  return *m_globalEnv;
}


void GlobalEnvironment::init()
{
  // initialize Xerces-C lib
  Schema::initialize();

  m_globalEnv = new GlobalEnvironment();

  // initialize the icu library
  // we do this here because we are sure that is used
  // from one thread only
  // see http://www.icu-project.org/userguide/design.html#Init_and_Termination
  // and http://www.icu-project.org/apiref/icu4c/uclean_8h.html
#ifndef ZORBA_NO_UNICODE
  {
#if defined U_STATIC_IMPLEMENTATION && (defined WIN32 || defined WINCE)
    {
      char    self_path[1024];
      GetModuleFileName(NULL, self_path, sizeof(self_path));
      //PathRemoveFileSpec(self_path);
      char  *filename;
      filename = strrchr(self_path, '\\');
      if(filename)
        filename[1] = 0;
      else
        self_path[0] = 0;
      //strcat(self_path, "\\");
      strcat(self_path, U_ICUDATA_NAME);//icudt39l.dat");
      strcat(self_path, ".dat");
      //unsigned char *icu_data;
      HANDLE    hfile;
      hfile = CreateFile(self_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
      assert(hfile != INVALID_HANDLE_VALUE);
      DWORD   icusize;
      icusize = GetFileSize(hfile, NULL);
      m_globalEnv->icu_appdata = new unsigned char[icusize];
      DWORD   nr_read;
      ReadFile(hfile, m_globalEnv->icu_appdata, icusize, &nr_read, NULL);
      CloseHandle(hfile);
      UErrorCode    data_err = U_ZERO_ERROR;
      udata_setCommonData(m_globalEnv->icu_appdata, &data_err);
      assert(data_err == U_ZERO_ERROR);

    //  u_setDataDirectory(self_path);
    }
#endif
    UErrorCode lICUInitStatus = U_ZERO_ERROR;
    u_init(&lICUInitStatus);
    assert(lICUInitStatus == U_ZERO_ERROR);
  }
#endif//ifndef ZORBA_NO_UNICODE
  
#ifdef ZORBA_XBROWSER
  m_globalEnv->m_store.reset(xqp::DOMStoreSingelton::getInstance()->getStore());
  //static_cast<store::SimpleStore *>(m_globalEnv->m_store.get())->init();
#else
  m_globalEnv->m_store.reset(new SimpleStore());
  static_cast<store::SimpleStore *>(m_globalEnv->m_store.get())->init();
#endif

  root_static_context *rctx = new root_static_context();
  m_globalEnv->m_rootStaticContext.reset(rctx);
  rctx->init();
  BuiltinFunctionLibrary::populateContext(m_globalEnv->m_rootStaticContext.get());

#ifdef ZORBA_MINIMAL_STORE
  //for minimal store init types is based on root type manager
#ifndef ZORBA_XBROWSER<commit_msg>yet another bugfix<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 <libxml/parser.h>
#include "common/common.h"

#ifndef ZORBA_NO_UNICODE
#include <unicode/uclean.h>
#endif//#ifndef ZORBA_NO_UNICODE

#include "common/common.h"

#ifdef ZORBA_WITH_REST
#include <curl/curl.h>
#endif

#ifndef ZORBA_NO_BIGNUMBERS
#include "zorbatypes/m_apm.h"
#endif

#include "globalenv.h"
#include "types/root_typemanager.h"
#include "context/root_static_context.h"
#include "functions/library.h"
#include "store/current_store_headers.h"
#include "compiler/api/compiler_api.h"
#include "types/schema/schema.h"
#ifndef ZORBA_NO_UNICODE
#include <unicode/utypes.h>
#include <unicode/udata.h>
#endif//#ifndef ZORBA_NO_UNICODE
#include "store/api/collection.h"

#ifdef ZORBA_XBROWSER
#include "DOMStoreSingelton.h"
#endif

using namespace zorba;

GlobalEnvironment * GlobalEnvironment::m_globalEnv = 0;


GlobalEnvironment& GlobalEnvironment::getInstance()
{
  assert(m_globalEnv);

  if (!m_globalEnv) {
    GlobalEnvironment::init();
  }
  return *m_globalEnv;
}


void GlobalEnvironment::init()
{
  // initialize Xerces-C lib
  Schema::initialize();

  m_globalEnv = new GlobalEnvironment();

  // initialize the icu library
  // we do this here because we are sure that is used
  // from one thread only
  // see http://www.icu-project.org/userguide/design.html#Init_and_Termination
  // and http://www.icu-project.org/apiref/icu4c/uclean_8h.html
#ifndef ZORBA_NO_UNICODE
  {
#if defined U_STATIC_IMPLEMENTATION && (defined WIN32 || defined WINCE)
    {
      char    self_path[1024];
      GetModuleFileName(NULL, self_path, sizeof(self_path));
      //PathRemoveFileSpec(self_path);
      char  *filename;
      filename = strrchr(self_path, '\\');
      if(filename)
        filename[1] = 0;
      else
        self_path[0] = 0;
      //strcat(self_path, "\\");
      strcat(self_path, U_ICUDATA_NAME);//icudt39l.dat");
      strcat(self_path, ".dat");
      //unsigned char *icu_data;
      HANDLE    hfile;
      hfile = CreateFile(self_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
      assert(hfile != INVALID_HANDLE_VALUE);
      DWORD   icusize;
      icusize = GetFileSize(hfile, NULL);
      m_globalEnv->icu_appdata = new unsigned char[icusize];
      DWORD   nr_read;
      ReadFile(hfile, m_globalEnv->icu_appdata, icusize, &nr_read, NULL);
      CloseHandle(hfile);
      UErrorCode    data_err = U_ZERO_ERROR;
      udata_setCommonData(m_globalEnv->icu_appdata, &data_err);
      assert(data_err == U_ZERO_ERROR);

    //  u_setDataDirectory(self_path);
    }
#endif
    UErrorCode lICUInitStatus = U_ZERO_ERROR;
    u_init(&lICUInitStatus);
    assert(lICUInitStatus == U_ZERO_ERROR);
  }
#endif//ifndef ZORBA_NO_UNICODE
  
#ifdef ZORBA_XBROWSER
  m_globalEnv->m_store.reset(xqp::DOMStoreSingelton::getInstance()->getStore());
  //static_cast<store::SimpleStore *>(m_globalEnv->m_store.get())->init();
#else
  m_globalEnv->m_store.reset(new store::SimpleStore());
  static_cast<store::SimpleStore *>(m_globalEnv->m_store.get())->init();
#endif

  root_static_context *rctx = new root_static_context();
  m_globalEnv->m_rootStaticContext.reset(rctx);
  rctx->init();
  BuiltinFunctionLibrary::populateContext(m_globalEnv->m_rootStaticContext.get());

#ifdef ZORBA_MINIMAL_STORE
  //for minimal store init types is based on root type manager
#ifndef ZORBA_XBROWSER
{'fraction_non_alphanumeric': 0.11916182782125725, 'fraction_numerical': 0.007068921989396617, 'mean_word_length': 4.28552368245497, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, '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': '4478687', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2723, 'n_words': 587}