Skip to content

Commit 720867d

Browse files
authored
Merge pull request #9 from devprabal/develop
Formatting with clang-format v17
2 parents 163cfbf + b19760d commit 720867d

8 files changed

Lines changed: 326 additions & 235 deletions

File tree

.clang-format

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Language: Cpp
2+
BasedOnStyle: Google
3+
4+
BreakBeforeBraces: Custom
5+
BraceWrapping:
6+
IndentBraces: false
7+
AfterEnum: true
8+
AfterStruct: true
9+
AfterClass: true
10+
AfterFunction: true
11+
AfterUnion: true
12+
AfterExternBlock: false
13+
AfterCaseLabel: false
14+
BeforeElse: false
15+
BeforeWhile: false
16+
AfterControlStatement: Never
17+
18+
AllowShortFunctionsOnASingleLine: Empty
19+
AllowShortIfStatementsOnASingleLine: WithoutElse
20+
AllowShortLoopsOnASingleLine: false
21+
AllowShortBlocksOnASingleLine: Empty
22+
AllowShortCaseLabelsOnASingleLine: false
23+
AllowShortEnumsOnASingleLine: false
24+
AlignArrayOfStructures: Right
25+
26+
#ContinuationIndentWidth: 2
27+
28+
Cpp11BracedListStyle: false
29+
IndentCaseLabels: false
30+
IndentCaseBlocks: false
31+
IndentGotoLabels: true
32+
IndentExternBlock: NoIndent
33+
34+
BinPackParameters: false
35+
BinPackArguments: false
36+
IndentPPDirectives: BeforeHash
37+
38+
KeepEmptyLinesAtTheStartOfBlocks: false
39+
InsertNewlineAtEOF: true
40+
KeepEmptyLinesAtEOF: true
41+
MaxEmptyLinesToKeep: 1
42+
43+
PointerAlignment: Left
44+
ReferenceAlignment: Right
45+
46+
ReflowComments: true
47+
SortIncludes: CaseSensitive
48+
SpaceAfterCStyleCast: false
49+
SpaceAfterLogicalNot: true
50+
SpaceBeforeAssignmentOperators: true
51+
SpaceBeforeCaseColon: false
52+
SpaceBeforeParens: ControlStatementsExceptControlMacros
53+
SpaceBeforeSquareBrackets: false
54+
55+
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
56+
AlignConsecutiveDeclarations: None
57+
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
58+
AlignEscapedNewlines: Left
59+
60+
IndentWidth: 4
61+
TabWidth: 4
62+
UseTab: Never
63+
ColumnLimit: 120
64+
LineEnding: LF
65+
66+
DisableFormat: false

.clang-format-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#./main.c

.github/workflows/clang-format.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: clang-format
2+
3+
on:
4+
# push:
5+
# branches:
6+
# - develop
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
build:
12+
name: Code Formatting
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
repository: ${{ github.event.pull_request.head.repo.full_name }}
19+
ref: ${{ github.event.pull_request.head.ref }}
20+
- uses: DoozyX/clang-format-lint-action@v0.17
21+
with:
22+
source: '.'
23+
extensions: 'h,c'
24+
exclude: './clang-format-ignore'
25+
clangFormatVersion: 17
26+
inplace: True
27+
- uses: EndBug/add-and-commit@v9.1.4
28+
with:
29+
default_author: github_actions
30+
committer_name: GitHub Actions
31+
author_name: Clang Robot
32+
message: '[clang-format-bot] Formatting changes'
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

list.c

Lines changed: 116 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,116 @@
1-
#include "list.h"
2-
3-
struct Head {
4-
Node* front;
5-
};
6-
7-
static void iterate_list(Head* head, node_func node_fptr, list_func user_fptr, void* user_func_args)
8-
{
9-
if(!node_fptr) {
10-
printf("\nNeed to provide function for node iteration\n");
11-
node_fptr = get_next_node;
12-
}
13-
14-
if(head) {
15-
Node* front = head->front;
16-
while(front) {
17-
void* node_data = get_node_data(front);
18-
if(user_fptr) {
19-
user_fptr(user_func_args, node_data);
20-
}
21-
front = node_fptr(front);
22-
}
23-
}
24-
}
25-
26-
static void increment(void* args, void* Node_data)
27-
{
28-
if(!args) {
29-
printf("\nPass unsigned type output storage\n");
30-
}
31-
if(!Node_data)
32-
{
33-
printf("\nNode data does not exist\n");
34-
}
35-
unsigned *count = (unsigned *)args;
36-
if(count) {
37-
++(*count);
38-
}
39-
}
40-
41-
Head* create_list(Config* config)
42-
{
43-
set_config(config);
44-
Head* head = calloc(1, sizeof(Head));
45-
if(head) return head;
46-
return NULL;
47-
}
48-
49-
void destroy_list(Head* head, void(* destroy_user_data_func)(void*, void*), void* user_args)
50-
{
51-
iterate_list(head, destroy_node, destroy_user_data_func, user_args);
52-
free(head);
53-
}
54-
55-
unsigned count_nodes(Head* head)
56-
{
57-
unsigned count = 0;
58-
iterate_list(head, get_next_node, increment, &count);
59-
return count;
60-
}
61-
62-
SLL_Result append_to_list(Head* head, unsigned size, void* data)
63-
{
64-
if (!head)
65-
{
66-
return SLL_FAIL;
67-
}
68-
69-
if (!(head->front))
70-
{
71-
head->front = create_node();
72-
return fill_node_data(head->front, size, data);
73-
}
74-
else
75-
{
76-
Node* this_node = head->front;
77-
Node* next_node = get_next_node(this_node);
78-
while (next_node)
79-
{
80-
this_node = next_node;
81-
next_node = get_next_node(next_node);
82-
}
83-
return append_node(this_node, size, data);
84-
}
85-
}
86-
87-
bool find_in_list(Head* head, void* item, bool(* compare_func)(void*, void*))
88-
{
89-
if(!compare_func)
90-
{
91-
printf("\nProvide function for data comparison\n");
92-
return false;
93-
}
94-
if(head) {
95-
Node* front = head->front;
96-
while(front) {
97-
Node* node_data = get_node_data(front);
98-
if(compare_func(item, node_data))
99-
{
100-
return true;
101-
}
102-
front = get_next_node(front);
103-
}
104-
}
105-
return false;
106-
}
107-
108-
void set_config(Config* config)
109-
{
110-
if(config && (config->list_data_alloc_func_fptr || config->list_data_dealloc_func_fptr))
111-
{
112-
if(NULL == config->list_data_alloc_func_fptr)
113-
{
114-
printf("\nProvide function for data allocation. Aborting.\n");
115-
exit(EXIT_FAILURE);
116-
}
117-
if(NULL == config->list_data_dealloc_func_fptr)
118-
{
119-
printf("\nProvide function for data deallocation. Aborting.\n");
120-
exit(EXIT_FAILURE);
121-
}
122-
set_node_data_alloc_dealloc_func(config->list_data_alloc_func_fptr, config->list_data_dealloc_func_fptr);
123-
}
124-
else
125-
{
126-
printf("\nProvide functions for data allocation/dealloc. Using calloc() and free() by default\n");
127-
}
128-
}
1+
#include "list.h"
2+
3+
struct Head
4+
{
5+
Node* front;
6+
};
7+
8+
static void iterate_list(Head* head, node_func node_fptr, list_func user_fptr, void* user_func_args)
9+
{
10+
if (! node_fptr) {
11+
printf("\nNeed to provide function for node iteration\n");
12+
node_fptr = get_next_node;
13+
}
14+
15+
if (head) {
16+
Node* front = head->front;
17+
while (front) {
18+
void* node_data = get_node_data(front);
19+
if (user_fptr) {
20+
user_fptr(user_func_args, node_data);
21+
}
22+
front = node_fptr(front);
23+
}
24+
}
25+
}
26+
27+
static void increment(void* args, void* Node_data)
28+
{
29+
if (! args) {
30+
printf("\nPass unsigned type output storage\n");
31+
}
32+
if (! Node_data) {
33+
printf("\nNode data does not exist\n");
34+
}
35+
unsigned* count = (unsigned*)args;
36+
if (count) {
37+
++(*count);
38+
}
39+
}
40+
41+
Head* create_list(Config* config)
42+
{
43+
set_config(config);
44+
Head* head = calloc(1, sizeof(Head));
45+
if (head) return head;
46+
return NULL;
47+
}
48+
49+
void destroy_list(Head* head, void (*destroy_user_data_func)(void*, void*), void* user_args)
50+
{
51+
iterate_list(head, destroy_node, destroy_user_data_func, user_args);
52+
free(head);
53+
}
54+
55+
unsigned count_nodes(Head* head)
56+
{
57+
unsigned count = 0;
58+
iterate_list(head, get_next_node, increment, &count);
59+
return count;
60+
}
61+
62+
SLL_Result append_to_list(Head* head, unsigned size, void* data)
63+
{
64+
if (! head) {
65+
return SLL_FAIL;
66+
}
67+
68+
if (! (head->front)) {
69+
head->front = create_node();
70+
return fill_node_data(head->front, size, data);
71+
} else {
72+
Node* this_node = head->front;
73+
Node* next_node = get_next_node(this_node);
74+
while (next_node) {
75+
this_node = next_node;
76+
next_node = get_next_node(next_node);
77+
}
78+
return append_node(this_node, size, data);
79+
}
80+
}
81+
82+
bool find_in_list(Head* head, void* item, bool (*compare_func)(void*, void*))
83+
{
84+
if (! compare_func) {
85+
printf("\nProvide function for data comparison\n");
86+
return false;
87+
}
88+
if (head) {
89+
Node* front = head->front;
90+
while (front) {
91+
Node* node_data = get_node_data(front);
92+
if (compare_func(item, node_data)) {
93+
return true;
94+
}
95+
front = get_next_node(front);
96+
}
97+
}
98+
return false;
99+
}
100+
101+
void set_config(Config* config)
102+
{
103+
if (config && (config->list_data_alloc_func_fptr || config->list_data_dealloc_func_fptr)) {
104+
if (NULL == config->list_data_alloc_func_fptr) {
105+
printf("\nProvide function for data allocation. Aborting.\n");
106+
exit(EXIT_FAILURE);
107+
}
108+
if (NULL == config->list_data_dealloc_func_fptr) {
109+
printf("\nProvide function for data deallocation. Aborting.\n");
110+
exit(EXIT_FAILURE);
111+
}
112+
set_node_data_alloc_dealloc_func(config->list_data_alloc_func_fptr, config->list_data_dealloc_func_fptr);
113+
} else {
114+
printf("\nProvide functions for data allocation/dealloc. Using calloc() and free() by default\n");
115+
}
116+
}

list.h

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
#ifndef LIST_H__
2-
#define LIST_H__
3-
4-
#include "node.h"
5-
6-
typedef struct Head Head;
7-
typedef node_data_alloc_func list_data_alloc_func;
8-
typedef node_data_dealloc_func list_data_dealloc_func;
9-
typedef struct Config {
10-
list_data_alloc_func list_data_alloc_func_fptr;
11-
list_data_dealloc_func list_data_dealloc_func_fptr;
12-
} Config;
13-
14-
typedef void(* list_func)(void*, void*);
15-
16-
Head* create_list(Config* config);
17-
void destroy_list(Head* head, void(* destroy_user_data_func)(void*, void*), void* user_args);
18-
unsigned count_nodes(Head* head);
19-
SLL_Result append_to_list(Head* head, unsigned size, void* data);
20-
bool find_in_list(Head* head, void* item, bool(*compare_func)(void*, void*));
21-
void set_config(Config* config);
22-
23-
#endif // LIST_H__
1+
#ifndef LIST_H__
2+
#define LIST_H__
3+
4+
#include "node.h"
5+
6+
typedef struct Head Head;
7+
typedef node_data_alloc_func list_data_alloc_func;
8+
typedef node_data_dealloc_func list_data_dealloc_func;
9+
typedef struct Config
10+
{
11+
list_data_alloc_func list_data_alloc_func_fptr;
12+
list_data_dealloc_func list_data_dealloc_func_fptr;
13+
} Config;
14+
15+
typedef void (*list_func)(void*, void*);
16+
17+
Head* create_list(Config* config);
18+
void destroy_list(Head* head, void (*destroy_user_data_func)(void*, void*), void* user_args);
19+
unsigned count_nodes(Head* head);
20+
SLL_Result append_to_list(Head* head, unsigned size, void* data);
21+
bool find_in_list(Head* head, void* item, bool (*compare_func)(void*, void*));
22+
void set_config(Config* config);
23+
24+
#endif // LIST_H__

0 commit comments

Comments
 (0)