fs_functions.h 3.77 KB
Newer Older
priyank's avatar
priyank committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

//Inserts attr values in all the fs's present in or_node
//if any fs is already having attr then it is updated. 
//Returns 0 on failure and 1 on success.
int add_attr_val(or_node *OR, char attr[], char value[])
{
	if (OR==NULL)
	{
		return 0;
	}
	else
	{
		int i;
		for (i=0; i<OR->fs_count; i++)
		{
			add_attr_val_2(OR->fs[i], attr, value);
		}
		return 1;
	}
}

//This inserts attr in a particular fs
//If fs already contains attr, then 0 (error) is returned.
//Else attr is inserted & 1 is returned.

int add_attr_val_2(fs_struct *fs, char attr[], char value[])
{
	if (fs==NULL)
		return 0;
	else
	{
		if( g_hash_table_lookup(fs->hash, attr)==NULL)
		{
			fs->key_count++;
			fs->keys=g_list_append(fs->keys, g_strdup(attr));
			g_hash_table_insert(fs->hash, g_strdup(attr), g_strdup(value));
			return 1;
		}
		else
			return 0;
	}
}

//This updates the "attr" in all the fs's of the or_node which contains the key "attr"
int update_attr_val(or_node *OR, char attr[], char value[])
{
	if (OR==NULL)
		return 0;
	else
	{
		int i;
		for (i=0; i<OR->fs_count; i++)
		{
			update_attr_val_2(OR->fs[i], attr, value);
		}
		return 1;
	}
}

//update attr value in the fs.
//If fs does not contain attr 0 is returned.
int update_attr_val_2(fs_struct *fs, char attr[], char value[])
{
	if (fs==NULL)
		return 0;
	else
	{
		if(g_hash_table_lookup(fs->hash, attr)!=NULL)
		{
			g_hash_table_insert(fs->hash, g_strdup(attr), g_strdup(value));
			return 1;
		}
		else
			return 0;
	}
}

//Deletes attr from or_node
int del_attr(or_node *OR, char attr[])
{
	if (OR==NULL)
		return 0;
	else
	{
		int i;
		for (i=0; i<OR->fs_count; i++)
		{
			delete_attr_2(OR->fs[i], attr);
		}
		return 1;
	}

}

//Deletes attr from fs_struct.
//if no key with attr is found 0 is returned. 
int delete_attr_2(fs_struct *fs, char attr[])
{
	if (fs==NULL)
		return 0;
	else if (g_hash_table_lookup(fs->hash, attr)!=NULL)
	{

		//removing attr from the list keys 
		GList *start= fs->keys;
		char *str;
		int j;
		for(j=0; j< fs->key_count; j++)
		{
			str= (char *)g_list_nth_data(start, 0);
			if(strcmp(str, attr)==0)
			{
				fs->keys= g_list_remove_link(fs->keys, start);
				break;
			}
			start= g_list_next(start);
		}	

		//decreasing the key count
		fs->key_count--;

		//removing from the hash table
		g_hash_table_remove(fs->hash, attr);
		return 1;
	}
	else
		return 0;

}

char *get_attr_val(fs_struct *fs, char attr[])
{
	char *str= (char *)g_hash_table_lookup(fs->hash, attr);
	if(str!=NULL)
		return str;
	else
	{
		str=(char *)malloc(sizeof(char));
		*str='\0';
		return str;
	}
}

//a rcursive call which returns a list
GList * tmp_get_nodes_with_attr_val(node *N, char attr[], char val[], GList *tmp)
{
	int i;
	if (N==NULL )
		return tmp;
	if(N->OR!=NULL)
	{
		for (i=0; i< N->OR->fs_count; i++)
		{
			if(g_hash_table_lookup(N->OR->fs[i]->hash, attr)!=NULL)
			{
				if ( strcmp((char *)g_hash_table_lookup( N->OR->fs[i]->hash, attr), val)==0)
				{
					tmp= g_list_append(tmp, (void *)N);
					break;
				}
			}
		}
	}

	GList *start= N->childs;
	for (i=0; i< N->child_count; i++)
	{
		tmp= tmp_get_nodes_with_attr_val( (node *)g_list_nth_data(start, 0), attr, val, tmp);
		start= g_list_next(start);
	}
	return tmp;
}

//get a list of nodes with or_node (fs) attrs equal to val
list_of_nodes *get_nodes_with_attr_val(node *N, char attr[], char val[])
{
	GList *tmp=NULL;
      	tmp= tmp_get_nodes_with_attr_val(N, attr, val, tmp);
	
	list_of_nodes *L= make_list_of_nodes_from_glist(tmp);
	g_list_free(tmp);

	return L;
}

void delete_fs_struct_from_or_node(or_node *OR, int pos)
{
	if (OR==NULL)
		return;
	int i;
	free(OR->fs[pos]);
	for(i=pos; i<OR->fs_count-1; i++)
	{
		OR->fs[i]=OR->fs[i+1];
	}
	OR->fs[OR->fs_count-1]=NULL;
	OR->fs_count--;
	return;
}

void add_fs_struct_to_or_node(or_node *OR, fs_struct *fs)
{
	OR->fs[OR->fs_count]=fs;
	OR->fs_count++;
}