static void
ftpTraverseDirectory(Ftp::Gateway * ftpState)
{
- wordlist *w;
debugs(9, 4, HERE << (ftpState->filepath ? ftpState->filepath : "<NULL>"));
safe_free(ftpState->dirpath);
}
/* Go to next path component */
- w = ftpState->pathcomps;
-
- ftpState->filepath = w->key;
-
- ftpState->pathcomps = w->next;
-
- delete w;
+ ftpState->filepath = wordlistChopHead(& ftpState->pathcomps);
/* Check if we are to CWD or RETR */
if (ftpState->pathcomps != NULL || ftpState->flags.isdir) {
const char *wordlistAdd(wordlist **, const char *) STUB_RETVAL(NULL)
void wordlistAddWl(wordlist **, wordlist *) STUB
void wordlistJoin(wordlist **, wordlist **) STUB
-wordlist *wordlistDup(const wordlist *) STUB_RETVAL(NULL)
void wordlistDestroy(wordlist **) STUB
while (*list)
list = &(*list)->next;
- *list = new wordlist;
-
- (*list)->key = xstrdup(key);
-
- (*list)->next = NULL;
-
+ *list = new wordlist(key);
return (*list)->key;
}
list = &(*list)->next;
for (; wl; wl = wl->next, list = &(*list)->next) {
- *list = new wordlist();
- (*list)->key = xstrdup(wl->key);
- (*list)->next = NULL;
+ *list = new wordlist(wl->key);
}
}
}
}
-wordlist *
-wordlistDup(const wordlist * w)
-{
- wordlist *D = NULL;
-
- while (NULL != w) {
- wordlistAdd(&D, w->key);
- w = w->next;
- }
-
- return D;
-}
-
SBufList
ToSBufList(wordlist *wl)
{
return rv;
}
+char *
+wordlistChopHead(wordlist **wl)
+{
+ if (*wl == nullptr)
+ return nullptr;
+
+ wordlist *w = *wl;
+ char *rv = w->key;
+ *wl = w->next;
+ delete w;
+ return rv;
+}
class wordlist
{
MEMPROXY_CLASS(wordlist);
+ friend void wordlistDestroy(wordlist ** list);
+ friend char *wordlistChopHead(wordlist **);
public:
+ wordlist() : key(nullptr), next(nullptr) {}
+ // create a new wordlist node, with a copy of k as key
+ explicit wordlist(const char *k) : key(xstrdup(k)), next(nullptr) {}
+
+ wordlist(const wordlist &) = delete;
+ wordlist &operator=(const wordlist &) = delete;
+
char *key;
wordlist *next;
+
+private:
+ // use wordlistDestroy instead
+ ~wordlist() = default;
};
class MemBuf;
*/
void wordlistJoin(wordlist **, wordlist **);
-/// duplicate a wordlist
-wordlist *wordlistDup(const wordlist *);
-
/// destroy a wordlist
void wordlistDestroy(wordlist **);
+/** remove the first element in a wordlist, and return its key
+ *
+ * \note the returned key must be freed by the caller using safe_free
+ * \note wl is altered so that it points to the second element
+ * \return nullptr if pointed-to wordlist is nullptr.
+ */
+char *wordlistChopHead(wordlist **);
+
/// convert a wordlist to a SBufList
SBufList ToSBufList(wordlist *);