Ejemplo n.º 1
0
// The first thing it get will be the key value
KeyValue *SKS::GetKeyValue(Position &pos)
{
	// Creates the key value to be sent back
	KeyValue *val = new KeyValue();

	// Position should be located one character after the colon
	val->SetName(GetName(pos));

	// Gets rid of leading whitespace
	while(isspace(m_fileContents[pos.pos]))
		Next(pos);
	// One reaches first character determine if a string or some sort of real number
	if(m_fileContents[pos.pos] == '"')
	{
		// move position to the first value of the string
		Next(pos);

		// Creates the string that will holst the value
		char * string = new char [MAX_STATEMENT];

		// loop until the end of the string
		int stringPos = 0;
		while(m_fileContents[pos.pos] != '"')
		{
			// One thing to note is that the contents of a string
			// value don't allow comments
			// Inserts any escape characters
			if(m_fileContents[pos.pos] == '\\')
			{
				Next(pos, false);
				if(m_fileContents[pos.pos] == '"') // Quotes
					string[stringPos] = m_fileContents[pos.pos];
				else if(m_fileContents[pos.pos] == '\'') // Quote
					string[stringPos] = m_fileContents[pos.pos];
				else if(m_fileContents[pos.pos] == '\\')
					string[stringPos] = m_fileContents[pos.pos];
				else if(m_fileContents[pos.pos] == 'n') // Newline
					string[stringPos] = '\n';
				else
				{
					cerr << "Unknown escape character \'" <<
						m_fileContents[pos.pos] << "' Line: " << pos.line << endl;
					// Just put the character after the backslash
					// in anyways
					string[stringPos] = m_fileContents[pos.pos];
				}

				// Increments their position
				stringPos++;
				Next(pos, false);
			}
			else
			{
				string[stringPos] = m_fileContents[pos.pos];
				stringPos++;
				Next(pos,false);
			}
		}

		// Adds null terminator to end of string
		string[stringPos] = NULL;

		Next(pos);  // Advances to the character after the ending quotes

		// Tells the key value what it is holding
		val->SetKeyValueType(TYPE_STRING);
		val->SetString(string);
		
	}
	else if(isdigit(m_fileContents[pos.pos]) || m_fileContents[pos.pos] == '-')
	{
		int integer = 0, fraction = 0;
		// string big enough to hold an 32 bit integer
		char num[11];
		
		bool isNegitive = false;
		if(m_fileContents[pos.pos] == '-')
		{
			// Tells that the number is negitive
			isNegitive = true;

			//Advanes to the actual first number
			Next(pos);

			//Gets rid of any spaces that might be inbetween the negitive sign
			// and the start of the number
			while(isspace(m_fileContents[pos.pos]))
				Next(pos);
		}

		// Read until a space is hit or a . is hit for a real number or the semi colon
		int i = 0;
		while(isdigit(m_fileContents[pos.pos]))
		{
			num[i++] = m_fileContents[pos.pos];
			Next(pos);
			
			if(i >= 10) // Bigger number then the 2 billion of a 4 byte integer
			{
				cerr << "Integer Overflow. Line:  " << pos.line << endl;
				break;
			}
		}
		// Puts null terminator at the end of the string
		num[i] = 0;

		// figures out the integer value
		integer = isNegitive ? -atoi(num) : atoi(num);



		// if there is a fraction part to the number
		if(m_fileContents[pos.pos] == '.')
		{
			Next(pos);
			i = 0;
			while(isdigit(m_fileContents[pos.pos]))
			{
				num[i++] = m_fileContents[pos.pos];

				Next(pos);

				if(i >= 10) // Bigger number then the 2 billion of a 4 byte integer
				{
					// Prints error message
					cerr << "Integer Overflow. Line:  " << pos.line << endl;

					// Advances the position past the rest of the number
					while(isdigit(m_fileContents[pos.pos]))
						Next(pos);
					break;
				}
			}
			// add null termintaor to string
			num[i] = 0;

			// figures out the fractal part
			fraction = atoi(num);

			//Tells the key value what it is holding
			val->SetKeyValueType(TYPE_REAL);
			if(isNegitive)
				val->SetReal(integer - fraction / (float)pow((double)10, (double)strlen(num)));
			else
				val->SetReal(integer + fraction / (float)pow((double)10, (double)strlen(num)));
		}
		else
		{
			// Tells the key value what it is holding
			val->SetKeyValueType(TYPE_INTEGER);
			// Update position to after the 
			val->SetInteger(integer);
		}
	}

	return val;
}