/*
 * This is all bit.c. It contains tables for Bit Vectors, 
 * as well as the lookup functions and writing/reading 
 */

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include "include.h"

int bit_lookup(const struct bit_type *bit_table, int max_bit, const char *name )
{	int i;
	for(i = 0; i < max_bit ; i++ )
	{	if(LOWER(name[0]) == LOWER(bit_table[i].name[0])
		&& !str_prefix(name, bit_table[i].name ) )
			return i;
	}
	return -1;
}
int PrntVal_lookup(const struct	bit_type *bit_table,  int max_bit, const char *name )
{	int i;
	for(i=0;i<max_bit;i++ )
	{	if(!strcasecmp(name, bit_table[i].PrntVal ))
			return i;
	}
	return -1;
}


char *bit_name(const struct bit_type *bit_table, int bit )
{	return bit_table[bit].name;
}

char *bit_PrntVal(const struct bit_type *bit_table, int bit )
{	return bit_table[bit].PrntVal;
}

void load_flags(const struct bit_type *bit_table, FILE *fp, int max_int, int *bit_array )
{	int i, bit;
	char *word;
	int tmpMax = fread_number(fp);
	
	for(i = 0; i < tmpMax; i++ )
	{	word = fread_word(fp);
		if( ( bit = PrntVal_lookup(bit_table, tmpMax, word ) ) == -1 )
		{	logfp(LOG_BUG, "load_flags: invalid bit Print Value, \"%s\"\n", word );
			fread_word(fp);
			continue;
		}
		bit_array[bit] = fread_number(fp);
	}
	if(tmpMax < max_int)
		for(; tmpMax < max_int ; ++tmpMax )
			bit_array[tmpMax] = 0;
	return;

}

void save_flags(const struct bit_type *bit_table, FILE *fp, int max_int, int *bit_array )
{	int i;
	fprintf(fp, "%d ", max_int);
	for( i = 0; i < max_int ; i++ )
		fprintf(fp, "%s %d ", bit_PrntVal(bit_table, i), bit_array[i] ); 
	fprintf(fp, "\n" );
	return;
}

void clear_bits(int *bArray, int max_bit )
{	int i;
	for( i = 0; i < max_bit ; i++ )
		UNSET_BIT(bArray, i );
	return;
}
const struct bit_type common_table [] =
{	{ "Afk",		"Ak",		COMMON_AFK		},
	{ "Ban",		"Bn",		COMMON_BANNED		},
	{ "Linkdead",		"Ld",		COMMON_LINKDEAD		},
	{ NULL,			NULL,		0			}
};

const struct bit_type channel_table[] =
{	// Example from MudCon V
/*	{ "General",		"Gn",	0	},
	{ "Advanced",		"Av",   1	},
	{ "Coding",		"Cd",	2	},
	{ "Intermud",		"Im",	3	},
	{ "Novices",		"Nv",	4	},
	{ "Administration",	"Ad",	5	},
	{ "Building",		"Bd",	6	},
	{ "Ethics",		"Et",	7	},
	{ "Legal",		"Lg", 	8	},
	{ "Webdesign",		"Wd",	9	},
	{ "Future",		"Ft",	10	}, */
	{ NULL,			NULL,	0	}
};