1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2016 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef STRINGSET_H
21 #define STRINGSET_H
22 
23 #include "xarray.h"
24 
25 class StringSet
26 {
27    xarray_m<char> set;
28 
29    const StringSet &operator=(const StringSet &); // disable assignment
30 
31 public:
32    StringSet();
33    StringSet(const char *const *s,int n);
34    StringSet(const StringSet &o);
35    StringSet(const char *s);
36 
Empty()37    void Empty() { set.truncate(); }
38    void Assign(const char *const *s,int n);
Assign(const char * s)39    void Assign(const char *s) { Assign(&s,1); }
40    bool IsEqual(const char *const *s,int n) const;
IsEqual(const StringSet & o)41    bool IsEqual(const StringSet &o) const { return IsEqual(o.Set(),o.Count()); }
42    void Append(const char *);
43    void AppendFormat(const char *,...) PRINTF_LIKE(2,3);
44    void InsertBefore(int,const char *);
45    void Replace(int,const char *);
46    char *Pop(int i=0);
Remove(int i)47    void Remove(int i) { xfree(Pop(i)); }
48 
Set()49    const char *const *Set() const { return set.get(); }
SetNonConst()50    char **SetNonConst() { return set.get_non_const(); }
Count()51    int Count() const { return set.count(); }
String(int i)52    const char *String(int i) const { return i>=0 && i<Count() ? set[i] : 0; }
LastString()53    const char *LastString() const { return String(Count()-1); }
54    const char *operator[](int i) const { return String(i); }
55 
56    void MoveHere(StringSet &o);
57 
borrow()58    char **borrow() { return set.borrow(); }
59 
qsort(xarray_m<char>::cmp_t cmp)60    void qsort(xarray_m<char>::cmp_t cmp) { set.qsort(cmp); }
default_cmp(const char ** a,const char ** b)61    static int default_cmp(const char **a,const char **b) { return strcmp(*a,*b); }
qsort()62    void qsort() { qsort(default_cmp); }
63 };
64 
65 #endif // STRINGSET_H
66