2 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
3 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
46 static wchar_t **wmonths;
47 static unsigned char **cmonths;
49 /* initialise months */
52 initialise_months(void)
54 const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4,
55 ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10,
60 if (MB_CUR_MAX == 1) {
61 if (cmonths == NULL) {
64 cmonths = sort_malloc(sizeof(unsigned char*) * 12);
65 for (int i = 0; i < 12; i++) {
67 tmp = (unsigned char *) nl_langinfo(item[i]);
71 printf("month[%d]=%s\n", i, tmp);
72 len = strlen((char*)tmp);
77 m = sort_malloc(len + 1);
78 memcpy(m, tmp, len + 1);
80 for (unsigned int j = 0; j < len; j++)
87 if (wmonths == NULL) {
90 wmonths = sort_malloc(sizeof(wchar_t *) * 12);
91 for (int i = 0; i < 12; i++) {
93 tmp = (unsigned char *) nl_langinfo(item[i]);
97 printf("month[%d]=%s\n", i, tmp);
98 len = strlen((char*)tmp);
101 while (isblank(*tmp))
103 m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
104 if (mbstowcs(m, (char*)tmp, len) == ((size_t) -1))
107 for (unsigned int j = 0; j < len; j++)
108 m[j] = towupper(m[j]);
116 * Compare two wide-character strings
119 wide_str_coll(const wchar_t *s1, const wchar_t *s2)
124 ret = wcscoll(s1, s2);
125 if (errno == EILSEQ) {
127 ret = wcscmp(s1, s2);
129 for (size_t i = 0; ; ++i) {
133 return ((c2 == L'\0') ? 0 : -1);
138 return ((int)(c1 - c2));
145 /* counterparts of wcs functions */
148 bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix)
152 fprintf(f, "%s%s%s", prefix, bws->data.cstr, suffix);
154 fprintf(f, "%s%S%s", prefix, bws->data.wstr, suffix);
157 const void* bwsrawdata(const struct bwstring *bws)
160 return (&(bws->data));
163 size_t bwsrawlen(const struct bwstring *bws)
166 return ((MB_CUR_MAX == 1) ? bws->len : SIZEOF_WCHAR_STRING(bws->len));
170 bws_memsize(const struct bwstring *bws)
173 return ((MB_CUR_MAX == 1) ? (bws->len + 2 + sizeof(struct bwstring)) :
174 (SIZEOF_WCHAR_STRING(bws->len + 1) + sizeof(struct bwstring)));
178 bws_setlen(struct bwstring *bws, size_t newlen)
181 if (bws && newlen != bws->len && newlen <= bws->len) {
184 bws->data.cstr[newlen] = '\0';
186 bws->data.wstr[newlen] = L'\0';
191 * Allocate a new binary string of specified size
196 struct bwstring *ret;
199 ret = sort_malloc(sizeof(struct bwstring) + 1 + sz);
201 ret = sort_malloc(sizeof(struct bwstring) +
202 SIZEOF_WCHAR_STRING(sz + 1));
206 ret->data.cstr[ret->len] = '\0';
208 ret->data.wstr[ret->len] = L'\0';
214 * Create a copy of binary string.
215 * New string size equals the length of the old string.
218 bwsdup(const struct bwstring *s)
224 struct bwstring *ret = bwsalloc(s->len);
227 memcpy(ret->data.cstr, s->data.cstr, (s->len));
229 memcpy(ret->data.wstr, s->data.wstr,
230 SIZEOF_WCHAR_STRING(s->len));
237 * Create a new binary string from a raw binary buffer.
240 bwssbdup(const wchar_t *str, size_t len)
244 return ((len == 0) ? bwsalloc(0) : NULL);
246 struct bwstring *ret;
251 for (size_t i = 0; i < len; ++i)
252 ret->data.cstr[i] = (unsigned char) str[i];
254 memcpy(ret->data.wstr, str, SIZEOF_WCHAR_STRING(len));
261 * Create a new binary string from a raw binary buffer.
264 bwscsbdup(const unsigned char *str, size_t len)
266 struct bwstring *ret;
272 memcpy(ret->data.cstr, str, len);
276 size_t charlen, chars, cptr;
280 s = (const char *) str;
282 memset(&mbs, 0, sizeof(mbs));
285 size_t n = MB_CUR_MAX;
289 charlen = mbrlen(s + cptr, n, &mbs);
296 ret->data.wstr[chars++] =
297 (unsigned char) s[cptr];
301 n = mbrtowc(ret->data.wstr + (chars++),
302 s + cptr, charlen, &mbs);
303 if ((n == (size_t)-1) || (n == (size_t)-2))
305 err(2, "mbrtowc error");
311 ret->data.wstr[ret->len] = L'\0';
318 * De-allocate object memory
321 bwsfree(const struct bwstring *s)
329 * Copy content of src binary string to dst.
330 * If the capacity of the dst string is not sufficient,
331 * then the data is truncated.
334 bwscpy(struct bwstring *dst, const struct bwstring *src)
336 size_t nums = src->len;
342 if (MB_CUR_MAX == 1) {
343 memcpy(dst->data.cstr, src->data.cstr, nums);
344 dst->data.cstr[dst->len] = '\0';
346 memcpy(dst->data.wstr, src->data.wstr,
347 SIZEOF_WCHAR_STRING(nums + 1));
348 dst->data.wstr[dst->len] = L'\0';
355 * Copy content of src binary string to dst,
356 * with specified number of symbols to be copied.
357 * If the capacity of the dst string is not sufficient,
358 * then the data is truncated.
361 bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size)
363 size_t nums = src->len;
371 if (MB_CUR_MAX == 1) {
372 memcpy(dst->data.cstr, src->data.cstr, nums);
373 dst->data.cstr[dst->len] = '\0';
375 memcpy(dst->data.wstr, src->data.wstr,
376 SIZEOF_WCHAR_STRING(nums + 1));
377 dst->data.wstr[dst->len] = L'\0';
384 * Copy content of src binary string to dst,
385 * with specified number of symbols to be copied.
386 * An offset value can be specified, from the start of src string.
387 * If the capacity of the dst string is not sufficient,
388 * then the data is truncated.
391 bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset,
395 if (offset >= src->len) {
396 dst->data.wstr[0] = 0;
399 size_t nums = src->len - offset;
406 if (MB_CUR_MAX == 1) {
407 memcpy(dst->data.cstr, src->data.cstr + offset,
409 dst->data.cstr[dst->len] = '\0';
411 memcpy(dst->data.wstr, src->data.wstr + offset,
412 SIZEOF_WCHAR_STRING(nums));
413 dst->data.wstr[dst->len] = L'\0';
420 * Write binary string to the file.
421 * The output is ended either with '\n' (nl == true)
422 * or '\0' (nl == false).
425 bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
428 if (MB_CUR_MAX == 1) {
429 size_t len = bws->len;
432 bws->data.cstr[len] = '\n';
434 if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
437 bws->data.cstr[len] = '\0';
438 } else if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
447 eols = zero_ended ? btowc('\0') : btowc('\n');
449 while (printed < BWSLEN(bws)) {
450 const wchar_t *s = bws->data.wstr + printed;
455 nums = fwprintf(f, L"%lc", *s);
463 nums = fwprintf(f, L"%ls", s);
470 fwprintf(f, L"%lc", eols);
471 return (printed + 1);
476 * Allocate and read a binary string from file.
477 * The strings are nl-ended or zero-ended, depending on the sort setting.
480 bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb)
484 eols = zero_ended ? btowc('\0') : btowc('\n');
486 if (!zero_ended && (MB_CUR_MAX > 1)) {
489 ret = fgetwln(f, len);
497 if (ret[*len - 1] == (wchar_t)eols)
500 return (bwssbdup(ret, *len));
502 } else if (!zero_ended && (MB_CUR_MAX == 1)) {
505 ret = fgetln(f, len);
513 if (ret[*len - 1] == '\n')
516 return (bwscsbdup((unsigned char*)ret, *len));
524 if (2 >= rb->fgetwln_z_buffer_size) {
525 rb->fgetwln_z_buffer_size += 256;
526 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
527 sizeof(wchar_t) * rb->fgetwln_z_buffer_size);
529 rb->fgetwln_z_buffer[*len] = 0;
545 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
546 rb->fgetwln_z_buffer_size += 256;
547 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
548 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
551 rb->fgetwln_z_buffer[*len] = c;
552 rb->fgetwln_z_buffer[++(*len)] = 0;
568 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
569 rb->fgetwln_z_buffer_size += 256;
570 rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
571 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
574 rb->fgetwln_z_buffer[*len] = c;
575 rb->fgetwln_z_buffer[++(*len)] = 0;
579 /* we do not count the last 0 */
580 return (bwssbdup(rb->fgetwln_z_buffer, *len));
585 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
586 size_t offset, size_t len)
588 size_t cmp_len, len1, len2;
595 if (len1 <= offset) {
596 return ((len2 <= offset) ? 0 : -1);
612 if (MB_CUR_MAX == 1) {
613 const unsigned char *s1, *s2;
615 s1 = bws1->data.cstr + offset;
616 s2 = bws2->data.cstr + offset;
618 res = memcmp(s1, s2, cmp_len);
621 const wchar_t *s1, *s2;
623 s1 = bws1->data.wstr + offset;
624 s2 = bws2->data.wstr + offset;
626 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
632 if (len1 < cmp_len && len1 < len2)
634 else if (len2 < cmp_len && len2 < len1)
642 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
644 size_t len1, len2, cmp_len;
658 res = bwsncmp(bws1, bws2, offset, cmp_len);
663 else if (len2 < len1)
671 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
676 for (i = 0; i < len; ++i) {
677 c1 = bws_get_iter_value(iter1);
678 c2 = bws_get_iter_value(iter2);
681 iter1 = bws_iterator_inc(iter1, 1);
682 iter2 = bws_iterator_inc(iter2, 1);
689 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
697 return ((len2 <= offset) ? 0 : -1);
705 if (MB_CUR_MAX == 1) {
706 const unsigned char *s1, *s2;
708 s1 = bws1->data.cstr + offset;
709 s2 = bws2->data.cstr + offset;
715 res = memcmp(s1, s2, len2);
718 } else if (len1 < len2) {
719 res = memcmp(s1, s2, len1);
723 res = memcmp(s1, s2, len1);
738 /* goto next non-zero part: */
739 while ((i < maxlen) &&
749 err(2, "bwscoll error 01");
752 } else if (s2[i] == 0)
755 res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
759 while ((i < maxlen) &&
772 } else if (s2[i] == 0)
776 err(2, "bwscoll error 02");
781 else if (len1 > len2)
787 const wchar_t *s1, *s2;
791 s1 = bws1->data.wstr + offset;
792 s2 = bws2->data.wstr + offset;
802 /* goto next non-zero part: */
803 while ((i < maxlen) &&
813 err(2, "bwscoll error 1");
816 } else if (s2[i] == 0)
819 res = wide_str_coll(s1 + i, s2 + i);
823 while ((i < maxlen) && s1[i] && s2[i])
835 } else if (s2[i] == 0)
839 err(2, "bwscoll error 2");
844 else if (len1 > len2)
854 * Correction of the system API
857 bwstod(struct bwstring *s0, bool *empty)
861 if (MB_CUR_MAX == 1) {
862 unsigned char *end, *s;
869 while (isblank(*s) && s < end)
877 ret = strtod((char*)s, &ep);
878 if ((unsigned char*) ep == s) {
883 wchar_t *end, *ep, *s;
889 while (iswblank(*s) && s < end)
897 ret = wcstod(s, &ep);
909 * A helper function for monthcoll. If a line matches
910 * a month name, it returns (number of the month - 1),
911 * while if there is no match, it just return -1.
915 bws_month_score(const struct bwstring *s0)
918 if (MB_CUR_MAX == 1) {
919 const unsigned char *end, *s;
925 while (isblank(*s) && s < end)
928 len = strlen((const char*)s);
930 for (int i = 11; i >= 0; --i) {
932 (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i]))))
937 const wchar_t *end, *s;
943 while (iswblank(*s) && s < end)
948 for (int i = 11; i >= 0; --i) {
949 if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
958 * Rips out leading blanks (-b).
961 ignore_leading_blanks(struct bwstring *str)
964 if (MB_CUR_MAX == 1) {
965 unsigned char *dst, *end, *src;
967 src = str->data.cstr;
969 end = src + str->len;
971 while (src < end && isblank(*src))
977 newlen = BWSLEN(str) - (src - dst);
984 bws_setlen(str, newlen);
987 wchar_t *dst, *end, *src;
989 src = str->data.wstr;
991 end = src + str->len;
993 while (src < end && iswblank(*src))
998 size_t newlen = BWSLEN(str) - (src - dst);
1005 bws_setlen(str, newlen);
1013 * Rips out nonprinting characters (-i).
1016 ignore_nonprinting(struct bwstring *str)
1018 size_t newlen = str->len;
1020 if (MB_CUR_MAX == 1) {
1021 unsigned char *dst, *end, *src;
1024 src = str->data.cstr;
1026 end = src + str->len;
1040 wchar_t *dst, *end, *src;
1043 src = str->data.wstr;
1045 end = src + str->len;
1059 bws_setlen(str, newlen);
1065 * Rips out any characters that are not alphanumeric characters
1069 dictionary_order(struct bwstring *str)
1071 size_t newlen = str->len;
1073 if (MB_CUR_MAX == 1) {
1074 unsigned char *dst, *end, *src;
1077 src = str->data.cstr;
1079 end = src + str->len;
1083 if (isalnum(c) || isblank(c)) {
1093 wchar_t *dst, *end, *src;
1096 src = str->data.wstr;
1098 end = src + str->len;
1102 if (iswalnum(c) || iswblank(c)) {
1112 bws_setlen(str, newlen);
1118 * Converts string to lower case(-f).
1121 ignore_case(struct bwstring *str)
1124 if (MB_CUR_MAX == 1) {
1125 unsigned char *end, *s;
1149 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
1152 if (MB_CUR_MAX == 1)
1153 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr);
1155 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr);