File: EStringList.h

package info (click to toggle)
miwm 1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,296 kB
  • ctags: 910
  • sloc: cpp: 8,179; sh: 231; makefile: 148
file content (127 lines) | stat: -rw-r--r-- 4,709 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// EStringList.h
#ifndef ESTRINGLIST_H
#define ESTRINGLIST_H
#include <iostream>
#include <string>
#include <list>
/**
License: See LICENSE file in this directory.
Author: stephan@wanderinghorse.net
*/

/**
This class represents a simple list of std::string objects.  It is a
proxy for a StringList object and only provides a few commonly-used
methods. It does not subclass list<string> because that class has no
virtual destructor, which means undefined behaviour when deleting a
pointer of EStringLast. :/ See the getList() member function for how
to get the underlying list.
*/
using std::string;
using std::list;

class EStringList
{
public:

        typedef std::list<std::string> StringList;
        typedef StringList::iterator StringListIterator;
        typedef StringList::const_iterator StringListConstIterator;

        typedef StringListIterator iterator;
        typedef StringListConstIterator const_iterator;

        // <<'s el.toString() to os.
        friend std::ostream & operator << (std::ostream &os, const EStringList &el);

        // adds el.toString() to string os.
        friend std::string & operator << (std::string &os, const EStringList &el);

        // Adds str to the end of the list.
        friend EStringList & operator += (EStringList &el, const std::string &str);


        EStringList();
        explicit EStringList( const StringList & );
//         EStringList( const EStringList & );
        virtual ~EStringList() {};

        EStringList::iterator begin() { return this->m_list.begin(); };
        EStringList::const_iterator begin() const  { return this->m_list.begin(); };
        EStringList::iterator end() { return this->m_list.end(); };
        EStringList::const_iterator end() const  { return this->m_list.end(); };

        EStringList::StringList & getList() /* const */;

        /**
           Sets the internal StringList to a copy of list.
        */
        void setList( const EStringList::StringList &list );

        /**
           Works similarly to the shell function of the same name.
           Returns the last popped-off argument.
         */
        std::string shift( unsigned int places = 1 );

        /**
           Returns the number of items in the list.
        */
        int count() const;

        static std::string join( const EStringList &strlist, const std::string &separator );
        /**
           converts strlist to an EStringList and returns the same as join( EStringList ... )
           Note that strlist is not const (design flaw), because it is passed to setStringList() of a temp object. :/
         */
        static std::string join( const StringList &strlist, const std::string &separator );

        /**
           Splits seq into a list of elements, separating at token 'separator'. It eliminates "empty"
           entries. e.g., split( "foo:bar:::foobar", ":" ) will return a list containing (foo,bar,foobar),
           without blank entries.
           A toggle, adding the ability to return empty elements, may be added later (i.e., when it's needed).
         */
        static EStringList split( const std::string &seq, const std::string &separator );

        /**
           tokenize() is subtely different from split(): it uses 
           EStdStringTokenizer::tokenize(), which will give results which are
           useful in contexts like parsing command-line arguments (or
           maybe elib::EReadline input).

           Bug: tokenize hoses any quotes in seq :/
         */
        static EStringList tokenize( const std::string &seq, const std::string &separators = string(" ") );

        static EStringList * loadFromFile( const std::string &fn );
        /**
           fn: filename.
           skipComments: lines starting with ';' or '#' are not included.
           skipEmptyLines: empty lines are not included.
           Returns false if it can't read the file, else true.
         */
        bool load( const std::string &fn, bool skipComments = true, bool skipEmptyLines = true );

        std::string randomEntry( bool removeIt = false );

        void clear() { this->m_list.clear(); };

        /**
           Useful if you want to call STL algorithms.
         */
        EStringList::StringList & list() { return this->m_list; };
  /**
  * Makes a std::string of this object, with each entry separated by separator.

  If quoteSpecialTokens is set to true, then any lines in this list
  which "look like they should be quoted" have double-quotes added to
  the front and back.
  */
    virtual std::string toString( const std::string &separator = "\n", bool quoteSpecialTokens = false ) const;

private:
        EStringList::StringList m_list;

};
#endif // ESTRINGLIST_H