Skip to content

Instantly share code, notes, and snippets.

@dvtate
Last active January 5, 2017 04:22
Show Gist options
  • Save dvtate/f12758c41d66be13ffa2 to your computer and use it in GitHub Desktop.
Save dvtate/f12758c41d66be13ffa2 to your computer and use it in GitHub Desktop.
Some functions I need to make to replace the PHP functions I had used before.
#ifndef xml_UTILS_H
#define xml_UTILS_H
#include <stdlib.h>
#include <string.h>
// c doesn't have bools...
typedef enum {notags, tags} tag_bool_t;
///gets the string between a start and end tag in a string str
///returns a null-pointer if no occurances found.
char* strBetween(const char const* str, const char* start, const char* end, tag_bool_t includeSE){
// copy the string into ans
char* ans = (char*) malloc( strlen(str) + 1 );
strcpy( ans, str);
//include the the start & end tags
if (includeSE) {
char* begin = strstr(ans, start);
//start search @ begin as end tag is after start
char* stop = strstr(begin, end);
if (begin == (char*) NULL || stop == (char*) NULL)
return (char*) NULL; //no occurances found
//include the end tag as well...
stop += strlen(end);
*stop = '\0'; //terminate the string
return begin;
} // else:
//remove start string
char* begin = strstr(ans, start);
begin += strlen(start);
//start search @ begin as end tag is after start
char* stop = strstr(begin, end);
if (begin == (char*) NULL || stop == (char*) NULL)
return (char*) NULL; //no occurances found
*stop = '\0'; //terminate the string
return begin;
}
#endif
#include <stdio.h>
#include "xml_utils.h"
int main(){
char* testString = "boring... <cool>good stuff here</cool>lame stuff here";
printf("\nThe string is: \"%s\"\n\n", testString);
printf("The cool stuff (with tags) = \"%s\"\n\n",
strBetween(testString, "<cool>", "</cool>", tags)
);
printf("The cool stuff (no tags) = \"%s\"\n\n",
strBetween(testString, "<cool>", "</cool>", notags)
);
}
@dvtate
Copy link
Author

dvtate commented Mar 6, 2016

I think I'll make a repository for my website...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment