Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyaCoder committed Oct 9, 2024
1 parent 1b2c12f commit 84901b9
Show file tree
Hide file tree
Showing 30 changed files with 5,563 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.vs
/out
61 changes: 61 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required (VERSION 3.8)

# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("CWebServer")

# 包含 pthread 的 include 目录
include_directories("D:/CppProject/pthreads-w32-2-9-1-release/Pre-built.2/include")

# 链接 pthread 的 lib 目录
link_directories("D:/CppProject/pthreads-w32-2-9-1-release/Pre-built.2/lib/x64")

# 添加源文件到可执行文件
add_executable (CWebServer
"Networking/Nodes/Server.c"
"Networking/Nodes/Server.h"
"test/main.c"
"Networking/Protocols/HTTPRequest.h"
"Networking/Protocols/HTTPRequest.c"
"DataStructures/Common/Node.h"
"DataStructures/Common/Node.c"
"DataStructures/Lists/LinkedList.c"
"DataStructures/Lists/LinkedList.h"
"DataStructures/Lists/Queue.h"
"DataStructures/Lists/Queue.c"
"DataStructures/Trees/BinarySearchTree.h"
"DataStructures/Trees/BinarySearchTree.c"
"DataStructures/Dictionary/Entry.h"
"DataStructures/Dictionary/Entry.c"
"DataStructures/Dictionary/Dictionary.c"
"DataStructures/Dictionary/Dictionary.h"
"Networking/Nodes/HTTPServer.h"
"Networking/Nodes/HTTPServer.c"
"System/ThreadPool.h"
"System/ThreadPool.c"
"Networking/Nodes/Client.c"
"Networking/Nodes/Client.h"
"thirdparty/cJSON/cJSON.c"
"thirdparty/cJSON/cJSON.h"
"thirdparty/rxiLog/log.c"
"thirdparty/rxiLog/log.h"
)

# 链接 pthread 库 (使用 pthreadVC2)
target_link_libraries(CWebServer pthreadVC2)

# 将 pthreadVC2.dll 复制到输出目录
add_custom_command(TARGET CWebServer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"D:/CppProject/pthreads-w32-2-9-1-release/Pre-built.2/dll/x64/pthreadVC2.dll"
$<TARGET_FILE_DIR:CWebServer>
)

# 设置 C++ 标准
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CWebServer PROPERTY CXX_STANDARD 20)
endif()
101 changes: 101 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux-debug",
"displayName": "Linux Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "macos-debug",
"displayName": "macOS Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
}
]
}
25 changes: 25 additions & 0 deletions DataStructures/Common/Node.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#include "Node.h"

#include <string.h>

struct Node node_constructor(void* data, int size)
{
if (size < 1)
{
printf("Invalid data size for node...\n");
exit(1);
}
struct Node node;
node.data = malloc(size);
memcpy(node.data, data, size);
node.prev = NULL;
node.next = NULL;
return node;
}

void node_destructor(struct Node* node)
{
free(node->data);
free(node);
}
19 changes: 19 additions & 0 deletions DataStructures/Common/Node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef Node_h
#define Node_h

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

struct Node
{
void* data;
struct Node* prev;
struct Node* next;
};

struct Node node_constructor(void* data, int size);
void node_destructor(struct Node* node);

#endif // !Node_h
63 changes: 63 additions & 0 deletions DataStructures/Dictionary/Dictionary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#include "Dictionary.h"
#include <string.h>

void insert_dict(struct Dictionary* dictionary, void* key, int key_size, void* value, int value_size);
void* search_dict(struct Dictionary* dictionary, void* key, int key_size);

struct Dictionary dictionary_constructor(int (*compare)(void* key_1, void* key_2))
{
struct Dictionary dictionary;
dictionary.binary_search_tree = binary_search_tree_constructor(compare);
dictionary.insert = insert_dict;
dictionary.search = search_dict;
return dictionary;
}

void dictionary_destructor(struct Dictionary* dictionary)
{
binary_search_tree_destructor(&dictionary->binary_search_tree);
}

void* search_dict(struct Dictionary* dictionary, void* key, int key_size)
{
int dummy_value = 0;
struct Entry searchable = entry_constructor(key, key_size, &dummy_value, sizeof(dummy_value));
void* result =
dictionary->binary_search_tree
.search(&dictionary->binary_search_tree, &searchable, sizeof(searchable));
if (result)
{
return ((struct Entry*)result)->value;
}
else
{
return NULL;
}
}


void insert_dict(struct Dictionary* dictionary, void* key, int key_size, void* value, int value_size)
{
struct Entry entry = entry_constructor(key, key_size, value, value_size);

dictionary->binary_search_tree.insert(&dictionary->binary_search_tree, &entry, sizeof(entry));

}

// MARK: PUBLIC HELPER FUNCTIONS
int compare_string_keys(void* entry_1, void* entry_2)
{
if (strcmp((char*)((struct Entry*)entry_1)->key, (char*)((struct Entry*)entry_2)->key) > 0)
{
return 1;
}
else if (strcmp((char*)((struct Entry*)entry_1)->key, (char*)((struct Entry*)entry_2)->key) < 0)
{
return -1;
}
else
{
return 0;
}
}
18 changes: 18 additions & 0 deletions DataStructures/Dictionary/Dictionary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef Dictionary_h
#define Dictionary_h

#include "../Trees/BinarySearchTree.h"
#include "Entry.h"

struct Dictionary
{
struct BinarySearchTree binary_search_tree;

void (*insert)(struct Dictionary* dictionary, void* key, int key_size, void* value, int value_size);
void* (*search)(struct Dictionary* dictionary, void* key, int key_size);
};

struct Dictionary dictionary_constructor(int (*compare)(void* key_1, void* key_2));
void dictionary_destructor(struct Dictionary* dictionary);
int compare_string_keys(void* entry_1, void* entry_2);
#endif // !Dictionary_h
23 changes: 23 additions & 0 deletions DataStructures/Dictionary/Entry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include "Entry.h"

#include <stdlib.h>
#include <string.h>


struct Entry entry_constructor(void* key, int key_size, void* value, int value_size)
{
struct Entry entry;
entry.key = malloc(key_size);
entry.value = malloc(value_size);
memcpy(entry.key, key, key_size);
memcpy(entry.value, value, value_size);
return entry;
}

void entry_destructor(struct Entry* entry)
{
free(entry->key);
free(entry->value);
free(entry);
}
12 changes: 12 additions & 0 deletions DataStructures/Dictionary/Entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef Entry_h
#define Entry_h

struct Entry
{
void* key;
void* value;
};

struct Entry entry_constructor(void* key, int key_size, void* value, int value_size);
void entry_destructor(struct Entry* entry);
#endif // !Entry_h
Loading

0 comments on commit 84901b9

Please sign in to comment.