-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirEntries.c
75 lines (59 loc) · 1.63 KB
/
DirEntries.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
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include "pcre.h"
#include "DirEntries.h"
/* Check whether the file ends with .wav extension
using PCRE
*/
bool match(char *str) {
if(str == NULL)
return false;
int v [15];
pcre* re;
const char* error;
int erroroffset;
int rc;
char* regex = "(.*?\\.wav)$";
re = pcre_compile(regex, 0, &error, &erroroffset, NULL);
if(! re) {
printf("Error in compiling regex \n");
return false;
}
rc = pcre_exec(re, NULL, str, strlen(str), 0,0,v, 15);
pcre_free(re);
if(rc < 0) {
// printf("Not matched \n");
return false;
}
else {
// printf("Matched %s \n",str);
return true;
}
};
/* Read the user provided Directory and list its contents
only files are used for matching purposes
*/
void ReadDir(char* dirpath, struct node** list) {
DIR* DirHandle;
struct dirent* Entry;
if((DirHandle = opendir(dirpath)) == NULL) {
printf("Directory cannot be opened \n");
exit(0);
}
while((Entry = readdir(DirHandle)) != NULL) {
//printf("Entry is : %s \n", Entry->d_name);
if(Entry->d_type != DT_DIR) {
bool matched;
if((matched = match(Entry->d_name))) {
struct node* toAdd = (struct node*)malloc(sizeof(struct node));
if(toAdd != NULL) {
strcpy(toAdd->filename,Entry->d_name);
// strcpy(toAdd->path, dirpath);
addNode(list, toAdd);
}
}
}
}
closedir(DirHandle);
}