diff --git a/CMakeLists.txt b/CMakeLists.txt index 1456339..96b3d94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -172,6 +172,8 @@ set(APP_SOURCES "src/core/overloading/AssignmentOperator.cpp" "src/core/overloading/ClassMemberAccessOperator.cpp" "src/core/overloading/AllocationOperator.cpp" + ## Date and Time + "src/core/datetime/CTime.cpp" ) # Test files diff --git a/README.md b/README.md index 9571397..82db833 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ ## 1. Overview +**TL;DR** +```bash +./r +``` + **Project Structure** ``` includes/ → Header files (.h, .hpp) @@ -50,6 +55,11 @@ Get-ChildItem -Recurse -Include *.cpp, *.h, *.hpp | ForEach-Object { clang-forma $ ./cpp_lab_project $ ./cpp_lab_project_test ``` + * Detect Memory Leak Using [valgrind](https://valgrind.org/) + ``` + $ sudo apt install valgrind + $ valgrind --leak-check=full -v ./cpp-lab + ``` * (Optional) Run static analysis - cppcheck ``` $ sudo apt-get install cppcheck @@ -100,6 +110,21 @@ docker image ls docker push DOCKER_USERNAME/cpp-lab ``` -## 6. TROUBLESHOOTING +## 6. TroubleShooting 1. `push access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed` => docker login / Docker Desktop login + +## 7. Evaluate Executable +- List all sections: +```bash +$ size ./build/cpp-lab +``` +- The expected output should be the following: +```bash + text data bss dec hex filename + 14791 792 280 15863 ./build/cpp-lab +``` + +- `.text`: Text Segment - the executable code size, including: complied function, inline, template, constants, string literals +- `.data`: Initialized Data Segment - the memory for the global, static variables, has init value. +- `.bss`: Uninitialized Data Segment - global and static variables that are not initialized \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..3e2906e --- /dev/null +++ b/run.sh @@ -0,0 +1,57 @@ +## NOTE: +## This file was initially generated with the assistance of AI. +## The code has been reviewed and may have been modified by the developer +## to ensure correctness, readability, and compliance with project requirements. +#!/usr/bin/env bash + +set -e # Exit immediately if a command fails + +PROJECT_EXEC="./build/cpp_lab_project" +BUILD_DIR="./build" + +clear +echo "==============================" +echo " Starting build pipeline... " +echo "==============================" + +# Check required tools +for tool in cmake cppcheck python3; do + if ! command -v $tool &> /dev/null; then + echo "[ERR]: $tool is not installed." + exit 1 + fi +done + +echo "" +echo "===========>> Building project..." +cmake --build "$BUILD_DIR" + +echo "" +echo "===========>> Running cppcheck..." +cppcheck \ + --enable=warning,style,performance,portability \ + --inconclusive \ + --inline-suppr \ + --quiet \ + --error-exitcode=1 \ + ./src ./include + +echo "[OK] Static analysis passed" + +echo "" +echo "===========>> Generating commit id..." +python3 ./private/genid.py + +echo "" +echo "===========>> Running program..." +if [ -f "$PROJECT_EXEC" ]; then + "$PROJECT_EXEC" +else + echo "[ERR] Executable not found: $PROJECT_EXEC" + exit 1 +fi + +echo "" +echo "==============================" +echo "Pipeline finished successfully!" +echo "==============================" \ No newline at end of file diff --git a/src/core/datetime/CTime.cpp b/src/core/datetime/CTime.cpp new file mode 100644 index 0000000..8969c29 --- /dev/null +++ b/src/core/datetime/CTime.cpp @@ -0,0 +1,39 @@ +#include +#include "ExampleRegistry.h" + +#include + +namespace { +void run() { + // **1. Get the current timestamp** + std::time_t now = time(NULL); + const auto* ts = ctime(&now); + std::cout << "Current time: " << ts << '\n'; + + // **2. Create the dt structure** + struct tm datetime = *localtime(&now); + std::cout << "Year: " << datetime.tm_year + 1900 << '\n'; + std::cout << "Current time stamp: " << mktime(&datetime) << '\n'; + + // **3. Formatting** + char output[50]; + strftime(output, 50, "%B %e, %Y", &datetime); + std::cout << output << "\n"; + strftime(output, 50, "%I:%M:%S %p", &datetime); + std::cout << output << "\n"; + strftime(output, 50, "%m/%d/%y", &datetime); + std::cout << output << "\n"; + strftime(output, 50, "%a %b %e %H:%M:%S %Y", &datetime); + std::cout << output << "\n"; +} +} // namespace + +class CTime : public IExample { + public: + std::string group() const override { return "core/datetime"; } + std::string name() const override { return "CTime"; } + std::string description() const override { return ""; } + void execute() override { run(); } +}; + +REGISTER_EXAMPLE(CTime, "core/datetime", "CTime"); \ No newline at end of file diff --git a/src/core/datetime/REAME.md b/src/core/datetime/REAME.md new file mode 100644 index 0000000..7fec4c5 --- /dev/null +++ b/src/core/datetime/REAME.md @@ -0,0 +1,23 @@ +# Date and Time +- : provides functions and types to work with date and time values including parsing and formatting (inherited from C) + +- : provides facilities to deal with the duration time points and clocks (std11), it is more modern and type-safe approach + +## ctime +- Data Types: + - `time_t`: for timestamps + - `struct tm`: for datetime structes + - Use `mktime()` to convert struct tm to time_t, and `localtime()` and `gmtime()` for vice versa +- `strftime()` format: +>%a Short representation of the weekday Fri +%b Short representation of the month name Dec +%B Full representation of the month name December +%d Day of the month with leading zero 09 +%e Day of the month with leading spaces 9 +%H 24-hour format of an hour 14 +%I 12-hour format of an hour 02 +%M Minutes within an hour 30 +%p AM or PM PM +%S Seconds within a minute 01 +%y 2-digit year representation 23 +%Y 4-digit year representation 2023 \ No newline at end of file diff --git a/src/core/exception/BasicHandle.cpp b/src/core/exception/BasicHandle.cpp index af5f026..77410e7 100644 --- a/src/core/exception/BasicHandle.cpp +++ b/src/core/exception/BasicHandle.cpp @@ -12,7 +12,7 @@ void run() { errorFnc(); } catch (std::exception& e) { std::cout << e.what(); - throw std::runtime_error("[M] Middle error \n"); + throw std::runtime_error("[M] Middle error \n"); // use as custom exception // throw; // rethrow } } catch (std::exception& e) { diff --git a/src/core/filehandle/StringStream.cpp b/src/core/filehandle/StringStream.cpp index 9185084..8eb3a12 100644 --- a/src/core/filehandle/StringStream.cpp +++ b/src/core/filehandle/StringStream.cpp @@ -10,7 +10,7 @@ void runStringStreamExample() { std::stringstream os{}; - // input + // input: std::istringstream iss; os << "0xF"; std::cout << os.str(); @@ -19,7 +19,7 @@ void runStringStreamExample() { os.clear(); std::cout << os.str(); - // output + // output: std::ostringstream oss; std::string bytesStr = os.str(); std::cout << bytesStr;