예제 #1
0
char *URL_unescape(char *url) {
	if (STR_DEF(url)) {
                register int x, y;
                for (x = 0, y = 0; url[y]; x++, y++) {
                        if ((url[x] = url[y]) == '+')
                                url[x] = ' ';
                        else if (url[x] == '%') {
                                if (! (url[x + 1] && url[x + 2]))
                                        break;
                                url[x] = x2b(url + y + 1);
                                y += 2;
                        }
                }
                url[x] = 0;
        }
	return url;
}
예제 #2
0
/*Main Method */
void main(int argc, char*argv[]){
    
    int addition = 0;
    int subtraction = 0;
    int mult = 0;
    char * firstArg;
    char * secArg;
    char * firstForm;
    char * secForm;
    int negFirst = 0;
    int negSec = 0;
    int firstFinal = 0;
    int secFinal = 0;
    char * firstBin;
    char * secBin;

    /*check arguments*/
    if (argc != 5){
      fprintf(stderr, "Argc is off---should be 5\n");
      return;
    }
   
   /*Check for addition and subtraction*/ 
    char sign = *argv[1];
    switch (sign){
      case '+': addition = 1;
                break;
      case '-': subtraction = 1;
                break;
      case '*': mult = 1;
                break;
      default: fprintf(stderr, "Only addition, subtraction and multiplication allowed\n");
    }

    firstArg = argv[2];
    secArg = argv[3];
    firstForm =  firstArg;
    secForm = secArg;

    /*check for negatives*/
    if(firstArg[0] == '-'){
      negFirst = 1;
      firstForm+=1;
    }
    if(secArg[0] == '-'){
      negSec = 1;
      secForm+=1;
    }

   switch(firstForm[0])
   {
     case 'd': /*decimal to binary*/
               firstForm++;
               firstFinal = atoi(firstForm);
               firstBin = (char*) malloc(3*(strlen(firstForm)));
               d2b(firstBin, firstFinal);
               break;
     case 'o': /*octal to binary */
               firstForm++;
               firstBin = (char*) malloc(3*(strlen(firstForm)));
               o2b(firstBin, firstForm);
               break;
     case 'b': /*no conversion necessary */
               firstForm++;
               firstBin = firstForm;
               break;
     case 'x': /*hexadecimal to binary */
               firstForm++;
               firstBin = (char*) malloc(4*(strlen(firstForm)));
               x2b(firstBin, firstForm);
               break;
     default: fprintf(stderr, "not a valid conversion base option\n");
              return;
   }

    switch(secForm[0])
   {
     case 'd': /*decimal to binary*/
               secForm++;
               secFinal = atoi(secForm);
               secBin = (char*) malloc(3*(strlen(secForm)));
               d2b(secBin, secFinal);
               break;
     case 'o': /*octal to binary */
               secForm++;
               secBin = (char*) malloc(3*(strlen(secForm)));
               o2b(secBin, secForm);
               break;
     case 'b': /*no conversion necessary */
               secForm++;
               secBin = secForm;
               break;
     case 'x': /*hexadecimal to binary */
               secForm++;
               secBin = (char*) malloc(4*(strlen(secForm)));
               x2b(secBin, secForm);
               break;
     default: fprintf(stderr, "not a valid conversion base option\n");
              return;
   }
   
/*
  int diff;
  diff = abs(strlen(firstBin) - strlen(secBin));
  char* shorter;
  char* shorterBin;
  char* longerBin;
  * if strings are different lengths,
   *  we must do some adjusting*
  if(diff > 0){
    if(strlen(firstBin) > strlen(secBin)){
      longerBin = firstBin;
      shorterBin = secBin;
      shorter = (char*) malloc(strlen(shorterBin));
    }
    else{
      longerBin = secBin;
      shorterBin = firstBin;
      shorter = (char*) malloc(strlen(shorterBin));
    }
    * add zeros to the front of the shorter binary string 
    * so they can be compared *
    strcpy(shorter, "0");
    int k;
    for(k=0; k<diff-1; k++){
      strcat(shorter, "0");
    }
    strcat(shorter, shorterBin);
  }
  else{
    longerBin = firstBin;
    shorter = secBin;
  }

  if (addition){
    binAdd(sum, longerBin, shorter);
    sum = reverse(sum);
    printf("binSum: %s\n", sum);
  }
  if (subtraction){
    binSub(sum, longerBin, shorter);
    printf("Subtraction sum: %s\n", sum); 
  }
*/

  /*decimal aritmatic*/
  int decAns = 0;
  if (addition){
    if(negFirst){
      decAns += -1*b2d(firstBin);
    }else{
      decAns += b2d(firstBin);
    }
    if(negSec){
      decAns += -1*b2d(secBin);
    }else{
      decAns += b2d(secBin);
    }
  }else{
  if (subtraction){
    if(negFirst){
      int addme = -1*b2d(firstBin);
      decAns = decAns + addme;
      
    }else{
      decAns += b2d(firstBin);
    }
    if(negSec){
      int addme = -1*b2d(secBin);
      decAns = decAns - addme;
    }else{
      decAns -= b2d(secBin);
    }
  }else{
    if (mult){
     printf("need to complete multiplication"); 
    }
  }
  }
/*convert to final output form*/
  char * ans = (char*) malloc(2*(strlen(firstBin)));
  char * temp = (char*) malloc(2*(strlen(firstBin)));
  char output = *argv[4];
  switch (output){
    case 'd': /*no conversion necessary*/
              printf("Answer: %d\n", decAns);
              break;
    case 'o': /*binary to octal*/
              d2b(temp, decAns);
              b2o(ans, temp);
              printf("Answer: %s\n", ans);
              break;
    case 'x': /*binary to hexadecimal*/
              d2b(temp, decAns);
              b2x(ans, temp);
              printf("Answer: %s\n", ans);
              break;
    case 'b': /*decimal to binary*/
              d2b(ans, decAns);
              printf("Answer: %s\n", ans);
              break;
    default: fprintf(stderr, "not a valid conversion output");
  }

}