Config File


Macros way heavily on your soul after a while. For me, the routine of going to source compiling and then running was a waste of time. I needed a config file loader fast, so this is what I came up with. It was on the fly and it works, what more can you ask for. And most of the code not, all of it, is below.
#
# glAnts - original config.ini
# file, change at your own risk
#
# if you remove this file, the program
# will create another one with the defaults
# so removing this file can be a good thing
#
#
# Also of note, if the system finds an error
# it goes with the default variable
#
# FORMAT has to be [VARIABLE_NAME]=VALUE;
# oh yeah, add the 'f' tag for float
# 
# [VARIABLE_NAME]=VALUEf;
# 
# COMMENTS are '#'
#
# Remove the comments below are your own risk
# they basically just define the default values
# used, if you remove them then you wont know
# what good values will look like
#
# The only things that really need changing
# are:
#
# USE_ANT_ENGINE: take out the little ants 
#
# MAX_FIRE_ANTS: max fighter ants
# 
# MAX_BOTS: number of worker ants
#
# INITIAL_ANT_FOOD: the health of the fighter
#					ants and the workers
#					try 10,000 and they will never die
#
# Also, you may comment one of the variables
# and get the default once the program runs
#

[GLANTS CONFIG]
# los default is 14.0 
[LINE_OF_SIGHT]=115.0f;

# Used with the ai, it attacks at any point given
# this radius
# default is 4.0
[ATTACK_RADIUS]=4.0f;

# The amount of damage a bullet will inflict
# the damage is reduced considerably
# at a greater distance and its impact
# is great at short distances
# try 5000, hehe
# default = 280
[BULLET_DAMAGE]=280.0f;

# defaut is 4.90
[MIN_BULLET_SPEED]=7.20f;

# You can get rid of the worker ant
# engine and just shoot stuff
[USE_ANT_ENGINE]=0;

# the number of attack ants
# default is 4, but that is on a 
# slow machine, try 50 for a 1ghz machine
[MAX_FIRE_ANTS]=6;	

# number of worker ants, default=120
[MAX_BOTS]=10;

# 0 = place food in random spot
# 1 = place food in centralized spots
[USE_GARDEN_AREA]=1;					

# the max number of pheromones
# an ant can leave
# default: 300
[MAX_TRAIL_STACK]=300;

# used with AI, if the ant life reaches
# this point, the ai goes berzerk
# and he will move a little faster
# default: 390
[DYING_STATE]=390;

# the number of pheromones
# the ants can leave on the grid
# default: 200, make sure not more than
# max_trail_stack
[MAX_PHEROMONES]=200;

# default: 1000 
[PHEROMONE_LIFE]=1000;

# the time before an ant drops a pheromone
# default: 40
[PHEROMONE_DROP]=40;

# the number of bullets
# the ai has before recycle
# this doesnt effect that much
# default: 20
[MAX_BULLETS]=20;

# this is reduce rapid-fire effect
# slows down the rate for firing speed
# default: 10
[MAX_FIRE_SPEED]=10;

# the amount of food that is on the grid
# default: 35
[MAX_GARDENS]=35;

# the bot's speed, need I say more
# the fire_ants travel at a little
# slower speed
# default: 0.09
[BOT_SPEED]=2.29f;

# when the bots accelerate
# this the top speed 
# default: 0.14
[BOT_MAX_SPEED]=3.04f;

# default: 1.1
[MIN_TURN_SPEED]=1.5f;

# default: 600
[CHECK_RESPAWN]=600;

# the max amount of food
# that can respawn at
# any given point in time
# default: 15
[GARD_RESPAWN_RATE]=15;

# default: 60
[MIN_STRAIGHT_STEPS]=60;

# default: 200
[MAX_STRAIGHT_STEPS]=200;

# default: 150
[MIN_STRAIGHT_STEPS_2]=150;

# default: 360
[MAX_STRAIGHT_STEPS_2]=360;

#
# this is the amount of food
# the ants get as well as the fire_ants
# default: 1000
[INITIAL_ANT_FOOD]=1000;

# the amount of food inside
# a block of food in the garden
# default: 7000
[INITIAL_GARD_FOOD]=7000;

# default: 0.3
[FOOD_WIDTH]=0.3f;

# default: 480
[INIT_FOOD_RATE]=480;

# default: 850
[MAX_FOOD_RATE]=850;

# default: 1.7
[MOVE_FOOD_RATE]=1.7f;
		
# default: 0.4
[FOOD_RATE]=0.4f;


//
// Process_ConfigFile
//
int Process_ConfigFile(char *buffer)
{
	int i;
	int res;

	// not case sensitive

	for (i = 0; i < MAX_ERRORS; i++)
	{
		res = strcmpi(buffer, error_str[i]);
		
		if (res == 0)
		{
			return i;
		} // end of the if 

	} // end of the for

	return -1;

} // end of the function 

//
// void Read_ConfigFile
// - 
//
// if the error tally comes up 
// with any errors it will have
// to create a default variable, sorry
//
void Read_ConfigFile(FILE *f)
{
	char c;

	char buffer[256];
	int c_index = 0;
	int res=0;
	char var[80];
	int float_flag = 0;

	int lines_read = 0;

	while (!feof(f))
	{

		c = fgetc(f);

		switch(c)
		{

			case '#':

				while(1) {
					c = fgetc(f);

					if (c == '\n')
						break;

				} // end of the while 

			break;
	

			case '[':

				c_index = 0;

				while(1)
				{
					c = fgetc(f);
				
					if (c == ']')
						break;

					buffer[c_index] = c;
					c_index++;

					if (c == '\n') {
						lines_read++;
						break;
					} // end of if 

				} // end of the while 

				// null terminate the command
				buffer[c_index]='\0';


				res = Process_ConfigFile(buffer);

				// now add the variable with the value
				//
				c = fgetc(f);
				
				// get the equals
				c_index = 0;
				float_flag = 0;

				while(1)
				{
					c = fgetc(f);

					// we found a float
					if (c == 'f') {
						float_flag = 1;
						break;
					} // end of the if 

					// break on a ';'
					if (c == ';')
						break;

					var[c_index] = c;
					c_index++;

					if (c == '\n')
						break;

				} // end of the while

				var[c_index] = '\0';

				
				// the final step, send in a 
				// variable
				Set_Variable(res,var,float_flag);

			break;

			default:break;

		}; // end switch
			
	} // end of the while 

} // end of the function 

//
// 
// Rewrite File
//
// if the file doesnt exist
//
void Rewrite_File(void)
{
	int i =0;
	int f_flag = 0;

	// who needs types
	void  *tmp = NULL;
	float *tmp_f = NULL;
	int	  *tmp_i = NULL;

	f_newfile = fopen(CONFIG_FILE_NAME, "w");


	for (i = 0; i < MAX_TXT_MSGS; i++)
	{
		fprintf(f_newfile, "%s", text_msg[i]);
	} // end of the for 



	for (i = 0; i < MAX_ERRORS; i++)
	{
		if (i == 0)
			continue;

		fprintf(f_newfile, "[");
		fprintf(f_newfile, "%s", error_str[i]);
		fprintf(f_newfile, "]=");

		f_flag = 0;

		tmp = Get_ConfValue(i, &f_flag);
		
		if (f_flag) {
			tmp_f = (float *)tmp;
			fprintf(f_newfile,"%0.2ff;\n", (float &)*tmp_f);
		} else{
			tmp_i = (int *)tmp;
			fprintf(f_newfile,"%d;\n", (int &)*tmp_i);
		} // end of if-else



		free(tmp);
		tmp = NULL;

	} // end of the for 

	fprintf(f_newfile, "\n\n");

	fclose(f_newfile);

} // end of the function