// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>

using namespace std;

void URL_to_components(char *url,
                       char *protocol,char *host,int *port,
                       char *path,char *un,char *pw)
{
   char *p = url;
   char *pp,*ppp;
   // find protocol
   if(strstr(p,"telnet:")==p) {
      strcpy(protocol,"telnet");
      p = p + strlen("telnet:");
   } else if(strstr(p,"http:")==p) {
      strcpy(protocol,"http");
      p = p + strlen("http:");
   } else if(strstr(p,"mailto:")==p) {
      strcpy(protocol,"mailto");
      p = p + strlen("mailto:");
   } else if(strstr(p,"gopher:")==p) {
      strcpy(protocol,"gopher");
      p = p + strlen("gopher:");
   } else if(strstr(p,"ftp:")==p) {
      strcpy(protocol,"ftp");
      p = p + strlen("ftp:");
   } else if(strstr(p,"file:")==p) {
      strcpy(protocol,"file");
      p = p + strlen("file:");
   } else if(strstr(p,"news:")==p) {
      strcpy(protocol,"news");
      p = p + strlen("news:");
   }
   // skip "//" if present
   if(strstr(p,"//")==p) p = p + strlen("//");
   // find username and password if present
   pp = strstr(p,"@");
   if(pp!=NULL) {
      ppp = strstr(p,":");
      if(ppp!=NULL) {
         strncpy(un,p,ppp-p);
         un[ppp-p] = '\0';
         strncpy(pw,ppp+1,(pp-ppp)-1);
         pw[(pp-ppp)-1] = '\0';
      } else {
         strncpy(un,p,pp-p);
         un[pp-p] = '\0';
         strcpy(pw,"");
      }
      p = pp + 1;
   } else {
      strcpy(un,"");
      strcpy(pw,"");
   }
   // find host
   pp = strpbrk(p,":/");
   if(pp==NULL) pp = p + strlen(p);
   strncpy(host,p,pp-p);
   host[pp-p] = '\0';
   p = pp;
   // find port if present
   if((*p)==':') {
      p++;
      (*port) = atoi(p);
   } else {
      (*port) = 0;
   }
   // find path if present
   pp = strstr(p,"/");
   if(pp!=NULL) strcpy(path,pp); else strcpy(path,"");
   pp = strstr(path,"#");
   if(pp!=NULL) *pp = '\0';
   return;
}

void test(char *url)
{
    char protocol[10],host[100],path[200],un[20],pw[20];
    int port;
    URL_to_components(url,protocol,host,&port,path,un,pw);
    cout << url << endl;
    cout << protocol << endl;
    cout << host << endl;
    cout << port << endl;
    cout << path << endl;
    cout << un << endl;
    cout << pw << endl;
}

int main()
{
    test("http://www.xxx.dk:80/foobar.php?f1=a&f2=bb");
    test("ftp://arne:hemmeligt@ftp.xxx.dk/noget.txt");
    return 0;
}
