Added better function for escaping strings.
authorMarkus Kauppila
Fri, 01 Jul 2011 13:16:51 +0300
changeset 57321da5af48b964
parent 5731 507fffef86ad
child 5733 9c9eeded847a
Added better function for escaping strings.
test/test-automation/xml.c
     1.1 --- a/test/test-automation/xml.c	Fri Jul 01 12:36:57 2011 +0300
     1.2 +++ b/test/test-automation/xml.c	Fri Jul 01 13:16:51 2011 +0300
     1.3 @@ -134,53 +134,85 @@
     1.4  const char *
     1.5  EscapeString(const char *string)
     1.6  {
     1.7 -	//const int bufferSize = 4096;
     1.8 -	char buffer[bufferSize];
     1.9 -	memset(buffer, 0, bufferSize);
    1.10 +	// Calculate the size of the escaped string
    1.11 +	int totalSize = 0;
    1.12  
    1.13 -	// prevents the code doing a 'bus error'
    1.14 -	char *stringBuffer = SDL_malloc(bufferSize);
    1.15 -	if(stringBuffer == NULL) {
    1.16 -		return NULL;
    1.17 -	}
    1.18 -	strncpy(stringBuffer, string, bufferSize);
    1.19 -
    1.20 -	// Ampersand (&) must be first, otherwise it'll mess up the other entities
    1.21 -	char *characters[] = {"&", "'", "\"", "<", ">"};
    1.22 -	char *entities[] = {"&amp;", "&apos;", "&quot;", "&lt;", "&gt;"};
    1.23 -	int maxCount = 5;
    1.24 +	const int maxCount = SDL_strlen(string);
    1.25  
    1.26  	int counter = 0;
    1.27  	for(; counter < maxCount; ++counter) {
    1.28 -		char *character = characters[counter];
    1.29 -		char *entity = entities[counter];
    1.30 +		char character = string[counter];
    1.31  
    1.32 -		if(strstr(stringBuffer, character) == NULL)
    1.33 -			continue;
    1.34 +		switch(character) {
    1.35 +		case '&': totalSize +=  5; //SDL_strlen("&amp;");
    1.36 +			break;
    1.37 +		case '\'': totalSize += 6; //SDL_strlen("&apos;");
    1.38 +			break;
    1.39 +		case '"': totalSize += 6; //SDL_strlen("&quot;");
    1.40 +			break;
    1.41 +		case '<': totalSize += 4; //SDL_strlen("&lt;");
    1.42 +			break;
    1.43 +		case  '>': totalSize += 4; //SDL_strlen("&gt;");
    1.44 +			break;
    1.45 +		default:
    1.46 +			totalSize += 1;
    1.47 +			break;
    1.48 +		}
    1.49 +	}
    1.50  
    1.51 -		char *token = strtok(stringBuffer, character);
    1.52 -		while(token) {
    1.53 -			char *nextToken = strtok(NULL, character);
    1.54 +	char *retBuffer = SDL_malloc(totalSize * sizeof(char));
    1.55 +	if(retBuffer == NULL) {
    1.56 +		return NULL;
    1.57 +	}
    1.58  
    1.59 -			int bytesLeft = bufferSize - SDL_strlen(buffer);
    1.60 -			if(bytesLeft) {
    1.61 -				strncat(buffer, token, bytesLeft);
    1.62 -			} else {
    1.63 -				// \! todo there's probably better way to report an error?
    1.64 -				fprintf(stderr, "xml.c | EscapingString: Buffer is full");
    1.65 -			}
    1.66 -
    1.67 -			if(nextToken)
    1.68 -				strcat(buffer, entity);
    1.69 -
    1.70 -			token = nextToken;
    1.71 +	// escape the string
    1.72 +	int retBufferCounter = 0;
    1.73 +	for(counter = 0; counter < maxCount; ++counter) {
    1.74 +		char character = string[counter];
    1.75 +		switch(character) {
    1.76 +		case '&':
    1.77 +			retBuffer[retBufferCounter++] = '&';
    1.78 +			retBuffer[retBufferCounter++] = 'a';
    1.79 +			retBuffer[retBufferCounter++] = 'm';
    1.80 +			retBuffer[retBufferCounter++] = 'p';
    1.81 +			retBuffer[retBufferCounter++] = ';';
    1.82 +			break;
    1.83 +		case '\'':
    1.84 +			retBuffer[retBufferCounter++] = '&';
    1.85 +			retBuffer[retBufferCounter++] = 'a';
    1.86 +			retBuffer[retBufferCounter++] = 'p';
    1.87 +			retBuffer[retBufferCounter++] = 'o';
    1.88 +			retBuffer[retBufferCounter++] = 's';
    1.89 +			retBuffer[retBufferCounter++] = ';';
    1.90 +			break;
    1.91 +		case '"':
    1.92 +			retBuffer[retBufferCounter++] = '&';
    1.93 +			retBuffer[retBufferCounter++] = 'q';
    1.94 +			retBuffer[retBufferCounter++] = 'u';
    1.95 +			retBuffer[retBufferCounter++] = 'o';
    1.96 +			retBuffer[retBufferCounter++] = 't';
    1.97 +			retBuffer[retBufferCounter++] = ';';
    1.98 +			break;
    1.99 +		case '<':
   1.100 +			retBuffer[retBufferCounter++] = '&';
   1.101 +			retBuffer[retBufferCounter++] = 'l';
   1.102 +			retBuffer[retBufferCounter++] = 't';
   1.103 +			retBuffer[retBufferCounter++] = ';';
   1.104 +			break;
   1.105 +		case  '>': totalSize += SDL_strlen("&gt;");
   1.106 +			retBuffer[retBufferCounter++] = '&';
   1.107 +			retBuffer[retBufferCounter++] = 'g';
   1.108 +			retBuffer[retBufferCounter++] = 't';
   1.109 +			retBuffer[retBufferCounter++] = ';';
   1.110 +			break;
   1.111 +		default:
   1.112 +			retBuffer[retBufferCounter++] = character;
   1.113 +			break;
   1.114  		}
   1.115  
   1.116 -		memcpy(stringBuffer, buffer, bufferSize);
   1.117 -		memset(buffer, 0, bufferSize);
   1.118  	}
   1.119  
   1.120 -	return stringBuffer;
   1.121 +	return retBuffer;
   1.122  }
   1.123  
   1.124