Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
#define MAX_STRING_SIZE 10
#define MAX_TABLE_SIZE 5
#define TRUE 1
#define FALSE 0
int match_found;
typedef struct node
{
char *data;
int block;
struct node * next;
}node;
node * head[MAX_TABLE_SIZE];
node * cur[MAX_TABLE_SIZE];
node * temp[MAX_TABLE_SIZE];
node * find;
int get_hash_key(int arg_assci_value)
{
int hash_key;
hash_key = arg_assci_value % MAX_TABLE_SIZE;
return hash_key;
}
void disp(int key)
{

if(head[key] == NULL)
{
printf("Empty List\n");
++
}
else
{
temp[key] = head[key];
printf("List is for hash key %d: ", key);
while ( temp[key] != NULL)
{
printf(" %s | %d ->",temp[key]->data, temp[key]->block);
temp[key] = temp[key]->next;
}
}
printf("\n");
}
void add_node_head(int count, int arg_block, int key, char *arg_data)
{
node * n = (node *) malloc(sizeof(struct node));
n-> data = malloc(sizeof(char) * count);
strcpy(n-> data,arg_data);

n-> block = arg_block;


n->next = NULL;
if(head[key] == NULL)
{
head[key] = n;
cur[key] = n;
}

A block device is one that is designed to operate in terms of the block I/O supp
orted by Digital UNIX. It is accessed through the buffer cache. A block device h
as an associated block device driver that performs I/O by using file system bloc
k-sized buffers from a buffer cache supplied by the kernel. Block device drivers
are particularly well-suited for disk drives, the most common block devices.
A character device is any device that can have streams of characters read from o
r written to it. A character device has a character device driver associated wit
h it that can be used for a device such as a line printer that handles one chara
cter at a time. However, character drivers are not limited to performing I/O a s
ingle character at a time (despite the name ``character'' driver). For example,
tape drivers frequently perform I/O in 10K chunks. A character device driver can
also be used where it is necessary to copy data directly to or from a user proc
ess. Because of their flexibility in handling I/O, many drivers are character dr
ivers. Line printers, interactive terminals, and graphics displays are examples
of devices that require character device drivers.
else
{
cur[key] = head[key];
head[key] = n;
head[key]->next = cur[key];
}
}
node * search_full_scope(char *find_data)
{
int i;
for(i = 0; i < MAX_TABLE_SIZE; i++)
{
cur[i] = head[i];
while(cur[i] != NULL)
{
if(strcmp(cur[i]->data,find_data) == 0)
{
printf("1st Match Returend\n");
match_found = TRUE;
return cur[i];
}
else{
cur[i] = cur[i]->next;
}
}
}
match_found = FALSE;
printf("Match not Found\n");
return cur[i-1];
}
node * search_in_scope(char *find_data, int block_num)
{
int i;
for(i = 0; i < MAX_TABLE_SIZE; i++)
{
cur[i] = head[i];
while(cur[i] != NULL)
{
if(strcmp(cur[i]->data,find_data) == 0 && cur[i]->block == block_num
)

{
printf("1st Match Returend\n");
match_found = TRUE;
return cur[i];
}
else{
cur[i] = cur[i]->next;
}
}
}
match_found = FALSE;
printf("Match not Found\n");
return cur[i-1];
}
int main()
{
char *input_read; // Pointer to stored Data read from file
char *temp_stroage;
//temp storage to store read data
char *find_string;
char temp_char;
int char_count = 0;

// Keep count of how many data has been read from fi

le
int
int
int
int
int
int
int
int
int
int

i = 0, temp = 0;
assci_value, total_assci_value = 0;
hash_key;
mstr_block = 0, curr_block = 0;
original_block_num = 0;
temp1 = 0, temp2 = 0;
nested = 0;
ch, find_block, insert_ch;
end_ch;
end = 0;

FILE *input_file_ptr;

// Pointer to input file

for(i = 0; i < MAX_TABLE_SIZE; i++)


{
head[i] = NULL;
}
input_file_ptr = fopen("temp.txt","r");
temp_stroage = malloc(sizeof(char) * 10);
find_string = malloc(sizeof(char) * 10);
while(temp == 0)
{
fscanf(input_file_ptr, "%s", temp_stroage);
//printf("data read is %s\n", temp_stroage);
i = 0;
// This loop will Count nubmber of char are read
while(temp_stroage[i] != '\0')
{
char_count++;
i++;
}
// This loop will break string into charecter

// Get integer valuse of charecter read


for(i = 0; i < char_count; i++)
{
temp_char = temp_stroage[i];
if( temp_char == '{')
{
if(curr_block == 0)
{
original_block_num = mstr_block + 1;
temp1 = mstr_block;
mstr_block++;
curr_block++;
}
else
{
if(temp1 > 0)
{
mstr_block++;
curr_block++;
original_block_num = curr_block + temp1;
}
else
{
curr_block++;
mstr_block++;
original_block_num++;
}
}
if(original_block_num < mstr_block)
{
temp2++;
nested++;
original_block_num += nested;
}
}
if( temp_char == '}')
{
curr_block--;
original_block_num--;
if(temp2 > 0)
{
original_block_num -= nested;
temp2 = 0;
}
}
assci_value = (int)temp_char;
total_assci_value += assci_value;
}
hash_key = get_hash_key(total_assci_value);
//printf("Block number is %d\n", original_block_num);
//printf("Number of char read is %d\n", char_count);
//printf("Total Assci value of string is %d\n", total_assci_value);
//printf("HASH KEY IS %d\n", hash_key);
if( temp_char == '{' || temp_char == '}')
{
// printf("Block Changed\n");
}
else

{
add_node_head(char_count, original_block_num, hash_key, temp_stroage
);
}
// If End of file is encountered then while loop will break
if(feof(input_file_ptr))
{
temp = 1;
}
total_assci_value = 0;
char_count = 0;
}
for(i = 0; i < MAX_TABLE_SIZE; i++)
{
disp(i);
printf("Hash Changed\n");
}
while(end == 0)
{
printf("Do you Want To Find?\n1.Find in Full scope\n2.Find in Scope\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
{
printf("What Do You Want to find\n");
scanf("%s", find_string);
find = search_full_scope(find_string);
if(match_found == FALSE)
{
printf("Do You Want to Insert?\n1.Yes\n2.No\n");
scanf("%d", &insert_ch);
switch(insert_ch)
{
case 1:
{
printf("In which Block you want to enter?\n");
scanf("%d", &find_block);
total_assci_value = 0;
char_count = strlen(find_string);
strcpy(temp_stroage, find_string);
for(i = 0; i < char_count; i++)
{
temp_char = temp_stroage[i];
assci_value = (int)temp_char;
total_assci_value += assci_value;
}
hash_key = get_hash_key(total_assci_value);
original_block_num = find_block;
add_node_head(char_count, original_block_num, hash_k
ey, temp_stroage);

printf("--------New Table is---------\n");


for(i = 0; i < MAX_TABLE_SIZE; i++)
{
disp(i);
printf("Hash Changed\n");
}
break;
}
case 2:
{
printf("No New Node Inserted\n");
break;
}
default:
{
printf("Wrong Choice Entered\n");
break;
}
}
match_found = TRUE;
}
else
{
printf("data: %s | block:%d \n",find->data, find->block);
}
break;
break;
}
case 2:
{
printf("What Do You Want to find\n");
scanf("%s", find_string);
printf("In which Scope You want To find\n");
scanf("%d", &find_block);
find = search_in_scope(find_string, find_block);
if(match_found == FALSE)
{
printf("Do You Want to Insert?\n1.Yes\n2.No\n");
scanf("%d", &insert_ch);
switch(insert_ch)
{
case 1:
{
total_assci_value = 0;
char_count = strlen(find_string);
strcpy(temp_stroage, find_string);
for(i = 0; i < char_count; i++)
{
temp_char = temp_stroage[i];
assci_value = (int)temp_char;
total_assci_value += assci_value;
}
hash_key = get_hash_key(total_assci_value);
original_block_num = find_block;
add_node_head(char_count, original_block_num, hash_k
ey, temp_stroage);
printf("--------New Table is---------\n");
for(i = 0; i < MAX_TABLE_SIZE; i++)

{
disp(i);
printf("Hash Changed\n");
}
break;
}
case 2:
{
printf("No New Node Inserted\n");
break;
}
default:
{
printf("Wrong Choice Entered\n");
break;
}
}
match_found = TRUE;
}
else
{
printf("data: %s | block:%d \n",find->data, find->block);
}
break;
}
default:
{
printf("Wrong Choice Entered\n");
break;
}
}
printf("Do You Want to Continue??\n1.Yes\n2.No\n");
scanf("%d", &end_ch);
if(end_ch == 2)
{
end = TRUE;
}
else
{
continue;
}
}
return 0;
}

You might also like