Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
510121ae640aaab0c1835d1ec8403355ccff9eba
[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         
12         if(!(fstreams = (fstreams_t) calloc(1, sizeof(s_fstreams_t))))
13                 return NULL;
14         
15         if(!(fstreams->items = vector_new(capacity, fn_finalize)))
16         {
17                 free(fstreams);
18                 return NULL;
19         }
20         
21         return fstreams;
22 }
23
24 int
25 fstreams_exclude(fstreams_t fstreams, excludes_t excludes)
26 {
27         vector_t to_erase;
28         fstream_t fstream;
29         
30         if(!fstreams || !excludes)
31                 return EINVAL;
32                 
33         if(excludes_is_empty(excludes))
34                 return 0;
35         
36         if(!(to_erase = vector_new(8, NULL)))
37                 return errno;
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         
97         if(!fstreams )
98                 return EINVAL;
99         
100         vector_rewind(fstreams->items);
101         
102         while((fstream = vector_get(fstreams->items)))
103         {
104                 fstream_open(fstream);
105                 vector_move_next(fstreams->items);
106         }
107         
108         
109         return 0;
110 }
111
112 int
113 fstreams_add(fstreams_t fstreams, fstream_t fstream)
114 {
115         if(!fstreams)
116                 return EINVAL;
117         
118         if(vector_push_back(fstreams->items, fstream))
119                 return errno;
120         
121         return 0;
122         
123 }
124
125 int
126 fstreams_free(void** fstreamsptr)
127 {
128         int rv;
129         
130         if(!(* fstreamsptr))
131                 return EINVAL;
132         
133         if(EAGAIN != (rv = vector_free(&((*((fstreams_t*)fstreamsptr))->items))))
134                 return rv;
135                 
136         free(*fstreamsptr);
137         
138         *fstreamsptr = NULL;
139         return 0;
140 }
141
142 int
143 fstreams_get_size(fstreams_t fstreams)
144 {
145         if(!fstreams)
146         {
147                 errno = EINVAL;
148                 return -1;
149         }
150         
151         return vector_get_size(fstreams->items);
152 }
153
154 int
155 fstreams_is_empty(fstreams_t fstreams)
156 {
157         if(!fstreams)
158         {
159                 errno = EINVAL;
160                 return -1;
161         }
162         
163         return vector_is_empty(fstreams->items);
164 }