Skip to content

Commit

Permalink
Merge pull request #76 from dzcode-io/75-add-readmemd-for-c-library
Browse files Browse the repository at this point in the history
Add `README.md` for C library
  • Loading branch information
omdxp authored Apr 1, 2024
2 parents 5350e3a + 3de669c commit 1f72b29
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ You can find more about each language by clicking on the language name
| [Java](./java) | n/a | n/a | n/a | n/a |
| [C#](./csharp) | n/a | n/a | n/a | n/a |
| [Rust](./rust) | WIP | WIP | WIP | n/a |
| [C](./c) | WIP | WIP | WIP | n/a |

## Contributing

Expand Down
2 changes: 2 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ add_executable(test test/test.c)

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

# 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)
target_link_libraries(example2 kuliya)

# Link the build and test executables with the "helpers" library
target_link_libraries(build helpers)
Expand Down
49 changes: 49 additions & 0 deletions c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# kuliya for C

Algeria's college hierarchy dataset as a C library

# Prerequisites

- [CMake](https://cmake.org/download/) build system (minimum required version 3.19)
- [Conan](https://conan.io/downloads) package manager
- [Clang](https://clang.llvm.org/get_started.html) or [GCC](https://gcc.gnu.org/releases.html) compiler (on macOS clang comes with the developer tools)
- Any IDE out there, preferably [VSCode](https://code.visualstudio.com/download) with the following extensions:
- [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
- [CMake](https://marketplace.visualstudio.com/items?itemName=twxs.cmake)
- [CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools)
- [conan-extension](https://marketplace.visualstudio.com/items?itemName=konicy.conan-extension)

# Get Started

- Install build dependencies:
```sh
cd c && conan install . --output-folder=build --build=missing
```
- Click on CMake Tools icon <img src="https://ms-vscode.gallerycdn.vsassets.io/extensions/ms-vscode/cmake-tools/1.18.16/1711685798086/Microsoft.VisualStudio.Services.Icons.Default" width="20px"/> in the extensions sidebar (Open [CMakeLists.txt](./CMakeLists.txt) if it doesn't appear for some reason).
- Follow these steps:

https://github.com/dzcode-io/kuliya/assets/48713070/3f693a1c-050d-4ee2-bfce-9ade59e772ed

# Usage

```c
#include <stdio.h>
#include <kuliya.h>

int main(void)
{
kuliya_init();
kuliya_schema *res = get_node_by_path("umkb/fst/dee/sec");
if (res != NULL)
{
printf("%s\n", res->name.en);
}
kuliya_deinit();
}
```
Check practical example usage [here](./example/main.c).
# Contribute
Feel free to ask for help in [#kuliya](https://dzcode.slack.com/archives/C01C0155CKC) group chat
24 changes: 12 additions & 12 deletions c/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ void append_to_data_file()
for (size_t i = 0; i < kuliya_with_terms_idx; ++i)
{
__s_kuliya_schema schema = kuliyas_with_terms[i];
fprintf(data_file, "\t%s.terms = malloc(sizeof(kuliya_terms));\n", schema.__varname);
fprintf(data_file, "\t%s.terms = (kuliya_terms *)malloc(sizeof(kuliya_terms));\n", schema.__varname);
fprintf(data_file, "\t%s.terms->per_year = %d;\n", schema.__varname, schema.terms.per_year);
fprintf(data_file, "\t%s.terms->number_of_slots = %zu;\n", schema.__varname, schema.__slots_length);
fprintf(data_file, "\t%s.terms->slots = malloc(%zu * sizeof(int));\n", schema.__varname, schema.__slots_length);
fprintf(data_file, "\t%s.terms->slots = (int *)malloc(%zu * sizeof(int));\n", schema.__varname, schema.__slots_length);
for (size_t j = 0; j < schema.__slots_length; ++j)
{
fprintf(data_file, "\t%s.terms->slots[%zu] = %d;\n", schema.__varname, j, schema.terms.slots[j]);
Expand Down Expand Up @@ -318,7 +318,7 @@ void parse_info_json(const unsigned char *json_path)
unsigned char data[length + 1];
memcpy(data, &buf[token->start], length);
data[length] = '\0';
if (STR_EQ("ar", data))
if (STR_EQ("ar", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -328,7 +328,7 @@ void parse_info_json(const unsigned char *json_path)
schema->name->ar = calloc(1, length + 1);
memcpy(schema->name->ar, data, length + 1);
}
if (STR_EQ("en", data))
if (STR_EQ("en", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -338,7 +338,7 @@ void parse_info_json(const unsigned char *json_path)
schema->name->en = calloc(1, length + 1);
memcpy(schema->name->en, data, length + 1);
}
if (STR_EQ("fr", data))
if (STR_EQ("fr", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -348,7 +348,7 @@ void parse_info_json(const unsigned char *json_path)
schema->name->fr = calloc(1, length + 1);
memcpy(schema->name->fr, data, length + 1);
}
if (STR_EQ("type", data))
if (STR_EQ("type", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -358,11 +358,11 @@ void parse_info_json(const unsigned char *json_path)
schema->type = calloc(1, length + 1);
memcpy(schema->type, data, length + 1);
}
if (STR_EQ("terms", data))
if (STR_EQ("terms", (char *)data))
{
schema->terms = calloc(1, sizeof(kuliya_terms));
}
if (STR_EQ("perYear", data))
if (STR_EQ("perYear", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -371,7 +371,7 @@ void parse_info_json(const unsigned char *json_path)
data[length] = '\0';
schema->terms->per_year = atoi(data);
}
if (STR_EQ("slots", data))
if (STR_EQ("slots", (char *)data))
{
token = &tokens[i + 1];
unsigned int length = token->end - token->start;
Expand All @@ -380,13 +380,13 @@ void parse_info_json(const unsigned char *json_path)
data[length] = '\0';
remove_chars(data, '[', ']', ' ');
unsigned char *rest = data;
unsigned char *ptr = u8_strtok(data, ",", &rest);
unsigned char *ptr = u8_strtok(data, (unsigned char *)",", &rest);
int tmp_slots[50];
while (ptr != NULL)
{
int val = atoi(ptr);
int val = atoi((const char *)ptr);
tmp_slots[s_idx++] = val;
ptr = u8_strtok(NULL, ",", &rest);
ptr = u8_strtok(NULL, (unsigned char *)",", &rest);
}
schema->terms->slots = calloc(1, s_idx * sizeof(*schema->terms->slots));
memcpy(schema->terms->slots, tmp_slots, s_idx * sizeof(int));
Expand Down
4 changes: 2 additions & 2 deletions c/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ kuliya_schema ua2aks_falaol_dala = {.name = {.ar = "قسم اللغة العرب

void __kuliya_init()
{
umkb_fst_dee_sec.terms = malloc(sizeof(kuliya_terms));
umkb_fst_dee_sec.terms = (kuliya_terms *)malloc(sizeof(kuliya_terms));
umkb_fst_dee_sec.terms->per_year = 2;
umkb_fst_dee_sec.terms->number_of_slots = 4;
umkb_fst_dee_sec.terms->slots = malloc(4 * sizeof(int));
umkb_fst_dee_sec.terms->slots = (int *)malloc(4 * sizeof(int));
umkb_fst_dee_sec.terms->slots[0] = 7;
umkb_fst_dee_sec.terms->slots[1] = 8;
umkb_fst_dee_sec.terms->slots[2] = 9;
Expand Down
35 changes: 18 additions & 17 deletions c/example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,32 @@ const char *match_node_type(const node_type type)
}

/**
* Print kuliya schema with JSON format in `stdout`.
* Print kuliya schema with JSON format to a file (.e.g `stdout`).
* @param out File output.
* @param schema Kuliya node schema.
*/
void print_kuliya_schema(const kuliya_schema *schema)
void print_kuliya_schema(FILE *out, const kuliya_schema *schema)
{
printf("{\n");
printf("\t\"name\": {\n");
printf("\t\t\"ar\": \"%s\",\n", schema->name.ar);
printf("\t\t\"en\": \"%s\",\n", schema->name.en);
printf("\t\t\"fr\": \"%s\"\n", schema->name.fr);
printf("\t},\n");
printf("\t\"type\": \"%s\"%s\n", match_node_type(schema->type), schema->terms != NULL ? "," : "");
fprintf(out, "{\n");
fprintf(out, "\t\"name\": {\n");
fprintf(out, "\t\t\"ar\": \"%s\",\n", schema->name.ar);
fprintf(out, "\t\t\"en\": \"%s\",\n", schema->name.en);
fprintf(out, "\t\t\"fr\": \"%s\"\n", schema->name.fr);
fprintf(out, "\t},\n");
fprintf(out, "\t\"type\": \"%s\"%s\n", match_node_type(schema->type), schema->terms != NULL ? "," : "");
if (schema->terms != NULL)
{
printf("\t\"terms\": {\n");
printf("\t\t\"perYear\": %d,\n", schema->terms->per_year);
printf("\t\t\"slots\": [%d", schema->terms->slots[0]);
fprintf(out, "\t\"terms\": {\n");
fprintf(out, "\t\t\"perYear\": %d,\n", schema->terms->per_year);
fprintf(out, "\t\t\"slots\": [%d", schema->terms->slots[0]);
for (size_t i = 1; i < schema->terms->number_of_slots; i++)
{
printf(", %d", schema->terms->slots[i]);
fprintf(out, ", %d", schema->terms->slots[i]);
}
printf("]\n");
printf("\t}\n");
fprintf(out, "]\n");
fprintf(out, "\t}\n");
}
printf("}\n");
fprintf(out, "}\n");
}

int main(void)
Expand All @@ -63,7 +64,7 @@ int main(void)
kuliya_schema *res = get_node_by_path("umkb/fst/dee/sec");
if (res != NULL)
{
print_kuliya_schema(res);
print_kuliya_schema(stdout, res);
}
kuliya_deinit();
}
32 changes: 32 additions & 0 deletions c/example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <kuliya.h>

class Kuliya
{
public:
Kuliya();
~Kuliya();
kuliya_schema *GetNodeByPath(const char *path);
};

Kuliya::Kuliya()
{
kuliya_init();
}

Kuliya::~Kuliya()
{
kuliya_deinit();
}

kuliya_schema *Kuliya::GetNodeByPath(const char *path)
{
return get_node_by_path(path);
}

int main(void)
{
Kuliya *kuliya = new Kuliya();
auto schema = kuliya->GetNodeByPath("umkb/fst/dee/sec");
std::cout << schema->name.en << "\n";
}

0 comments on commit 1f72b29

Please sign in to comment.