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