vimLine fix
[opsoft.git] / silentbob / gclib / src / earray.cxx
blobd56825859907b4f150c8c9fd8ab759b68d037374
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <gclib/gclib_c.h>
8 #include <gclib/gclib.h>
10 /* Extendent array */
11 EArray::EArray (int asize)
15 int EArray::add (long one)
17 return DArray::add (LPCHAR (one));
20 int EArray::sfind (char * d_find)
22 int i;
23 int d_size = get_size ();
24 char ** d_array = get_skeleton ();
26 for (i = 0; i < d_size; i++) {
27 if (EQ (d_find, d_array[i]))
28 return i;
31 return -1;
34 int EArray::snfind (char * d_find)
36 int i;
37 int d_size = get_size ();
38 char ** d_array = get_skeleton ();
40 for (i = 0; i < d_size; i++) {
41 if (!strncmp (d_find, d_array[i], strlen (d_array[i])))
42 return i;
45 return -1;
48 void EArray::push (char ** d_push)
50 int i = 0;
51 if (d_push == NULL)
52 return;
54 while (d_push[i]) {
55 add ((long) d_push[i]);
56 ++i;
60 int EArray::snfind_fast (char *str, int n)
62 int first;
63 int last;
64 int fact;
65 int check;
66 int move;
68 first = 0;
69 last = get_size ();
70 --last;
71 if (last < 0)
72 return -1;
74 if (strcmp (get (0), str) > 0)
75 return -1;
77 while (true) {
78 move = (last - first) >> 1;
79 if (! move) {
80 if (EQ (get (first), str))
81 return first;
82 if (EQ (get (last), str))
83 return last;
84 return -1;
87 check = first + move;
88 if (n)
89 fact = strncmp (get (check), str, n);
90 else
91 fact = strcmp (get (check), str);
93 if (fact < 0) { // [check] < str
94 first = check;
95 } else if (! fact) { // [check] == str
96 return check;
97 } else if (fact > 0) { // [chec] > str
98 last = check;
102 if (EQ (get (first), str))
103 return first;
105 return -1;
108 int EArray::from_pipe (int fd)
110 char * buf;
111 FILE * m_file;
113 m_file = fdopen (fd, "r");
114 if (m_file == NULL)
115 return false;
117 buf = CNEW (char, 256);
118 while (true) {
119 if (fgets (buf, 256, m_file) == NULL)
120 break;
122 add (strdup (buf));
125 fclose (m_file);
126 DROP (buf);
127 return get_size ();
130 char * EArray::join (char * mid, int *len_out)
132 char *Ret;
133 char *ptr;
134 int count;
135 int len = 0;
136 int mid_len;
137 int i;
139 if (! mid) {
140 mid_len = 0;
141 mid = (char *) "";
142 } else
143 mid_len = strlen (mid);
145 count = get_size ();
146 for (i = 0; i < count; i++)
147 len += strlen (get (i));
149 if (mid)
150 len += count * mid_len;
152 len++;
153 Ret = CNEW (char, len);
154 ptr = Ret;
155 for (i = 0; i < count; i++) {
156 strcpy (ptr, (char *) get (i));
157 ptr += strlen (get (i));
158 strcpy (ptr, mid);
159 ptr += mid_len;
162 if (len_out)
163 *len_out = len;
165 return Ret;
168 int EArray::strings_to_file (char * f_name)
170 FILE * d_file;
171 int d_len;
172 int i;
174 d_file = fopen (f_name, "w");
175 if (! d_file)
176 return -1;
178 d_len = get_size ();
179 for (i = 0; i < d_len; ++i)
180 fprintf (d_file, "%s\n", get (i));
182 fclose (d_file);
183 return d_len;
186 EArray & EArray::operator << (char *S)
188 add (S);
189 return *this;
192 int EArray::expand (EArray * e)
194 int count;
195 int i;
197 if (! e)
198 return get_size ();
200 count = e->get_size ();
201 if (! count)
202 return get_size ();
204 for (i = 0; i < count; ++i)
205 add (e->get (i));
207 return get_size ();
210 int EArray::expand (char *buf, char * split)
212 char * S;
213 char * ptr = buf;
214 int split_len;
215 char * last;
217 if (!buf || !split || !(*buf))
218 return -1;
220 last = &buf[strlen (buf)-1];
221 split_len = strlen (split);
222 while((S = Dmid_memmem (ptr, last, split, split_len)) && S) {
223 add (Dstrndup (ptr, S - ptr));
224 S += split_len;
225 ptr = S;
228 if (ptr <= last)
229 add (Dstrndup (ptr, last-ptr+1));
231 return get_size ();
234 int EArray::dump (char * d_join)
236 int i;
237 int count;
239 count = get_size ();
240 for (i = 0; i < count; ++i)
241 printf ("%s%s", get (i), d_join);
243 return count;