From 77a917d11689f558847f22b24bab1505accf5484 Mon Sep 17 00:00:00 2001 From: skywind3000 Date: Tue, 17 Mar 2020 00:20:06 +0800 Subject: [PATCH] update czmod.z --- czmod/czmod.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 6 deletions(-) diff --git a/czmod/czmod.c b/czmod/czmod.c index 6e10896..9c29f8f 100644 --- a/czmod/czmod.c +++ b/czmod/czmod.c @@ -11,11 +11,15 @@ #include #include #include +#include -#ifdef __linux -#include +#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) +#include +#elif defined(__linux) +// #include #endif +#include "iposix.c" #include "imembase.c" @@ -73,7 +77,7 @@ static const char *get_data_file(void) if (text != NULL) { return text->ptr; } - text = os_getenv("_ZL_DATA"); + text = os_getenv("_ZL_DATA2"); if (text) { return text->ptr; } @@ -150,7 +154,7 @@ PathItem* item_new(const char *path, int rank, uint32_t timestamp) return item; }; -ib_array* ib_array_new_path(void) +ib_array* ib_array_new_items(void) { return ib_array_new((void (*)(void*))item_delete); } @@ -165,22 +169,145 @@ ib_array* data_load(const char *filename) if (content == NULL) { return NULL; } + else { + ib_array *lines = ib_string_split_c(content, '\n'); + int size = ib_array_size(lines); + int i; + ib_array *items = ib_array_new_items(); + for (i = 0; i < size; i++) { + ib_string *text = (ib_string*)ib_array_index(lines, i); + int p1 = ib_string_find_c(text, '|', 0); + if (p1 >= 0) { + int p2 = ib_string_find_c(text, '|', p1 + 1); + if (p2 >= 0) { + uint32_t timestamp; + int rank; + text->ptr[p1] = 0; + text->ptr[p2] = 0; + rank = (int)atoi(text->ptr + p1 + 1); + timestamp = (uint32_t)strtoul(text->ptr + p2 + 1, NULL, 10); + PathItem *ni = item_new(text->ptr, rank, timestamp); + ib_array_push(items, ni); + } + } + } + ib_array_delete(lines); + return items; + } return NULL; } +//--------------------------------------------------------------------- +// save data +//--------------------------------------------------------------------- +void data_save(const char *filename, ib_array *items) +{ + ib_string *tmpname = ib_string_new_from(filename); + FILE *fp; + while (1) { + char tmp[100]; + ib_string_assign(tmpname, filename); + sprintf(tmp, ".%u%03u%d", (uint32_t)time(NULL), + (uint32_t)(clock() % 1000), rand() % 10000); + ib_string_append(tmpname, tmp); + if (iposix_path_isdir(tmpname->ptr) < 0) break; + } + fp = fopen(tmpname->ptr, "w"); + if (fp) { + int size = ib_array_size(items); + int i; + for (i = 0; i < size; i++) { + PathItem *item = (PathItem*)ib_array_index(items, i); + fprintf(fp, "%s|%u|%u\n", + item->path->ptr, item->rank, item->timestamp); + } + fclose(fp); + #ifdef _WIN32 + ReplaceFileA(filename, tmpname->ptr, NULL, 2, NULL, NULL); + #else + rename(tmpname->ptr, filename); + #endif + } + ib_string_delete(tmpname); +} + + +//--------------------------------------------------------------------- +// insert data +//--------------------------------------------------------------------- +void data_add(ib_array *items, const char *path) +{ + ib_string *target = ib_string_new_from(path); + int i = 0, size, found = 0; +#if defined(_WIN32) + for (i = 0; i < target->size; i++) { + if (target->ptr[i] == '/') target->ptr[i] = '\\'; + else { + target->ptr[i] = (char)tolower(target->ptr[i]); + } + } +#endif + size = ib_array_size(items); + for (i = 0; i < size; i++) { + PathItem *item = (PathItem*)ib_array_index(items, i); + int equal = 0; + #if defined(_WIN32) + if (item->path->size == target->size) { + char *src = item->path->ptr; + char *dst = target->ptr; + int avail = target->size; + for (; avail > 0; src++, dst++, avail--) { + if (tolower(src[0]) != dst[0]) break; + } + equal = (avail == 0)? 1 : 0; + } + #else + if (ib_string_compare(item->path, target) == 0) { + equal = 1; + } + #endif + if (equal) { + found = 1; + item->rank++; + item->timestamp = (uint32_t)time(NULL); + } + } + if (!found) { + PathItem *ni = item_new(target->ptr, 1, (uint32_t)time(NULL)); + ib_array_push(items, ni); + } + ib_string_delete(target); +} + + +//--------------------------------------------------------------------- +// add to database +//--------------------------------------------------------------------- +void z_add(const char *newpath) +{ + const char *data = get_data_file(); + ib_array *items = data_load(data); + if (items == NULL) { + items = ib_array_new_items(); + } + data_add(items, newpath); + data_save(data, items); + ib_array_delete(items); +} + + //--------------------------------------------------------------------- // main entry //--------------------------------------------------------------------- int main(int argc, char *argv[]) { if (argc <= 1) { - printf("Hello, World !!\n"); - printf("data: %s\n", get_data_file()); return 0; } if (strcmp(argv[1], "--add") == 0) { if (argc >= 3) { + z_add(argv[2]); } } return 0;