diff --git a/packages/base/any/onlp/src/onlplib/module/inc/onlplib/file.h b/packages/base/any/onlp/src/onlplib/module/inc/onlplib/file.h index 250047c3..33096eaf 100644 --- a/packages/base/any/onlp/src/onlplib/module/inc/onlplib/file.h +++ b/packages/base/any/onlp/src/onlplib/module/inc/onlplib/file.h @@ -27,6 +27,20 @@ #include +/** + * @brief Read the size of the given file. + * @param fmt Filename format string. + * @param vargs Filename format arguments. + */ +int onlp_file_vsize(const char* fmt, va_list vargs); + +/** + * @brief Read the size of the given file. + * @param fmt Filename format string. + * @param ... Filename format arguments. + */ +int onlp_file_size(const char* fmt, ...); + /** * @brief Read and return the contents of the given file. * @param data Receives the data. @@ -47,6 +61,43 @@ int onlp_file_vread(uint8_t* data, int max, int* len, const char* fmt, va_list v */ int onlp_file_read(uint8_t* data, int max, int* len, const char* fmt, ...); + +/** + * @brief Read and return the contents of the given file. + * @param[out] data Receives the contents. + * @param fmt The filename format string. + * @param vargs The filename format args. + */ +int onlp_file_vread_all(uint8_t** data, const char* fmt, va_list vargs); + +/** + * @brief Read and return the contents of the given file. + * @param[out] data Receives the contents. + * @param fmt The filename format string. + * @param ... The filename format args. + */ +int onlp_file_read_all(uint8_t** data, const char* fmt, ...); + +/** + * @brief Read and return the contents of the given file. + * @param[out] rv Receives the contents. + * @param fmt The filename format string. + * @param vargs The filename format args. + * @note The contents of the file are assumed to be a string. + * Trailing newlines are removed. + */ +int onlp_file_vread_str(char** str, const char* fmt, va_list vargs); + +/** + * @brief Read and return the contents of the given file. + * @param[out] rv Receives the contents. + * @param fmt The filename format string. + * @param ... The filename format args. + * @note The contents of the file are assumed to be a string. + * Trailing newlines are removed. + */ +int onlp_file_read_str(char** str, const char* fmt, ...); + /** * @brief Read and return the integer contents of the given file. * @param value Receives the integer value. diff --git a/packages/base/any/onlp/src/onlplib/module/src/file.c b/packages/base/any/onlp/src/onlplib/module/src/file.c index a1abd74d..ef31b840 100644 --- a/packages/base/any/onlp/src/onlplib/module/src/file.c +++ b/packages/base/any/onlp/src/onlplib/module/src/file.c @@ -112,6 +112,34 @@ vopen__(char** dst, int flags, const char* fmt, va_list vargs) return (fd > 0) ? fd : ONLP_STATUS_E_MISSING; } +int +onlp_file_vsize(const char* fmt, va_list vargs) +{ + int rv; + struct stat sb; + + char* fname = aim_vfstrdup(fmt, vargs); + if(stat(fname, &sb) != -1) { + rv = sb.st_size; + } + else { + rv = ONLP_STATUS_E_MISSING; + } + aim_free(fname); + return rv; +} + +int +onlp_file_size(const char* fmt, ...) +{ + int rv; + va_list vargs; + va_start(vargs, fmt); + rv = onlp_file_vsize(fmt, vargs); + va_end(vargs); + return rv; +} + int onlp_file_vread(uint8_t* data, int max, int* len, const char* fmt, va_list vargs) @@ -149,6 +177,72 @@ onlp_file_read(uint8_t* data, int max, int* len, const char* fmt, ...) return rv; } +int +onlp_file_vread_all(uint8_t** data, const char* fmt, va_list vargs) +{ + int rv; + uint8_t* contents = NULL; + char * fname = NULL; + int fsize, rsize; + + if(data == NULL || fmt == NULL) { + return ONLP_STATUS_E_PARAM; + } + + fname = aim_vdfstrdup(fmt, vargs); + + *data = NULL; + + if((fsize = onlp_file_size(fname)) > 0) { + contents = aim_zmalloc(fsize); + if((rv = onlp_file_read(contents, fsize, &rsize, fname)) >= 0) { + *data = contents; + rv = rsize; + } + } + else { + rv = fsize; + } + aim_free(fname); + return rv; +} + +int +onlp_file_read_all(uint8_t** data, const char* fmt, ...) +{ + int rv; + va_list vargs; + va_start(vargs, fmt); + rv = onlp_file_vread_all(data, fmt, vargs); + va_end(vargs); + return rv; +} + +int +onlp_file_vread_str(char** str, const char* fmt, va_list vargs) +{ + int rv = onlp_file_vread_all((uint8_t**)str, fmt, vargs); + if(rv > 0) { + while(rv && ( (*str)[rv-1] == '\n' || (*str)[rv-1] == '\r')) { + (*str)[rv-1] = 0; + rv--; + } + } + return rv; + +} + +int +onlp_file_read_str(char** str, const char* fmt, ...) +{ + int rv; + va_list vargs; + va_start(vargs, fmt); + rv = onlp_file_vread_str(str, fmt, vargs); + va_end(vargs); + return rv; +} + int onlp_file_vread_int(int* value, const char* fmt, va_list vargs) {