Skip to content

Instantly share code, notes, and snippets.

@samrogerson
Last active December 22, 2015 19:49
Show Gist options
  • Save samrogerson/6522398 to your computer and use it in GitHub Desktop.
Save samrogerson/6522398 to your computer and use it in GitHub Desktop.
An example url protocol stripper for bryn
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *urlStrip(char *inputUrl, int *ret_sz) {
/* if inputUrl isn't allocated dynamically you doin't need to pass sz_url you can just do sizeof(inputUlr) */
int pos = 0;
int sz_url = strlen(inputUrl);
while(pos < sz_url - 1 && inputUrl[pos] != '/' && inputUrl[pos+1] != '/') {
/*printf("%d -> %c\n", pos, inputUrl[pos]);*/
pos++;
}
if(pos < sz_url - 1) pos+=2;
else pos = 0;
char *urlBuff;
*ret_sz = sz_url - pos + 1;
urlBuff = (char*) malloc(*ret_sz);
memcpy(urlBuff, &inputUrl[pos], sz_url - pos);
return urlBuff;
}
int main() {
char *http_example = "http://www.google.com";
char *https_example = "https://www.google.com";
char *www_example = "www.google.com";
int sz;
char *http_stripped = urlStrip(http_example, &sz);
printf("%s -> %s\n",http_example, http_stripped);
char *https_stripped = urlStrip(http_example, &sz);
printf("%s -> %s\n",https_example, https_stripped);
char *www_stripped = urlStrip(www_example, &sz);
printf("%s -> %s\n",www_example, www_stripped);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment