]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/sort/bwstring.h
Remove spurious newline
[FreeBSD/FreeBSD.git] / usr.bin / sort / bwstring.h
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 "mem.h"
42
43 extern bool byte_sort;
44
45 /* wchar_t is of 4 bytes: */
46 #define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
47
48 /*
49  * Binary "wide" string
50  */
51 struct bwstring
52 {
53         size_t                          len;
54         union
55         {
56                 wchar_t         wstr[0];
57                 unsigned char   cstr[0];
58         }                               data;
59 };
60
61 struct reader_buffer
62 {
63         wchar_t                 *fgetwln_z_buffer;
64         size_t                   fgetwln_z_buffer_size;
65 };
66
67 typedef void *bwstring_iterator;
68
69 #define BWSLEN(s) ((s)->len)
70
71 struct bwstring *bwsalloc(size_t sz);
72
73 size_t bwsrawlen(const struct bwstring *bws);
74 const void* bwsrawdata(const struct bwstring *bws);
75 void bws_setlen(struct bwstring *bws, size_t newlen);
76 size_t bws_memsize(const struct bwstring *bws);
77 double bwstod(struct bwstring *s0, bool *empty);
78 int bws_month_score(const struct bwstring *s0);
79
80 struct bwstring *ignore_leading_blanks(struct bwstring *str);
81 struct bwstring *ignore_nonprinting(struct bwstring *str);
82 struct bwstring *dictionary_order(struct bwstring *str);
83 struct bwstring *ignore_case(struct bwstring *str);
84
85 void bwsprintf(FILE*, struct bwstring*, const char *prefix, const char *suffix);
86 void bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos);
87
88 struct bwstring *bwsdup(const struct bwstring *s);
89 struct bwstring *bwssbdup(const wchar_t *str, size_t size);
90 struct bwstring *bwscsbdup(const unsigned char *str, size_t size);
91 void bwsfree(const struct bwstring *s);
92 size_t bwscpy(struct bwstring *dst, const struct bwstring *src);
93 struct bwstring *bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size);
94 struct bwstring *bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size);
95 int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
96 int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len);
97 int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
98 size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
99 struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb);
100
101 static inline bwstring_iterator
102 bws_begin(struct bwstring *bws)
103 {
104
105         return (bwstring_iterator) (&(bws->data));
106 }
107
108 static inline bwstring_iterator
109 bws_end(struct bwstring *bws)
110 {
111
112         return ((MB_CUR_MAX == 1) ?
113             (bwstring_iterator) (bws->data.cstr + bws->len) :
114             (bwstring_iterator) (bws->data.wstr + bws->len));
115 }
116
117 static inline bwstring_iterator
118 bws_iterator_inc(bwstring_iterator iter, size_t pos)
119 {
120
121         if (MB_CUR_MAX == 1)
122                 return ((unsigned char *) iter) + pos;
123         else
124                 return ((wchar_t*) iter) + pos;
125 }
126
127 static inline wchar_t
128 bws_get_iter_value(bwstring_iterator iter)
129 {
130
131         if (MB_CUR_MAX == 1)
132                 return *((unsigned char *) iter);
133         else
134                 return *((wchar_t*) iter);
135 }
136
137 int
138 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
139
140 #define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)])
141
142 void initialise_months(void);
143
144 #endif /* __BWSTRING_H__ */