Skip to content

Commit e90516a

Browse files
committed
Initial commit of rewrite
1 parent 75e657d commit e90516a

27 files changed

Lines changed: 858 additions & 2524 deletions

.clang-format

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
BasedOnStyle: LLVM
3+
Language: Cpp
4+
5+
UseTab: Never
6+
IndentWidth: 4
7+
TabWidth: 4
8+
ContinuationIndentWidth: 4
9+
ColumnLimit: 0
10+
DerivePointerAlignment: false
11+
AlignAfterOpenBracket: DontAlign
12+
13+
BreakBeforeBraces: Allman
14+
BraceWrapping:
15+
AfterControlStatement: Always
16+
AfterEnum: true
17+
AfterFunction: true
18+
AfterStruct: true
19+
AfterUnion: true
20+
BeforeElse: true
21+
BeforeWhile: true
22+
SplitEmptyFunction: true
23+
SplitEmptyRecord: true
24+
SplitEmptyNamespace: true
25+
26+
AllowShortBlocksOnASingleLine: Never
27+
AllowShortIfStatementsOnASingleLine: Never
28+
AllowShortLoopsOnASingleLine: false
29+
AllowShortFunctionsOnASingleLine: None
30+
AllowShortCaseLabelsOnASingleLine: false
31+
32+
SortIncludes: false
33+
IncludeBlocks: Preserve
34+
35+
PointerAlignment: Left
36+
SpaceBeforeParens: Never
37+
SpacesInParentheses: false
38+
SpacesInSquareBrackets: false
39+
SpaceBeforeAssignmentOperators: true
40+
BinPackArguments: false
41+
BinPackParameters: false
42+
Cpp11BracedListStyle: false
43+
44+
IndentCaseLabels: true
45+
46+
AlignConsecutiveMacros: Consecutive
47+
AlignConsecutiveAssignments: Consecutive
48+
AlignConsecutiveDeclarations: Consecutive
49+
50+
ReflowComments: false
51+
KeepEmptyLinesAtTheStartOfBlocks: false
52+
MaxEmptyLinesToKeep: 2
53+
...

.githooks/pre-commit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env sh
2+
set -eu
3+
4+
printf '%s\n' 'Running formatter (make format)...'
5+
make format
6+
7+
if git diff --quiet --exit-code; then
8+
printf '%s\n' 'Formatting already clean.'
9+
exit 0
10+
fi
11+
12+
printf '%s\n' 'Formatting changes were applied. Commit aborted; please review, stage, and commit again.'
13+
exit 1

.github/copilot-instructions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
applyTo: "src/**.c, src/**.h"
3+
---
4+
# About templates and sections of files
5+
When doing edits, always respect the file template in .vscode/c.code-snippets.
6+
There are separate templates for .h and .c files.
7+
The templates contain sections.
8+
Put the new code in the correct section of the file.
9+
If a section is missing, add it.
10+
Keep the sections in the order they are in the template.
11+
After creating an empty file, add the appropiate template first.
12+
Never put function definitions in header files. Only put function declarations in header files.
13+
14+
# About memory allocations
15+
Never check if malloc(), calloc() or realloc() returned NULL. Assume that they always succeed. Do not add error handling for failed memory allocations.
16+
17+
# About comments
18+
When creating a struct or enum, add doxygen comments to it.
19+
When adding a function, add doxygen comments to the declaration in the header file (public function) or to the declaration in the source file, section "Private Function Declarations" (private function).
20+
21+
# About naming conventions
22+
The naming convention for public functions is `moduleName_functionName` (implemented in moduleName.c).
23+
functionName should be a verb describing the action of the function.
24+
Use camel case for function names and variable names.
25+
Use uppercase letters and underscores for macro names and enum values.
26+
27+
Do not use the ! operator for boolean conditions. Write condition == false instead.
28+
Do not create functions, variables or defines that are not used.
29+
Never declare multiple variables in the same line.
30+
Make order of operations explicit with parentheses, even if they are not strictly necessary.

.github/workflows/build.yml

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1-
name: build
1+
name: Build
22

33
on:
44
push:
5-
branches: [ master ]
65
pull_request:
7-
branches: [ master ]
86

97
jobs:
10-
build:
8+
check-formatting:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
13+
- name: Install clang-format
14+
run: sudo apt-get install -y clang-format
1115

16+
- name: Check formatting
17+
run: make format-check
18+
19+
check-cppcheck:
1220
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Install dependencies
25+
run: sudo apt-get install -y cppcheck
1326

27+
- name: Run cppcheck
28+
run: make check
29+
30+
build:
31+
runs-on: ubuntu-latest
1432
steps:
15-
- uses: actions/checkout@v2
16-
- name: make
17-
run: make
33+
- uses: actions/checkout@v4
34+
35+
- name: Install dependencies
36+
run: sudo apt-get install -y libreadline-dev
37+
38+
- name: Build
39+
run: make
40+
41+
- name: Upload executable
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: ctable
45+
path: bin/release/ctable

.gitignore

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
# Build artifacts
2-
bin/
3-
4-
# Visual Studio configuration files
5-
.vscode
6-
7-
# Valgrind files
8-
vgcore*
9-
massif*
10-
callgrind*
1+
bin

.vscode/c.code-snippets

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"Source File Template": {
3+
"scope": "c",
4+
"prefix": "c-file",
5+
"description": "Template for new C source files with project chapter layout",
6+
"isFileTemplate": true,
7+
"body": [
8+
"/**",
9+
" * @file ${TM_FILENAME}",
10+
" */",
11+
"",
12+
"",
13+
"//***************************************************************************//",
14+
"//************************** INCLUDES ***************************************//",
15+
"//***************************************************************************//",
16+
"",
17+
"#include <stdint.h>",
18+
"#include <stdbool.h>",
19+
"",
20+
"#include \"${1:${TM_FILENAME_BASE}.h}\"",
21+
"",
22+
"//***************************************************************************//",
23+
"//************************** PRIVATE DEFINES *******************************//",
24+
"//***************************************************************************//",
25+
"",
26+
"",
27+
"//***************************************************************************//",
28+
"//************************** PRIVATE TYPEDEFS *******************************//",
29+
"//***************************************************************************//",
30+
"",
31+
"",
32+
"//***************************************************************************//",
33+
"//************************** PRIVATE VARIABLE DECLARATIONS ******************//",
34+
"//***************************************************************************//",
35+
"",
36+
"",
37+
"//***************************************************************************//",
38+
"//************************** PRIVATE FUNCTION DECLARATIONS ******************//",
39+
"//***************************************************************************//",
40+
"",
41+
"",
42+
"//***************************************************************************//",
43+
"//************************** PRIVATE FUNCTION DEFINITIONS *******************//",
44+
"//***************************************************************************//",
45+
"",
46+
"",
47+
"//***************************************************************************//",
48+
"//************************** PUBLIC FUNCTION DEFINITIONS ********************//",
49+
"//***************************************************************************//",
50+
"",
51+
""
52+
]
53+
},
54+
"Header File Template": {
55+
"scope": "c,cpp",
56+
"prefix": "h-file",
57+
"description": "Template for new C header files with project chapter layout",
58+
"isFileTemplate": true,
59+
"body": [
60+
"/**",
61+
" * @file ${TM_FILENAME}",
62+
" */",
63+
"",
64+
"",
65+
"#pragma once",
66+
"",
67+
"//***************************************************************************//",
68+
"//************************** INCLUDES ***************************************//",
69+
"//***************************************************************************//",
70+
"",
71+
"#include <stdint.h>",
72+
"#include <stdbool.h>",
73+
"",
74+
"//***************************************************************************//",
75+
"//************************** PUBLIC DEFINES ********************************//",
76+
"//***************************************************************************//",
77+
"",
78+
"",
79+
"//***************************************************************************//",
80+
"//************************** PUBLIC TYPEDEFS ********************************//",
81+
"//***************************************************************************//",
82+
"",
83+
"",
84+
"//***************************************************************************//",
85+
"//************************** PUBLIC VARIABLE DECLARATIONS *******************//",
86+
"//***************************************************************************//",
87+
"",
88+
"",
89+
"//***************************************************************************//",
90+
"//************************** PUBLIC FUNCTION DECLARATIONS *******************//",
91+
"//***************************************************************************//",
92+
"",
93+
""
94+
]
95+
}
96+
}

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug ctable",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/bin/debug/ctable",
9+
"args": [],
10+
"stopAtEntry": false,
11+
"cwd": "${workspaceFolder}",
12+
"externalConsole": false,
13+
"MIMode": "gdb",
14+
"preLaunchTask": "make debug"
15+
}
16+
]
17+
}

.vscode/paste_snippet.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Paste a C/H file template into a target file from VS Code snippets."""
2+
3+
import json
4+
import os
5+
import pathlib
6+
import re
7+
import sys
8+
9+
10+
def main() -> int:
11+
"""Load a snippet template, resolve placeholders, and write it to a file.
12+
13+
Expected CLI args:
14+
1) target file path (.c or .h)
15+
2) snippets JSON file path
16+
"""
17+
# Validate CLI usage and target file extension.
18+
if len(sys.argv) < 3:
19+
print("Usage: paste_snippet.py <target-file> <snippets-file>")
20+
return 1
21+
22+
file_path = sys.argv[1]
23+
snippets_path = sys.argv[2]
24+
25+
if not file_path:
26+
print("No active file. Open a .c or .h file first.")
27+
return 1
28+
29+
ext = pathlib.Path(file_path).suffix.lower()
30+
if ext not in {".c", ".h"}:
31+
print(f"Unsupported file extension: {ext} (use .c or .h)")
32+
return 1
33+
34+
# Pick the template key based on the destination file extension.
35+
key = "Source File Template" if ext == ".c" else "Header File Template"
36+
37+
# Load snippet definitions from the shared snippets file.
38+
with open(snippets_path, "r", encoding="utf-8") as file_handle:
39+
snippets = json.load(file_handle)
40+
41+
if key not in snippets:
42+
print(f"Snippet not found: {key}")
43+
return 1
44+
45+
body = snippets[key].get("body", [])
46+
filename = os.path.basename(file_path)
47+
filename_base = os.path.splitext(filename)[0]
48+
49+
def resolve(line: str) -> str:
50+
"""Resolve supported VS Code snippet placeholders in one line.
51+
52+
Supports:
53+
- ${TM_FILENAME}
54+
- ${TM_FILENAME_BASE}
55+
- ${N:defaultValue} -> defaultValue
56+
"""
57+
line = line.replace("${TM_FILENAME}", filename).replace("${TM_FILENAME_BASE}", filename_base)
58+
pattern = re.compile(r"\$\{\d+:([^{}]+)\}")
59+
previous = None
60+
while previous != line:
61+
previous = line
62+
line = pattern.sub(lambda match: match.group(1), line)
63+
line = line.replace("${TM_FILENAME}", filename).replace("${TM_FILENAME_BASE}", filename_base)
64+
return line
65+
66+
# Build output text line-by-line from the snippet body.
67+
text = "\n".join(resolve(line) for line in body) + "\n"
68+
path = pathlib.Path(file_path)
69+
70+
# Safety guard: do not overwrite non-empty files.
71+
if path.exists() and path.read_text(encoding="utf-8").strip():
72+
print(f"Refusing to overwrite non-empty file: {file_path}")
73+
return 1
74+
75+
# Ensure the directory exists and write the generated content.
76+
path.parent.mkdir(parents=True, exist_ok=True)
77+
path.write_text(text, encoding="utf-8")
78+
print(f"Pasted snippet into {file_path}")
79+
return 0
80+
81+
82+
if __name__ == "__main__":
83+
raise SystemExit(main())

.vscode/tasks.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "make debug",
6+
"type": "shell",
7+
"command": "make debug",
8+
"group": "build",
9+
"problemMatcher": "$gcc"
10+
},
11+
{
12+
"label": "paste snippet into current file",
13+
"type": "shell",
14+
"command": "python3",
15+
"args": [
16+
"${workspaceFolder}/.vscode/paste_snippet.py",
17+
"${file}",
18+
"${workspaceFolder}/.vscode/c.code-snippets"
19+
],
20+
"problemMatcher": []
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)