xref: /freebsd/usr.bin/sort/bwstring.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
7  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if !defined(__BWSTRING_H__)
33 #define	__BWSTRING_H__
34 
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <sysexits.h>
39 #include <wchar.h>
40 
41 #include "sort.h"
42 #include "mem.h"
43 
44 extern bool byte_sort;
45 
46 /* wchar_t is of 4 bytes: */
47 #define	SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
48 
49 struct wstr {
50 	size_t len;
51 	wchar_t str[];
52 };
53 
54 struct cstr {
55 	size_t len;
56 	char str[];
57 };
58 
59 /*
60  * Binary "wide" string
61  */
62 struct bwstring
63 {
64 	union {
65 		struct wstr wdata;
66 		struct cstr cdata;
67 	};
68 };
69 
70 struct reader_buffer
71 {
72 	wchar_t			*fgetwln_z_buffer;
73 	size_t			 fgetwln_z_buffer_size;
74 };
75 
76 typedef void *bwstring_iterator;
77 
78 #define	BWSLEN(s) ((mb_cur_max == 1) ? (s)->cdata.len : (s)->wdata.len)
79 struct bwstring *bwsalloc(size_t sz);
80 
81 size_t bwsrawlen(const struct bwstring *bws);
82 const void* bwsrawdata(const struct bwstring *bws);
83 void bws_setlen(struct bwstring *bws, size_t newlen);
84 size_t bws_memsize(const struct bwstring *bws);
85 double bwstod(struct bwstring *s0, bool *empty);
86 int bws_month_score(const struct bwstring *s0);
87 
88 struct bwstring *ignore_leading_blanks(struct bwstring *str);
89 struct bwstring *ignore_nonprinting(struct bwstring *str);
90 struct bwstring *dictionary_order(struct bwstring *str);
91 struct bwstring *ignore_case(struct bwstring *str);
92 
93 void bwsprintf(FILE*, struct bwstring*, const char *prefix, const char *suffix);
94 void bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos);
95 
96 struct bwstring *bwsdup(const struct bwstring *s);
97 struct bwstring *bwssbdup(const wchar_t *str, size_t size);
98 struct bwstring *bwscsbdup(const unsigned char *str, size_t size);
99 void bwsfree(const struct bwstring *s);
100 size_t bwscpy(struct bwstring *dst, const struct bwstring *src);
101 struct bwstring *bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size);
102 struct bwstring *bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size);
103 int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
104 int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len);
105 int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
106 size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
107 struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb);
108 
109 static inline bwstring_iterator
110 bws_begin(struct bwstring *bws)
111 {
112 
113 	return ((bwstring_iterator)bws->wdata.str);
114 }
115 
116 static inline bwstring_iterator
117 bws_end(struct bwstring *bws)
118 {
119 
120 	return ((mb_cur_max == 1) ?
121 	    (bwstring_iterator) (bws->cdata.str + bws->cdata.len) :
122 	    (bwstring_iterator) (bws->wdata.str + bws->wdata.len));
123 }
124 
125 static inline bwstring_iterator
126 bws_iterator_inc(bwstring_iterator iter, size_t pos)
127 {
128 
129 	if (mb_cur_max == 1)
130 		return ((unsigned char *) iter) + pos;
131 	else
132 		return ((wchar_t*) iter) + pos;
133 }
134 
135 static inline wchar_t
136 bws_get_iter_value(bwstring_iterator iter)
137 {
138 
139 	if (mb_cur_max == 1)
140 		return *((unsigned char *) iter);
141 	else
142 		return *((wchar_t*) iter);
143 }
144 
145 int
146 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
147 
148 #define	BWS_GET(bws, pos) ((mb_cur_max == 1) ? (bws->cdata.str[(pos)]) : bws->wdata.str[(pos)])
149 
150 void initialise_months(void);
151 
152 #endif /* __BWSTRING_H__ */
153