summaryrefslogtreecommitdiffhomepage
path: root/include/CompilerKit/Detail/Config.h
blob: 6137e2ad9c56975727a1f9a39b8406ab21139808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ========================================

  Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license

======================================== */

#pragma once

/// =========================================================== ///
/// @file detail/Config.h
/// @author Amlal El Mahrouss
/// @brief Basic defines and types for CompilerKit.
/// =========================================================== ///

#include <CompilerKit/Detail/PreConfig.h>

namespace CompilerKit {
inline constexpr int kBaseYear = 1900;
using STLString = std::string;

inline STLString current_date() noexcept {
  auto time_data   = time(nullptr);
  auto time_struct = gmtime(&time_data);

  STLString fmt = std::to_string(kBaseYear + time_struct->tm_year);

  fmt += "-";
  fmt += std::to_string(time_struct->tm_mon + 1);
  fmt += "-";
  fmt += std::to_string(time_struct->tm_mday);

  return fmt;
}

inline bool to_str(Char* str, Int32 limit, Int32 base) noexcept {
  if (limit == 0) return false;

  Int32 copy_limit = limit;
  Int32 cnt        = 0;
  Int32 ret        = base;

  while (limit != 1) {
    ret      = ret % 10;
    str[cnt] = ret;

    ++cnt;
    --limit;
    --ret;
  }

  str[copy_limit] = '\0';
  return true;
}

inline bool install_signal(Int32 signal, void (*handler)(int)) noexcept {
  if (handler == nullptr) return false;

  if (::signal(signal, handler) == SIG_ERR) {
    return false;
  }

  return true;
}
}  // namespace CompilerKit