The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_STRINGARRAY_JUCEHEADER__
  19. #define __JUCE_STRINGARRAY_JUCEHEADER__
  20. #include "juce_String.h"
  21. #include "../containers/juce_Array.h"
  22. //==============================================================================
  23. /**
  24. A special array for holding a list of strings.
  25. @see String, StringPairArray
  26. */
  27. class JUCE_API StringArray
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty string array */
  32. StringArray() throw();
  33. /** Creates a copy of another string array */
  34. StringArray (const StringArray& other);
  35. /** Creates a copy of an array of string literals.
  36. @param strings an array of strings to add. Null pointers in the array will be
  37. treated as empty strings
  38. @param numberOfStrings how many items there are in the array
  39. */
  40. StringArray (const juce_wchar** strings, int numberOfStrings);
  41. /** Creates a copy of an array of string literals.
  42. @param strings an array of strings to add. Null pointers in the array will be
  43. treated as empty strings
  44. @param numberOfStrings how many items there are in the array
  45. */
  46. StringArray (const char** strings, int numberOfStrings);
  47. /** Creates a copy of a null-terminated array of string literals.
  48. Each item from the array passed-in is added, until it encounters a null pointer,
  49. at which point it stops.
  50. */
  51. explicit StringArray (const juce_wchar** strings);
  52. /** Creates a copy of a null-terminated array of string literals.
  53. Each item from the array passed-in is added, until it encounters a null pointer,
  54. at which point it stops.
  55. */
  56. explicit StringArray (const char** strings);
  57. /** Destructor. */
  58. ~StringArray();
  59. /** Copies the contents of another string array into this one */
  60. StringArray& operator= (const StringArray& other);
  61. //==============================================================================
  62. /** Compares two arrays.
  63. Comparisons are case-sensitive.
  64. @returns true only if the other array contains exactly the same strings in the same order
  65. */
  66. bool operator== (const StringArray& other) const;
  67. /** Compares two arrays.
  68. Comparisons are case-sensitive.
  69. @returns false if the other array contains exactly the same strings in the same order
  70. */
  71. bool operator!= (const StringArray& other) const;
  72. //==============================================================================
  73. /** Returns the number of strings in the array */
  74. inline int size() const throw() { return strings.size(); };
  75. /** Returns one of the strings from the array.
  76. If the index is out-of-range, an empty string is returned.
  77. Obviously the reference returned shouldn't be stored for later use, as the
  78. string it refers to may disappear when the array changes.
  79. */
  80. const String& operator[] (int index) const throw();
  81. /** Searches for a string in the array.
  82. The comparison will be case-insensitive if the ignoreCase parameter is true.
  83. @returns true if the string is found inside the array
  84. */
  85. bool contains (const String& stringToLookFor,
  86. bool ignoreCase = false) const;
  87. /** Searches for a string in the array.
  88. The comparison will be case-insensitive if the ignoreCase parameter is true.
  89. @param stringToLookFor the string to try to find
  90. @param ignoreCase whether the comparison should be case-insensitive
  91. @param startIndex the first index to start searching from
  92. @returns the index of the first occurrence of the string in this array,
  93. or -1 if it isn't found.
  94. */
  95. int indexOf (const String& stringToLookFor,
  96. bool ignoreCase = false,
  97. int startIndex = 0) const;
  98. //==============================================================================
  99. /** Appends a string at the end of the array. */
  100. void add (const String& stringToAdd);
  101. /** Inserts a string into the array.
  102. This will insert a string into the array at the given index, moving
  103. up the other elements to make room for it.
  104. If the index is less than zero or greater than the size of the array,
  105. the new string will be added to the end of the array.
  106. */
  107. void insert (int index, const String& stringToAdd);
  108. /** Adds a string to the array as long as it's not already in there.
  109. The search can optionally be case-insensitive.
  110. */
  111. void addIfNotAlreadyThere (const String& stringToAdd, bool ignoreCase = false);
  112. /** Replaces one of the strings in the array with another one.
  113. If the index is higher than the array's size, the new string will be
  114. added to the end of the array; if it's less than zero nothing happens.
  115. */
  116. void set (int index, const String& newString);
  117. /** Appends some strings from another array to the end of this one.
  118. @param other the array to add
  119. @param startIndex the first element of the other array to add
  120. @param numElementsToAdd the maximum number of elements to add (if this is
  121. less than zero, they are all added)
  122. */
  123. void addArray (const StringArray& other,
  124. int startIndex = 0,
  125. int numElementsToAdd = -1);
  126. /** Breaks up a string into tokens and adds them to this array.
  127. This will tokenise the given string using whitespace characters as the
  128. token delimiters, and will add these tokens to the end of the array.
  129. @returns the number of tokens added
  130. */
  131. int addTokens (const String& stringToTokenise,
  132. bool preserveQuotedStrings);
  133. /** Breaks up a string into tokens and adds them to this array.
  134. This will tokenise the given string (using the string passed in to define the
  135. token delimiters), and will add these tokens to the end of the array.
  136. @param stringToTokenise the string to tokenise
  137. @param breakCharacters a string of characters, any of which will be considered
  138. to be a token delimiter.
  139. @param quoteCharacters if this string isn't empty, it defines a set of characters
  140. which are treated as quotes. Any text occurring
  141. between quotes is not broken up into tokens.
  142. @returns the number of tokens added
  143. */
  144. int addTokens (const String& stringToTokenise,
  145. const String& breakCharacters,
  146. const String& quoteCharacters);
  147. /** Breaks up a string into lines and adds them to this array.
  148. This breaks a string down into lines separated by \\n or \\r\\n, and adds each line
  149. to the array. Line-break characters are omitted from the strings that are added to
  150. the array.
  151. */
  152. int addLines (const String& stringToBreakUp);
  153. //==============================================================================
  154. /** Removes all elements from the array. */
  155. void clear();
  156. /** Removes a string from the array.
  157. If the index is out-of-range, no action will be taken.
  158. */
  159. void remove (int index);
  160. /** Finds a string in the array and removes it.
  161. This will remove the first occurrence of the given string from the array. The
  162. comparison may be case-insensitive depending on the ignoreCase parameter.
  163. */
  164. void removeString (const String& stringToRemove,
  165. bool ignoreCase = false);
  166. /** Removes any duplicated elements from the array.
  167. If any string appears in the array more than once, only the first occurrence of
  168. it will be retained.
  169. @param ignoreCase whether to use a case-insensitive comparison
  170. */
  171. void removeDuplicates (bool ignoreCase);
  172. /** Removes empty strings from the array.
  173. @param removeWhitespaceStrings if true, strings that only contain whitespace
  174. characters will also be removed
  175. */
  176. void removeEmptyStrings (bool removeWhitespaceStrings = true);
  177. /** Moves one of the strings to a different position.
  178. This will move the string to a specified index, shuffling along
  179. any intervening elements as required.
  180. So for example, if you have the array { 0, 1, 2, 3, 4, 5 } then calling
  181. move (2, 4) would result in { 0, 1, 3, 4, 2, 5 }.
  182. @param currentIndex the index of the value to be moved. If this isn't a
  183. valid index, then nothing will be done
  184. @param newIndex the index at which you'd like this value to end up. If this
  185. is less than zero, the value will be moved to the end
  186. of the array
  187. */
  188. void move (int currentIndex, int newIndex) throw();
  189. /** Deletes any whitespace characters from the starts and ends of all the strings. */
  190. void trim();
  191. /** Adds numbers to the strings in the array, to make each string unique.
  192. This will add numbers to the ends of groups of similar strings.
  193. e.g. if there are two "moose" strings, they will become "moose (1)" and "moose (2)"
  194. @param ignoreCaseWhenComparing whether the comparison used is case-insensitive
  195. @param appendNumberToFirstInstance whether the first of a group of similar strings
  196. also has a number appended to it.
  197. @param preNumberString when adding a number, this string is added before the number
  198. @param postNumberString this string is appended after any numbers that are added
  199. */
  200. void appendNumbersToDuplicates (bool ignoreCaseWhenComparing,
  201. bool appendNumberToFirstInstance,
  202. const juce_wchar* preNumberString = JUCE_T(" ("),
  203. const juce_wchar* postNumberString = JUCE_T(")"));
  204. //==============================================================================
  205. /** Joins the strings in the array together into one string.
  206. This will join a range of elements from the array into a string, separating
  207. them with a given string.
  208. e.g. joinIntoString (",") will turn an array of "a" "b" and "c" into "a,b,c".
  209. @param separatorString the string to insert between all the strings
  210. @param startIndex the first element to join
  211. @param numberOfElements how many elements to join together. If this is less
  212. than zero, all available elements will be used.
  213. */
  214. const String joinIntoString (const String& separatorString,
  215. int startIndex = 0,
  216. int numberOfElements = -1) const;
  217. //==============================================================================
  218. /** Sorts the array into alphabetical order.
  219. @param ignoreCase if true, the comparisons used will be case-sensitive.
  220. */
  221. void sort (bool ignoreCase);
  222. //==============================================================================
  223. /** Reduces the amount of storage being used by the array.
  224. Arrays typically allocate slightly more storage than they need, and after
  225. removing elements, they may have quite a lot of unused space allocated.
  226. This method will reduce the amount of allocated storage to a minimum.
  227. */
  228. void minimiseStorageOverheads();
  229. //==============================================================================
  230. juce_UseDebuggingNewOperator
  231. private:
  232. Array <String> strings;
  233. };
  234. #endif // __JUCE_STRINGARRAY_JUCEHEADER__