-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.c
More file actions
192 lines (172 loc) · 3.75 KB
/
read.c
File metadata and controls
192 lines (172 loc) · 3.75 KB
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
#include <stdio.h>
#include"read.h"
#include<string.h>
#include<stdlib.h>
void read( FILE *file)
{
static const char filename[] = "trial.mk";
file = fopen ( filename, "r" );
count=0;
tempcount=1;
if ( file != NULL )
{
char line [ len ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
fputs ( line, stdout ); /* write the line on the output sreen*/
printf("\n");
if(line[0]=='\t')
{
endins(line);
}
else
{
count++;
tokenize(line);/* calls the function which tokenizes the string into required parts*/
}
}
fclose ( file );
}
else
{
perror ( filename );
}
}
void tokenize(char * line)
{
char *result = NULL;
char str[len][len];//double dimensional array to store the tokenized strings.
int i=0,n=0;
result = strtok( line, ":" );
while( result != NULL )
{
//tokenizes the string based on the ':' operator.
strcpy(str[i],result);
i++;//keeps a count of the number of tokenized parts
result = strtok( NULL, ":" );
}
for(n=0;n<i;n++)
{
tokenline(str[n]); // each of the tokenized part is further tokenized based on words.
}
}
void tokenline(char * line)
{
int l=k;//stores the starting count.
int i;
char *result = NULL;
result = strtok( line, " " );
while( result != NULL )
{
strcpy(s[k],result);// copies the tokenized strings into a double dimensional array.
k++;
result = strtok( NULL, " " );
}
subString(l,k);//calls the substring function which removes new line character.
}
void subString(int l,int k) //this function is used to remove the newline character.
{
int i;
char *temp=NULL;
int j;
for(i=l;i<k;i++)
{
char *string=s[i];
j=0;
while(string[j]!='\n'&&(string[j]!='\0'))
{
j++;
}
temp=strndup(string+0,j);
printf("\t %s\n",temp);
if(temp[0]!='\0')
endins(temp);
}
}
void endins(char *s)
{
struct node *newnode;
char s2[len];
strcpy(s2,s);
newnode=malloc(sizeof(struct node));
newnode->down = newnode->right = newnode->left = 0;
newnode->store=malloc(sizeof(s2));
strcpy(newnode->store,s);
if(head==NULL)
{
head=newnode;
return;
}
else
{
if(tempcount==count)
{
struct node *temp=head;
while(temp->down!=NULL)// to travel the list downwards
{
temp=temp->down;
}
if(newnode->store[0]=='\t')// in case it is a command to be executed, then it will get stored on the left side of the //respective node. i. e in case client has a command gcc -c main.c io.c, this statement //will be inserted to the left node of client
{
while(temp->left!=NULL)
{
temp=temp->left;
}
temp->left=newnode;
}
else
{
if((newnode->store[0]!=' ')||(newnode->store[0]!='\n')||(newnode->store[0]!='\t'))
{
while(temp->right!=NULL)// to store the dependencies on the right side of the node.
{
temp=temp->right;
}
temp->right=newnode;
}
}
}
else if((count-1)==tempcount)
{
tempcount=count;
struct node *temp=head;
while(temp->down!=NULL)
{
temp=temp->down;
}
temp->down=newnode;
}
}
}
void disp()
{
struct node *temp;
struct node *temp2;
temp=head;
temp2=head;
while(temp!=NULL)
{
printf("\tleft %s",temp->store);// prints the left side of the node.
temp2=temp->left;
while(temp2!=NULL)
{
printf("\tleft %s",temp2->store);
temp2=temp2->left;
}
temp=temp->down;
temp2=temp;
}
temp=head;
temp2=head;
while(temp!=NULL)// prints the right side.
{
while(temp2!=NULL)
{
printf("right : %s\t",temp2->store);
temp2=temp2->right;
}
printf("\n");
temp=temp->down;
temp2=temp;
}
}