-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilemap.c
133 lines (123 loc) · 1.99 KB
/
filemap.c
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
/**
* @file filemap.c
* @brief POJ 1057
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define XEND '#'
#define XFIN '*'
#define XFILE 'f'
#define XDIR 'd'
#define XCLOSE ']'
struct xfile {
char name[128];
struct xfile *next;
};
/* Input Buffer */
char buf[BUFSIZ];
int set=0;
int indent=0;
void do_indents(void)
{
int i=indent;
while(i--){
printf("| ");
}
fflush(stdout);
}
/* Add file to file list */
void append(struct xfile *f,char *stuff)
{
struct xfile *tmp=NULL;
/* If current node is already empty, store it here */
if(f->name[0]=='\0'){
strcpy(f->name,stuff);
return;
}
/* If the inserting node should precede current node */
if(strcmp(f->name,stuff)>0){
/* Insert it here */
tmp=f->next;
f->next=(struct xfile*)malloc(sizeof(struct xfile));
f->next->next=tmp;
strcpy(f->next->name,f->name);
strcpy(f->name,stuff);
return;
}
if(f->next==NULL){
f->next=(struct xfile*)malloc(sizeof(struct xfile));
f->next->next=NULL;
f->next->name[0]='\0';
}
append(f->next,stuff);
}
/* Print everything */
void print_stuff(struct xfile *f)
{
if(f->name[0]=='\0'){
return;
}
do_indents();
puts(f->name);
if(f->next){
print_stuff(f->next);
}
}
/* NOTE: This frees the input argument as well */
void destroy(struct xfile *f)
{
if(f->next){
destroy(f->next);
}
free(f);
}
int handle_dir(int root)
{
int ret=0;
struct xfile *files=(struct xfile*)malloc(sizeof(struct xfile));
files->name[0]='\0';
files->next=NULL;
indent++;
scan: scanf("%s",buf);
if(buf[0]==XEND){
destroy(files);
exit(0);
}
if(root){
printf("DATA SET %d:\n",++set);
puts("ROOT");
root=0;
}
switch(buf[0]){
case XFIN:
ret=1;
goto out;
case XCLOSE:
goto out;
case XDIR:
do_indents();
puts(buf);
if(handle_dir(0)){
ret=1;
goto out;
}
break;
case XFILE:
append(files,buf);
break;
}
goto scan;
out: indent--;
print_stuff(files);
destroy(files);
return ret;
}
int main(void)
{
loop:
handle_dir(1);
putchar('\n');
goto loop;
return 0;
}