Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Add Interpretor Mode&Add Submodule for nlohmann/json#4222 (#6#7)
Browse files Browse the repository at this point in the history
Add Artifact cache
Add submodule dectection for 2 workflows
TODO:
 -[ ] force to shutdown the long-time process

---------

Co-authored-by: M2030123 <[email protected]>
Co-authored-by: jixiangyuyu <[email protected]>
  • Loading branch information
3 people committed Dec 22, 2023
1 parent e1965b8 commit f8d51c4
Show file tree
Hide file tree
Showing 55 changed files with 25,077 additions and 28 deletions.
26 changes: 11 additions & 15 deletions .github/workflows/cmake-single-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:

steps:
- uses: actions/checkout@v3

with:
submodules: true
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
Expand All @@ -30,27 +31,22 @@ jobs:
- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Cache build directory
uses: actions/cache@v2
with:
path: |
build
key: $${{ runner.os }}-cmake-${{ hashFiles('**/CMakeLists.txt') }}
restore-keys: |
${{ runner.os }}-cmake-
- name: Upload Executables(Linux) as Artifact
uses: actions/upload-artifact@v2
with:
name: executables
path: |
${{github.workspace}}/build
execute:
runs-on: ubuntu-latest
needs: build
steps:
- name: create build
run: cd & mkdir build
- uses: actions/download-artifact@v2
with:
name: executables
path: |
${{github.workspace}}/build
- name: Display structure of downloaded files
run: ls -R .
- name: Run all executable file
run: find "build" -type f -executable -execdir echo "-------- {} --------------------" \; -execdir timeout 30s {} \;
run: find "build" -type f -executable -execdir echo "-------- {} --------------------" \; -execdir timeout 10s {} \;
- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: '27 23 * * 3'

jobs:
analyze:
name: Analyze
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'c-cpp' ]
# CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ]
# Use only 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Interpreter Pattern/include/json"]
path = Interpreter Pattern/include/json
url = https://github.com/nlohmann/json
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_subdirectory("Prototype Pattern")
add_subdirectory("Iterator Pattern")
add_subdirectory("Flyweight Pattern")
add_subdirectory("Strategy Pattern")

add_subdirectory("Interpreter Pattern")
# 设置编译器选项(可选)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

11 changes: 11 additions & 0 deletions Interpreter Pattern/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 指定 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 设置项目名称
project(NewEnergyCarInterpretor)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/include/json)
# 添加头文件和源文件
add_executable(interpretor VIN.cpp)
target_link_libraries(interpretor PRIVATE nlohmann_json::nlohmann_json)

192 changes: 192 additions & 0 deletions Interpreter Pattern/VIN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include<filesystem>
using namespace std::filesystem;
using namespace std;

static int weight_coefficient[17] = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 };

class VINDecoder {
public:
VINDecoder(const nlohmann::json& data) : jsonData(data) {}

bool decodeVIN(const std::string& vin) {
if (vin.length() != 17) {
cerr << "错误:无效的VIN码长度" << endl;
return false;
}

if (!decodeWMI(vin.substr(0, 3)) ||
!decodeVDS(vin.substr(3, 5)) ||
!decodeVIS(vin.substr(9, 8))) {
return false;
}

// 验证第九位数字(校验位)
if (!validateCheckDigit(vin)) {
cerr << "错误:VIN码验证失败" << endl;
exit(1);
return false;
}

return true;
}

// 获取解码后的信息的公有成员函数
const std::string& getRegion() const { return region; }
const std::string& getManufacturer() const { return manufacturer; }
const std::string& getVehicleType() const { return vehicleType; }
const std::string& getBodyStyle() const { return bodyStyle; }
const std::string& getTransmission() const { return transmission; }
const std::string& getYear() const { return year; }
const std::string& getX() const { return x; }
const std::string& getY() const { return y; }
const std::string& getZ() const { return z; }

private:
bool decodeWMI(const std::string& wmiPart) {
region = jsonData["WMI_1"].value(wmiPart.substr(0, 1), "未知");
manufacturer = jsonData["WMI_2"].value(wmiPart.substr(1, 1), "未知");
vehicleType = jsonData["WMI_3"].value(wmiPart.substr(2, 1), "未知");
return true;
}

bool decodeVDS(const std::string& vdsPart) {
bodyStyle = jsonData["VDS_4"].value(vdsPart.substr(0, 1), "未知");
transmission = jsonData["VDS_5"].value(vdsPart.substr(1, 1), "未知");
x = jsonData["VDS_6"].value(vdsPart.substr(2, 1), "未知");
y = jsonData["VDS_78"].value(vdsPart.substr(3, 2), "未知");
// 根据需要添加更多的 VDS 解码逻辑...
return true;
}

bool decodeVIS(const std::string& visPart) {
year = jsonData["VIS_10"].value(visPart.substr(0, 1), "未知");
z = jsonData["BMW"].value(visPart.substr(1, 1), "未知");
// 根据需要添加更多的 VIS 解码逻辑...
return true;
}

bool validateCheckDigit(const std::string& vin) {
int checkDigit = vin[8];

int sum = 0;
for (int i = 0; i < 17; ++i) {
if (vin[i] >= '0' && vin[i] <= '9') {
sum += (vin[i] - '0') * weight_coefficient[i];
}
else if (vin[i] >= 'A' && vin[i] <= 'Z' && vin[i] != 'I' && vin[i] != 'O' && vin[i] != 'Q') {
if (vin[i] >= 'A' && vin[i] <= 'I')
sum += (vin[i] - 'A' + 1) * weight_coefficient[i];
else if (vin[i] >= 'J' && vin[i] <= 'R')
sum += (vin[i] - 'J' + 1) * weight_coefficient[i];
else if (vin[i] >= 'S' && vin[i] <= 'Z')
sum += (vin[i] - 'S' + 2) * weight_coefficient[i];
}
else {
cout << "VIN中不能包含 I、O、Q 三个英文字母,请重新检查VIN码" << endl;
// 直接终止程序
exit(1);
return false;
}
}

int remainder = sum % 11;
if (remainder == 10) {
//cout << remainder << endl;
return checkDigit == 'X';
}
else {
//cout << remainder << endl;
return checkDigit == (remainder + '0');
}
}

private:
const nlohmann::json& jsonData;

std::string region;
std::string manufacturer;
std::string vehicleType;
std::string bodyStyle;
std::string transmission;
std::string year;
std::string x;
std::string y;
std::string z;
};

int main() {
try {
// 从文件读取 JSON 数据
path currentPath = current_path();

while (currentPath.filename().string() != "out")
currentPath = currentPath.parent_path();
currentPath = currentPath.parent_path();
current_path(currentPath);
path subdirectory("Interpreter Pattern");

nlohmann::json jsonData;
try {
if (exists(currentPath / subdirectory)) {
current_path(currentPath/subdirectory);
cout << "Successfully enter the subdirectory:" << currentPath / subdirectory << endl;
}
}
catch(filesystem_error& e){
std::cerr << "Exception Encountered: " << e.what() << endl;
}
ifstream jsonFile("data.json");
// 检查文件是否成功打开
if (!jsonFile.is_open()) {
cerr << "错误:无法打开JSON文件" << endl;
return 1;
}

// 从文件读取 JSON 数据
jsonFile >> jsonData;
jsonFile.close();

// VIN 码待解码
std::string vin = "21VAM033936123456";

// 创建 VIN 解码器
VINDecoder vinDecoder(jsonData);

// 解码 VIN 码
bool ret = vinDecoder.decodeVIN(vin);

// 输出解码信息
cout << "Region:" << vinDecoder.getRegion() << endl;
cout << "Manufacture:" << vinDecoder.getManufacturer() << endl;
cout << "Type of Vehicle:" << vinDecoder.getVehicleType() << endl;
cout << "Style of body:" << vinDecoder.getBodyStyle() << endl;
cout << "Transmission&Fuel system:" << vinDecoder.getTransmission() << endl;
cout << "Configuration for safety:" << vinDecoder.getX() << endl;
cout << "Type of body:" << vinDecoder.getY() << endl;
cout << "Built Year:"<< vinDecoder.getYear() << endl;
cout << "Assembly Plant:" << vinDecoder.getZ() << endl;
cout << "Manufacture seq. No.:" << vin.substr(11, 6) << endl;

if (ret) {
cout << "VIN code has been verified successfully" << endl;
}
else {
cout << "VIN code verification failed" << endl;
}
}
catch (const nlohmann::json::parse_error& e) {
// 输出详细的解析错误信息
cerr << "JSON parse error:" << e.what() << " At byte" << e.byte << endl;
return 1;
}
catch (const exception& e) {
// 捕获其他可能的异常
cerr << "Exception occured:" << e.what() << endl;
return 1;
}

return 0;
}
Loading

0 comments on commit f8d51c4

Please sign in to comment.