diff --git a/HISTORY b/HISTORY index eaaaa9b..7b9a50f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.54 2021-10-10 +Version 1.54 2021-10-19 * fast_allocator.[hc]: correct reclaim_interval logic * shared_func.[hc]: add functions getFileContentEx1 and getFileContent1 * fc_queue.[hc]: add function fc_queue_timedpeek @@ -7,6 +7,7 @@ Version 1.54 2021-10-10 * add files: sorted_queue.[hc] * add files: array_allocator.[hc] * add files: sorted_array.[hc] + * shared_func.[hc]: add function fc_read_lines Version 1.53 2021-06-30 * process_action support action status diff --git a/src/shared_func.c b/src/shared_func.c index 52e0c88..3b3b085 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1509,6 +1509,41 @@ int fd_gets(int fd, char *buff, const int size, int once_bytes) return pDest - buff; } +ssize_t fc_read_lines(int fd, char *buf, const size_t size) +{ + ssize_t count; + ssize_t old; + int remain; + char *last; + + if ((count=fc_safe_read(fd, buf, size)) <= 0) + { + return count; + } + + if ((last=(char *)fc_memrchr(buf, '\n', count)) == NULL) + { + last = buf; + } + else + { + last++; //skip \n + } + + old = count; + count = last - buf; + remain = old - count; + if (remain > 0) + { + if (lseek(fd, -1 * remain, SEEK_CUR) < 0) + { + return -1; + } + } + + return count; +} + int set_rlimit(int resource, const rlim_t value) { struct rlimit limit; diff --git a/src/shared_func.h b/src/shared_func.h index a53f4e4..a5818c7 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -880,6 +880,15 @@ ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte); */ ssize_t fc_safe_read(int fd, char *buf, const size_t count); +/** read integral lines from file + * parameters: + * fd: the fd to read + * buf: the buffer to store the line + * size: max read bytes + * return: error no , 0 success, != 0 fail +*/ +ssize_t fc_read_lines(int fd, char *buf, const size_t size); + /** ftok with hash code * parameters: * path: the file path diff --git a/src/uniq_skiplist.h b/src/uniq_skiplist.h index d14ecc0..2bcbcd3 100644 --- a/src/uniq_skiplist.h +++ b/src/uniq_skiplist.h @@ -139,6 +139,24 @@ static inline int uniq_skiplist_init_pair_ex(UniqSkiplistPair *pair, return 0; } +static inline UniqSkiplist *uniq_skiplist_new_by_pair( + UniqSkiplistPair *pair, const int level_count) +{ + if (pair->skiplist == NULL) { + pair->skiplist = uniq_skiplist_new(&pair->factory, level_count); + } + + return pair->skiplist; +} + +static inline void uniq_skiplist_free_by_pair(UniqSkiplistPair *pair) +{ + if (pair->skiplist != NULL) { + uniq_skiplist_free(pair->skiplist); + pair->skiplist = NULL; + } +} + int uniq_skiplist_insert(UniqSkiplist *sl, void *data); int uniq_skiplist_delete_ex(UniqSkiplist *sl, void *data, const bool need_free);