<commit_before>// Copyright 2014 Google Inc. All rights reserved.
//
// 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.

// clang-format off
#include "precompiled.h"
#include "fplbase/utilities.h"
// clang-format on

#if defined(__ANDROID__)
#include <string>
#endif  // defined(__ANDROID__)

// Header files for mmap API.
#ifdef _WIN32
#else
// Platforms with POSIX
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#endif  // _WIN32

#if defined(FPLBASE_BACKEND_STDLIB)
#if !defined(_CRT_SECURE_NO_DEPRECATE)
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <fcntl.h>
#include <stdarg.h>
#include <cstdio>

#if defined(__ANDROID__)
#if defined(FPLBASE_BACKEND_SDL)
#include "SDL_thread.h"
#endif  // defined(FPLBASE_BACKEND_SDL)
#include <android/log.h>
namespace {
static AAssetManager *g_asset_manager = nullptr;
}
#endif  // defined(__ANDROID__)

// clang-format off
#if defined(__APPLE__)
#include "TargetConditionals.h"
#include <CoreFoundation/CoreFoundation.h>
#endif  // defined(__APPLE__)
// clang-format on

#endif  // defined(FPLBASE_BACKEND_STDLIB)

namespace fplbase {

#ifdef FPLBASE_BACKEND_SDL
static_assert(kApplication ==
                  static_cast<LogCategory>(SDL_LOG_CATEGORY_APPLICATION),
              "update kApplication");
static_assert(kError == static_cast<LogCategory>(SDL_LOG_CATEGORY_ERROR),
              "update kError");
static_assert(kSystem == static_cast<LogCategory>(SDL_LOG_CATEGORY_SYSTEM),
              "update kSystem");
static_assert(kAudio == static_cast<LogCategory>(SDL_LOG_CATEGORY_AUDIO),
              "update kAudio");
static_assert(kVideo == static_cast<LogCategory>(SDL_LOG_CATEGORY_VIDEO),
              "update kVideo");
static_assert(kRender == static_cast<LogCategory>(SDL_LOG_CATEGORY_RENDER),
              "update kRender");
static_assert(kInput == static_cast<LogCategory>(SDL_LOG_CATEGORY_INPUT),
              "update kInput");
static_assert(kCustom == static_cast<LogCategory>(SDL_LOG_CATEGORY_CUSTOM),
              "update kCustom");
#endif  // FPLBASE_BACKEND_SDL

// Function called by LoadFile().
static LoadFileFunction g_load_file_function = LoadFileRaw;

LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
  LoadFileFunction previous_function = g_load_file_function;
  if (load_file_function) {
    g_load_file_function = load_file_function;
  } else {
    g_load_file_function = LoadFileRaw;
  }
  return previous_function;
}

bool LoadFile(const char *filename, std::string *dest) {
  assert(g_load_file_function);
  return g_load_file_function(filename, dest);
}

#ifdef FPLBASE_BACKEND_SDL
bool LoadFileRaw(const char *filename, std::string *dest) {
  auto handle = SDL_RWFromFile(filename, "rb");
  if (!handle) {
    LogError(kError, "LoadFile fail on %s", filename);
    return false;
  }
  auto len = static_cast<size_t>(SDL_RWseek(handle, 0, RW_SEEK_END));
  SDL_RWseek(handle, 0, RW_SEEK_SET);
  dest->assign(len, 0);
  size_t rlen = static_cast<size_t>(SDL_RWread(handle, &(*dest)[0], 1, len));
  SDL_RWclose(handle);
  return len == rlen && len > 0;
}
#elif defined(FPLBASE_BACKEND_STDLIB)
#if defined(__ANDROID__)
bool LoadFileRaw(const char *filename, std::string *dest) {
  if (!g_asset_manager) {
    LogError(kError,<commit_msg>    Protect g_load_file_function with a mutex.<commit_after>// Copyright 2014 Google Inc. All rights reserved.
//
// 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.

// clang-format off
#include "precompiled.h"
#include "fplbase/utilities.h"
#include "fplutil/mutex.h"
// clang-format on

#if defined(__ANDROID__)
#include <string>
#endif  // defined(__ANDROID__)

// Header files for mmap API.
#ifdef _WIN32
#else
// Platforms with POSIX
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#endif  // _WIN32

#if defined(FPLBASE_BACKEND_STDLIB)
#if !defined(_CRT_SECURE_NO_DEPRECATE)
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <fcntl.h>
#include <stdarg.h>
#include <cstdio>

#if defined(__ANDROID__)
#if defined(FPLBASE_BACKEND_SDL)
#include "SDL_thread.h"
#endif  // defined(FPLBASE_BACKEND_SDL)
#include <android/log.h>
namespace {
static AAssetManager *g_asset_manager = nullptr;
}
#endif  // defined(__ANDROID__)

// clang-format off
#if defined(__APPLE__)
#include "TargetConditionals.h"
#include <CoreFoundation/CoreFoundation.h>
#endif  // defined(__APPLE__)
// clang-format on

#endif  // defined(FPLBASE_BACKEND_STDLIB)

namespace fplbase {

#ifdef FPLBASE_BACKEND_SDL
static_assert(kApplication ==
                  static_cast<LogCategory>(SDL_LOG_CATEGORY_APPLICATION),
              "update kApplication");
static_assert(kError == static_cast<LogCategory>(SDL_LOG_CATEGORY_ERROR),
              "update kError");
static_assert(kSystem == static_cast<LogCategory>(SDL_LOG_CATEGORY_SYSTEM),
              "update kSystem");
static_assert(kAudio == static_cast<LogCategory>(SDL_LOG_CATEGORY_AUDIO),
              "update kAudio");
static_assert(kVideo == static_cast<LogCategory>(SDL_LOG_CATEGORY_VIDEO),
              "update kVideo");
static_assert(kRender == static_cast<LogCategory>(SDL_LOG_CATEGORY_RENDER),
              "update kRender");
static_assert(kInput == static_cast<LogCategory>(SDL_LOG_CATEGORY_INPUT),
              "update kInput");
static_assert(kCustom == static_cast<LogCategory>(SDL_LOG_CATEGORY_CUSTOM),
              "update kCustom");
#endif  // FPLBASE_BACKEND_SDL

// Function called by LoadFile().
static fplutil::Mutex g_load_file_function_mutex_;
static LoadFileFunction g_load_file_function = LoadFileRaw;

LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
  fplutil::MutexLock lock(g_load_file_function_mutex_);
  LoadFileFunction previous_function = g_load_file_function;
  if (load_file_function) {
    g_load_file_function = load_file_function;
  } else {
    g_load_file_function = LoadFileRaw;
  }
  return previous_function;
}

bool LoadFile(const char *filename, std::string *dest) {
  LoadFileFunction load_file_function;
  {
    fplutil::MutexLock lock(g_load_file_function_mutex_);
    load_file_function = g_load_file_function;
  }
  assert(load_file_function);
  return load_file_function(filename, dest);
}

#ifdef FPLBASE_BACKEND_SDL
bool LoadFileRaw(const char *filename, std::string *dest) {
  auto handle = SDL_RWFromFile(filename, "rb");
  if (!handle) {
    LogError(kError, "LoadFile fail on %s", filename);
    return false;
  }
  auto len = static_cast<size_t>(SDL_RWseek(handle, 0, RW_SEEK_END));
  SDL_RWseek(handle, 0, RW_SEEK_SET);
  dest->assign(len, 0);
  size_t rlen = static_cast<size_t>(SDL_RWread(handle, &(*dest)[0], 1, len));
  SDL_RWclose(handle);
  return len == rlen && len > 0;
}
#elif defined(FPLBASE_BACKEND_STDLIB)
#if defined(__ANDROID__)
bool LoadFileRaw(const char *filename, std::string *dest) {
  if (!g_asset_manager) {
    LogError(kError,
{'fraction_non_alphanumeric': 0.11064372211599745, 'fraction_numerical': 0.004588910133843212, 'mean_word_length': 4.829123328380386, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6228397', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2614, 'n_words': 599}