Created
June 7, 2018 06:42
-
-
Save kurahaupo/1957d07b35445992aa25648cf64572fc to your computer and use it in GitHub Desktop.
demo of string handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This functions takes a path to a directory (C:/blah) and a file name (foobar.txt) then returns the full path of the file (C:/blah/foobar.txt) */ | |
size_t sn_make_path(char *buffer, size_t bsize, const char *dir_path, const char *file_name, char dir_sep) { | |
size_t dir_len = strlen(dir_path); | |
size_t file_len = strlen(file_name); | |
int add_slash = dir_len > 0 && dir_path[dir_len - 1] != dir_sep; | |
size_t file_path_len = dir_len + file_len + add_slash; | |
if (bsize > 0) { | |
if (dir_len+1>bsize) | |
dir_len = bsize-1; | |
if (add_slash+1>bsize-dir_len) | |
add_slash = 0; | |
if (file_len+1>bsize-dir_len-add_slash) | |
file_len = bsize-dir_len-add_slash-1; | |
memcpy(buffer, dir_path, dir_len); | |
if (add_slash) | |
buffer[dir_len] = dir_sep; | |
memcpy(buffer+dir_len+add_slash, file_path, file_len); | |
buffer[dir_len+add_slash+file_len] = 0; | |
} | |
return file_path_len; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment