From f8431897e91f8315b13d509f9f3121e6366be7b9 Mon Sep 17 00:00:00 2001 From: youmetme <321640253@qq.com> Date: Sat, 2 Nov 2024 21:29:23 +0800 Subject: [PATCH] init --- .gitignore | 2 + CMakeLists.txt | 20 ++++++ LICENSE | 21 ++++++ README.md | 26 +++++++ conanfile.py | 58 ++++++++++++++++ include/README.md | 1 + include/latch/base/lt-encoding.h | 9 +++ include/latch/base/lt-stats.h | 10 +++ include/latch/base/lt-types.h | 36 ++++++++++ include/latch/container/lt-bytes.h | 12 ++++ include/latch/container/lt-dlist.h | 0 include/latch/container/lt-map.h | 0 include/latch/container/lt-slist.h | 0 include/latch/container/lt-string.h | 18 +++++ include/latch/hash/lt-sha.h | 0 include/latch/http/lt-http.h | 11 +++ include/latch/http/lt-https.h | 0 include/latch/io/lt-file.h | 37 ++++++++++ include/latch/latch.h | 23 ++++++ include/latch/lt-version.h | 10 +++ include/latch/memory/lt-memory.h | 18 +++++ include/latch/os/lt-os.h | 5 ++ include/latch/os/lt-path.h | 10 +++ include/latch/socket/lt-tcp.h | 0 include/latch/socket/lt-udp.h | 0 script/test.sh | 3 + src/CMakeLists.txt | 36 ++++++++++ src/container/map/lt-map.c | 0 src/container/string/lt-string.c | 96 ++++++++++++++++++++++++++ src/io/cross/unix/lt-file-linux.c | 22 ++++++ src/io/cross/unix/lt-file-linux.h | 23 ++++++ src/io/cross/windows/lt-file-windows.c | 14 ++++ src/io/cross/windows/lt-file-windows.h | 23 ++++++ src/io/lt-file.c | 60 ++++++++++++++++ src/memory/lt-memory-libc.c | 18 +++++ src/memory/lt-memory-libc.h | 11 +++ src/memory/lt-memory-pool.c | 7 ++ src/memory/lt-memory-pool.h | 11 +++ src/memory/lt-memory.c | 58 ++++++++++++++++ src/os/lt-path.c | 1 + tests/CMakeLists.txt | 7 ++ tests/container/CMakeLists.txt | 10 +++ tests/container/test-lt-string.c | 31 +++++++++ tests/io/CMakeLists.txt | 11 +++ tests/io/test-lt-file.c | 16 +++++ 45 files changed, 785 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 README.md create mode 100644 conanfile.py create mode 100644 include/README.md create mode 100644 include/latch/base/lt-encoding.h create mode 100644 include/latch/base/lt-stats.h create mode 100644 include/latch/base/lt-types.h create mode 100644 include/latch/container/lt-bytes.h create mode 100644 include/latch/container/lt-dlist.h create mode 100644 include/latch/container/lt-map.h create mode 100644 include/latch/container/lt-slist.h create mode 100644 include/latch/container/lt-string.h create mode 100644 include/latch/hash/lt-sha.h create mode 100644 include/latch/http/lt-http.h create mode 100644 include/latch/http/lt-https.h create mode 100644 include/latch/io/lt-file.h create mode 100644 include/latch/latch.h create mode 100644 include/latch/lt-version.h create mode 100644 include/latch/memory/lt-memory.h create mode 100644 include/latch/os/lt-os.h create mode 100644 include/latch/os/lt-path.h create mode 100644 include/latch/socket/lt-tcp.h create mode 100644 include/latch/socket/lt-udp.h create mode 100644 script/test.sh create mode 100644 src/CMakeLists.txt create mode 100644 src/container/map/lt-map.c create mode 100644 src/container/string/lt-string.c create mode 100644 src/io/cross/unix/lt-file-linux.c create mode 100644 src/io/cross/unix/lt-file-linux.h create mode 100644 src/io/cross/windows/lt-file-windows.c create mode 100644 src/io/cross/windows/lt-file-windows.h create mode 100644 src/io/lt-file.c create mode 100644 src/memory/lt-memory-libc.c create mode 100644 src/memory/lt-memory-libc.h create mode 100644 src/memory/lt-memory-pool.c create mode 100644 src/memory/lt-memory-pool.h create mode 100644 src/memory/lt-memory.c create mode 100644 src/os/lt-path.c create mode 100644 tests/CMakeLists.txt create mode 100644 tests/container/CMakeLists.txt create mode 100644 tests/container/test-lt-string.c create mode 100644 tests/io/CMakeLists.txt create mode 100644 tests/io/test-lt-file.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d73e005 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..673cf86 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.28...3.30) + +set(CMAKE_EXPORT_COMPILE_COMMANDS yes) + +project(latch) +set(OUTPUT_BUILD_DIR ${PROJECT_SOURCE_DIR}/build) + +option(TEST "是否启动单元测试" ON) +option(SHARED "是否编译为动态库" OFF) + +include_directories(include) + +add_subdirectory(src) + +message(是否开启编译测试单元?${TEST}) +if (TEST) + enable_testing() + add_subdirectory(tests) +endif() + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c5aaac8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 youmetme + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1445505 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Latch 一个c语言stl库 + + + +# 构建 +## conan2构建 +```shell +conan create . +``` +## conan2使用 +`conanfile.txt` +```txt +[requires] +latch/0.1.0 + +[generators] +CMakeDeps +CMakeToolchain + +[layout] +cmake_layout +``` +编辑好`conanfile.txt`后执行以下命令 +```shell +conan install . -b missing +``` \ No newline at end of file diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000..ea52dfc --- /dev/null +++ b/conanfile.py @@ -0,0 +1,58 @@ +from conan import ConanFile +from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout +from conan.tools.files import copy +import os + + +class LatchRecipe(ConanFile): + name = "latch" + version = "0.1.0" + license = "MIT" + author = "321640253@qq.com" + url = "https://github.com/WangZhongDian/latch.git" + description = "一个c开发增强库,简单易用,提供常用的开发工具与容器" + topics = ("latch", "C", "simple", "easy-to-use", "glib2","tbox") + + settings = "os", "compiler", "build_type", "arch" + options = {"shared": [True, False], "fPIC": [True, False], "test": [True, False]} + default_options = {"shared": False, "fPIC": True, "test": True} + + exports_sources = "include/*", "CMakeLists.txt", "src/*", "tests/*" + + def config_options(self): + if self.settings.os == "Windows": + del self.options.fPIC + + def configure(self): + if self.options.shared: + self.options.rm_safe("fPIC") + + def layout(self): + cmake_layout(self) + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.variables["SHARED"] = True if self.options.shared else False + tc.variables["TEST"] = True if self.options.test else False + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + if self.options.test: + cmake.test() + + def package(self): + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) + copy(self, pattern="*.h", src=os.path.join(self.source_folder, "include"), dst=os.path.join(self.package_folder, "include")) + copy(self, pattern="*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) + copy(self, pattern="*.so", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) + copy(self, pattern="*.lib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) + copy(self, pattern="*.dll", src=self.build_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False) + copy(self, pattern="*.dylib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False) + + def package_info(self): + self.cpp_info.libs = ["latch"] \ No newline at end of file diff --git a/include/README.md b/include/README.md new file mode 100644 index 0000000..9690d6f --- /dev/null +++ b/include/README.md @@ -0,0 +1 @@ +# 存放公开的API接口 \ No newline at end of file diff --git a/include/latch/base/lt-encoding.h b/include/latch/base/lt-encoding.h new file mode 100644 index 0000000..a644e77 --- /dev/null +++ b/include/latch/base/lt-encoding.h @@ -0,0 +1,9 @@ +#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__ \ No newline at end of file diff --git a/include/latch/base/lt-stats.h b/include/latch/base/lt-stats.h new file mode 100644 index 0000000..a72f7b8 --- /dev/null +++ b/include/latch/base/lt-stats.h @@ -0,0 +1,10 @@ +#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__ diff --git a/include/latch/base/lt-types.h b/include/latch/base/lt-types.h new file mode 100644 index 0000000..5a3b057 --- /dev/null +++ b/include/latch/base/lt-types.h @@ -0,0 +1,36 @@ +#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__ \ No newline at end of file diff --git a/include/latch/container/lt-bytes.h b/include/latch/container/lt-bytes.h new file mode 100644 index 0000000..63c4a84 --- /dev/null +++ b/include/latch/container/lt-bytes.h @@ -0,0 +1,12 @@ +#ifndef __LATCH_CONTAINER_BYTES_H__ +#define __LATCH_CONTAINER_BYTES_H__ + +#include "latch/base/lt-types.h" + +typedef struct __lt_bytes { + lt_size_t length; + lt_byte *data; + void (*close)(struct __lt_bytes *self); +} LT_Bytes; + +#endif // __LATCH_CONTAINER_BYTES_H__ \ No newline at end of file diff --git a/include/latch/container/lt-dlist.h b/include/latch/container/lt-dlist.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/container/lt-map.h b/include/latch/container/lt-map.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/container/lt-slist.h b/include/latch/container/lt-slist.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/container/lt-string.h b/include/latch/container/lt-string.h new file mode 100644 index 0000000..3515e16 --- /dev/null +++ b/include/latch/container/lt-string.h @@ -0,0 +1,18 @@ +#ifndef __LATCH_CONTAINER_STRING_H__ +#define __LATCH_CONTAINER_STRING_H__ + +#include "latch/base/lt-types.h" + +typedef struct __lt_string { + lt_size_t length; + char *data; + void (*close)(struct __lt_string *self); +} LT_String; + + +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); + +#endif // __LATCH_CONTAINER_STRING_H__ \ No newline at end of file diff --git a/include/latch/hash/lt-sha.h b/include/latch/hash/lt-sha.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/http/lt-http.h b/include/latch/http/lt-http.h new file mode 100644 index 0000000..f0d8809 --- /dev/null +++ b/include/latch/http/lt-http.h @@ -0,0 +1,11 @@ +#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; + + +#endif //__LT_HTTP_H__ \ No newline at end of file diff --git a/include/latch/http/lt-https.h b/include/latch/http/lt-https.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/io/lt-file.h b/include/latch/io/lt-file.h new file mode 100644 index 0000000..b161288 --- /dev/null +++ b/include/latch/io/lt-file.h @@ -0,0 +1,37 @@ +#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 + +typedef enum { + LT_FILE_SENK_BEG = 0, + LT_FILE_SENK_CUR, + LT_FILE_SENK_END, +} lt_file_senk_flag_t; + +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 文件权限修改 + +#endif // __LATCH_FILE_H__ \ No newline at end of file diff --git a/include/latch/latch.h b/include/latch/latch.h new file mode 100644 index 0000000..b73bc43 --- /dev/null +++ b/include/latch/latch.h @@ -0,0 +1,23 @@ +#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 diff --git a/include/latch/lt-version.h b/include/latch/lt-version.h new file mode 100644 index 0000000..683984a --- /dev/null +++ b/include/latch/lt-version.h @@ -0,0 +1,10 @@ +#ifndef __LATCH_VERSION_H__ +#define __LATCH_VERSION_H__ + +#define LATCH_MAJOR_VERSION 0 +#define LATCH_MINOR_VERSION 1 +#define LATCH_PATCH_VERSION 0 + +#define LATCH_VERSION "0.1.0" + +#endif // __LATCH_VERSION_H__ \ No newline at end of file diff --git a/include/latch/memory/lt-memory.h b/include/latch/memory/lt-memory.h new file mode 100644 index 0000000..82c49ba --- /dev/null +++ b/include/latch/memory/lt-memory.h @@ -0,0 +1,18 @@ +#ifndef __LATCH_MEMORY_H__ +#define __LATCH_MEMORY_H__ + +#include "latch/base/lt-types.h" + +typedef enum lt_memory_type { + 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); + +void lt_set_default_memory_type(lt_memory_type_e type); + +#endif // __LATCH_MEMORY_H__ diff --git a/include/latch/os/lt-os.h b/include/latch/os/lt-os.h new file mode 100644 index 0000000..2398c32 --- /dev/null +++ b/include/latch/os/lt-os.h @@ -0,0 +1,5 @@ +#ifndef __LATCH_OS_H__ +#define __LATCH_OS_H__ + + +#endif // __LATCH_OS_H__ \ No newline at end of file diff --git a/include/latch/os/lt-path.h b/include/latch/os/lt-path.h new file mode 100644 index 0000000..607e4bc --- /dev/null +++ b/include/latch/os/lt-path.h @@ -0,0 +1,10 @@ +#ifndef __LATCH_PATH_H__ +#define __LATCH_PATH_H__ + +#include "latch/base/lt-types.h" + +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); + +#endif // __LATCH_PATH_H__ \ No newline at end of file diff --git a/include/latch/socket/lt-tcp.h b/include/latch/socket/lt-tcp.h new file mode 100644 index 0000000..e69de29 diff --git a/include/latch/socket/lt-udp.h b/include/latch/socket/lt-udp.h new file mode 100644 index 0000000..e69de29 diff --git a/script/test.sh b/script/test.sh new file mode 100644 index 0000000..47f8bf9 --- /dev/null +++ b/script/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +#使用该脚本编译并测试项目,或者使用ide的插件进行编译和测试 +cmake build -B build . && cd build && cmake --build . && ctest \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..aca1c99 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,36 @@ +project(latch) + +if(SHARED) + add_library(${PROJECT_NAME} SHARED) +else() + add_library(${PROJECT_NAME} STATIC) +endif() + + +aux_source_directory("memory" MEMORY_SRC) +target_sources( + ${PROJECT_NAME} PRIVATE + ${MEMORY_SRC} +) + +#编译容器模块 +aux_source_directory("container/string" CONTAINER_SRC) +aux_source_directory("container/map" CONTAINER_SRC) +target_sources( + ${PROJECT_NAME} PRIVATE + ${CONTAINER_SRC} +) + +#编译IO模块 +aux_source_directory("io" IO_SRC) +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + aux_source_directory("io/cross/unix" IO_SRC) +elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") + aux_source_directory("io/cross/windows" IO_SRC) +endif() +target_sources( + ${PROJECT_NAME} PRIVATE + ${IO_SRC} +) + + diff --git a/src/container/map/lt-map.c b/src/container/map/lt-map.c new file mode 100644 index 0000000..e69de29 diff --git a/src/container/string/lt-string.c b/src/container/string/lt-string.c new file mode 100644 index 0000000..27b7f55 --- /dev/null +++ b/src/container/string/lt-string.c @@ -0,0 +1,96 @@ +#include "latch/container/lt-string.h" +#include "latch/base/lt-types.h" +#include "latch/memory/lt-memory.h" +#include +#include + +static void __LT_String_free(LT_String *self){ + if(self)free(self->data); + free(self); + return; +} + +/** +* @brief 创建字符串 +*/ +LT_String *lt_string_new(const char *data){ + LT_String *str = (LT_String*)lt_malloc(sizeof(LT_String)); + if(!str) return LT_NULL_PTR; + + str->length = strlen(data); + str->data = (char*)malloc(str->length + 1); + strcpy(str->data, data); + str->close = __LT_String_free; + return str; +} + +void lt_string_reNew(LT_String* str, const char *data){ + lt_size_t newLength = strlen(data); + char* newData = (char*)malloc(newLength + 1); + strcpy(newData, data); + free(str->data); + str->data = newData; + str->length = newLength; +} + + +/** +* @brief 字符串反转 +*/ +LT_String *lt_string_reverse(LT_String *self){ + LT_String *str = (LT_String*)malloc(sizeof(LT_String)); + if(!str) return LT_NULL_PTR; + + str->data = (char*)malloc(self->length + 1); + for(lt_int64 i = self->length - 1, j = 0; i >= 0; --i, ++j){ + str->data[j] = self->data[i]; + } + str->length = self->length; + str->data[self->length] = '\0'; + str->close = __LT_String_free; + return str; +} + +/** +* @brief 字符串内容切割,step为正 +*/ +static LT_String* __lt_string_cut_step_P(LT_String *self, lt_int64 start, lt_int64 end, lt_int64 step){ + if(end > self->length || start > end || start > self->length) + return LT_NULL_PTR; + + LT_String *str = (LT_String*)malloc(sizeof(LT_String)); + if(!str) return LT_NULL_PTR; + + str->close = __LT_String_free; + str->length = (end - start) / step; + str->data = (char*)malloc(str->length + 1); + for(lt_int64 i = 0; i < str->length; ++i){ + if((start + i * step)>= self->length) break; + str->data[i] = self->data[(start + i * step)]; + } + str->data[str->length] = '\0'; + return str; +} + +/** +* @brief 字符串内容切割,step为负 +*/ +static LT_String* __lt_string_cut_step_N(LT_String *self, lt_int64 start, lt_int64 end, lt_int64 step){ + LT_String *self_res =lt_string_reverse(self);//字符串反转 + LT_String *new_str = __lt_string_cut_step_P(self_res, start, end, -step); + __LT_String_free(self_res); + return new_str; +} + +/** +* @brief 字符串内容切割 +* @param self 切割的字符串 +* @param start 切割的起始位置 +* @param end 切割的结束位置 +* @param step 切割的步长,为负表示反转 +*/ +LT_String *lt_string_cut(LT_String *self, lt_int64 start, lt_int64 end, lt_int64 step){ + if (step > 0)return __lt_string_cut_step_P(self, start, end, step); + if (step < 0)return __lt_string_cut_step_N(self, start, end, step); + return LT_NULL_PTR; +} \ No newline at end of file diff --git a/src/io/cross/unix/lt-file-linux.c b/src/io/cross/unix/lt-file-linux.c new file mode 100644 index 0000000..c9dcc09 --- /dev/null +++ b/src/io/cross/unix/lt-file-linux.c @@ -0,0 +1,22 @@ +#include "lt-file-linux.h" +#include "latch/base/lt-types.h" +#include + + +lt_time_t lt_cross_file_get_create_time(LT_String* path){ + struct stat fileStat; + if (stat(path->data, &fileStat) == -1){ + return 0; + } + lt_time_t createTime = fileStat.st_ctime; + return createTime; +} + +lt_time_t lt_cross_file_get_modify_time(LT_String* path){ + struct stat fileStat; + if (stat(path->data, &fileStat) == -1){ + return 0; + } + lt_time_t modifyTime = fileStat.st_mtime; + return modifyTime; +} \ No newline at end of file diff --git a/src/io/cross/unix/lt-file-linux.h b/src/io/cross/unix/lt-file-linux.h new file mode 100644 index 0000000..d4c24c9 --- /dev/null +++ b/src/io/cross/unix/lt-file-linux.h @@ -0,0 +1,23 @@ +#ifndef __LATCH_FILE_UNIX_H__ +#define __LATCH_FILE_UNIX_H__ + +#include "latch/base/lt-types.h" +#include "latch/container/lt-string.h" + +/** +* @brief 获取文件创建时间戳 +* @brief 标准接口:禁止以任何形式改动。每个平台实现对应的接口实现 +* @param path 文件路径 +* @return lt_time_t 文件创建时间戳 +*/ +lt_time_t lt_cross_file_get_create_time(LT_String* path); + +/** +* @brief 获取文件修改时间戳 +* @brief 标准接口:禁止以任何形式改动。每个平台实现对应的接口实现 +* @param path 文件路径 +* @return lt_time_t 文件创建时间戳 +*/ +lt_time_t lt_cross_file_get_modify_time(LT_String* path); + +#endif //__LATCH_FILE_UNIX_H__ \ No newline at end of file diff --git a/src/io/cross/windows/lt-file-windows.c b/src/io/cross/windows/lt-file-windows.c new file mode 100644 index 0000000..23ac086 --- /dev/null +++ b/src/io/cross/windows/lt-file-windows.c @@ -0,0 +1,14 @@ +#ifdef __WIN32__ +#include "lt-file-windows.h" +#include + + +lt_time_t lt_cross_file_get_create_time(LT_String* path){ + return 0;//TODO 实现windows的创建时间 +} + +lt_time_t lt_cross_file_get_modify_time(LT_String* path){ + return 0;//TODO 实现windows的修改时间 +} + +#endif \ No newline at end of file diff --git a/src/io/cross/windows/lt-file-windows.h b/src/io/cross/windows/lt-file-windows.h new file mode 100644 index 0000000..5180707 --- /dev/null +++ b/src/io/cross/windows/lt-file-windows.h @@ -0,0 +1,23 @@ +#ifndef __LATCH_FILE_WINDOWS_H__ +#define __LATCH_FILE_WINDOWS_H__ + +#include "latch/base/lt-types.h" +#include "latch/container/lt-string.h" + +/** +* @brief 获取文件创建时间戳 +* @brief 标准接口:禁止以任何形式改动。每个平台实现对应的接口实现 +* @param path 文件路径 +* @return lt_time_t 文件创建时间戳 +*/ +lt_time_t lt_cross_file_get_create_time(LT_String* path); + +/** +* @brief 获取文件修改时间戳 +* @brief 标准接口:禁止以任何形式改动。每个平台实现对应的接口实现 +* @param path 文件路径 +* @return lt_time_t 文件创建时间戳 +*/ +lt_time_t lt_cross_file_get_modify_time(LT_String* path); + +#endif //__LATCH_FILE_WINDOWS_H__ \ No newline at end of file diff --git a/src/io/lt-file.c b/src/io/lt-file.c new file mode 100644 index 0000000..f90369e --- /dev/null +++ b/src/io/lt-file.c @@ -0,0 +1,60 @@ +#include "latch/io/lt-file.h" +#include "latch/base/lt-types.h" +#include "latch/container/lt-string.h" +#include "latch/memory/lt-memory.h" +#include + +#ifdef __linux__ +#include "cross/unix/lt-file-linux.h" +#elif __WIN32__ +#include "cross/windows/lt-file-windows.h" +#endif + +/** +* @brief 获取文件大小 +*/ +static lt_size_t __lt_file_get_size(FILE *fp){ + fseek(fp, 0L, SEEK_END); + lt_size_t size = ftell(fp); + rewind(fp); + return size; +} + +/** +* @brief 关闭文件 +*/ +static void __lt_file_close(LT_File *file){ + fclose(file->fp); + file->name->close(file->name); + file->path->close(file->path); + lt_free(file); +} + +/** +* @brief 打开文件 +* @param path 文件路径 +* @param mode 文件打开模式,"w","r","rb" +*/ +LT_File *lt_file_open(const char *path, const char *mode){ + + LT_File *file = (LT_File*)lt_malloc(sizeof(LT_File)); + if (file==LT_NULL_PTR)return LT_NULL_PTR; + + FILE *fp = fopen(path, mode); + if (fp==LT_NULL_PTR){ + lt_free(file); + return LT_NULL_PTR; + } + + file->fp = fp; + file->path = lt_string_new(path); + file->size = __lt_file_get_size(fp); + file->create_time = lt_cross_file_get_create_time(file->path); + file->modify_time = lt_cross_file_get_modify_time(file->path); + file->name = lt_string_new(path); + file->close = __lt_file_close; + file->read_bytes = LT_NULL_PTR; //TODO 未实现 + file->write_bytes = LT_NULL_PTR; //TODO 未实现 + + return file; +} \ No newline at end of file diff --git a/src/memory/lt-memory-libc.c b/src/memory/lt-memory-libc.c new file mode 100644 index 0000000..d865061 --- /dev/null +++ b/src/memory/lt-memory-libc.c @@ -0,0 +1,18 @@ +#include "lt-memory-libc.h" +#include + +void *lt_malloc_libc(lt_size_t size){ + return malloc(size); +} + +void *lt_calloc_libc(lt_size_t nmemb, lt_size_t size){ + return calloc(nmemb, size); +} + +void *lt_realloc_libc(void *ptr, lt_size_t size){ + return realloc(ptr, size); +} + +void lt_free_libc(void *ptr){ + free(ptr); +} \ No newline at end of file diff --git a/src/memory/lt-memory-libc.h b/src/memory/lt-memory-libc.h new file mode 100644 index 0000000..3c969f6 --- /dev/null +++ b/src/memory/lt-memory-libc.h @@ -0,0 +1,11 @@ +#ifndef __LATCH_MEMORY_LIBC_H__ +#define __LATCH_MEMORY_LIBC_H__ + +#include "latch/base/lt-types.h" + +void *lt_malloc_libc(lt_size_t size); +void *lt_calloc_libc(lt_size_t nmemb, lt_size_t size); +void *lt_realloc_libc(void *ptr, lt_size_t size); +void lt_free_libc(void *ptr); + +#endif //__LATCH_MEMORY_LIBC_H__ \ No newline at end of file diff --git a/src/memory/lt-memory-pool.c b/src/memory/lt-memory-pool.c new file mode 100644 index 0000000..b802fc1 --- /dev/null +++ b/src/memory/lt-memory-pool.c @@ -0,0 +1,7 @@ +#include "lt-memory-pool.h" + + + + + + diff --git a/src/memory/lt-memory-pool.h b/src/memory/lt-memory-pool.h new file mode 100644 index 0000000..2f3b74a --- /dev/null +++ b/src/memory/lt-memory-pool.h @@ -0,0 +1,11 @@ +#ifndef __LATCH_MEMORY_POOL_H__ +#define __LATCH_MEMORY_POOL_H__ + +#include "latch/base/lt-types.h" + +void *lt_malloc_pool(lt_size_t size); +void *lt_calloc_pool(lt_size_t nmemb, lt_size_t size); +void *lt_realloc_pool(void *ptr, lt_size_t size); +void lt_free_pool(void *ptr); + +#endif //__LATCH_MEMORY_POOL_H__ \ No newline at end of file diff --git a/src/memory/lt-memory.c b/src/memory/lt-memory.c new file mode 100644 index 0000000..c608c63 --- /dev/null +++ b/src/memory/lt-memory.c @@ -0,0 +1,58 @@ +#include "latch/memory/lt-memory.h" +#include "latch/base/lt-types.h" +#include "lt-memory-libc.h" + +lt_memory_type_e lt_default_memory_manager_G = LT_MEMORY_TYPE_LIBC; + + +void *lt_malloc(lt_size_t size){ + switch (lt_default_memory_manager_G) { + case LT_MEMORY_TYPE_LIBC: + return lt_malloc_libc(size); + break; + case LT_MEMORY_TYPE_POOL: + return LT_NULL_PTR; + break; + default: + return LT_NULL_PTR; + } +} + + +void *lt_calloc(lt_size_t nmemb, lt_size_t size){ + switch (lt_default_memory_manager_G) { + case LT_MEMORY_TYPE_LIBC: + return lt_calloc_libc(nmemb, size); + break; + case LT_MEMORY_TYPE_POOL: + return LT_NULL_PTR; + break; + default: + return LT_NULL_PTR; + } +} +void *lt_realloc(void *ptr, lt_size_t size){ + switch (lt_default_memory_manager_G) { + case LT_MEMORY_TYPE_LIBC: + return lt_realloc_libc(ptr, size); + break; + case LT_MEMORY_TYPE_POOL: + return LT_NULL_PTR; + break; + default: + return LT_NULL_PTR; + } +} + +void lt_free(void *ptr){ + switch (lt_default_memory_manager_G) { + case LT_MEMORY_TYPE_LIBC: + return lt_free_libc(ptr); + break; + case LT_MEMORY_TYPE_POOL: + return ; + break; + default: + return ; + } +} \ No newline at end of file diff --git a/src/os/lt-path.c b/src/os/lt-path.c new file mode 100644 index 0000000..c2b8202 --- /dev/null +++ b/src/os/lt-path.c @@ -0,0 +1 @@ +#include "latch/latch.h" \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..c5e9a3e --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,7 @@ +project(test) + + + +add_subdirectory(container) +add_subdirectory(io) + diff --git a/tests/container/CMakeLists.txt b/tests/container/CMakeLists.txt new file mode 100644 index 0000000..a1c6dd3 --- /dev/null +++ b/tests/container/CMakeLists.txt @@ -0,0 +1,10 @@ +project(test-container) + + +add_executable(${PROJECT_NAME}-string test-lt-string.c) +target_link_libraries(${PROJECT_NAME}-string latch) +if(UNIX) +add_test(${PROJECT_NAME}-string ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-string) +elseif(WIN32) +add_test(${PROJECT_NAME}-string ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-string.exe) +endif() diff --git a/tests/container/test-lt-string.c b/tests/container/test-lt-string.c new file mode 100644 index 0000000..f85d711 --- /dev/null +++ b/tests/container/test-lt-string.c @@ -0,0 +1,31 @@ +#include "latch/container/lt-string.h" +#include + + +int main(int argc, char *argv[]){ + printf("LT_String 创建测试\n"); + LT_String *str = lt_string_new("Hello, world!"); + printf("字符串内容: %s\n", str->data); + printf("字符串长度: %ld\n", str->length); + + printf("LT_String cut测试 start=0,end=11,step=1\n"); + LT_String *str2 = lt_string_cut(str, 0, 11, 1); + printf("字符串内容: %s\n", str2->data); + printf("字符串长度: %ld\n", str2->length); + str2->close(str2); + + printf("LT_String cut测试-反转cut start=0,end=11,step=-1\n"); + LT_String *str3 = lt_string_cut(str, 0, 11, -1); + printf("字符串内容: %s\n", str3->data); + printf("字符串长度: %ld\n", str3->length); + str3->close(str3); + + printf("LT_String 测试反转\n"); + LT_String *str4 = lt_string_reverse(str); + printf("字符串内容: %s\n", str4->data); + printf("字符串长度: %ld\n", str4->length); + str4->close(str4); + + str->close(str); + return 0; +} \ No newline at end of file diff --git a/tests/io/CMakeLists.txt b/tests/io/CMakeLists.txt new file mode 100644 index 0000000..4c31f89 --- /dev/null +++ b/tests/io/CMakeLists.txt @@ -0,0 +1,11 @@ +project(test-io) + + +add_executable(${PROJECT_NAME}-file test-lt-file.c) +target_link_libraries(${PROJECT_NAME}-file latch) + +if(UNIX) +add_test(${PROJECT_NAME}-file ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-file) +elseif(WIN32) +add_test(${PROJECT_NAME}-file ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-file.exe) +endif() \ No newline at end of file diff --git a/tests/io/test-lt-file.c b/tests/io/test-lt-file.c new file mode 100644 index 0000000..48f309c --- /dev/null +++ b/tests/io/test-lt-file.c @@ -0,0 +1,16 @@ +#include "latch/io/lt-file.h" +#include + +int main(int argc, char *argv[]){ + LT_File* f = lt_file_open("test.txt", "w"); + printf("LT_File size: %ld\n", sizeof(LT_File)); + printf("File: %p\n", f); + printf("File name: %s\n", f->name->data); + printf("File path: %s\n", f->path->data); + printf("File size: %ld\n", f->size); + printf("file create time %ld\n", f->create_time); + printf("file modify time %ld\n", f->modify_time); + + f->close(f); + return 0; +} \ No newline at end of file