This commit is contained in:
2024-12-03 17:12:18 +08:00
parent 95925cc855
commit f2a470c59d
62 changed files with 1332 additions and 477 deletions

View File

@@ -1,9 +0,0 @@
#ifndef __LATCH_ENCODING_H__
#define __LATCH_ENCODING_H__
typedef enum _latch_encoding_type {
LT_ENCODING_UTF8 = 0,
LT_ENCODING_UTF16LE = 1,
} lt_encoding_type_t;
#endif //__LATCH_ENCODING_H__

View File

@@ -1,10 +0,0 @@
#ifndef __LATCH_STATS_H__
#define __LATCH_STATS_H__
typedef enum _latch_error {
LT_ERROR_OK = 0,
LT_ERROR_MEMORY,
LT_ERROR_IO,
} lt_error_t;
#endif // __LATCH_STATS_H__

View File

@@ -1,36 +0,0 @@
#ifndef __LATCH_TYPES_H__
#define __LATCH_TYPES_H__
typedef unsigned char lt_u8;
typedef unsigned short lt_u16;
typedef unsigned int lt_u32;
typedef unsigned long lt_u64;
typedef signed char lt_int8;
typedef signed short lt_int16;
typedef signed int lt_int32;
typedef signed long lt_int64;
typedef unsigned char lt_byte;
typedef lt_u64 lt_size_t;
typedef lt_u64 lt_time_t;
typedef lt_u8 lt_bool;
#define true 1
#define false 0
#define LT_NULL 0
#define LT_NULL_PTR ((void *)0)
typedef union {
lt_u8 u8;
lt_u16 u16;
lt_u32 u32;
lt_u64 u64;
lt_int8 i8;
lt_int16 i16;
lt_int32 i32;
lt_int64 i64;
lt_byte byte;
lt_bool bool;
lt_size_t size_t;
} lt_any_type_t;
#endif // __LATCH_TYPES_H__

View File

@@ -0,0 +1,16 @@
#ifndef __LT_ARRAY_H__
#define __LT_ARRAY_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LT_ARRAY_H__

View File

@@ -1,12 +1,20 @@
#ifndef __LATCH_CONTAINER_BYTES_H__
#define __LATCH_CONTAINER_BYTES_H__
#include "latch/base/lt-types.h"
#include <stddef.h>
typedef struct __lt_bytes {
lt_size_t length;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct lt_bytes_s {
size_t length;
lt_byte *data;
void (*close)(struct __lt_bytes *self);
} LT_Bytes;
void (*close)(struct lt_bytes_s *self);
} lt_bytes_t;
#endif // __LATCH_CONTAINER_BYTES_H__
#ifdef __cplusplus
}
#endif
#endif // __LATCH_CONTAINER_BYTES_H__

View File

@@ -0,0 +1,12 @@
#ifndef __LT_DLIST_H__
#define __LT_DLIST_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LT_DLIST_H__

View File

@@ -0,0 +1,10 @@
#ifndef __LT_MAP_H__
#define __LT_MAP_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LT_MAP_H__

View File

@@ -0,0 +1,46 @@
#ifndef __LATCH_CONTAINER_SLIST_H__
#define __LATCH_CONTAINER_SLIST_H__
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
struct lt_slist_node_s {
void *data;
struct lt_slist_node_s *next;
};
typedef struct lt_slist_s {
struct lt_slist_node_s *head;
struct lt_slist_node_s *tail;
size_t size;
size_t type_size;
} lt_slist_t;
/**
*!...
* @brief 新建一个单链表
* @param type_size 单链表存储的数据类型大小
* @return lt_slist_t*
*/
lt_slist_t *lt_slist_new(size_t type_size);
/**
* @brief 单链表添加数据
* @param
* @return
*/
bool lt_slist_append(lt_slist_t *self, void *data);
void *lt_slist_pop(lt_slist_t *self);
void *lt_slist_get(lt_slist_t *self, size_t index);
#ifdef __cplusplus
}
#endif
#endif // __LATCH_CONTAINER_SLIST_H__

View File

@@ -1,18 +1,61 @@
#ifndef __LATCH_CONTAINER_STRING_H__
#define __LATCH_CONTAINER_STRING_H__
#include "latch/base/lt-types.h"
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct __lt_string {
lt_size_t length;
typedef struct lt_string_s {
/// 字符串长度
size_t length;
/// 字符串数据
char *data;
void (*close)(struct __lt_string *self);
} LT_String;
} lt_string_t;
/**
* @brief 创建字符串
* @param data 字符串数据
* @return lt_string_t*
*/
lt_string_t *lt_string_new(const char *data);
LT_String *lt_string_new(const char *data);
void lt_string_reNew(LT_String* str, const char *data);
LT_String *lt_string_reverse(LT_String *self);
LT_String *lt_string_cut(LT_String *self, lt_int64 start, lt_int64 end, lt_int64 step);
/**
* @brief 销毁字符串
* @param str 字符串指针
* @return void
*/
void lt_string_close(lt_string_t *str);
#endif // __LATCH_CONTAINER_STRING_H__
/**
* @brief 重新设置字符串的值
* @param str 字符串指针
* @param data 字符串数据
* @return
*/
void lt_string_reNew(lt_string_t *str, const char *data);
/**
* @brief 字符串反转
* @param str 字符串指针
* @return
*/
lt_string_t *lt_string_reverse(lt_string_t *str);
/**
* @brief 字符串切片
* @param str 字符串指针
* @param start 开始位置
* @param end 结束位置
* @param step 步长
* @return
*/
lt_string_t *
lt_string_cut(lt_string_t *self, int32_t start, int32_t end, int32_t step);
#ifdef __cplusplus
}
#endif
#endif // __LATCH_CONTAINER_STRING_H__

View File

@@ -0,0 +1,10 @@
#ifndef __LT_SHA_H__
#define __LT_SHA_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LT_SHA_H__

View File

@@ -0,0 +1,98 @@
#ifndef __LT_HTTP_BASE_H__
#define __LT_HTTP_BASE_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief HTTP status code
* is_informational | 100 <= status <= 199 | HTTP Semantics RFC 9110, Section 15
* is_success | 200 <= status <= 299 | HTTP Semantics RFC 9110, Section 15
* is_redirection | 300 <= status <= 399 | HTTP Semantics RFC 9110, Section 15
* is_client_error | 400 <= status <= 499 | HTTP Semantics RFC 9110, Section 15
* is_server_error | 500 <= status <= 599 | HTTP Semantics RFC 9110, Section 15
*/
typedef enum lt_http_status_code_e {
LT_HTTP_CONTINUE = 100,
LT_HTTP_SWITCHING_PROTOCOLS = 101,
LT_HTTP_PROCESSING = 102,
LT_HTTP_EARLY_HINTS = 103,
LT_HTTP_OK = 200,
LT_HTTP_CREATED = 201,
LT_HTTP_ACCEPTED = 202,
LT_HTTP_NON_AUTHORITATIVE = 203,
LT_HTTP_NO_CONTENT = 204,
LT_HTTP_RESET_CONTENT = 205,
LT_HTTP_PARTIAL_CONTENT = 206,
LT_HTTP_MULTI_STATUS = 207,
LT_HTTP_ALREADY_REPORTED = 208,
LT_HTTP_IM_USED = 226,
LT_HTTP_MULTIPLE_CHOICES = 300,
LT_HTTP_MOVED_PERMANENTLY = 301,
LT_HTTP_FOUND = 302,
LT_HTTP_SEE_OTHER = 303,
LT_HTTP_NOT_MODIFIED = 304,
LT_HTTP_USE_PROXY = 305,
LT_HTTP_SWITCH_PROXY = 306,
LT_HTTP_TEMPORARY_REDIRECT = 307,
LT_HTTP_PERMANENT_REDIRECT = 308,
LT_HTTP_BAD_REQUEST = 400,
LT_HTTP_UNAUTHORIZED = 401,
LT_HTTP_PAYMENT_REQUIRED = 402,
LT_HTTP_FORBIDDEN = 403,
LT_HTTP_NOT_FOUND = 404,
LT_HTTP_METHOD_NOT_ALLOWED = 405,
LT_HTTP_NOT_ACCEPTABLE = 406,
LT_HTTP_PROXY_AUTHENTICATION_REQUIRED = 407,
LT_HTTP_REQUEST_TIMEOUT = 408,
LT_HTTP_CONFLICT = 409,
LT_HTTP_GONE = 410,
LT_HTTP_LENGTH_REQUIRED = 411,
LT_HTTP_PRECONDITION_FAILED = 412,
LT_HTTP_PAYLOAD_TOO_LARGE = 413,
LT_HTTP_URI_TOO_LONG = 414,
LT_HTTP_UNSUPPORTED_MEDIA_TYPE = 415,
LT_HTTP_RANGE_NOT_SATISFIABLE = 416,
LT_HTTP_EXPECTATION_FAILED = 417,
LT_HTTP_I_AM_A_TEAPOT = 418,
LT_HTTP_MISDIRECTED_REQUEST = 421,
LT_HTTP_UNPROCESSABLE_ENTITY = 422,
LT_HTTP_LOCKED = 423,
LT_HTTP_FAILED_DEPENDENCY = 424,
LT_HTTP_TOO_EARLY = 425,
LT_HTTP_UPGRADE_REQUIRED = 426,
LT_HTTP_PRECONDITION_REQUIRED = 428,
LT_HTTP_TOO_MANY_REQUESTS = 429,
LT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
LT_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
LT_HTTP_INTERNAL_SERVER_ERROR = 500,
LT_HTTP_NOT_IMPLEMENTED = 501,
LT_HTTP_BAD_GATEWAY = 502,
LT_HTTP_SERVICE_UNAVAILABLE = 503,
LT_HTTP_GATEWAY_TIMEOUT = 504,
LT_HTTP_VERSION_NOT_SUPPORTED = 505,
LT_HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506,
LT_HTTP_INSUFFICIENT_STORAGE = 507,
LT_HTTP_LOOP_DETECTED = 508,
LT_HTTP_NOT_EXTENDED = 510,
LT_HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511,
} lt_http_status_code_e;
typedef enum lt_http_method_e {
LT_HTTP_GET = 0,
LT_HTTP_HEAD,
LT_HTTP_POST,
LT_HTTP_PUT,
LT_HTTP_DELETE,
LT_HTTP_CONNECT,
LT_HTTP_OPTIONS,
LT_HTTP_TRACE,
LT_HTTP_PATCH,
} lt_http_method_e;
#ifdef __cplusplus
}
#endif
#endif // __LT_HTTP_BASE_H__

View File

@@ -1,11 +1,32 @@
#ifndef __LT_HTTP_H__
#define __LT_HTTP_H__
typedef enum __lt_http_code{
LT_HTTP_OK = 200,
LT_HTTP_BAD_REQUEST = 400,
LT_HTTP_NOT_FOUND = 404,
} lt_http_status_code;
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#endif //__LT_HTTP_H__
typedef struct lt_http_s {
// TODO
} lt_http_t;
lt_http_t *lt_http_init(void);
void lt_http_close(lt_http_t *h);
int lt_http_listen(lt_http_t *h,
char *host,
int port,
uint32_t blocksize); // TODO
int lt_http_connect(lt_http_t *h,
char *host,
uint32_t port,
uint32_t timeout,
uint32_t blocksize); // TODO
#ifdef __cplusplus
}
#endif
#endif //__LT_HTTP_H__

View File

@@ -0,0 +1,10 @@
#ifndef __LT_HTTPS_H__
#define __LT_HTTPS_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LT_HTTPS_H__

View File

@@ -1,37 +1,20 @@
#ifndef __LATCH_FILE_H__
#define __LATCH_FILE_H__
#include "latch/base/lt-stats.h"
#include "latch/base/lt-types.h"
#include "latch/container/lt-bytes.h"
#include "latch/container/lt-string.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
LT_FILE_SENK_BEG = 0,
LT_FILE_SENK_CUR,
LT_FILE_SENK_END,
} lt_file_senk_flag_t;
int64_t lt_get_file_size(FILE *f);
time_t lt_get_file_mtime(FILE *f);
time_t lt_get_file_ctime(FILE *f);
typedef struct __LT_File {
LT_String *name;
LT_String *path;
lt_size_t size;
lt_time_t create_time;
lt_time_t modify_time;
FILE *fp;
lt_error_t *(*read_bytes)(struct __LT_File *self, lt_size_t size,
LT_Bytes *read_buffer);
lt_error_t (*write_bytes)(struct __LT_File *self, LT_Bytes *write_buffer);
void (*seek)(struct __LT_File *self, lt_int64 offset,
lt_file_senk_flag_t flag);
void (*close)(struct __LT_File *self);
} LT_File;
LT_File *lt_file_open(const char *path, const char *mode);
lt_bool lt_file_copy(const char *src, const char *dst); //TODO 文件复制
lt_bool lt_file_remove(const char *path); //TODO 文件删除
lt_bool lt_file_create(const char *path, const char *mode); //TODO 文件创建
lt_bool lt_file_chmod(const char *path, lt_u32 mode); //TODO 文件权限修改
#ifdef __cplusplus
}
#endif
#endif // __LATCH_FILE_H__

View File

@@ -1,23 +0,0 @@
#ifndef __LATCH_H__
#define __LATCH_H__
#include "latch/lt-version.h"
//基础
#include "latch/base/lt-types.h"
#include "latch/base/lt-stats.h"
//容器
#include "latch/container/lt-string.h"
#include "latch/container/lt-bytes.h"
#include "latch/container/lt-dlist.h"
#include "latch/container/lt-map.h"
#include "latch/container/lt-slist.h"
#include "latch/memory/lt-memory.h"
#include "latch/io/lt-file.h"
#endif

View File

@@ -7,4 +7,12 @@
#define LATCH_VERSION "0.1.0"
#endif // __LATCH_VERSION_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif // __LATCH_VERSION_H__

View File

@@ -0,0 +1,35 @@
#ifndef __LATCH_MEMORY_DEBUG_H__
#define __LATCH_MEMORY_DEBUG_H__
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct lt_memory_debug_info_s {
char *file;
int line;
void *ptr;
size_t size;
struct lt_memory_debug_info_s *next;
} lt_memory_debug_info_t;
typedef struct lt_memory_debug_info_list_s {
lt_memory_debug_info_t *head;
lt_memory_debug_info_t *tail;
size_t size;
} lt_memory_debug_info_list_t;
void lt_memory_debug_info_add(const char *file,
int line,
void *ptr,
size_t size);
void lt_memory_debug_print_info();
bool lt_memory_debug_info_remove_prt(void *ptr);
#ifdef __cplusplus
}
#endif
#endif // __LATCH_MEMORY_DEBUG_H__

View File

@@ -1,18 +1,39 @@
#ifndef __LATCH_MEMORY_H__
#define __LATCH_MEMORY_H__
#include "latch/base/lt-types.h"
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum lt_memory_type {
LT_MEMORY_TYPE_LIBC = 1,
LT_MEMORY_TYPE_POOL,
LT_MEMORY_TYPE_LIBC = 1,
LT_MEMORY_TYPE_POOL,
} lt_memory_type_e;
void *lt_malloc(lt_size_t size);
void *lt_calloc(lt_size_t nmemb, lt_size_t size);
void *lt_realloc(void *ptr, lt_size_t size);
void lt_free(void *ptr);
#define lt_malloc(size) lt_malloc_direct((size), __FILE__, __LINE__)
#define lt_calloc(nmemb, size) \
lt_calloc_direct((nmemb), (size), __FILE__, __LINE__)
#define lt_realloc(ptr, size) \
lt_realloc_direct((ptr), (size), __FILE__, __LINE__)
#define lt_free(ptr) lt_free_direct((ptr), __FILE__, __LINE__)
void lt_set_default_memory_type(lt_memory_type_e type);
void *lt_malloc_direct(size_t size, char *file, int line);
void *lt_calloc_direct(size_t nmemb, size_t size, char *file, int line);
void *lt_realloc_direct(void *ptr, size_t size, char *file, int line);
void lt_free_direct(void *ptr, char *file, int line);
void lt_memory_set_default_type(lt_memory_type_e type);
void lt_memory_set_functions(void *(*malloc_func)(size_t size),
void *(*calloc_func)(size_t nmemb,
size_t size),
void *(*realloc_func)(void *ptr, size_t size),
void (*free_func)(void *ptr));
#ifdef __cplusplus
}
#endif
#endif // __LATCH_MEMORY_H__

View File

@@ -1,5 +1,23 @@
#ifndef __LATCH_OS_H__
#define __LATCH_OS_H__
#ifndef __LT_OS_H__
#define __LT_OS_H__
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#endif // __LATCH_OS_H__
uint32_t lt_os_get_cpu_count(void);
uint64_t lt_os_get_memory_size(void);
char *lt_os_get_username(void);
char *lt_os_get_hostname(void);
char *lt_os_get_cwd(void);
bool lt_os_mkdir(const char *path, uint32_t mode);
#ifdef __cplusplus
}
#endif
#endif // __LT_OS_H__

View File

@@ -1,10 +1,24 @@
#ifndef __LATCH_PATH_H__
#define __LATCH_PATH_H__
#include "latch/base/lt-types.h"
#include <stdbool.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
lt_bool lt_path_is_file(const char *path);
lt_bool lt_path_is_dir(const char *path);
lt_bool lt_path_exists(const char *path);
bool lt_path_is_file(const char *path);
bool lt_path_is_dir(const char *path);
bool lt_path_is_link(const char *path);
bool lt_path_exists(const char *path);
void lt_path_join(char *dest,
size_t dest_size,
const char *left,
const char *right);
bool lt_path_split(const char *path, char *dir, char *file);
#endif // __LATCH_PATH_H__
#ifdef __cplusplus
}
#endif
#endif // __LATCH_PATH_H__

View File

@@ -0,0 +1,23 @@
#ifndef __LT_SOCKET_BASE_H__
#define __LT_SOCKET_BASE_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef enum lt_socket_type_e {
LT_SOCKET_TYPE_TCP,
LT_SOCKET_TYPE_UDP,
LT_SOCKET_TYPE_RAW,
} lt_socket_type_e;
typedef enum lt_socket_ip_type_e {
LT_SOCKET_IP_TYPE_IPV4,
LT_SOCKET_IP_TYPE_IPV6,
} lt_socket_ip_type_e;
#ifdef __cplusplus
}
#endif
#endif // __LT_SOCKET_BASE_H__

View File

@@ -0,0 +1,25 @@
#ifndef __LATCH_SOCKET_H__
#define __LATCH_SOCKET_H__
#include "lt-socket-base.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct lt_socket_s {
lt_socket_type_e type;
} lt_socket_t;
lt_socket_t *lt_socket_new(lt_socket_type_e type);
void lt_socket_close(lt_socket_t *socket);
void lt_socket_bind(lt_socket_t *socket, const char *ip, int port);
void lt_socket_listen(lt_socket_t *socket, int max_backlog);
lt_socket_t *lt_socket_accept(lt_socket_t *socket);
#ifdef __cplusplus
}
#endif
#endif // __LATCH_SOCKET_H__