Exemplo n.º 1
0
/*
	* Returns true if it has successfully loaded all the lines from _inputFile
	* without encoutering ANY format or syntax error into our parameters.
	* FOR MORE INFORMATION ABOUT THE FORMAT OF THE FILE, PLEASE TAKE A LOOK AT World.txt.
	*/
bool World::analyseFile()
{
	Reader* reader = new Reader(inputFile);
	if (reader && reader->Open())
	{
		try
		{
			//Keeps in memory the number of loops we currently have to do.
			int* valueQuantity;

			//Look for the first parameter: the continents.
			reader->flushUntilYou('[');
			reader->getMultipleFormats("[Continents=%u]\n", &valueQuantity);

			//For every continent.
			for (int i = 0; i < *valueQuantity; i++)
			{
				char* continentInfo;
				int* registryValue;
				//We want to put in memory the name of it until an '=' an then its reference value (a.k.a. registryValue).
				reader->getMultipleFormats("%l==%u\n", &continentInfo, &registryValue);
				Continent* continentObject = new Continent(continentInfo, *registryValue);
				//Once we have the infos of one, we add it to our vector.
				continentsVector->push_back(continentObject);
			}

			//Look for the first parameter: the countries.
			reader->flushUntilYou('[');
			reader->getMultipleFormats("[Territories=%u]\n", &valueQuantity);

			//For every country.
			for (int i = 0; i < *valueQuantity; i++)
			{
				char* countryInfo;
				int* registryValue;
				//We want to put in memory the name of it until an '=' an then its reference value (a.k.a. registryValue).
				reader->getMultipleFormats("%l==%u\n", &countryInfo, &registryValue);
				Country* countryObject = new Country(countryInfo, *registryValue);
				//Once we have the infos of one, we add it to our vector.
				countriesVector->push_back(countryObject);
			}

			//Look for the first parameter: the links between continents.
			reader->flushUntilYou('[');
			reader->getMultipleFormats("[Links=%u]\n", &valueQuantity);
			numberOfLinks = *valueQuantity;

			//For every link.
			for (int i = 0; i < *valueQuantity; i++)
			{
				int *countryRegistryValue, *continentRegistryValue, *countryPositionX, *countryPositionY;
				int** countryLinks = new int*[10]; //We eventually won't need more than 10 spaces.
													/*
													* We first select the country we want to link, and then we load its related cartesian position
													* and continent and put then into several variables.
													*/
				reader->getMultipleFormats("%u,%u,%u,%u", &countryRegistryValue, &countryPositionX, &countryPositionY, &continentRegistryValue);
				vector<Country*>* linksVector = new vector<Country*>();
				//Then, as long as the line does not end, we take the values of the linked countries.
				for (int j = 0; reader->byteAtActualPosition() != '\n'; j++)
				{
					if (j > 9) break;
					reader->getMultipleFormats(",%u", &countryLinks[j]);
					linksVector->push_back(countriesVector->at(*countryLinks[j]));
				}
				//Flush the '\n' meaning that we are ready for the next line to be loaded.
				reader->flushNextByte();

				//Set all the infos we just loaded to its proper country.
				countriesVector->at(*countryRegistryValue)->setCartesian(*countryPositionX, *countryPositionY);
				countriesVector->at(*countryRegistryValue)->setContinent(continentsVector->at(*continentRegistryValue));
				countriesVector->at(*countryRegistryValue)->connect(linksVector);
			}
		}
		//File does not respond.
		catch (fileNotGood fng) {
			lastErrorMessage = fng.what();
			return false;
		}
		//Syntax error: better stop operation as soon as possible.
		catch (FormatSyntax fs) {
			lastErrorMessage = fs.what();
			return false;
		}
		//Close the file whatever happened not to loose data.
		reader->Close();
		return true;
	}
	else
		lastErrorMessage = "Could not open the input file.";
	return false;
}