From a9e82600b701d889ed196f077e43f0d6e11569a6 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Thu, 21 Jul 2022 18:30:53 +0800 Subject: [PATCH] add function fc_get_first_lines --- HISTORY | 3 ++- src/shared_func.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/shared_func.h | 3 +++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/HISTORY b/HISTORY index 74bc0e4..340331d 100644 --- a/HISTORY +++ b/HISTORY @@ -1,7 +1,8 @@ -Version 1.59 2022-06-25 +Version 1.59 2022-07-21 * open file with flag O_CLOEXEC * add global var g_set_cloexec and macro FC_SET_CLOEXEC + * add function fc_get_first_lines Version 1.58 2022-06-04 * add function conn_pool_connect_server_ex1 to support service name diff --git a/src/shared_func.c b/src/shared_func.c index 37fd469..85ab39b 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -3712,9 +3712,11 @@ int fc_get_first_line(const char *filename, char *buff, read_bytes = buff_size - 1; if ((result=getFileContentEx(filename, buff, 0, &read_bytes)) != 0) { + line->len = 0; return result; } if (read_bytes == 0) { + line->len = 0; return ENOENT; } @@ -3724,6 +3726,7 @@ int fc_get_first_line(const char *filename, char *buff, "file: %s, line no: 1, " "expect new line char \"\\n\"", __LINE__, filename); + line->len = 0; return EINVAL; } line->str = buff; @@ -3731,6 +3734,54 @@ int fc_get_first_line(const char *filename, char *buff, return 0; } +int fc_get_first_lines(const char *filename, char *buff, + const int buff_size, string_t *lines, int *count) +{ + int result; + int target_count; + int64_t read_bytes; + char *p; + char *end; + char *line_end; + + if (*count <= 0) { + lines->len = 0; + return EINVAL; + } + + read_bytes = buff_size - 1; + if ((result=getFileContentEx(filename, buff, 0, &read_bytes)) != 0) { + *count = 0; + lines->len = 0; + return result; + } + if (read_bytes == 0) { + *count = 0; + lines->len = 0; + return ENOENT; + } + + target_count = *count; + *count = 0; + p = buff; + end = buff + read_bytes; + while (p < end) { + line_end = (char *)memchr(p, '\n', end - p); + if (line_end == NULL) { + break; + } + + p = line_end + 1; + if (++(*count) == target_count) { + break; + } + } + + lines->str = buff; + lines->len = p - buff; + return (*count > 0 ? 0 : ENOENT); +} + int fc_get_last_line(const char *filename, char *buff, const int buff_size, int64_t *file_size, string_t *line) { @@ -3739,10 +3790,12 @@ int fc_get_last_line(const char *filename, char *buff, int result; if ((result=getFileSize(filename, file_size)) != 0) { + line->len = 0; return result; } if (*file_size == 0) { + line->len = 0; return ENOENT; } @@ -3755,9 +3808,11 @@ int fc_get_last_line(const char *filename, char *buff, if ((result=getFileContentEx(filename, buff, offset, &read_bytes)) != 0) { + line->len = 0; return result; } if (read_bytes == 0) { + line->len = 0; return ENOENT; } @@ -3782,16 +3837,19 @@ int fc_get_last_lines(const char *filename, char *buff, int result; if (*count <= 0) { + lines->len = 0; return EINVAL; } if ((result=getFileSize(filename, &file_size)) != 0) { *count = 0; + lines->len = 0; return result; } if (file_size == 0) { *count = 0; + lines->len = 0; return ENOENT; } @@ -3804,10 +3862,13 @@ int fc_get_last_lines(const char *filename, char *buff, if ((result=getFileContentEx(filename, buff, offset, &read_bytes)) != 0) { + *count = 0; + lines->len = 0; return result; } if (read_bytes == 0) { *count = 0; + lines->len = 0; return ENOENT; } diff --git a/src/shared_func.h b/src/shared_func.h index 687d764..bdf42ff 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -1156,6 +1156,9 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path); int fc_get_first_line(const char *filename, char *buff, const int buff_size, string_t *line); +int fc_get_first_lines(const char *filename, char *buff, + const int buff_size, string_t *lines, int *count); + int fc_get_last_line(const char *filename, char *buff, const int buff_size, int64_t *file_size, string_t *line);