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