1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/dir.h>
4 #include <sys/stat.h>
5 #include <string.h>
6 #include <stream.h>
7
8 class mapping
9 {
10 private:
11 char *s[10];
12 int oc;
13 public:
mapping(char * sent)14 mapping(char *sent )
15 { strcpy(s[0] = new char[strlen(sent)+1],sent) ; s[1]=NULL ; oc = 1 ; }
~mapping()16 ~mapping() { for(int t=0;s[t];t++) delete(s[t]); }
17 int cons_check(char *);
print()18 void print()
19 {
20 cout << oc << ' ' ;
21 for(int t=0; s[t] ; t++) cout << s[t];
22 cout << '\n' ;
23 }
24 };
25
26 class maps
27 {
28 private:
29 mapping maptab[1000];
30 public:
31 }
32
33 class files
34 {
35 private:
36 char *names[1024];
37 int index;
38 int match();
39 char *matchrules[100];
40 public:
41 files(int *,int *);
42 int register(char *);
43 int (*err_ch)();
44 };
45
46 class file_handler
47 {
48 private:
49 DIR *topdir;
50 maps *mapings;
51 files *mapfiles;
52 files *datafiles;
53 files *xrasfiles
54 files *progfiles;
55 public:
56 file_handler();
57 int init_files(char *);
58 int load_mapfile();
59 };
60
files(char * rules,int (* err_ch_f)())61 files::files(char *rules,int (*err_ch_f)())
62 {
63 char *s1,*s2;
64 int t = 0;
65 err_ch = err_ch_f;
66 index = 0;
67 names[0] = NULL;
68 strcpy((temp= new char[strlen(rules)+1]),rules);
69 for(;;)
70 {
71 for(s1=s2=temp;*s2!='|' && *s2;s2++);
72 if (*s2) break;
73 s2=0;
74 matchrules[t]=s1;
75 }
76 }
77
78 files::register(char *filename)
79 {
80 if (*match)(*filname,
81 }
82
83 file_handler::file_handler()
84 {
85 mapfile = new files;
86 mapfiles = new files;
87 datafiles = new files;
88 xrasfiles = new files;
89 progfiles = new files;
90 for(int t=0;t<1024;maptab[t++]=NULL);
91 }
92
93 int match_last(char *expr,char *file_name)
94 {
95 int a,b;
96 for(a=strlen(expr),b=strlen(file_name);a,b,expr[a]==file_name[b];a--,b--);
97 if ((a==-1) ||( b==-1)) return(0);
98 return(1);
99 }
100
101 int files::init_files(char *path)
102 {
103 int tmp = find_files(path,"mapping",strcmp,mapfile_n);
104
105 if (tmp != 1)
106 {
107 cout << "Found " << tmp << " occurenses of file mapping\n";
108 return(1);
109 }
110 mapfiles_index = 0;
111 mapfiles_n[find_files(path,".mapping",match_last,mapfiles_n)] = NULL;
112
113 datafiles_index = 0;
114 datafiles_n[find_files(path,".data",match_last,datafiles_n)] = NULL;
115
116 xrasfiles_index = 0;
117 xrasfiles_n[find_files(path,".xras",match_last,xrasfiles_n)] = NULL;
118
119 progfiles_index = 0;
120 tmp = 0;
121 tmp += find_files(path,".c" ,match_last,&xrasfiles_n[tmp]);
122 tmp += find_files(path,".cc",match_last,&xrasfiles_n[tmp]);
123 tmp += find_files(path,".h" ,match_last,&xrasfiles_n[tmp]);
124 xrasfiles_n[tmp]=NULL;
125 return(0);
126 }
127
128 files::load_mapfile()
129 {
130 int t;
131
132 fopen(mapfile_n[1024]
133 for(t=0;t<1024;t++) maptab[t]=NULL;
134 while(buffer = fgets(buffer,199,stdin))
135 if ( (*buffer != '!') && (s1=strchr(buffer,'"')) && (s2=strchr(s1+1,'"')))
136 {
137 *(++s2) = 0;
138 if ((t=mapnumber(s1)) != -1)
139 {
140 if (!maptab[t]) maptab[t] = new mapping(s1);
141 else maptab[t]->cons_check(s1);
142 }
143 }
144 for(t=0;t<1000;t++) if (maptab[t]) maptab[t]->print();
145 }
146
147 int mapping::cons_check(char *sent)
148 {
149 int t;
150 oc++;
151 for(t=0;s[t];t++) if (!strcmp(s[t],sent)) return(0);
152 strcpy(s[t]=new char[strlen(sent)+1],sent);
153 s[t+1] = NULL;
154 return(1);
155 }
156
157 int mapnumber(char *s)
158 {
159 int retur;
160
161 if (*s != '"') return(-1);
162 s++;
163 if ((*s!='#')||(s[1]<'0')||(s[1]>'9')||(s[2]<'0')||(s[2]>'9')
164 ||(s[3]<'0')||(s[3]>'9')||(s[4]!='_')) return(-1);
165 sscanf(&s[1],"%d",&retur);
166 return(retur);
167 }
168
169 int find_files(char *dirname,char *match,int (*func)(char *,char *),char **files)
170 {
171 DIR *dir;
172 char *dirs[100],temp[500];
173 struct direct *dp;
174 struct stat *buf = new struct stat;
175 int t=0,f=0;
176
177 if (!(dir = opendir(dirname))) return(0);
178
179 for (dp=readdir(dir);dp;dp=readdir(dir))
180 if ( strcmp(dp->gd_name,".") && strcmp(dp->gd_name,"..") )
181 {
182 sprintf(temp,"%s/%s",dirname,dp->gd_name);
183 lstat(temp,buf);
184 if (buf->st_mode & S_IFDIR)
185 strcpy(dirs[t++] = new char[strlen(temp)+1],temp);
186 else if ((buf->st_mode & S_IFREG) && (! (*func)(match,dp->gd_name)))
187 {
188 strcpy(files[f++] = new char[strlen(temp)+1],temp);
189 cout << "File " << files[f-1] << " matches " << match << "\n";
190 }
191 }
192 closedir(dir);
193
194 for(;t;t--)
195 {
196 f+=find_files(dirs[t-1],match,func,&files[f]);
197 delete(dirs[t-1]);
198 }
199 return(f);
200 }
201
202 int main(int argc,char **argv)
203 {
204 int t;
205 char *s1,*s2,*buffer = new char[200];
206 class mapping *maptab[1000];
207 files int_files;
208
209 if ( ((argc!=1)&&(int_files.init_files(argv[1])))
210 && (int_files.init_files(".")) )
211 {
212 printf("Error finding files\n");
213 return(0);
214 }
215 return(0);
216 }
217