likle / cwalk

Path library for C/C++. Cross-Platform for Linux, FreeBSD, Windows and MacOS. Supports UNIX and Windows path styles on those platforms.
https://likle.github.io/cwalk/
MIT License
250 stars 39 forks source link

Suggestion: functions to determine buffer size #26

Closed ilobilo closed 2 years ago

ilobilo commented 2 years ago

Add support to just return absolute path length so i can allocate exact amount of memory in an array

size_t cwk_path_get_absolute_length(const char *base, const char *path)

this would return 6 then i would allocate 6 + 1 bytes to a char array example usage:

size_t length = cwk_path_get_absolute_length("/hello/", "/world") + 1; // 6 + 1 = 7
char *buffer = malloc(length);
cwk_path_get_absolute("/hello/", "/world", buffer, length);
printf("Path: %s", buffer); // "/world"

Same for relative

likle commented 2 years ago

Hi @ilobilo ! You should be able to achieve this already with the current API. Using your example:

size_t length = cwk_path_get_absolute("/hello/", "/world", NULL, 0) + 1; // 6 + 1 = 7
char *buffer = malloc(length);
cwk_path_get_absolute("/hello/", "/world", buffer, length);
printf("Path: %s", buffer); // "/world"

Does that help you?

ilobilo commented 2 years ago

yes that works, thank you!

ilobilo commented 2 years ago

cool project btw

likle commented 2 years ago

awesome! thanks for your feedback!