Code:
//dot_structure.c
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dot_structure.h"
struct tree_node
{
char *word;
// int word_len;
struct tree_node *left;
struct tree_node *right;
};
//function .
static int readline(FILE *,char *,int); //to read a line from a file
static int getstring(char *,char *,int ); //to read a string from the line read above
static struct tree_node *addtree(struct tree_node *,char *); //function to add a node to the tree
static struct tree_node *talloc(); //used in the addtree function
char *strdup1(char *); //used in the addtree function
static int count_elements(struct tree_node *p,int *len); //create the index array
static void fill_index_storage(struct tree_node *p); //fill the index array created in the above function
static void build_tree(); //tree to store the elements dynamically
static void build_index(); //builds index from the tree structure
static void generate_dot_structure(); //creats the dot structure from the index
static void build_structure(); //creates the relationship details
static void store_structure(); //stores the dot structure and index on file
static void add_to_structure(char *,char *,char *);
static int get_index(char *);
static void mark_index(int ,int );
//debug
static void print_elements(struct tree_node *p); // debug function.should be deleted at the time of production
//variables.
static int start_pos;
static struct tree_node *root; //root node of the tree structure
static char *index_array[MAXXWORD];
dot_structure dotty;
static int total_elements;
static int max_element_len;
//code
int init()
{
build_tree();
print_elements(root);
build_index();
generate_dot_structure();
build_structure();
store_structure();
}
void build_structure()
{
FILE *input;
char *line,*string;
char *prev_job,*curr_job,*next_job;
line = (char *)malloc(MAXXWORD);
string = (char *) malloc (max_element_len);
prev_job = (char *)malloc(max_element_len); //Predecessor job
curr_job = (char *)malloc(max_element_len); //current job
next_job = (char *)malloc(max_element_len); //sucessor job
int job_switch = 1;
int len,strlen;
if((input = fopen("report.txt","r")) == NULL)
{
printf("\nError in function build_structure - file open failed");
exit(1);
}
while((len = readline(input,line,MAXXWORD)) != -1)
{
// printf("\n%s\n",line);
start_pos = 0;
job_switch = 1;
while((strlen = getstring(&line[start_pos],string,len)) != -1)
{
if(strlen == 0) //Means the partiuclar column is blank
{
job_switch++;
continue;
}
switch(job_switch)
{
case 1:
strcpy(prev_job,string);
job_switch++;
break;
case 2:
strcpy(curr_job,string);
job_switch++;
break;
case 3:
strcpy(next_job,string);
job_switch++;
break;
default :
printf("Problem occured");
exit(1);
}
}
if (strlen == -1 && *line == '-')
{
*prev_job = *curr_job = *next_job = '\0';
continue;
}
add_to_structure(prev_job,curr_job,next_job);
}
fclose(input);
free(line);
free(string);
free(prev_job);
free(curr_job);
free(next_job);
}
static void add_to_structure(char *prev,char *curr,char *next)
{
int row,col;
//printf("\nPrevious job %s length %i current job %s length %i next job %s length %i", prev, strlen(prev), curr, strlen(curr), next, strlen(next));
if(strlen(curr) == 0)
printf("\nCurrent job can never be zero. Logic failed");
//mapping prev and curr job
if(strlen(prev) > 0)
{
row=get_index(prev);
col=get_index(curr);
if(row == -1 || col == -1)
{
printf("error");
exit(1);
}
mark_index(row,col);
}
//mapping curr and sucessor job
if(strlen(next) > 0)
{
row=col;
col=get_index(next);
if(row == -1 || col == -1)
{
printf("error");
exit(1);
}
mark_index(row,col);
}
}
static int get_index(char *s)
{
int i=0;
for(i=0;i<total_elements;i++)
{
if(strcmp(s,index_array[i]) == 0)
{
return i;
}
}
return -1;
}
static void mark_index(int row,int col)
{
int disp;
disp = row * total_elements + col;
dotty[disp] = '1';
}
static void print_elements(struct tree_node *p) // debug function.should be deleted at the time of production
{
static int count;
if (p->left != NULL)
print_elements(p->left);
if (p->right != NULL)
print_elements(p->right);
printf("\nThe element name is :%s/ end here",p->word);
}
static void build_tree()
{
root=NULL;
FILE *input;
char *string,*line;
line=(char *)malloc(MAXXWORD);
string =(char *)malloc(MAXXWORD);
int len,strlen;
if((input = fopen("report.txt","r")) == NULL)
{
printf("\nError in opening the input file\n");
exit(1);
}
while((len = readline(input,line,MAXXWORD)) != -1)
{
start_pos=0;
while((strlen = getstring(&line[start_pos],string,len))!=-1)
{
if (strlen > 0)
{
if(max_element_len < strlen)
max_element_len = strlen;
root=addtree(root,string);
}
}
}
fclose(input);
free(line);
free(string);
}
static int readline(FILE *fp,char *line,int maxxword)
{
register int c;
int n=0;
register char *s;
s=line;
while(((c=getc(fp))!=EOF) && (n++ < maxxword))
if((*s++=c)=='\n')
break;
*s='\0';
if (c == EOF && n ==0)
return c;
return strlen(line);
}
static int getstring(char *line,char *string,int len)
{
//int strlength=0;
int trail_blank=0; //number of trailing blanks;
const int pipe = 124;
char *temp;
temp=string;
*temp='\0';
if (start_pos >= len) //EOL
return -1;
if(*line == '-') //skip the line with "-" at the starting
return -1;
while(*line == ' ' && start_pos<len ) //skipping the spaces
{
line++;
start_pos++;
}
while(*line != pipe && *line !='\n' && start_pos<len)
{
if (*line == ' ' || *line =='\r')
trail_blank++;
else
trail_blank=0;
*temp++=*line++;
start_pos++;
// strlength++;
}
//temp++;
for(;trail_blank >0;trail_blank--)
temp--;
start_pos++;
*temp = '\0';
return strlen(string);
}
//adding elements to the tree.
static struct tree_node *addtree(struct tree_node *p,char *w)
{
int cond;
if(p==NULL)
{
p=talloc();
p->word = strdup1(w); //allocation space for the word in the tree
// *p->word_len = strlen(p->word) + 1;
p->left=NULL;
p->right=NULL;
}
else if((cond = strcmp(p->word,w)) == 0) //duplicate entry
{
// p->count++;
// continue;
}
else if(cond < 0)
{
p->left = addtree(p->left,w);
}
else
{
p->right = addtree(p->right,w);
}
return p;
}
static struct tree_node *talloc()
{
return (struct tree_node *)malloc(sizeof(struct tree_node));
}
char *strdup1(char *s)
{
char *p;
p = (char *)malloc(strlen(s) + 1);
if(p!=NULL)
strcpy(p,s);
return p;
}
static void build_index()
{
int word_len =0;
total_elements = count_elements(root,&word_len);
// index_array = (char **)malloc(total_elements+1);
fill_index_storage(root);
}
static int count_elements(struct tree_node *p,int *len)
{
static int count;
if (p->left != NULL)
count_elements(p->left,len);
if (p->right != NULL)
count_elements(p->right,len);
count++;
return count;
}
static void fill_index_storage(struct tree_node *p)
{
static int i;
if (p->left != NULL)
fill_index_storage(p->left);
if (p->right != NULL)
fill_index_storage(p->right);
char *w;
w=strdup1(p->word);
index_array[i] = w;
i++;
}
static void generate_dot_structure()
{
int i =0;
dotty=(dot_structure)malloc((total_elements*total_elements) + 1);
for (i=0;i<(total_elements*total_elements);i++)
{
dotty[i]='0';
}
dotty[++i]='\0';
}
static void store_structure()
{
// continue;
}