Skip to content

Instantly share code, notes, and snippets.

@kurahaupo
Created June 7, 2018 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurahaupo/1957d07b35445992aa25648cf64572fc to your computer and use it in GitHub Desktop.
Save kurahaupo/1957d07b35445992aa25648cf64572fc to your computer and use it in GitHub Desktop.
demo of string handling
/* 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