mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-06-06 05:08:45 +08:00
Update
This commit is contained in:
@@ -51,6 +51,7 @@ set(SOURCE_FILE
|
||||
time_util.cpp
|
||||
encoding.cpp
|
||||
str_util.cpp
|
||||
c_linked_list.cpp
|
||||
)
|
||||
set(SOURCE_FILE_HEADERS
|
||||
cfileop.h
|
||||
@@ -66,6 +67,7 @@ set(SOURCE_FILE_HEADERS
|
||||
encoding.h
|
||||
str_util.h
|
||||
linked_list.h
|
||||
c_linked_list.h
|
||||
)
|
||||
|
||||
add_library(utils STATIC ${SOURCE_FILE} ${SOURCE_FILE_HEADERS})
|
||||
|
||||
44
c_linked_list.cpp
Normal file
44
c_linked_list.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "c_linked_list.h"
|
||||
#include "linked_list.h"
|
||||
|
||||
int c_linked_list_append(c_linked_list** list, void* data) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)(*list);
|
||||
auto re = linked_list_append(li, &data);
|
||||
*list = (c_linked_list*)li;
|
||||
return re;
|
||||
}
|
||||
|
||||
int c_linked_list_append_head(c_linked_list** list, void* data) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)(*list);
|
||||
auto re = linked_list_append_head(li, &data);
|
||||
*list = (c_linked_list*)li;
|
||||
return re;
|
||||
}
|
||||
|
||||
void c_linked_list_clear(c_linked_list** list, void(*free_func)(void*)) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)(*list);
|
||||
linked_list_clear(li, free_func);
|
||||
*list = (c_linked_list*)li;
|
||||
}
|
||||
|
||||
size_t c_linked_list_count(c_linked_list* list) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)list;
|
||||
return linked_list_count(li);
|
||||
}
|
||||
|
||||
void c_linked_list_free_tail(c_linked_list** list, void(*free_func)(void*)) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)(*list);
|
||||
linked_list_free_tail(li, free_func);
|
||||
*list = (c_linked_list*)li;
|
||||
}
|
||||
|
||||
void c_linked_list_remove(c_linked_list** node, void(*free_func)(void*)) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)(*node);
|
||||
linked_list_remove(li);
|
||||
*node = (c_linked_list*)li;
|
||||
}
|
||||
|
||||
c_linked_list* c_linked_list_tail(c_linked_list* list) {
|
||||
struct LinkedList<void*>* li = (struct LinkedList<void*>*)list;
|
||||
return (c_linked_list*)linked_list_tail(li);
|
||||
}
|
||||
25
c_linked_list.h
Normal file
25
c_linked_list.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _UTIL_C_LINKED_LIST_H
|
||||
#define _UTIL_C_LINKED_LIST_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
/**
|
||||
* @brief Struct are same with struct LinkedList<void*>
|
||||
*/
|
||||
typedef struct c_linked_list {
|
||||
void* d;
|
||||
struct c_linked_list* prev;
|
||||
struct c_linked_list* next;
|
||||
} c_linked_list;
|
||||
int c_linked_list_append(c_linked_list** list, void* data);
|
||||
int c_linked_list_append_head(c_linked_list** list, void* data);
|
||||
void c_linked_list_clear(c_linked_list** list, void(*free_func)(void*));
|
||||
size_t c_linked_list_count(c_linked_list* list);
|
||||
void c_linked_list_free_tail(c_linked_list** list, void(*free_func)(void*));
|
||||
void c_linked_list_remove(c_linked_list** node, void(*free_func)(void*));
|
||||
c_linked_list* c_linked_list_tail(c_linked_list* list);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -530,3 +530,8 @@ bool fileop::listdir(std::string path, std::list<std::string>& filelist, bool ig
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string fileop::filename(std::string path) {
|
||||
auto loc = path.find_last_of('.');
|
||||
return loc == -1 ? path : path.substr(0, loc);
|
||||
}
|
||||
|
||||
6
fileop.h
6
fileop.h
@@ -163,5 +163,11 @@ namespace fileop {
|
||||
* @return true if successed.
|
||||
*/
|
||||
bool listdir(std::string path, std::list<std::string>& filelist, bool ignore_hidden_file = true);
|
||||
/**
|
||||
* @brief Return a path without file extension
|
||||
* @param path Path
|
||||
* @return Result
|
||||
*/
|
||||
std::string filename(std::string path);
|
||||
}
|
||||
#endif
|
||||
|
||||
110
linked_list.h
110
linked_list.h
@@ -2,6 +2,7 @@
|
||||
#define _UTIL_LINKED_LIST_H
|
||||
#include <list>
|
||||
#include <malloc.h>
|
||||
#include <stddef.h>
|
||||
template <typename T>
|
||||
struct LinkedList {
|
||||
T d;
|
||||
@@ -33,6 +34,45 @@ bool linked_list_append(struct LinkedList<T>*& list, struct LinkedList<T>** tail
|
||||
return linked_list_append(list, (T*)nullptr, tail);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool linked_list_append_head(struct LinkedList<T>*& list, T* data = nullptr) {
|
||||
bool have_list = list;
|
||||
struct LinkedList<T>* tmp = (struct LinkedList<T>*)malloc(sizeof(LinkedList<T>));
|
||||
if (!tmp) return false;
|
||||
memset(tmp, 0, sizeof(LinkedList<T>));
|
||||
if (data) tmp->d = T(*data); else tmp->d = T();
|
||||
if (have_list) {
|
||||
tmp->next = list;
|
||||
list->prev = tmp;
|
||||
}
|
||||
list = tmp;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename ... Args>
|
||||
bool linked_list_append_list(struct LinkedList<T>*& list, struct LinkedList<T>* list2, bool(*compare_func)(T, T, Args...) = nullptr, Args... args) {
|
||||
if (!list2) return true;
|
||||
struct LinkedList<T>* t = list2;
|
||||
struct LinkedList<T>* tail = linked_list_tail(list);
|
||||
if (!compare_func || !linked_list_have(list, t->d, compare_func, args...)) {
|
||||
if (!linked_list_append<T>(list, &t->d)) return false;
|
||||
}
|
||||
while (t->next) {
|
||||
t = t->next;
|
||||
if (!compare_func || !linked_list_have(list, t->d, compare_func, args...)) {
|
||||
if (!linked_list_append<T>(list, &t->d)) {
|
||||
if (tail == nullptr) {
|
||||
linked_list_clear(list);
|
||||
} else {
|
||||
if (tail->next) linked_list_clear(tail->next);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void linked_list_clear(struct LinkedList<T>*& list, void(*free_func)(T) = nullptr) {
|
||||
if (!list) return;
|
||||
@@ -48,4 +88,74 @@ void linked_list_clear(struct LinkedList<T>*& list, void(*free_func)(T) = nullpt
|
||||
free(t);
|
||||
list = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t linked_list_count(struct LinkedList<T>* list) {
|
||||
if (!list) return 0;
|
||||
struct LinkedList<T>* t = list;
|
||||
size_t c = 1;
|
||||
while (t->next) {
|
||||
t = t->next;
|
||||
c += 1;
|
||||
}
|
||||
if (list->prev) {
|
||||
t = t->prev;
|
||||
c += 1;
|
||||
while (t->prev) {
|
||||
t = t->prev;
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void linked_list_free_tail(struct LinkedList<T>*& list, void(*free_func)(T) = nullptr) {
|
||||
if (!list) return;
|
||||
struct LinkedList<T>* t = linked_list_tail(list);
|
||||
if (t == list) {
|
||||
linked_list_clear(list, free_func);
|
||||
} else {
|
||||
if (free_func) free_func(t->d);
|
||||
if (t->prev) {
|
||||
t->prev->next = nullptr;
|
||||
}
|
||||
free(t);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename ... Args>
|
||||
bool linked_list_have(struct LinkedList<T>* list, T data, bool(*compare_func)(T, T, Args...), Args... args) {
|
||||
if (!list || !compare_func) return false;
|
||||
struct LinkedList<T>* t = list;
|
||||
if (compare_func(t->d, data, args...)) return true;
|
||||
while (t->next) {
|
||||
t = t->next;
|
||||
if (compare_func(t->d, data, args...)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void linked_list_remove(struct LinkedList<T>*& node, void(*free_func)(T) = nullptr) {
|
||||
if (!node) return;
|
||||
if (node->prev) {
|
||||
node->prev->next = node->next;
|
||||
}
|
||||
if (node->next) {
|
||||
node->next->prev = node->prev;
|
||||
}
|
||||
if (free_func) free_func(node->d);
|
||||
free(node);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct LinkedList<T>* linked_list_tail(struct LinkedList<T>* list) {
|
||||
if (!list) return nullptr;
|
||||
struct LinkedList<T>* t = list;
|
||||
while (t->next) {
|
||||
t = t->next;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
#endif
|
||||
|
||||
16
str_util.cpp
16
str_util.cpp
@@ -28,4 +28,20 @@ std::string str_util::str_replace(std::string input, std::string pattern, std::s
|
||||
else break;
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
std::list<std::string> str_util::str_split(std::string input, std::string pattern, size_t max) {
|
||||
if (max == 0) return {};
|
||||
std::list<std::string> li;
|
||||
auto loc = input.find(pattern, 0);
|
||||
auto last_loc = 0;
|
||||
auto len = pattern.length();
|
||||
while (loc != -1 && li.size() < max) {
|
||||
li.push_back(input.substr(last_loc, loc - last_loc));
|
||||
last_loc = loc + len;
|
||||
if (last_loc < input.length()) loc = input.find(pattern, max(0, last_loc));
|
||||
else break;
|
||||
}
|
||||
if (last_loc <= input.length()) li.push_back(input.substr(last_loc, input.length() - last_loc));
|
||||
return li;
|
||||
}
|
||||
10
str_util.h
10
str_util.h
@@ -1,5 +1,7 @@
|
||||
#ifndef _UTILS_STR_UTIL_H
|
||||
#define _UTILS_STR_UTIL_H
|
||||
#include <list>
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
namespace str_util {
|
||||
/**
|
||||
@@ -17,5 +19,13 @@ namespace str_util {
|
||||
* @return Result string
|
||||
*/
|
||||
std::string str_replace(std::string input, std::string pattern, std::string new_content);
|
||||
/**
|
||||
* @brief Split string with pattern.
|
||||
* @param input Input string
|
||||
* @param pattern Partten
|
||||
* @param max Maximum count of result.
|
||||
* @return Result.
|
||||
*/
|
||||
std::list<std::string> str_split(std::string input, std::string pattern, size_t max = -1);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user