1/*
2 * Copyright 2011 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _STRING_LIST_H
6#define _STRING_LIST_H
7
8
9#include <BeBuild.h>
10#include <Flattenable.h>
11#include <List.h>
12#include <String.h>
13
14
15class BStringList : public BFlattenable {
16public:
17								BStringList(int32 count = 20);
18								BStringList(const BStringList& other);
19	virtual						~BStringList();
20
21	// Adding and removing items.
22			bool				Add(const BString& string, int32 index);
23			bool				Add(const BString& string);
24			bool				Add(const BStringList& list, int32 index);
25			bool				Add(const BStringList& list);
26
27			bool				Remove(const BString& string,
28									bool ignoreCase = false);
29			bool				Remove(const BStringList& list,
30									bool ignoreCase = false);
31			BString				Remove(int32 index);
32			bool				Remove(int32 index, int32 count);
33			bool				Replace(int32 index, const BString& string);
34
35			void				MakeEmpty();
36
37	// Reorder items
38			void				Sort(bool ignoreCase = false);
39									// TODO: Sort() with custom sort function.
40			bool				Swap(int32 indexA, int32 indexB);
41			bool				Move(int32 fromIndex, int32 toIndex);
42
43	// Retrieve items
44			BString				StringAt(int32 index) const;
45			BString				First() const;
46			BString				Last() const;
47
48	// Query
49			bool				HasString(const BString& string,
50									bool ignoreCase = false) const;
51			int32				IndexOf(const BString& string,
52									bool ignoreCase = false) const;
53			int32				CountStrings() const;
54			bool				IsEmpty() const;
55
56			BString				Join(const char* separator, int32 length = -1)
57									const;
58
59	// Iteration
60			void				DoForEach(bool (*func)(const BString& string));
61			void				DoForEach(bool (*func)(const BString& string,
62									void* arg2), void* arg2);
63
64			BStringList&		operator=(const BStringList& other);
65			bool				operator==(const BStringList& other) const;
66			bool				operator!=(const BStringList& other) const;
67
68	// BFlattenable
69	virtual	bool				IsFixedSize() const;
70	virtual	type_code			TypeCode() const;
71	virtual	bool				AllowsTypeCode(type_code code) const;
72	virtual	ssize_t				FlattenedSize() const;
73	virtual	status_t			Flatten(void* buffer, ssize_t size) const;
74	virtual	status_t			Unflatten(type_code code, const void* buffer,
75									ssize_t size);
76
77private:
78			void				_IncrementRefCounts() const;
79			void				_DecrementRefCounts() const;
80
81			BString				_Join(const char* separator, int32 length)
82									const;
83
84private:
85			BList				fStrings;
86};
87
88
89inline bool
90BStringList::HasString(const BString& string, bool ignoreCase) const
91{
92	return IndexOf(string, ignoreCase) >= 0;
93}
94
95
96inline bool
97BStringList::operator!=(const BStringList& other) const
98{
99	return !(*this == other);
100}
101
102
103#endif	// _STRING_LIST_H
104