Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27905ee3e0fadcfa2a39fd3b67710dfac2849a97
[simgrid.git] / tools / tesh2 / src / fstreams.c
1 #include <fstreams.h>
2 #include <excludes.h>
3 #include <fstream.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
6
7 fstreams_t
8 fstreams_new(int capacity, fn_finalize_t fn_finalize)
9 {
10         fstreams_t fstreams;
11         fstreams = xbt_new0(s_fstreams_t, 1);
12         
13         if(!(fstreams->items = vector_new(capacity, fn_finalize)))
14         {
15                 free(fstreams);
16                 return NULL;
17         }
18         
19         return fstreams;
20 }
21
22 int
23 fstreams_exclude(fstreams_t fstreams, excludes_t excludes)
24 {
25         vector_t to_erase;
26         fstream_t fstream;
27         
28         if(!fstreams || !excludes)
29                 return EINVAL;
30                 
31         if(excludes_is_empty(excludes))
32                 return 0;
33         
34         if(!(to_erase = vector_new(8, NULL)))
35                 return errno;
36         
37         INFO0("excluding file streams");
38         
39         /* collecte the file streams to exclude */
40         vector_rewind(fstreams->items);
41         
42         while((fstream = vector_get(fstreams->items)))
43         {
44                 if(excludes_contains(excludes, fstream))
45                         vector_push_back(to_erase, fstream);
46                 
47                 
48                 vector_move_next(fstreams->items);
49         }
50         
51         if(!vector_is_empty(to_erase))
52         {
53         
54                 /* erase the file streams to exclude from the vector of file streams to run */
55                 vector_rewind(to_erase);
56                 
57                 while((fstream = vector_get(to_erase)))
58                 {
59                         vector_erase(fstreams->items, fstream);
60                         
61                         vector_move_next(to_erase);
62                 }
63         }
64         
65         return vector_free(&to_erase);
66 }
67
68 int 
69 fstreams_contains(fstreams_t fstreams, fstream_t fstream)
70 {
71         register fstream_t cur;
72         
73         if(!fstreams || !fstream)
74         {
75                 errno = EINVAL;
76                 return 0;
77         }
78         
79         vector_rewind(fstreams->items);
80         
81         while((cur = vector_get(fstreams->items)))
82         {
83                 if(!strcmp(cur->name, fstream->name) && !strcmp(cur->directory, fstream->directory))
84                         return 1;
85                 
86                 vector_move_next(fstreams->items);
87         }
88         
89         return 0;
90 }
91
92 int
93 fstreams_load(fstreams_t fstreams)
94 {
95         register fstream_t fstream;
96         const char* directory = NULL;
97         
98         
99         if(!fstreams )
100                 return EINVAL;
101         
102         vector_rewind(fstreams->items);
103         
104         while((fstream = vector_get(fstreams->items)))
105         {
106                 chdir(root_directory->name);
107                 
108                 if(!directory || strcmp(directory, fstream->directory))
109                 {
110                         directory = fstream->directory; 
111                         
112                         if(!dont_want_display_directory)
113                                 INFO1("entering directory \"%s\"",directory);
114                                 
115                 }
116                 
117                 chdir(fstream->directory);
118                 
119                 fstream_open(fstream);
120                 
121                 vector_move_next(fstreams->items);
122         }
123         
124         return 0;
125 }
126
127 int
128 fstreams_add(fstreams_t fstreams, fstream_t fstream)
129 {
130         if(!fstreams)
131                 return EINVAL;
132         
133         if(vector_push_back(fstreams->items, fstream))
134                 return errno;
135         
136         return 0;
137         
138 }
139
140 int
141 fstreams_free(void** fstreamsptr)
142 {
143         if(!(* fstreamsptr))
144                 return EINVAL;
145                 
146         
147         if((errno = vector_free(&((*((fstreams_t*)fstreamsptr))->items))))
148                 return errno;
149                 
150         free(*fstreamsptr);
151         
152         *fstreamsptr = NULL;
153         return 0;
154 }
155
156 int
157 fstreams_get_size(fstreams_t fstreams)
158 {
159         if(!fstreams)
160         {
161                 errno = EINVAL;
162                 return -1;
163         }
164         
165         return vector_get_size(fstreams->items);
166 }
167
168 int
169 fstreams_is_empty(fstreams_t fstreams)
170 {
171         if(!fstreams)
172         {
173                 errno = EINVAL;
174                 return -1;
175         }
176         
177         return vector_is_empty(fstreams->items);
178 }