Skip to content

Commit

Permalink
update cmake structure
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Mar 31, 2024
1 parent e1c7540 commit 94edfa8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 45 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.overrideCommand": ["cargo", "clippy", "--message-format=json"]
"rust-analyzer.check.overrideCommand": [
"cargo",
"clippy",
"--message-format=json"
],
"files.associations": {
"*.h": "c",
},
"cmake.sourceDirectory": "/Users/Omar/Projects/Rust/kuliya/c"
}
25 changes: 25 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,40 @@ cmake_minimum_required(VERSION 3.19)
project(kuliya)

include_directories(.)
include_directories(helpers)

# Add the helpers subdirectory
add_subdirectory(helpers)

# Create the shared library "kuliya"
add_library(kuliya SHARED kuliya.h)

# Set the linker language to C
set_target_properties(kuliya PROPERTIES LINKER_LANGUAGE C)

# Create the test executable
add_executable(test test/test.c)

# Create the example executable
add_executable(example example/main.c)

# Create the build executable
add_executable(build build.c)

# Link the test and example executables with the "kuliya" library
target_link_libraries(test kuliya)
target_link_libraries(example kuliya)

# Link the build executable with the "helpers" library
target_link_libraries(build helpers)

# Find and link the jsmn library
find_package(jsmn REQUIRED)
target_link_libraries(build jsmn::jsmn)

# Link the "helpers" static library with libunistring
find_package(libunistring REQUIRED)
target_link_libraries(helpers PRIVATE libunistring::libunistring)

# Print a success message
message("CMakeLists.txt updated successfully.")
2 changes: 1 addition & 1 deletion c/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <sys/types.h>
#include <jsmn.h>

#include "helpers/string.h"
#include "helpers/my_string.h"
#include "helpers/file.h"

#define TOK_SIZE 128
Expand Down
1 change: 1 addition & 0 deletions c/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[requires]
jsmn/1.1.0
libunistring/1.1

[generators]
CMakeDeps
Expand Down
2 changes: 2 additions & 0 deletions c/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(helpers SHARED file.h my_string.h my_string.c)
set_target_properties(helpers PROPERTIES LINKER_LANGUAGE C)
43 changes: 43 additions & 0 deletions c/helpers/my_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "my_string.h"

void remove_chars(char *str, int c, ...)
{
va_list args;
va_start(args, c);

while (42) // The answer for everything
{
char char_to_remove = va_arg(args, int);
if (char_to_remove == '\0')
break;

size_t j = 0;
for (size_t i = 0; str[i] != '\0'; ++i)
{
if (str[i] != char_to_remove)
str[j++] = str[i];
}
str[j] = '\0';
}

va_end(args);

// This is to confirm deleting the first vararg character
size_t j = 0;
for (size_t i = 0; str[i] != '\0'; ++i)
{
if (str[i] != c)
str[j++] = str[i];
}
str[j] = '\0';
}

void replace_char(char *str, char find, char replace)
{
char *current_pos = strchr(str, find);
while (current_pos)
{
*current_pos = replace;
current_pos = strchr(current_pos, find);
}
}
46 changes: 4 additions & 42 deletions c/helpers/string.h → c/helpers/my_string.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __STRING_H__
#define __STRING_H__
#ifndef __MY_STRING_H__
#define __MY_STRING_H__

#include <string.h>
#include <stdarg.h>
Expand Down Expand Up @@ -27,37 +27,7 @@
* @param c Characters to be removed from the given string.
* @returns This function do not return anything.
*/
void remove_chars(char *str, int c, ...)
{
va_list args;
va_start(args, c);

while (42) // The answer for everything
{
char char_to_remove = va_arg(args, int);
if (char_to_remove == '\0')
break;

size_t j = 0;
for (size_t i = 0; str[i] != '\0'; ++i)
{
if (str[i] != char_to_remove)
str[j++] = str[i];
}
str[j] = '\0';
}

va_end(args);

// This is to confirm deleting the first vararg character
size_t j = 0;
for (size_t i = 0; str[i] != '\0'; ++i)
{
if (str[i] != c)
str[j++] = str[i];
}
str[j] = '\0';
}
void remove_chars(char *str, int c, ...);

/**
* Replace a character with another character from a given string.
Expand All @@ -67,14 +37,6 @@ void remove_chars(char *str, int c, ...)
* @param replace Character to replace the origin character.
* @returns This function do not return anything.
*/
void replace_char(char *str, char find, char replace)
{
char *current_pos = strchr(str, find);
while (current_pos)
{
*current_pos = replace;
current_pos = strchr(current_pos, find);
}
}
void replace_char(char *str, char find, char replace);

#endif
2 changes: 1 addition & 1 deletion c/test/test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <assert.h>
#include "test.h"
#include "helpers/string.h"
#include "helpers/my_string.h"

int test_get_existing_schema(void)
{
Expand Down

0 comments on commit 94edfa8

Please sign in to comment.