From ec1ca30c7598f09e9d7a14c1ade9b1e2dbe74bf4 Mon Sep 17 00:00:00 2001 From: Francesco Date: Fri, 24 Oct 2025 20:18:43 +0200 Subject: [PATCH] style: clang-format --- .clang-format | 8 ++++++-- hashmap/myhashmap.c | 7 +++++-- hashmap/myhashmap.h | 6 ++++-- socket/mysocket.c | 50 ++++++++++++++++++++++----------------------- string/mystring.c | 10 ++++++--- string/mystring.h | 3 ++- test/hashmap/hm1.c | 7 +++++-- vector/myvector.c | 6 ++++-- 8 files changed, 58 insertions(+), 39 deletions(-) diff --git a/.clang-format b/.clang-format index 312c052..c22baa5 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,9 @@ BasedOnStyle: Google -ColumnLimit: 160 + UseTab: Always -IndentWidth: 4 TabWidth: 4 +IndentWidth: 4 + +ColumnLimit: 100 +PointerAlignment: Right +AllowShortFunctionsOnASingleLine: None diff --git a/hashmap/myhashmap.c b/hashmap/myhashmap.c index 912478d..d407c9a 100644 --- a/hashmap/myhashmap.c +++ b/hashmap/myhashmap.c @@ -7,7 +7,9 @@ /* * @brief Returns the mutex ID. */ -static size_t get_mutex(hashmap_s *hashmap, size_t hash) { return hash % hashmap->num_locks; } +static size_t get_mutex(hashmap_s *hashmap, size_t hash) { + return hash % hashmap->num_locks; +} static size_t get_bucket_index(hashmap_s *hashmap, void *key) { unsigned int hash = hashmap->hash(key); @@ -53,7 +55,8 @@ static bucket_s *find_bucket(hashmap_s *hashmap, void *key, bucket_s **prev) { return NULL; } -hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) { +hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, + free_value_f *free_value_fn, size_t key_size, size_t value_size) { hashmap_s *hashmap = malloc(sizeof(hashmap_s)); if (hashmap == NULL) { return NULL; diff --git a/hashmap/myhashmap.h b/hashmap/myhashmap.h index f492696..f5ea5ad 100644 --- a/hashmap/myhashmap.h +++ b/hashmap/myhashmap.h @@ -75,7 +75,8 @@ typedef struct hashmap { * @param[in] value_size Size in bytes of each value to be stored. * @return A pointer to the newly initialized hash map, or NULL on failure. */ -hashmap_s *hm_new(hash_f *hash, equal_f *equal, free_key_f *free_key, free_value_f *free_value, size_t key_size, size_t value_size); +hashmap_s *hm_new(hash_f *hash, equal_f *equal, free_key_f *free_key, free_value_f *free_value, + size_t key_size, size_t value_size); /** * @brief Free all resources used by the hash map. @@ -101,7 +102,8 @@ void hm_free_bucket(bucket_s *bucket); * @param[in] hashmap Pointer to the hash map. * @param[in] key Pointer to the key to insert (will be copied, must not be NULL). * @param[in] value Pointer to the value to insert (will be copied, must not be NULL). - * @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure). + * @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory + * allocation failure). */ bool hm_set(hashmap_s *hashmap, void *key, void *value); diff --git a/socket/mysocket.c b/socket/mysocket.c index 5e184f2..50bdad6 100644 --- a/socket/mysocket.c +++ b/socket/mysocket.c @@ -39,37 +39,37 @@ int sock_platform_shutdown() { } int sock_readall(int sockfd, void *buf, size_t bufsize) { - char *p = (char *)buf; - size_t total_read = 0; + char *p = (char *)buf; + size_t total_read = 0; - while (1) { - int n = recv(sockfd, p, bufsize - total_read, 0); + while (1) { + int n = recv(sockfd, p, bufsize - total_read, 0); - if (n > 0) { - p += n; - total_read += n; + if (n > 0) { + p += n; + total_read += n; - if (total_read == bufsize) { - break; - } - } else if (n == 0) { - break; - } else { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - break; - } else if (errno == EINTR) { - continue; - } else { - return -1; - } - } - } + if (total_read == bufsize) { + break; + } + } else if (n == 0) { + break; + } else { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + break; + } else if (errno == EINTR) { + continue; + } else { + return -1; + } + } + } - return total_read; + return total_read; } -int sock_writeall(int socket, const void* buf, size_t n) { - const char* p = (char*)buf; +int sock_writeall(int socket, const void *buf, size_t n) { + const char *p = (char *)buf; size_t bytes_to_write = n; int bytes_written; diff --git a/string/mystring.c b/string/mystring.c index 2dde00f..9f95128 100644 --- a/string/mystring.c +++ b/string/mystring.c @@ -27,7 +27,9 @@ static void buffer_destructor(void *buf) { free(tb); } -static void buffer_key_init(void) { tss_create(&buffer_key, buffer_destructor); } +static void buffer_key_init(void) { + tss_create(&buffer_key, buffer_destructor); +} /* Returns the next power of two of a number */ static size_t next_power_two(size_t len) { @@ -477,7 +479,8 @@ int string_insert(string_s *string, size_t index, const char *text) { } /* Shift bytes */ - memmove((char *)string->data + ((index + text_len) * sizeof(char)), (char *)string->data + (index * sizeof(char)), string->size - index + 1); + memmove((char *)string->data + ((index + text_len) * sizeof(char)), + (char *)string->data + (index * sizeof(char)), string->size - index + 1); /* Insert new text */ memcpy((char *)string->data + (index * sizeof(char)), text, text_len); string->size = new_size; @@ -502,7 +505,8 @@ int string_remove(string_s *string, size_t index, size_t length) { return -1; } - memmove((char *)string->data + (index * sizeof(char)), (char *)string->data + ((index + length) * sizeof(char)), string->size - length); + memmove((char *)string->data + (index * sizeof(char)), + (char *)string->data + ((index + length) * sizeof(char)), string->size - length); string->size -= length; string->data[string->size] = '\0'; diff --git a/string/mystring.h b/string/mystring.h index f65e48d..ebff71a 100644 --- a/string/mystring.h +++ b/string/mystring.h @@ -18,7 +18,8 @@ typedef struct string { * @brief Create a new string initialized with the given text. * * @param text Initial text. - * @param initial_capacity Initial buffer capacity (including null terminator). Pass 0 to auto-calculate. + * @param initial_capacity Initial buffer capacity (including null terminator). Pass 0 to + * auto-calculate. * @return Pointer to the new string, or NULL on failure. */ string_s *string_new(const char *text, size_t initial_capacity); diff --git a/test/hashmap/hm1.c b/test/hashmap/hm1.c index 02fb189..19610c5 100644 --- a/test/hashmap/hm1.c +++ b/test/hashmap/hm1.c @@ -38,7 +38,9 @@ bool my_equal_fun(const void *key_a, const void *key_b) { } /* And our last two functions, the free key and value called inside mcl_hm_remove() */ -void my_free_key(void *key) { free(key); } +void my_free_key(void *key) { + free(key); +} void my_free_value(void *value) { struct my_custom_type *mct = (struct my_custom_type *)value; @@ -52,7 +54,8 @@ void test_hm1(void) { /* This hashmap will contain names as keys and a custom type as value */ size_t key_size = sizeof(char) * MAX_STR_LEN; size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN; - hashmap_s *map = hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); + hashmap_s *map = + hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); assert(map != NULL); /* Make a new value */ diff --git a/vector/myvector.c b/vector/myvector.c index b9b6b48..8142f75 100644 --- a/vector/myvector.c +++ b/vector/myvector.c @@ -230,7 +230,8 @@ int vec_insert(vec_s *vec, size_t index, void *value) { /* Shift memory and copy the new value */ size_t tmp_size = vec->size - index + 1; - memmove((char *)vec->data + (index * vec->elem_size), (char *)vec->data + ((index + 1) * vec->elem_size), tmp_size * vec->elem_size); + memmove((char *)vec->data + (index * vec->elem_size), + (char *)vec->data + ((index + 1) * vec->elem_size), tmp_size * vec->elem_size); memcpy((char *)vec->data + (index * vec->elem_size), value, vec->elem_size); vec->size++; @@ -254,7 +255,8 @@ int vec_remove(vec_s *vec, size_t index) { size_t size = vec->size - index; /* Overwrite bytes */ - memmove((char *)vec->data + (index * vec->elem_size), (char *)vec->data + ((index + 1) * vec->elem_size), size); + memmove((char *)vec->data + (index * vec->elem_size), + (char *)vec->data + ((index + 1) * vec->elem_size), size); vec->size--; mtx_unlock(&vec->lock);