diff --git a/HISTORY b/HISTORY index d85dc5c..95dae8d 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-08-22 +Version 1.44 2020-08-24 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex diff --git a/src/shared_func.c b/src/shared_func.c index 2eba4af..ffaed57 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -3097,3 +3097,71 @@ int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *create) *create = true; return 0; } + +int fc_get_first_line(const char *filename, char *buff, + const int buff_size, string_t *line) +{ + int result; + int64_t read_bytes; + char *line_end; + + read_bytes = buff_size - 1; + if ((result=getFileContentEx(filename, buff, 0, &read_bytes)) != 0) { + return result; + } + if (read_bytes == 0) { + return ENOENT; + } + + line_end = (char *)memchr(buff, '\n', read_bytes); + if (line_end == NULL) { + logError("file: "__FILE__", line: %d, " + "file: %s, line no: 1, " + "expect new line char \"\\n\"", + __LINE__, filename); + return EINVAL; + } + line->str = buff; + line->len = line_end - buff + 1; + return 0; +} + +int fc_get_last_line(const char *filename, char *buff, + const int buff_size, int64_t *file_size, string_t *line) +{ + int64_t offset; + int64_t read_bytes; + int result; + + if ((result=getFileSize(filename, file_size)) != 0) { + return result; + } + + if (*file_size == 0) { + return ENOENT; + } + + if (*file_size >= buff_size) { + offset = (*file_size - buff_size) + 1; + } else { + offset = 0; + } + read_bytes = (*file_size - offset) + 1; + if ((result=getFileContentEx(filename, buff, + offset, &read_bytes)) != 0) + { + return result; + } + if (read_bytes == 0) { + return ENOENT; + } + + line->str = (char *)fc_memrchr(buff, '\n', read_bytes - 1); + if (line->str == NULL) { + line->str = buff; + } else { + line->str += 1; //skip \n + } + line->len = (buff + read_bytes) - line->str; + return 0; +} diff --git a/src/shared_func.h b/src/shared_func.h index 3031fa6..20fb748 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -971,6 +971,12 @@ static inline int fc_check_mkdir(const char *path, const mode_t mode) return fc_check_mkdir_ex(path, mode, &create); } +int fc_get_first_line(const char *filename, char *buff, + const int buff_size, string_t *line); + +int fc_get_last_line(const char *filename, char *buff, + const int buff_size, int64_t *file_size, string_t *line); + #ifdef __cplusplus } #endif