Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //unit test: https://www.onlinegdb.com/online_c_compiler
- //copy paste example, press Run
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- //taken from https://stackoverflow.com/questions/9052490/find-the-count-of-substring-in-string
- //modified by Coto
- static int count_substr(const char *str, const char* substr, bool overlap) {
- if (strlen(substr) == 0) return -1; // forbid empty substr
- int count = 0;
- int increment = overlap ? 1 : strlen(substr);
- for (char* s = (char*)str; (s = strstr(s, substr)); s += increment)
- ++count;
- return count;
- }
- static char outPath[256]; //used by split function as output path buffer
- typedef void(*split_fn)(const char *, size_t, char * ,int indexToLeftOut, char * delim);
- void split(const char *str, char sep, split_fn fun, char * outBuf, int indexToLeftOut)
- {
- unsigned int start = 0, stop = 0;
- for (stop = 0; str[stop]; stop++) {
- if (str[stop] == sep) {
- fun(str + start, stop - start, outBuf, indexToLeftOut, &sep);
- start = stop + 1;
- }
- }
- fun(str + start, stop - start, outBuf, indexToLeftOut, &sep);
- }
- static int index = 0;
- //this callback debugs every character separated from split()
- void print(const char *str, size_t len, char * outBuf, int indexToLeftOut, char * delim){
- if(index != indexToLeftOut){
- char localBuf[256];
- snprintf(localBuf,len+1,"%s",str);
- printf(" %d:%s%s:%d\n", (int)len, localBuf, delim,index);
- index++;
- }
- }
- //this callback builds an output path (outBuf) and filters out the desired index. (used as a trim last directory callback)
- void buildPath(const char *str, size_t len, char * outBuf, int indexToLeftOut, char * delim){
- if(index != indexToLeftOut){
- if(strlen(outBuf) == 0){
- snprintf(outBuf,len+2,"%s%s",str, delim);
- }
- else{
- char localBuf[256];
- sprintf(localBuf,"%s",outBuf);
- snprintf(outBuf,strlen(outBuf)+len+2,"%s%s%s",localBuf,str,delim);
- }
- index++;
- }
- }
- //this callback splits the haystack found in a stream, in the outBuf
- #define TOP_ITEMS_SPLIT (int)(10)
- char * outSplitBuf[TOP_ITEMS_SPLIT][256];
- void splitCallback(const char *str, size_t len, char * outBuf, int indexToLeftOut, char * delim){
- snprintf((char*)&outSplitBuf[index][0],len+1,"%s",str);
- index++;
- }
- int getLastDirFromPath(char * stream, char * haystack, char * outBuf){
- index = 0;
- int indexToLeftOut = count_substr(stream, haystack, false);
- int indexToLeftOutCopy = indexToLeftOut;
- if(indexToLeftOutCopy > 1){ //allow index 0 to exist, so it's always left the minimum directory
- indexToLeftOutCopy--;
- }
- split(stream, (char)*haystack, buildPath, outBuf, indexToLeftOutCopy);
- return indexToLeftOut;
- }
- int str_split(char * stream, char * haystack, char * outBuf){
- index = 0;
- int indexToLeftOut = count_substr(stream, haystack, false);
- split(stream, (char)*haystack, splitCallback, outBuf, indexToLeftOut);
- return indexToLeftOut;
- }
- int main(){
- char str[] = "0:/folder0/folder1/";
- char * delimiter = "/";
- printf("getLastDirFromPath():Occurrences:%d->%s \n",getLastDirFromPath(str, delimiter, outPath),outPath);
- int item = 1; //item to show in the split buffer that was found by str_split() call
- char * splitBuf = (char*)&outSplitBuf[item][0];
- str_split(str, delimiter, NULL);
- printf("str_split():%s",splitBuf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement