Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## 1. Overview
**TL;DR**
```bash
./r
```

**Project Structure**
```
includes/ → Header files (.h, .hpp)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
57 changes: 57 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -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 "=============================="
39 changes: 39 additions & 0 deletions src/core/datetime/CTime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include "ExampleRegistry.h"

#include <ctime>

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");
23 changes: 23 additions & 0 deletions src/core/datetime/REAME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Date and Time
- <ctime> : provides functions and types to work with date and time values including parsing and formatting (inherited from C)

- <chrono>: 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
2 changes: 1 addition & 1 deletion src/core/exception/BasicHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/filehandle/StringStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void runStringStreamExample() {

std::stringstream os{};

// input
// input: std::istringstream iss;
os << "0xF";
std::cout << os.str();

Expand All @@ -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;

Expand Down