Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent the rest of the code (examples, buildtools, doc...) except for examples/SMPI...
[simgrid.git] / tools / tesh2 / src / directory.c
1 #include <directory.h>\r
2 #include <fstreams.h>\r
3 #include <fstream.h>\r
4 \r XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
5 \r\rdirectory_t \r directory_new(const char *name) \r
6 {
7   \rdirectory_t directory;
8   \r\rif (!name)
9     \r {
10     \rerrno = EINVAL;
11     \rreturn NULL;
12     \r}
13   \r\rdirectory = xbt_new0(s_directory_t, 1);
14   \r\r\rdirectory->name = strdup(name);
15   \r\r\rdirectory->stream = NULL;
16   \r\rreturn directory;
17 \r}
18
19 \r\rint \r directory_open(directory_t directory) \r
20 {
21   \rif (!directory || directory->stream)
22     \rreturn EINVAL;
23   \r\rif (!(directory->stream = opendir(directory->name)))
24     \rreturn errno;
25   \r\rreturn 0;
26 \r}
27
28 \r\r\rint \r directory_close(directory_t directory) \r
29 {
30   \rif (!directory)
31     \rreturn EINVAL;
32   \r\rif (!directory->stream)
33     \rreturn EBADF;
34   \r\rif (closedir(directory->stream))
35     \rreturn errno;
36   \r\rdirectory->stream = NULL;
37   \rreturn 0;
38 \r}
39
40 \r\rint \r
41 directory_load(directory_t directory, fstreams_t fstreams,
42                xbt_dynar_t suffixes) \r
43 {
44   \rstruct dirent *entry = { 0 };
45   \rs_fstream_t sfstream = {
46   0};
47   \rchar *suffix;
48   \runsigned int i;
49   \rint has_valid_suffix;
50   \rint is_empty = 1;
51   \rint rv;
52   \r\rif (!directory || !fstreams)
53     \rreturn EINVAL;
54   \r\rif (!directory->stream)
55     \rreturn EBADF;
56   \r\rsfstream.directory = strdup(directory->name);
57   \r\rwhile ((entry = readdir(directory->stream)))
58     \r {
59     \rhas_valid_suffix = 0;
60     \r\rxbt_dynar_foreach(suffixes, i, suffix) \r {
61       \rif (!strncmp
62            (suffix,
63             entry->d_name + (strlen(entry->d_name) - strlen(suffix)),
64             strlen(suffix)))
65         \r {
66         \rhas_valid_suffix = 1;
67         \rbreak;
68         \r}
69     \r}
70     \r\rif (!has_valid_suffix)
71       \rcontinue;
72     \r\rsfstream.name = strdup(entry->d_name);
73     \r\r
74         /* check first if the file stream is already in the file streams to run */ \r
75         if (fstreams_contains(fstreams, &sfstream))
76       \r {
77       \rWARN1("file %s already registred", entry->d_name);
78       \rfree(sfstream.name);
79       \rcontinue;
80       \r}
81     \r\r
82         /* add the fstream to the list of file streams to run */ \r
83         if ((rv =
84              fstreams_add(fstreams,
85                           fstream_new(directory->name, entry->d_name))))
86       \r {
87       \rfree(sfstream.directory);
88       \rfree(sfstream.name);
89       \rreturn rv;
90       \r}
91     \r\ris_empty = 0;
92     \rfree(sfstream.name);
93     \r}
94   \r\rif (is_empty)
95     \rWARN1("no tesh file found in the directory %s", directory->name);
96   \r\rfree(sfstream.directory);
97   \r\r\rreturn 0;
98 \r}
99
100 \r\rint \r directory_free(void **directoryptr) \r
101 {
102   \rdirectory_t directory;
103   \r\rif (!(*directoryptr))
104     \rreturn EINVAL;
105   \r\rdirectory = *((directory_t *) directoryptr);
106   \r\rif (directory->stream)
107     \rif (directory_close(directory))
108       \rreturn errno;
109   \r\rif (directory->name)
110     \rfree(directory->name);
111   \r\rfree(*directoryptr);
112   \r*directoryptr = NULL;
113   \r\rreturn 0;
114 \r}
115
116 \r\rconst char *\r directory_get_name(directory_t directory) \r
117 {
118   \rif (!directory)
119     \r {
120     \rerrno = EINVAL;
121     \rreturn NULL;
122     \r}
123   \r\rreturn (const char *) directory->name;
124 \r\r