4
5 lstat(FileName.c_str(),&StatBuffer);
6 if((StatBuffer.st_mode & S_IFDIR) == -1)
7 {
8 cout << «could not get stats on file» << endl;
9 return(0);
10 }
11 else{
12 if(StatBuffer.st_mode & S_IFDIR){
13 return(1);
14 }
15 }
16 return(0);
17 }
18
19
20 int isRegular(string FileName)
21 {
22 struct stat StatBuffer;
23
24 lstat(FileName.c_str(),&StatBuffer);
25 if((StatBuffer.st_mode & S_IFDIR) == -1)
26 {
27 cout << «could not get stats on file» << endl;
28 return(0);
29 }
30 else{
31 if(StatBuffer.st_mode & S_IFREG){
32 return(1);
33 }
34 }
35 return(0);
36 }
37
38
39 void depthFirstTraversal(const char *CurrentDir)
40 {
41 DIR *DirP;
42 string Temp;
43 string FileName;
44 struct dirent *EntryP;
45 chdir(CurrentDir);
46 cout << «Searching Directory: " << CurrentDir << endl;
47 DirP = opendir(CurrentDir);
48
49 if(DirP == NULL){
50 cout << «could not open file» << endl;
51 return;
52 }
53 EntryP = readdir(DirP);
54 while(EntryP != NULL)
55 {
56 Temp.erase();
57 FileName.erase();
58 Temp = EntryP->d_name;
59 if((Temp != ".») && (Temp != "..»)){
60 FileName.assign(CurrentDir);
61 FileName.append(1,'/');
62 FileName.append(EntryP->d_name);
63 if(isDirectory(FileName)){
64 string NewDirectory;
65 NewDirectory = FileName;
66 depthFirstTraversal(NewDirectory.c_str());
67 }
68 else{
69 if(isRegular(FileName)){
70 int Flag;
71 Flag = FileName.find(".cpp»);
72 if(Flag > 0){
73 pthread_mutex_lock(&CountMutex);
74 FileCount++;
75 pthread_mutex_unlock(&CountMutex);
76 pthread_mutex_lock(&QueueMutex);
77 TextFiles.push(FileName);
78 pthread_mutex_unlock(&QueueMutex);
79 }
80 }
81 }
82
83 }
84 EntryP = readdir(DirP);
85 }
86 closedir(DirP);
87 }
88
89
90
91 void *task(void *X)
92 {
93 char *Directory;
94 Directory = static_cast
95 depthFirstTraversal(Directory);
96 return(NULL);
97
98 }
Программа 4.6 содержит поток-«потребитель», который выполняет ных ключевых слов.
// Программа 4.6
1 void *keySearch(void *X)
2 {
3 string Temp, Filename;
4 less
5
6 while(!Keyfile.eof() && Keyfile.good())
7 {
8 Keyfile >> Temp;
9 if(!Keyfile.eof()){
10 KeyWords.insert(Temp);
11 }
12 }