Skip to content

Instantly share code, notes, and snippets.

@whb07
Created May 12, 2020 11:36
Show Gist options
  • Save whb07/6bcb1a42edbcd8a1f3d8fe53623fcc84 to your computer and use it in GitHub Desktop.
Save whb07/6bcb1a42edbcd8a1f3d8fe53623fcc84 to your computer and use it in GitHub Desktop.
int findLastSlash(const char *filepath, int len_filepath){
int idx = 0;
int location = -1;
while(filepath[idx] != '\0' && idx < len_filepath){
if(filepath[idx] == '\\' || filepath[idx] == '/'){
location = idx;
}
idx++;
}
return location;
}
char * getBaseName(char *filepath, int len_filepath) {
int location = findLastSlash(filepath, len_filepath);
char *tmp;
if(location > -1){
location++;
tmp = (char *) malloc((unsigned long)(len_filepath - location));
int idx = 0;
while(location < len_filepath && filepath[location] != '\0'){
tmp[idx] = filepath[location];
idx++;
location++;
}
tmp[idx] = '\0';
return tmp;
}
tmp = (char *) malloc((unsigned long)(len_filepath));
strncpy(tmp, filepath, (unsigned long) len_filepath);
return tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment