Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some new files used to check Tesh
[simgrid.git] / tools / tesh2 / src / directory.c
1 #include <directory.h>\r
2 #include <fstreams.h>\r
3 #include <fstream.h>\r
4 \r
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
6 \r
7 directory_t\r
8 directory_new(const char* name)\r
9 {\r
10         directory_t directory;\r
11         \r
12         if(!name)\r
13         {\r
14                 errno = EINVAL;\r
15                 return NULL;\r
16         }\r
17         \r
18         directory = xbt_new0(s_directory_t, 1);\r
19         \r
20         \r
21         directory->name = strdup(name); \r
22         \r
23                 \r
24         directory->stream = NULL;\r
25         \r
26         return directory;\r
27 }\r
28 \r
29 int\r
30 directory_open(directory_t directory)\r
31 {\r
32         if(!directory || directory->stream)\r
33                 return EINVAL;\r
34                 \r
35         if(!(directory->stream = opendir(directory->name)))\r
36                 return errno;\r
37         \r
38         return 0;       \r
39 }\r
40 \r
41 \r
42 int\r
43 directory_close(directory_t directory)\r
44 {\r
45         if(!directory)\r
46                 return EINVAL;\r
47                 \r
48         if(!directory->stream)\r
49                 return EBADF;\r
50                 \r
51         if(closedir(directory->stream))\r
52                 return errno;\r
53                 \r
54         directory->stream = NULL;\r
55         return 0;\r
56 }\r
57 \r
58 int\r
59 directory_load(directory_t directory, fstreams_t fstreams, xbt_dynar_t suffixes)\r
60 {\r
61         struct dirent* entry ={0};\r
62         s_fstream_t sfstream = {0};\r
63         char* suffix;\r
64         unsigned int i;\r
65         int has_valid_suffix;\r
66         int is_empty = 1;\r
67         int rv;\r
68         \r
69         if(!directory || !fstreams)\r
70                 return EINVAL;\r
71                 \r
72         if(!directory->stream)\r
73                 return EBADF;\r
74                 \r
75         sfstream.directory = strdup(directory->name);\r
76                 \r
77         while((entry = readdir(directory->stream)))\r
78         {\r
79                 has_valid_suffix = 0;\r
80                 \r
81                 xbt_dynar_foreach(suffixes, i, suffix)\r
82                 {\r
83                         if(!strncmp(suffix, entry->d_name + (strlen(entry->d_name) - strlen(suffix)), strlen(suffix)))\r
84                         {\r
85                                 has_valid_suffix = 1;\r
86                                 break;\r
87                         }\r
88                 }\r
89                 \r
90                 if(!has_valid_suffix)\r
91                         continue;\r
92                         \r
93                 sfstream.name = strdup(entry->d_name);\r
94                 \r
95                 /* check first if the file stream is already in the file streams to run */\r
96                 if(fstreams_contains(fstreams, &sfstream))\r
97                 {\r
98                         WARN1("file %s already registred", entry->d_name);\r
99                         free(sfstream.name);\r
100                         continue;\r
101                 }\r
102                 \r
103                 /* add the fstream to the list of file streams to run */\r
104                 if((rv = fstreams_add(fstreams, fstream_new(directory->name, entry->d_name))))\r
105                 {\r
106                         free(sfstream.directory);\r
107                         free(sfstream.name);\r
108                         return rv;\r
109                 }\r
110                         \r
111                 is_empty = 0;\r
112                 free(sfstream.name);\r
113         }\r
114         \r
115         if(is_empty)\r
116                 WARN1("no tesh file found in the directory %s", directory->name);       \r
117                 \r
118         free(sfstream.directory);\r
119         \r
120                         \r
121         return 0;       \r
122 }\r
123 \r
124 int\r
125 directory_free(void** directoryptr)\r
126 {\r
127         directory_t directory;\r
128         \r
129         if(!(*directoryptr))\r
130                 return EINVAL;\r
131                 \r
132         directory = *((directory_t*)directoryptr);\r
133         \r
134         if(directory->stream)\r
135                 if(directory_close(directory))\r
136                         return errno;\r
137         \r
138         if(directory->name)\r
139                 free(directory->name);\r
140         \r
141         free(*directoryptr);\r
142         *directoryptr  = NULL;\r
143         \r
144         return 0;       \r
145 }\r
146 \r
147 const char*\r
148 directory_get_name(directory_t directory)\r
149 {\r
150         if(!directory)\r
151         {\r
152                 errno = EINVAL;\r
153                 return NULL;\r
154         }\r
155         \r
156         return (const char*)directory->name;\r
157 }\r