1*52e4174eSschwarze /* $OpenBSD: bwstring.h,v 1.3 2019/05/15 09:07:46 schwarze Exp $ */
279428148Smillert
379428148Smillert /*-
479428148Smillert * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
579428148Smillert * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
679428148Smillert * All rights reserved.
779428148Smillert *
879428148Smillert * Redistribution and use in source and binary forms, with or without
979428148Smillert * modification, are permitted provided that the following conditions
1079428148Smillert * are met:
1179428148Smillert * 1. Redistributions of source code must retain the above copyright
1279428148Smillert * notice, this list of conditions and the following disclaimer.
1379428148Smillert * 2. Redistributions in binary form must reproduce the above copyright
1479428148Smillert * notice, this list of conditions and the following disclaimer in the
1579428148Smillert * documentation and/or other materials provided with the distribution.
1679428148Smillert *
1779428148Smillert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1879428148Smillert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979428148Smillert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079428148Smillert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2179428148Smillert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2279428148Smillert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2379428148Smillert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2479428148Smillert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2579428148Smillert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2679428148Smillert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2779428148Smillert * SUCH DAMAGE.
2879428148Smillert */
2979428148Smillert
3079428148Smillert #if !defined(__BWSTRING_H__)
3179428148Smillert #define __BWSTRING_H__
3279428148Smillert
3379428148Smillert #include <stdbool.h>
3479428148Smillert #include <stdio.h>
3579428148Smillert #include <errno.h>
3679428148Smillert #include <wchar.h>
3779428148Smillert
3879428148Smillert #include "mem.h"
3979428148Smillert
40*52e4174eSschwarze static const size_t sort_mb_cur_max = 1;
4179428148Smillert
4279428148Smillert /* wchar_t is of 4 bytes: */
4379428148Smillert #define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
4479428148Smillert
4579428148Smillert /*
4679428148Smillert * Binary "wide" string
4779428148Smillert */
4879428148Smillert struct bwstring {
4979428148Smillert size_t len;
5079428148Smillert union
5179428148Smillert {
5279428148Smillert wchar_t wstr[0];
5379428148Smillert unsigned char cstr[0];
5479428148Smillert } data;
5579428148Smillert };
5679428148Smillert
5779428148Smillert struct reader_buffer {
5879428148Smillert wchar_t *fgetwln_z_buffer;
5979428148Smillert size_t fgetwln_z_buffer_size;
6079428148Smillert };
6179428148Smillert
6279428148Smillert typedef void *bwstring_iterator;
6379428148Smillert
6479428148Smillert #define BWSLEN(s) ((s)->len)
6579428148Smillert
6679428148Smillert struct bwstring *bwsalloc(size_t sz);
6779428148Smillert
6879428148Smillert size_t bwsrawlen(const struct bwstring *bws);
6979428148Smillert const void* bwsrawdata(const struct bwstring *bws);
7079428148Smillert void bws_setlen(struct bwstring *bws, size_t newlen);
7179428148Smillert size_t bws_memsize(const struct bwstring *bws);
7279428148Smillert double bwstod(struct bwstring *s0, bool *empty);
7379428148Smillert int bws_month_score(const struct bwstring *s0);
7479428148Smillert
7579428148Smillert struct bwstring *ignore_leading_blanks(struct bwstring *str);
7679428148Smillert struct bwstring *ignore_nonprinting(struct bwstring *str);
7779428148Smillert struct bwstring *dictionary_order(struct bwstring *str);
7879428148Smillert struct bwstring *ignore_case(struct bwstring *str);
7979428148Smillert
8079428148Smillert void bwsprintf(FILE*, struct bwstring*, const char *prefix, const char *suffix);
8179428148Smillert void bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos);
8279428148Smillert
8379428148Smillert struct bwstring *bwsdup(const struct bwstring *s);
8479428148Smillert struct bwstring *bwssbdup(const wchar_t *str, size_t size);
8579428148Smillert struct bwstring *bwscsbdup(const unsigned char *str, size_t size);
8679428148Smillert void bwsfree(struct bwstring *s);
8779428148Smillert size_t bwscpy(struct bwstring *dst, const struct bwstring *src);
8879428148Smillert struct bwstring *bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size);
8979428148Smillert struct bwstring *bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size);
9079428148Smillert int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
9179428148Smillert int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len);
9279428148Smillert int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
9379428148Smillert size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
9479428148Smillert struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb);
9579428148Smillert
9679428148Smillert static inline bwstring_iterator
bws_begin(struct bwstring * bws)9779428148Smillert bws_begin(struct bwstring *bws)
9879428148Smillert {
9979428148Smillert
10079428148Smillert return (bwstring_iterator) (&(bws->data));
10179428148Smillert }
10279428148Smillert
10379428148Smillert static inline bwstring_iterator
bws_end(struct bwstring * bws)10479428148Smillert bws_end(struct bwstring *bws)
10579428148Smillert {
10679428148Smillert
10779428148Smillert return ((sort_mb_cur_max == 1) ?
10879428148Smillert (bwstring_iterator) (bws->data.cstr + bws->len) :
10979428148Smillert (bwstring_iterator) (bws->data.wstr + bws->len));
11079428148Smillert }
11179428148Smillert
11279428148Smillert static inline bwstring_iterator
bws_iterator_inc(bwstring_iterator iter,size_t pos)11379428148Smillert bws_iterator_inc(bwstring_iterator iter, size_t pos)
11479428148Smillert {
11579428148Smillert
11679428148Smillert if (sort_mb_cur_max == 1)
11779428148Smillert return ((unsigned char *) iter) + pos;
11879428148Smillert else
11979428148Smillert return ((wchar_t*) iter) + pos;
12079428148Smillert }
12179428148Smillert
12279428148Smillert static inline wchar_t
bws_get_iter_value(bwstring_iterator iter)12379428148Smillert bws_get_iter_value(bwstring_iterator iter)
12479428148Smillert {
12579428148Smillert
12679428148Smillert if (sort_mb_cur_max == 1)
12779428148Smillert return *((unsigned char *) iter);
12879428148Smillert else
12979428148Smillert return *((wchar_t*) iter);
13079428148Smillert }
13179428148Smillert
13279428148Smillert int
13379428148Smillert bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
13479428148Smillert
13579428148Smillert #define BWS_GET(bws, pos) ((sort_mb_cur_max == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)])
13679428148Smillert
13779428148Smillert void initialise_months(void);
13879428148Smillert
13979428148Smillert #endif /* __BWSTRING_H__ */
140