Logo AND Algorithmique Numérique Distribuée

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