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);
132 /* Portable implementation of vsaprintf by use of vsnprintf */
134 vasprintf(char **ret, const char *fmt, va_list ap)
139 va_copy(argcopy, ap);
140 size = vsnprintf(NULL, 0, fmt, argcopy); /* Returns the number of output characters */
141 buf = (char *)malloc(size + 1);
147 n = vsnprintf(buf, size + 1, fmt, ap);
157 asprintf(char **ret, const char *fmt, ...)
162 retval = vasprintf(ret, fmt, ap);
167 #endif /* MISSING_ASPRINTF */
169 #pragma mark ====== Below codes are distributed under the BSD License =======
174 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
176 * strtok_r, from Berkeley strtok
177 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
179 * Copyright (c) 1988, 1993
180 * The Regents of the University of California. All rights reserved.
182 * Redistribution and use in source and binary forms, with or without
183 * modification, are permitted provided that the following conditions
185 * 1. Redistributions of source code must retain the above copyright
186 * notices, this list of conditions and the following disclaimer.
187 * 2. Redistributions in binary form must reproduce the above copyright
188 * notices, this list of conditions and the following disclaimer in the
189 * documentation and/or other materials provided with the distribution.
190 * 3. All advertising materials mentioning features or use of this software
191 * must display the following acknowledgement:
192 * This product includes software developed by Softweyr LLC, the
193 * University of California, Berkeley, and its contributors.
194 * 4. Neither the name of the University nor the names of its contributors
195 * may be used to endorse or promote products derived from this software
196 * without specific prior written permission.
198 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
199 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
200 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
201 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
202 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
203 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
204 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
205 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
206 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
207 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
208 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212 strtok_r(char *s, const char *delim, char **last)
217 if (s == NULL && (s = *last) == NULL)
221 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
225 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
230 if (c == 0) { /* no non-delimiter characters */
237 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
238 * Note that delim must have one NUL; we stop if we see that, too.
242 spanp = (char *)delim;
244 if ((sc = *spanp++) == c) {
256 #endif /* MISSING_STRTOK_R */
258 #if MISSING_MERGESORT
260 * Copyright (c) 1992, 1993
261 * The Regents of the University of California. All rights reserved.
263 * This code is derived from software contributed to Berkeley by
266 * Redistribution and use in source and binary forms, with or without
267 * modification, are permitted provided that the following conditions
269 * 1. Redistributions of source code must retain the above copyright
270 * notice, this list of conditions and the following disclaimer.
271 * 2. Redistributions in binary form must reproduce the above copyright
272 * notice, this list of conditions and the following disclaimer in the
273 * documentation and/or other materials provided with the distribution.
274 * 3. All advertising materials mentioning features or use of this software
275 * must display the following acknowledgement:
276 * This product includes software developed by the University of
277 * California, Berkeley and its contributors.
278 * 4. Neither the name of the University nor the names of its contributors
279 * may be used to endorse or promote products derived from this software
280 * without specific prior written permission.
282 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
283 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
284 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
285 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
286 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
287 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295 #if defined(LIBC_SCCS) && !defined(lint)
296 static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
297 #endif /* LIBC_SCCS and not lint */
298 //#include <sys/cdefs.h>
299 //__FBSDID("$FreeBSD: src/lib/libc/stdlib/merge.c,v 1.6 2002/03/21 22:48:42 obrien Exp $");
302 * Hybrid exponential search/linear search merge sort with hybrid
303 * natural/pairwise first pass. Requires about .3% more comparisons
304 * for random data than LSMS with pairwise first pass alone.
305 * It works for objects as small as two bytes.
309 #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
311 /* #define NATURAL to get hybrid natural merge.
312 * (The default is pairwise merging.)
315 #include <sys/types.h>
321 typedef unsigned char u_char;
323 static void setup(u_char *, u_char *, size_t, size_t, int (*)());
324 static void insertionsort(u_char *, size_t, size_t, int (*)());
326 #define ISIZE sizeof(int)
327 #define PSIZE sizeof(u_char *)
328 #define ICOPY_LIST(src, dst, last) \
330 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
332 #define ICOPY_ELT(src, dst, i) \
334 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
337 #define CCOPY_LIST(src, dst, last) \
341 #define CCOPY_ELT(src, dst, i) \
347 * Find the next possible pointer head. (Trickery for forcing an array
348 * to do double duty as a linked list when objects do not align with word
351 /* Assumption: PSIZE is a power of 2. */
352 #define EVAL(p) (u_char **) \
354 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
357 * Arguments are as for qsort.
360 mergesort(base, nmemb, size, cmp)
364 int (*cmp)(const void *, const void *);
368 u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
369 u_char *list2, *list1, *p2, *p, *last, **p1;
371 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
381 * Stupid subtraction for the Cray.
384 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
387 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
391 setup(list1, list2, nmemb, size, cmp);
392 last = list2 + nmemb * size;
394 while (*EVAL(list2) != last) {
397 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
400 f2 = l1 = list1 + (p2 - list2);
403 l2 = list1 + (p2 - list2);
404 while (f1 < l1 && f2 < l2) {
405 if ((*cmp)(f1, f2) <= 0) {
414 if (!big) { /* here i = 0 */
415 while ((b += size) < t && cmp(q, b) >sense)
421 EXPONENTIAL: for (i = size; ; i <<= 1)
422 if ((p = (b + i)) >= t) {
423 if ((p = t - size) > b &&
424 (*cmp)(q, p) <= sense)
429 } else if ((*cmp)(q, p) <= sense) {
437 i = (((t - b) / size) >> 1) * size;
438 if ((*cmp)(q, p = b + i) <= sense)
444 FASTCASE: while (i > size)
446 p = b + (i >>= 1)) <= sense)
455 ICOPY_LIST(f2, tp2, b);
456 ICOPY_ELT(f1, tp2, i);
458 CCOPY_LIST(f2, tp2, b);
459 CCOPY_ELT(f1, tp2, i);
463 ICOPY_LIST(f1, tp2, b);
464 ICOPY_ELT(f2, tp2, i);
466 CCOPY_LIST(f1, tp2, b);
467 CCOPY_ELT(f2, tp2, i);
473 ICOPY_LIST(f2, tp2, l2);
475 CCOPY_LIST(f2, tp2, l2);
476 } else if (f1 < l1) {
478 ICOPY_LIST(f1, tp2, l1);
480 CCOPY_LIST(f1, tp2, l1);
484 tp2 = list1; /* swap list1, list2 */
487 last = list2 + nmemb*size;
490 memmove(list2, list1, nmemb*size);
497 #define swap(a, b) { \
501 tmp = *a; *a++ = *s; *s++ = tmp; \
505 #define reverse(bot, top) { \
510 tmp = *bot; *bot++ = *s; *s++ = tmp; \
517 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
518 * increasing order, list2 in a corresponding linked list. Checks for runs
519 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
520 * is defined. Otherwise simple pairwise merging is used.)
523 setup(list1, list2, n, size, cmp)
525 int (*cmp)(const void *, const void *);
526 u_char *list1, *list2;
528 int i, length, size2, tmp, sense;
529 u_char *f1, *f2, *s, *l2, *last, *p2;
533 insertionsort(list1, n, size, cmp);
534 *EVAL(list2) = (u_char*) list2 + n*size;
538 * Avoid running pointers out of bounds; limit n to evens
542 insertionsort(list1 + (n - i) * size, i, size, cmp);
543 last = list1 + size * (n - i);
544 *EVAL(list2 + (last - list1)) = list2 + n * size;
549 sense = (cmp(f1, f1 + size) > 0);
550 for (; f1 < last; sense = !sense) {
552 /* Find pairs with same sense. */
553 for (f2 = f1 + size2; f2 < last; f2 += size2) {
554 if ((cmp(f2, f2+ size) > 0) != sense)
558 if (length < THRESHOLD) { /* Pairwise merge */
560 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
562 swap (f1, f1 + size);
563 } while ((f1 += size2) < f2);
564 } else { /* Natural merge */
566 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
567 if ((cmp(f2-size, f2) > 0) != sense) {
568 p2 = *EVAL(p2) = f2 - list1 + list2;
570 reverse(f1, f2-size);
575 reverse (f1, f2-size);
577 if (f2 < last || cmp(f2 - size, f2) > 0)
578 p2 = *EVAL(p2) = f2 - list1 + list2;
580 p2 = *EVAL(p2) = list2 + n*size;
583 #else /* pairwise merge only. */
584 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
585 p2 = *EVAL(p2) = p2 + size2;
586 if (cmp (f1, f1 + size) > 0)
593 * This is to avoid out-of-bounds addresses in sorting the
597 insertionsort(a, n, size, cmp)
600 int (*cmp)(const void *, const void *);
602 u_char *ai, *s, *t, *u, tmp;
605 for (ai = a+size; --n >= 1; ai += size)
606 for (t = ai; t > a; t -= size) {
613 #endif /* MISSING_MERGESORT */