#include <stdio.h>
#include <string.h>
#include "vars.h"

#define MAX 1024


 /**********************************************
  ********** EXPAND URL TO struc url ***********/
int cache2url(struct p_url *t_url, char *k)
{
 char *k1;
        #ifdef DEBUG_CACHE2URL
 int l;
	fprintf(stderr,"expand: ");
	   for (l=0;l<strlen(k);l++)
	    if (k[l]==1)  fprintf(stderr,"!");
	    else fprintf(stderr,"%c",k[l]);

           fprintf(stderr,"\n");
	#endif

       //get 2 level domen

          k1=strchr(k,1);

	  if (!k1) 
	    {
	     k1=strchr(k,0);
	     memcpy(t_url->twoleveldomen,k,k1-k);
	     t_url->twoleveldomen[k1-k]=0;
	     t_url->subdomen[0]=0;
	     t_url->port[0]=0;
	     t_url->dirs[0]=0;
            #ifdef DEBUG_CACHE2URL
	    fprintf(stderr,"  2ld: %s\n",t_url->twoleveldomen);
	    fprintf(stderr,"  sub: %s\n",t_url->subdomen);
	    fprintf(stderr,"  prt: %s\n",t_url->port);
	    fprintf(stderr,"  dir: %s\n",t_url->dirs);
	    fprintf(stderr,"RETURN\n");	    
	    #endif
	    return(1);	     
	    }
          memcpy(t_url->twoleveldomen,k,k1-k);
	  t_url->twoleveldomen[k1-k]=0;
          #ifdef DEBUG_CACHE2URL
	  fprintf(stderr,"  2ld: %s\n",t_url->twoleveldomen);
	  #endif
	
	//get subdomen
          if (*(k1+1)!=1)
	     {
	      k=strchr(k1+1,1);
              memcpy(t_url->subdomen,k1+1,k-k1-1);
	      t_url->subdomen[k-k1-1]='.';
	      t_url->subdomen[k-k1]=0;	      
	     }
	  else 
	     {
	      t_url->subdomen[0]=0;
	      k=k1+1;
	     }
          #ifdef DEBUG_CACHE2URL
	  fprintf(stderr,"  sub: %s\n",t_url->subdomen);
	  #endif

	  //get port
          if (*(k+1)!=1)
	     {
	      k1=strchr(k+1,1);
	      t_url->port[0]=':';
              memcpy(&t_url->port[1],k+1,k1-k-1);
	      t_url->port[k1-k]=0;
	     }
	  else 
	     {
	      t_url->port[0]=0;
	      k1=k+1;
	     }
          #ifdef DEBUG_CACHE2URL
	  fprintf(stderr,"  prt: %s\n",t_url->port);
	  #endif

	  //get dirs
          if (*(k1+1)!=1)
	     {
	      k=strchr(k1+1,0);
              memcpy(t_url->dirs,k1+1,k-k1-1);
	      t_url->dirs[k-k1-2]=0;
	     }
	  else 
	     {
	      t_url->dirs[0]=0;
	     }
	  #ifdef DEBUG_CACHE2URL
	  fprintf(stderr,"  dir: %s\n",t_url->dirs);
	  #endif
 return(0);
}
