mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-14 11:49:48 +00:00
update
This commit is contained in:
142
czmod.c
142
czmod.c
@@ -117,6 +117,148 @@ ib_string *load_content(const char *filename)
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// path item
|
||||
//---------------------------------------------------------------------
|
||||
typedef struct
|
||||
{
|
||||
ib_string *path;
|
||||
int rank;
|
||||
uint32_t timestamp;
|
||||
uint64_t frecence;
|
||||
} PathItem;
|
||||
|
||||
static void item_delete(PathItem *item)
|
||||
{
|
||||
if (item) {
|
||||
if (item->path) {
|
||||
ib_string_delete(item->path);
|
||||
item->path = NULL;
|
||||
}
|
||||
ikmem_free(item);
|
||||
}
|
||||
};
|
||||
|
||||
PathItem* item_new(const char *path, int rank, uint32_t timestamp)
|
||||
{
|
||||
PathItem* item = (PathItem*)ikmem_malloc(sizeof(PathItem));
|
||||
assert(item);
|
||||
item->path = ib_string_new_from(path);
|
||||
item->rank = rank;
|
||||
item->timestamp = timestamp;
|
||||
item->frecence = 0;
|
||||
return item;
|
||||
};
|
||||
|
||||
ib_array* ib_array_new_path(void)
|
||||
{
|
||||
return ib_array_new((void (*)(void*))item_delete);
|
||||
}
|
||||
|
||||
int ib_string_find_size(const ib_string *str, const char *src,
|
||||
int len, int start)
|
||||
{
|
||||
char *text = str->ptr;
|
||||
int pos = (start < 0)? 0 : start;
|
||||
int endup = str->size - len;
|
||||
char ch;
|
||||
if (len <= 0) return pos;
|
||||
for (ch = src[0]; pos <= endup; pos++) {
|
||||
if (text[pos] == ch) {
|
||||
if (memcmp(text + pos, src, len) == 0)
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ib_string_find(const ib_string *str, const char *src, int start)
|
||||
{
|
||||
return ib_string_find_size(str, src, (int)strlen(src), start);
|
||||
}
|
||||
|
||||
int ib_string_find_c(const ib_string *str, char ch, int start)
|
||||
{
|
||||
const char *text = str->ptr;
|
||||
int pos = (start < 0)? 0 : start;
|
||||
int endup = str->size;
|
||||
for (; pos < endup; pos++) {
|
||||
if (text[pos] == ch) return pos;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
ib_array* ib_string_split_size(const ib_string *str, const char *sep,
|
||||
int len)
|
||||
{
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
ib_array *array = ib_array_new((void (*)(void*))ib_string_delete);
|
||||
int start = 0;
|
||||
while (0) {
|
||||
int pos = ib_string_find_size(str, sep, len, start);
|
||||
if (pos < 0) {
|
||||
ib_string *newstr = ib_string_new();
|
||||
ib_string_assign_size(newstr, str->ptr + start,
|
||||
str->size - start);
|
||||
ib_array_push(array, newstr);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
ib_string* newstr = ib_string_new();
|
||||
ib_string_assign_size(newstr, str->ptr + start, pos - start);
|
||||
start = pos + len;
|
||||
ib_array_push(array, newstr);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
ib_array* ib_string_split_c(const ib_string *str, char sep)
|
||||
{
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
ib_array *array = ib_array_new((void (*)(void*))ib_string_delete);
|
||||
int start = 0;
|
||||
while (1) {
|
||||
int pos = ib_string_find_c(str, sep, start);
|
||||
if (pos < 0) {
|
||||
ib_string *newstr = ib_string_new();
|
||||
ib_string_assign_size(newstr, str->ptr + start,
|
||||
str->size - start);
|
||||
ib_array_push(array, newstr);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
ib_string *newstr = ib_string_new();
|
||||
ib_string_assign_size(newstr, str->ptr + start, pos - start);
|
||||
start = pos + 1;
|
||||
ib_array_push(array, newstr);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// load data
|
||||
//---------------------------------------------------------------------
|
||||
ib_array* data_load(const char *filename)
|
||||
{
|
||||
ib_string *content = load_content(filename);
|
||||
if (content == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// main entry
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user