Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 112 lines (89 sloc) 1.94 kb
ca2a529 -
Joe Hitchens authored
1 #ifndef com_sleepless_strlist_cpp
2 #define com_sleepless_strlist_cpp
3
4 // Copyright 1998-2004 Sleepless Software Inc. All Rights Reserved
5
6 /*
7
8 Similar to List, but takes char * instead of void * and uses
9 different names for some functions, like remove() instead of rem().
10
11 This also has sorting functions.
12
13 void append(const char *s)
14 void remove(int n)
15 void clear()
16 const char *get(int n)
17 int find(const char *s)
18 int findWithCase(const char *s)
19 void sort()
20 void sortCase()
21 int length()
22
23
24 */
25
26 #include "list.cpp"
27 #include "str.cpp"
28
29
30 struct StrList
31 {
32 List list;
33
34
35 // Adds item to end of list. 's' is duplicated, so caller's
36 // pointer does not need to be preserved.
37 void append(const char *s)
38 {
39 s = strdup(s);
40 if(s)
41 list.append((void *)s);
42 }
43
44 // Removes item at index 'n' from list if 'n' is a valid index.
45 void remove(int n)
46 {
47 char *s = (char *)list.rem((int)n);
48 if(s)
49 free(s);
50 }
51
52 // Remove all items from list.
53 void clear()
54 {
55 int l = list.length();
56 for(int i = 0; i < l; i++)
57 remove(0);
58 }
59
60 // Return read-only pointer to item with index 'n'.
61 const char *get(int n)
62 {
63 return (const char *)list.get(n);
64 }
65
66 // Returns index of first item that matches 's' or -1 if not found.
67 int find(const char *s)
68 {
69 int len = list.length();
70 for(int i = 0; i < len; i++)
71 {
72 const char *ms = (const char *)list.get(i);
73 if(Str::equals(ms, s))
74 return i;
75 }
76 return -1;
77 }
78
79 // Same as find() but case sensitive
80 int findWithCase(const char *s)
81 {
82 int len = list.length();
83 for(int i = 0; i < len; i++)
84 {
85 const char *ms = (const char *)list.get(i);
86 if(Str::equalsCase(ms, s))
87 return i;
88 }
89 return -1;
90 }
91
92 // Sorts items on list
93 void sort()
94 {
95 list.sort(Str::compare);
96 }
97
98 // Sorts items on list with case sensitivity.
99 void sortCase()
100 {
101 list.sort(Str::compareCase);
102 }
103
104 int length()
105 {
106 return list.length();
107 }
108
109 };
110
111 #endif // com_sleepless_strlist_cpp
Something went wrong with that request. Please try again.