
#include "../global.h"
#include "../survey.h"

#include "shared_survey_string.h"
#include "shared_translate_url.h"
#include "shared_cs_util.h"







char *replace_sp(char *str)
{
  unsigned int i,j = 0;
  char *newstring;


  if (str == NULL)
    return NULL;
  newstring = (char *)malloc(sizeof(char) * ( (strlen(str) * 5) + 1));
  if(!newstring)
      cs_critical_error (ERR_MALLOC_FAILED, "");
      
  for (i = 0; i < strlen(str); i++) {
    if (str[i] == '<') {
      newstring[j] = '&';
      newstring[j+1] = 'l';
      newstring[j+2] = 't';
      newstring[j+3] = ';';
      j += 3;
      j++;
    }
    else if (str[i] == '>') {
      newstring[j] = '&';
      newstring[j+1] = 'g';
      newstring[j+2] = 't';
      newstring[j+3] = ';';
      j += 3;
      j++;
    }
    else if(str[i] == '\'')   /* was &apos; */
    {
      newstring[j] = '&';
      newstring[j+1] = '#';
      newstring[j+2] = '3';
      newstring[j+3] = '9';
      newstring[j+4] = ';';
      j += 4;
      j++;
    }
    else if(str[i] == '&')
    {
      newstring[j] = '&';
      newstring[j+1] = 'a';
      newstring[j+2] = 'm';
      newstring[j+3] = 'p';
      newstring[j+4] = ';';
      j += 4;
      j++;
    }
    else if(str[i] != '\"')
    {
      newstring[j] = str[i];
      j++;
    }
  }
  newstring[j] = '\0';
  return newstring;
}


void
strtrim (char *string)
{
  char *ptr;
  char *newstring;
  int len;

  len = strlen (string);
  newstring = (char *) malloc ((len + 1) * sizeof (char));
  if (!newstring)
       cs_critical_error (ERR_MALLOC_FAILED, "");

  for (ptr = string; *ptr && isspace (*ptr); ptr++);
  strncpy (newstring, ptr, len + 1);

  if (strlen (newstring))
  {
      ptr = strrchr (newstring, '\0');
     for (ptr--; (ptr >= newstring) && isspace (*ptr); ptr--);
        *(ptr + 1) = '\0';
 }

  strncpy (string, newstring, len + 1);
  free (newstring);

}



char *malloc_str(const char *str)
{
     char *new_str = (char *)malloc(sizeof(char) * strlen(str) +1);
     if(new_str != NULL)
	strcpy(new_str, str);
     else
	cs_critical_error(ERR_MALLOC_FAILED, "");
     return new_str;
}
void
decode_space(char *string)
{
    char *ptr;
    for(ptr = string; *ptr; ptr++)
    {
	 switch (*ptr)
	 {
	      case QMARK_SUB: *ptr = '?'; break;
	      case AMP_SUB: *ptr = '&'; break;
              case EQUAL_SUB: *ptr = '='; break;
              case '[': *ptr = '#'; break;
	     default:
             break;
         }

   }
}

