6 #pragma mark ====== Below codes are distributed under GPL ======
9 * Created by Toshi Nagata on 08/11/06.
10 * Copyright 2008 Toshi Nagata. All rights reserved.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation version 2 of the License.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
28 #if defined(__WXMSW__) || defined(__CMDMSW__)
31 translate_char(char *p, int from, int to)
34 if ((unsigned char)*p == from)
40 fix_dosish_path(char *p)
42 translate_char(p, '/', '\\');
46 translate_char(char *p, int from, int to)
50 fix_dosish_path(char *p)
58 _strdup(const char *src)
60 char *s = (char *)malloc(strlen(src) + 1);
67 _wcsdup(const wchar_t *src)
72 len = wcslen(src) + 1;
73 dst = (wchar_t *)malloc(len * sizeof(wchar_t));
76 wmemcpy(dst, src, len);
79 char *strdup(const char *src) { return _strdup(src); }
80 wchar_t *wcsdup(const wchar_t *src) { return _wcsdup(src); }
87 strpbrk(const char *cs, const char *ct)
91 for (sc1 = cs; *sc1 != '\0'; sc1++) {
92 for (sc2 = ct; *sc2 != '\0'; sc2++) {
101 strsep(char **stringp, const char *delim)
103 char *start = *stringp;
110 ptr = start + strlen(start);
112 ptr = strpbrk(start, delim);
133 vasprintf(char **ret, const char *fmt, va_list ap)
137 buf = (char *)malloc(size);
144 int n = vsnprintf(buf, size, fmt, ap);
149 buf = (char *)realloc(buf, size);
164 asprintf(char **ret, const char *fmt, ...)
169 retval = vasprintf(ret, fmt, ap);
174 #endif /* MISSING_ASPRINTF */
176 #pragma mark ====== Below codes are distributed under the BSD License =======
181 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
183 * strtok_r, from Berkeley strtok
184 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
186 * Copyright (c) 1988, 1993
187 * The Regents of the University of California. All rights reserved.
189 * Redistribution and use in source and binary forms, with or without
190 * modification, are permitted provided that the following conditions
192 * 1. Redistributions of source code must retain the above copyright
193 * notices, this list of conditions and the following disclaimer.
194 * 2. Redistributions in binary form must reproduce the above copyright
195 * notices, this list of conditions and the following disclaimer in the
196 * documentation and/or other materials provided with the distribution.
197 * 3. All advertising materials mentioning features or use of this software
198 * must display the following acknowledgement:
199 * This product includes software developed by Softweyr LLC, the
200 * University of California, Berkeley, and its contributors.
201 * 4. Neither the name of the University nor the names of its contributors
202 * may be used to endorse or promote products derived from this software
203 * without specific prior written permission.
205 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
206 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
208 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
209 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
210 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
211 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
212 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
213 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
214 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
215 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
219 strtok_r(char *s, const char *delim, char **last)
224 if (s == NULL && (s = *last) == NULL)
228 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
232 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
237 if (c == 0) { /* no non-delimiter characters */
244 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
245 * Note that delim must have one NUL; we stop if we see that, too.
249 spanp = (char *)delim;
251 if ((sc = *spanp++) == c) {
263 #endif /* MISSING_STRTOK_R */
265 #if MISSING_MERGESORT
267 * Copyright (c) 1992, 1993
268 * The Regents of the University of California. All rights reserved.
270 * This code is derived from software contributed to Berkeley by
273 * Redistribution and use in source and binary forms, with or without
274 * modification, are permitted provided that the following conditions
276 * 1. Redistributions of source code must retain the above copyright
277 * notice, this list of conditions and the following disclaimer.
278 * 2. Redistributions in binary form must reproduce the above copyright
279 * notice, this list of conditions and the following disclaimer in the
280 * documentation and/or other materials provided with the distribution.
281 * 3. All advertising materials mentioning features or use of this software
282 * must display the following acknowledgement:
283 * This product includes software developed by the University of
284 * California, Berkeley and its contributors.
285 * 4. Neither the name of the University nor the names of its contributors
286 * may be used to endorse or promote products derived from this software
287 * without specific prior written permission.
289 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
290 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
292 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
293 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
295 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302 #if defined(LIBC_SCCS) && !defined(lint)
303 static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
304 #endif /* LIBC_SCCS and not lint */
305 //#include <sys/cdefs.h>
306 //__FBSDID("$FreeBSD: src/lib/libc/stdlib/merge.c,v 1.6 2002/03/21 22:48:42 obrien Exp $");
309 * Hybrid exponential search/linear search merge sort with hybrid
310 * natural/pairwise first pass. Requires about .3% more comparisons
311 * for random data than LSMS with pairwise first pass alone.
312 * It works for objects as small as two bytes.
316 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
318 /* #define NATURAL to get hybrid natural merge.
319 * (The default is pairwise merging.)
322 #include <sys/types.h>
328 typedef unsigned char u_char;
330 static void setup(u_char *, u_char *, size_t, size_t, int (*)());
331 static void insertionsort(u_char *, size_t, size_t, int (*)());
333 #define ISIZE sizeof(int)
334 #define PSIZE sizeof(u_char *)
335 #define ICOPY_LIST(src, dst, last) \
337 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
339 #define ICOPY_ELT(src, dst, i) \
341 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
344 #define CCOPY_LIST(src, dst, last) \
348 #define CCOPY_ELT(src, dst, i) \
354 * Find the next possible pointer head. (Trickery for forcing an array
355 * to do double duty as a linked list when objects do not align with word
358 /* Assumption: PSIZE is a power of 2. */
359 #define EVAL(p) (u_char **) \
361 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
364 * Arguments are as for qsort.
367 mergesort(base, nmemb, size, cmp)
371 int (*cmp)(const void *, const void *);
375 u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
376 u_char *list2, *list1, *p2, *p, *last, **p1;
378 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
388 * Stupid subtraction for the Cray.
391 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
394 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
398 setup(list1, list2, nmemb, size, cmp);
399 last = list2 + nmemb * size;
401 while (*EVAL(list2) != last) {
404 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
407 f2 = l1 = list1 + (p2 - list2);
410 l2 = list1 + (p2 - list2);
411 while (f1 < l1 && f2 < l2) {
412 if ((*cmp)(f1, f2) <= 0) {
421 if (!big) { /* here i = 0 */
422 while ((b += size) < t && cmp(q, b) >sense)
428 EXPONENTIAL: for (i = size; ; i <<= 1)
429 if ((p = (b + i)) >= t) {
430 if ((p = t - size) > b &&
431 (*cmp)(q, p) <= sense)
436 } else if ((*cmp)(q, p) <= sense) {
444 i = (((t - b) / size) >> 1) * size;
445 if ((*cmp)(q, p = b + i) <= sense)
451 FASTCASE: while (i > size)
453 p = b + (i >>= 1)) <= sense)
462 ICOPY_LIST(f2, tp2, b);
463 ICOPY_ELT(f1, tp2, i);
465 CCOPY_LIST(f2, tp2, b);
466 CCOPY_ELT(f1, tp2, i);
470 ICOPY_LIST(f1, tp2, b);
471 ICOPY_ELT(f2, tp2, i);
473 CCOPY_LIST(f1, tp2, b);
474 CCOPY_ELT(f2, tp2, i);
480 ICOPY_LIST(f2, tp2, l2);
482 CCOPY_LIST(f2, tp2, l2);
483 } else if (f1 < l1) {
485 ICOPY_LIST(f1, tp2, l1);
487 CCOPY_LIST(f1, tp2, l1);
491 tp2 = list1; /* swap list1, list2 */
494 last = list2 + nmemb*size;
497 memmove(list2, list1, nmemb*size);
504 #define swap(a, b) { \
508 tmp = *a; *a++ = *s; *s++ = tmp; \
512 #define reverse(bot, top) { \
517 tmp = *bot; *bot++ = *s; *s++ = tmp; \
524 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
525 * increasing order, list2 in a corresponding linked list. Checks for runs
526 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
527 * is defined. Otherwise simple pairwise merging is used.)
530 setup(list1, list2, n, size, cmp)
532 int (*cmp)(const void *, const void *);
533 u_char *list1, *list2;
535 int i, length, size2, tmp, sense;
536 u_char *f1, *f2, *s, *l2, *last, *p2;
540 insertionsort(list1, n, size, cmp);
541 *EVAL(list2) = (u_char*) list2 + n*size;
545 * Avoid running pointers out of bounds; limit n to evens
549 insertionsort(list1 + (n - i) * size, i, size, cmp);
550 last = list1 + size * (n - i);
551 *EVAL(list2 + (last - list1)) = list2 + n * size;
556 sense = (cmp(f1, f1 + size) > 0);
557 for (; f1 < last; sense = !sense) {
559 /* Find pairs with same sense. */
560 for (f2 = f1 + size2; f2 < last; f2 += size2) {
561 if ((cmp(f2, f2+ size) > 0) != sense)
565 if (length < THRESHOLD) { /* Pairwise merge */
567 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
569 swap (f1, f1 + size);
570 } while ((f1 += size2) < f2);
571 } else { /* Natural merge */
573 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
574 if ((cmp(f2-size, f2) > 0) != sense) {
575 p2 = *EVAL(p2) = f2 - list1 + list2;
577 reverse(f1, f2-size);
582 reverse (f1, f2-size);
584 if (f2 < last || cmp(f2 - size, f2) > 0)
585 p2 = *EVAL(p2) = f2 - list1 + list2;
587 p2 = *EVAL(p2) = list2 + n*size;
590 #else /* pairwise merge only. */
591 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
592 p2 = *EVAL(p2) = p2 + size2;
593 if (cmp (f1, f1 + size) > 0)
600 * This is to avoid out-of-bounds addresses in sorting the
604 insertionsort(a, n, size, cmp)
607 int (*cmp)(const void *, const void *);
609 u_char *ai, *s, *t, *u, tmp;
612 for (ai = a+size; --n >= 1; ai += size)
613 for (t = ai; t > a; t -= size) {
620 #endif /* MISSING_MERGESORT */