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