-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathczmod.c
651 lines (602 loc) · 15.2 KB
/
czmod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//=====================================================================
//
// czmod.c - c module to boost z.lua
//
// Created by skywind on 2020/03/11
// Last Modified: 2020/03/11 16:37:01
//
//=====================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <time.h>
#include <assert.h>
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
#include <windows.h>
#elif defined(__linux)
// #include <linux/limits.h>
#include <sys/file.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#endif
#define IB_STRING_SSO 256
#include "system/iposix.h"
#include "system/imembase.h"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
//----------------------------------------------------------------------
// INLINE
//----------------------------------------------------------------------
#ifndef INLINE
#if defined(__GNUC__)
#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
#define INLINE __inline__ __attribute__((always_inline))
#else
#define INLINE __inline__
#endif
#elif (defined(_MSC_VER) || defined(__WATCOMC__))
#define INLINE __inline
#else
#define INLINE
#endif
#endif
#if (!defined(__cplusplus)) && (!defined(inline))
#define inline INLINE
#endif
//---------------------------------------------------------------------
// internal functions
//---------------------------------------------------------------------
static const char *get_data_file(void);
//---------------------------------------------------------------------
// get environ
//---------------------------------------------------------------------
static ib_string* os_getenv(const char *name)
{
char *p = getenv(name);
if (p == NULL) {
return NULL;
}
ib_string *text = ib_string_new();
ib_string_assign(text, p);
return text;
}
//---------------------------------------------------------------------
// get data file
//---------------------------------------------------------------------
static const char *get_data_file(void)
{
static ib_string *text = NULL;
if (text != NULL) {
return text->ptr;
}
text = os_getenv("_ZL_DATA");
if (text) {
return text->ptr;
}
text = os_getenv("HOME");
if (text == NULL) {
text = os_getenv("USERPROFILE");
}
if (text == NULL) {
return NULL;
}
ib_string_append(text, "/.zlua");
return text->ptr;
}
//---------------------------------------------------------------------
// load file content
//---------------------------------------------------------------------
ib_string *load_content(const char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return NULL;
}
int fd = fileno(fp);
flock(fd, LOCK_SH);
ib_string *text = ib_string_new();
size_t block = 65536;
ib_string_resize(text, block);
size_t pos = 0;
while (feof(fp) == 0) {
size_t avail = text->size - pos;
if (avail < block) {
ib_string_resize(text, text->size + block);
avail = text->size - pos;
}
size_t hr = fread(&(text->ptr[pos]), 1, avail, fp);
pos += hr;
}
flock(fd, LOCK_UN);
fclose(fp);
ib_string_resize(text, pos);
return text;
}
//---------------------------------------------------------------------
// path item
//---------------------------------------------------------------------
typedef struct
{
ib_string *path;
int rank;
uint32_t timestamp;
double frecent;
} 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->frecent = rank;
return item;
};
// compare item
int item_compare(const void *p1, const void *p2)
{
PathItem *n1 = (PathItem*)p1;
PathItem *n2 = (PathItem*)p2;
if (n1->frecent == n2->frecent) return 0;
return (n1->frecent > n2->frecent)? 1 : -1;
}
ib_array* ib_array_new_items(void)
{
return ib_array_new((void (*)(void*))item_delete);
}
//---------------------------------------------------------------------
// load data
//---------------------------------------------------------------------
ib_array* data_load(const char *filename)
{
ib_string *content = load_content(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);
}
//---------------------------------------------------------------------
// save data
//---------------------------------------------------------------------
void data_write(const char *filename, ib_array *items)
{
FILE *fp;
int fd;
fp = fopen(filename, "w+");
if (fp) {
int size = ib_array_size(items);
int i;
fd = fileno(fp);
flock(fd, LOCK_EX);
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);
}
fflush(fp);
flock(fd, LOCK_UN);
fclose(fp);
}
}
//---------------------------------------------------------------------
// 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);
}
//---------------------------------------------------------------------
// match string
//---------------------------------------------------------------------
int string_match(const char *text, int argc, const char *argv[])
{
int enhanced = 1;
int pos = 0;
int i;
if (argc == 0) return 0;
for (i = 0; i < argc; i++) {
const char *keyword = argv[i];
const char *p = strstr(text + pos, keyword);
if (p == NULL) {
return -1;
}
pos = (int)(p - text) + ((int)strlen(keyword));
}
if (enhanced != 0) {
const char *keyword = argv[argc - 1];
const char *p1 = strrchr(text, '/');
const char *p2;
if (p1 == NULL) {
p1 = strrchr(text, '\\');
if (p1 == NULL) {
return 0;
}
}
else {
p2 = strrchr(text, '\\');
if (p2 != NULL) {
if (p2 > p1) {
p1 = p2;
}
}
}
p2 = strstr(p1, keyword);
if (p2 == NULL) {
return -2;
}
}
return 0;
}
//---------------------------------------------------------------------
// score path
//---------------------------------------------------------------------
void data_score(ib_array *items, int mode)
{
uint32_t current = (uint32_t)time(NULL);
int count = (int)ib_array_size(items);
int i;
for (i = 0; i < count; i++) {
PathItem* item = (PathItem*)ib_array_index(items, i);
if (mode == 0) {
uint32_t ts = current - item->timestamp;
if (ts < 3600) {
item->frecent = item->rank * 4;
}
else if (ts < 86400) {
item->frecent = item->rank * 2;
}
else if (ts < 604800) {
item->frecent = item->rank * 0.5;
}
else {
item->frecent = item->rank * 0.25;
}
}
else if (mode == 1) {
item->frecent = item->rank;
}
else {
uint32_t ts = current - item->timestamp;
item->frecent = -((double)ts);
}
}
ib_array_sort(items, item_compare);
ib_array_reverse(items);
}
//---------------------------------------------------------------------
// display data
//---------------------------------------------------------------------
void data_print(ib_array *items)
{
int count = (int)ib_array_size(items);
int i;
for (i = 0; i < count; i++) {
PathItem *item = (PathItem*)ib_array_index(items, count - i - 1);
printf("%.2f: %s\n", item->frecent, item->path->ptr);
}
}
//---------------------------------------------------------------------
// match
//---------------------------------------------------------------------
ib_array* data_match(int argc, const char *argv[])
{
const char *data = get_data_file();
ib_array *items = data_load(data);
ib_array *result = NULL;
if (items == NULL) {
return NULL;
}
result = ib_array_new_items();
while (ib_array_size(items)) {
PathItem* item = (PathItem*)ib_array_pop(items);
int valid = 0;
if (argc == 0) {
valid = 1;
} else {
int hr = string_match(item->path->ptr, argc, argv);
if (hr == 0) {
valid = 1;
}
}
if (valid) {
ib_array_push(result, item);
} else {
item_delete(item);
}
}
ib_array_delete(items);
data_score(result, 0);
return result;
}
//---------------------------------------------------------------------
// update database
//---------------------------------------------------------------------
void z_update(const char *newpath)
{
const char *data = get_data_file();
int fd = open(data, O_CREAT | O_RDWR, 0666);
ib_array *items;
if (fd < 0) {
return;
}
flock(fd, LOCK_EX);
off_t size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
items = ib_array_new_items();
if (size > 0) {
ib_string *content = ib_string_new();
int avail = (int)size;
int start = 0;
ib_array *array;
int i, count;
ib_string_resize(content, (int)size);
while (avail > 0) {
int hr = read(fd, content->ptr + start, avail);
if (hr <= 0) {
break;
}
avail -= hr;
start += hr;
}
lseek(fd, 0, SEEK_SET);
array = ib_string_split(content, "\n", 1);
ib_string_delete(content);
for (count = ib_array_size(array), i = 0; i < count; i++) {
ib_string *line = (ib_string*)ib_array_index(array, i);
int p1 = ib_string_find_c(line, '|', 0);
int p2 = 0;
if (p1 < 0) {
continue;
}
p2 = ib_string_find_c(line, '|', p1 + 1);
if (p2 >= 0) {
uint32_t timestamp;
int rank;
line->ptr[p1] = 0;
line->ptr[p2] = 0;
rank = (int)atoi(line->ptr + p1 + 1);
timestamp = (uint32_t)strtoul(line->ptr + p2 + 1, NULL, 10);
PathItem *ni = item_new(line->ptr, rank, timestamp);
ib_array_push(items, ni);
}
}
ib_array_delete(array);
}
{
int i, count = ib_array_size(items);
int strsize = (int)strlen(newpath);
int found = 0;
uint64_t total = 0;
for (i = 0; i < count; i++) {
PathItem *item = (PathItem*)ib_array_index(items, i);
total += item->rank;
}
if (total >= 5000) {
ib_array *na = ib_array_new((void (*)(void*))item_delete);
for (i = 0; i < count; i++) {
PathItem *item = (PathItem*)ib_array_index(items, i);
item->rank = (item->rank * 9) / 10;
}
while (ib_array_size(items) > 0) {
PathItem *item = (PathItem*)ib_array_pop(items);
if (item->rank == 0) {
item_delete(item);
} else {
ib_array_push(na, item);
}
}
ib_array_delete(items);
items = na;
ib_array_reverse(items);
count = (int)ib_array_size(items);
}
for (i = 0; i < count; i++) {
PathItem *item = (PathItem*)ib_array_index(items, i);
if (item->path->size == strsize) {
if (memcmp(item->path->ptr, newpath, strsize) == 0) {
item->rank += 1;
item->timestamp = (uint32_t)time(NULL);
found = 1;
break;
}
}
}
if (found == 0) {
PathItem *item = item_new(newpath, 1, (uint32_t)time(NULL));
ib_array_push(items, item);
}
}
{
int i, count;
count = (int)ib_array_size(items);
static char text[PATH_MAX + 60];
ib_string *content = ib_string_new();
int avail, start;
for (i = 0; i < count; i++) {
PathItem *item = (PathItem*)ib_array_index(items, i);
sprintf(text, "%s|%u|%u\n", item->path->ptr, item->rank, item->timestamp);
ib_string_append(content, text);
}
start = 0;
avail = content->size;
while (avail > 0) {
int hr = write(fd, content->ptr + start, avail);
if (hr <= 0) {
break;
}
start += hr;
avail -= hr;
}
ftruncate(fd, content->size);
ib_string_delete(content);
}
flock(fd, LOCK_UN);
close(fd);
}
//---------------------------------------------------------------------
// 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);
#if 0
data_save(data, items);
#else
data_write(data, items);
#endif
ib_array_delete(items);
}
//---------------------------------------------------------------------
// match and display
//---------------------------------------------------------------------
void z_echo(int argc, const char *argv[])
{
ib_array *items = data_match(argc, argv);
if (items == NULL) {
return;
}
if (ib_array_size(items) > 0) {
PathItem *item = ib_array_obj(items, PathItem*, 0);
printf("%s\n", item->path->ptr);
}
ib_array_delete(items);
}
//---------------------------------------------------------------------
// main entry
//---------------------------------------------------------------------
int main(int argc, const char *argv[])
{
if (argc <= 1) {
return 0;
}
if (strcmp(argv[1], "--add") == 0) {
if (argc >= 3) {
#if 0
z_add(argv[2]);
#else
z_update(argv[2]);
#endif
}
}
else if (strcmp(argv[1], "-e") == 0) {
z_echo(argc - 2, argv + 2);
}
return 0;
}