init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
.cache
|
||||||
|
build
|
||||||
20
CMakeLists.txt
Normal file
20
CMakeLists.txt
Normal file
@@ -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()
|
||||||
|
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -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.
|
||||||
26
README.md
Normal file
26
README.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
58
conanfile.py
Normal file
58
conanfile.py
Normal file
@@ -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"]
|
||||||
1
include/README.md
Normal file
1
include/README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# 存放公开的API接口
|
||||||
9
include/latch/base/lt-encoding.h
Normal file
9
include/latch/base/lt-encoding.h
Normal file
@@ -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__
|
||||||
10
include/latch/base/lt-stats.h
Normal file
10
include/latch/base/lt-stats.h
Normal file
@@ -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__
|
||||||
36
include/latch/base/lt-types.h
Normal file
36
include/latch/base/lt-types.h
Normal file
@@ -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__
|
||||||
12
include/latch/container/lt-bytes.h
Normal file
12
include/latch/container/lt-bytes.h
Normal file
@@ -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__
|
||||||
0
include/latch/container/lt-dlist.h
Normal file
0
include/latch/container/lt-dlist.h
Normal file
0
include/latch/container/lt-map.h
Normal file
0
include/latch/container/lt-map.h
Normal file
0
include/latch/container/lt-slist.h
Normal file
0
include/latch/container/lt-slist.h
Normal file
18
include/latch/container/lt-string.h
Normal file
18
include/latch/container/lt-string.h
Normal file
@@ -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__
|
||||||
0
include/latch/hash/lt-sha.h
Normal file
0
include/latch/hash/lt-sha.h
Normal file
11
include/latch/http/lt-http.h
Normal file
11
include/latch/http/lt-http.h
Normal file
@@ -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__
|
||||||
0
include/latch/http/lt-https.h
Normal file
0
include/latch/http/lt-https.h
Normal file
37
include/latch/io/lt-file.h
Normal file
37
include/latch/io/lt-file.h
Normal file
@@ -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 <stdio.h>
|
||||||
|
|
||||||
|
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__
|
||||||
23
include/latch/latch.h
Normal file
23
include/latch/latch.h
Normal file
@@ -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
|
||||||
10
include/latch/lt-version.h
Normal file
10
include/latch/lt-version.h
Normal file
@@ -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__
|
||||||
18
include/latch/memory/lt-memory.h
Normal file
18
include/latch/memory/lt-memory.h
Normal file
@@ -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__
|
||||||
5
include/latch/os/lt-os.h
Normal file
5
include/latch/os/lt-os.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef __LATCH_OS_H__
|
||||||
|
#define __LATCH_OS_H__
|
||||||
|
|
||||||
|
|
||||||
|
#endif // __LATCH_OS_H__
|
||||||
10
include/latch/os/lt-path.h
Normal file
10
include/latch/os/lt-path.h
Normal file
@@ -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__
|
||||||
0
include/latch/socket/lt-tcp.h
Normal file
0
include/latch/socket/lt-tcp.h
Normal file
0
include/latch/socket/lt-udp.h
Normal file
0
include/latch/socket/lt-udp.h
Normal file
3
script/test.sh
Normal file
3
script/test.sh
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#使用该脚本编译并测试项目,或者使用ide的插件进行编译和测试
|
||||||
|
cmake build -B build . && cd build && cmake --build . && ctest
|
||||||
36
src/CMakeLists.txt
Normal file
36
src/CMakeLists.txt
Normal file
@@ -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}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
0
src/container/map/lt-map.c
Normal file
0
src/container/map/lt-map.c
Normal file
96
src/container/string/lt-string.c
Normal file
96
src/container/string/lt-string.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include "latch/container/lt-string.h"
|
||||||
|
#include "latch/base/lt-types.h"
|
||||||
|
#include "latch/memory/lt-memory.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
22
src/io/cross/unix/lt-file-linux.c
Normal file
22
src/io/cross/unix/lt-file-linux.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "lt-file-linux.h"
|
||||||
|
#include "latch/base/lt-types.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
23
src/io/cross/unix/lt-file-linux.h
Normal file
23
src/io/cross/unix/lt-file-linux.h
Normal file
@@ -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__
|
||||||
14
src/io/cross/windows/lt-file-windows.c
Normal file
14
src/io/cross/windows/lt-file-windows.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifdef __WIN32__
|
||||||
|
#include "lt-file-windows.h"
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
23
src/io/cross/windows/lt-file-windows.h
Normal file
23
src/io/cross/windows/lt-file-windows.h
Normal file
@@ -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__
|
||||||
60
src/io/lt-file.c
Normal file
60
src/io/lt-file.c
Normal file
@@ -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 <stdio.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
18
src/memory/lt-memory-libc.c
Normal file
18
src/memory/lt-memory-libc.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include "lt-memory-libc.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
11
src/memory/lt-memory-libc.h
Normal file
11
src/memory/lt-memory-libc.h
Normal file
@@ -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__
|
||||||
7
src/memory/lt-memory-pool.c
Normal file
7
src/memory/lt-memory-pool.c
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include "lt-memory-pool.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
11
src/memory/lt-memory-pool.h
Normal file
11
src/memory/lt-memory-pool.h
Normal file
@@ -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__
|
||||||
58
src/memory/lt-memory.c
Normal file
58
src/memory/lt-memory.c
Normal file
@@ -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 ;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/os/lt-path.c
Normal file
1
src/os/lt-path.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "latch/latch.h"
|
||||||
7
tests/CMakeLists.txt
Normal file
7
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
project(test)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
add_subdirectory(container)
|
||||||
|
add_subdirectory(io)
|
||||||
|
|
||||||
10
tests/container/CMakeLists.txt
Normal file
10
tests/container/CMakeLists.txt
Normal file
@@ -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()
|
||||||
31
tests/container/test-lt-string.c
Normal file
31
tests/container/test-lt-string.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include "latch/container/lt-string.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
11
tests/io/CMakeLists.txt
Normal file
11
tests/io/CMakeLists.txt
Normal file
@@ -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()
|
||||||
16
tests/io/test-lt-file.c
Normal file
16
tests/io/test-lt-file.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include "latch/io/lt-file.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user