4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
7 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #if !defined(__BWSTRING_H__)
33 #define __BWSTRING_H__
44 extern bool byte_sort;
46 /* wchar_t is of 4 bytes: */
47 #define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
60 * Binary "wide" string
72 wchar_t *fgetwln_z_buffer;
73 size_t fgetwln_z_buffer_size;
76 typedef void *bwstring_iterator;
78 #define BWSLEN(s) ((mb_cur_max == 1) ? (s)->cdata.len : (s)->wdata.len)
79 struct bwstring *bwsalloc(size_t sz);
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);
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);
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);
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);
109 static inline bwstring_iterator
110 bws_begin(struct bwstring *bws)
113 return ((bwstring_iterator)bws->wdata.str);
116 static inline bwstring_iterator
117 bws_end(struct bwstring *bws)
120 return ((mb_cur_max == 1) ?
121 (bwstring_iterator) (bws->cdata.str + bws->cdata.len) :
122 (bwstring_iterator) (bws->wdata.str + bws->wdata.len));
125 static inline bwstring_iterator
126 bws_iterator_inc(bwstring_iterator iter, size_t pos)
130 return ((unsigned char *) iter) + pos;
132 return ((wchar_t*) iter) + pos;
135 static inline wchar_t
136 bws_get_iter_value(bwstring_iterator iter)
140 return *((unsigned char *) iter);
142 return *((wchar_t*) iter);
146 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
148 #define BWS_GET(bws, pos) ((mb_cur_max == 1) ? (bws->cdata.str[(pos)]) : bws->wdata.str[(pos)])
150 void initialise_months(void);
152 #endif /* __BWSTRING_H__ */