glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / transport / transport-testing-filenames.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15 /**
16  * @file transport-testing-filenames.c
17  * @brief convenience string manipulation functions for tests
18  * @author Matthias Wachs
19  * @author Christian Grothoff
20  */
21 #include "transport-testing.h"
22
23
24 /**
25  * Removes all directory separators from absolute filename
26  *
27  * @param file the absolute file name, e.g. as found in argv[0]
28  * @return extracted file name, has to be freed by caller
29  */
30 static char *
31 extract_filename (const char *file)
32 {
33   char *pch = GNUNET_strdup (file);
34   char *backup = pch;
35   char *filename = NULL;
36   char *res;
37
38 #if WINDOWS
39   if ((strlen (pch) >= 3) && pch[1] == ':')
40   {
41     if (NULL != strstr (pch, "\\"))
42     {
43       pch = strtok (pch, "\\");
44       while (pch != NULL)
45       {
46         pch = strtok (NULL, "\\");
47         if (pch != NULL)
48           filename = pch;
49       }
50     }
51   }
52   if (filename != NULL)
53     pch = filename; /* If we miss the next condition, filename = pch will
54                      * not harm us.
55                      */
56 #endif
57   if (NULL != strstr (pch, "/"))
58   {
59     pch = strtok (pch, "/");
60     while (pch != NULL)
61     {
62       pch = strtok (NULL, "/");
63       if (pch != NULL)
64       {
65         filename = pch;
66       }
67     }
68   }
69   else
70     filename = pch;
71
72   res = GNUNET_strdup (filename);
73   GNUNET_free (backup);
74   return res;
75 }
76
77
78 /**
79  * Extracts the test filename from an absolute file name and removes
80  * the extension
81  *
82  * @param file absolute file name
83  * @return the result
84  */
85 char *
86 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file)
87 {
88   char *backup = extract_filename (file);
89   char *filename = backup;
90   char *dotexe;
91   char *ret;
92
93   if (NULL == filename)
94     return NULL;
95
96   /* remove "lt-" */
97   filename = strstr (filename, "test");
98   if (NULL == filename)
99   {
100     GNUNET_free (backup);
101     return NULL;
102   }
103
104   /* remove ".exe" */
105   if (NULL != (dotexe = strstr (filename, ".exe")))
106     dotexe[0] = '\0';
107   ret = GNUNET_strdup (filename);
108   GNUNET_free (backup);
109   return ret;
110 }
111
112
113 /**
114  * Extracts the filename from an absolute file name and removes the extension
115  *
116  * @param file absolute file name
117  * @return the result
118  */
119 char *
120 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file)
121 {
122   char *src = extract_filename (file);
123   char *split;
124
125   split = strstr (src, ".");
126   if (NULL != split)
127     split[0] = '\0';
128   return src;
129 }
130
131
132 /**
133  * Extracts the plugin name from an absolute file name and the test name
134  *
135  * @param file absolute file name
136  * @param test test name
137  * @return the result
138  */
139 char *
140 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
141                                                const char *test)
142 {
143   char *filename;
144   char *dotexe;
145   char *e = extract_filename (file);
146   char *t = extract_filename (test);
147   char *ret;
148
149   if (NULL == e)
150     goto fail;
151   /* remove "lt-" */
152   filename = strstr (e, "tes");
153   if (NULL == filename)
154     goto fail;
155   /* remove ".exe" */
156   if (NULL != (dotexe = strstr (filename, ".exe")))
157     dotexe[0] = '\0';
158
159   /* find last _ */
160   filename = strstr (filename, t);
161   if (NULL == filename)
162     goto fail;
163   /* copy plugin */
164   filename += strlen (t);
165   if ('\0' != *filename)
166     filename++;
167   ret = GNUNET_strdup (filename);
168   goto suc;
169 fail:
170   ret = NULL;
171 suc:
172   GNUNET_free (t);
173   GNUNET_free (e);
174   return ret;
175 }
176
177
178 /**
179  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
180  * if existing ".exe"-prefix and adds the peer-number
181  *
182  * @param file filename of the test, e.g. argv[0]
183  * @param count peer number
184  * @return the result
185  */
186 char *
187 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
188                                           int count)
189 {
190   char *filename = extract_filename (file);
191   char *backup = filename;
192   char *dotexe;
193   char *ret;
194
195   if (NULL == filename)
196     return NULL;
197   /* remove "lt-" */
198   filename = strstr (filename, "test");
199   if (NULL == filename)
200     goto fail;
201   /* remove ".exe" */
202   if (NULL != (dotexe = strstr (filename, ".exe")))
203     dotexe[0] = '\0';
204   GNUNET_asprintf (&ret,
205                    "%s_peer%u.conf",
206                    filename,
207                    count);
208   GNUNET_free (backup);
209   return ret;
210 fail:
211   GNUNET_free (backup);
212   return NULL;
213 }
214
215
216 /* end of transport-testing-filenames.c */