1235267Sgabor/* $FreeBSD$ */ 2235267Sgabor 3235267Sgabor/*- 4235267Sgabor * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 5251245Sgabor * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com> 6235267Sgabor * All rights reserved. 7235267Sgabor * 8235267Sgabor * Redistribution and use in source and binary forms, with or without 9235267Sgabor * modification, are permitted provided that the following conditions 10235267Sgabor * are met: 11235267Sgabor * 1. Redistributions of source code must retain the above copyright 12235267Sgabor * notice, this list of conditions and the following disclaimer. 13235267Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14235267Sgabor * notice, this list of conditions and the following disclaimer in the 15235267Sgabor * documentation and/or other materials provided with the distribution. 16235267Sgabor * 17235267Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18235267Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19235267Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20235267Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21235267Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22235267Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23235267Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24235267Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25235267Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26235267Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27235267Sgabor * SUCH DAMAGE. 28235267Sgabor */ 29235267Sgabor 30235267Sgabor#if !defined(__BWSTRING_H__) 31265160Spfg#define __BWSTRING_H__ 32235267Sgabor 33235267Sgabor#include <stdbool.h> 34235267Sgabor#include <stdio.h> 35235267Sgabor#include <errno.h> 36235267Sgabor#include <sysexits.h> 37235267Sgabor#include <wchar.h> 38235267Sgabor 39235267Sgabor#include "mem.h" 40235267Sgabor 41235267Sgaborextern bool byte_sort; 42235267Sgabor 43235267Sgabor/* wchar_t is of 4 bytes: */ 44265160Spfg#define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t)) 45235267Sgabor 46235267Sgabor/* 47235267Sgabor * Binary "wide" string 48235267Sgabor */ 49235267Sgaborstruct bwstring 50235267Sgabor{ 51235267Sgabor size_t len; 52235267Sgabor union 53235267Sgabor { 54235267Sgabor wchar_t wstr[0]; 55235267Sgabor unsigned char cstr[0]; 56235267Sgabor } data; 57235267Sgabor}; 58235267Sgabor 59235267Sgaborstruct reader_buffer 60235267Sgabor{ 61235267Sgabor wchar_t *fgetwln_z_buffer; 62235267Sgabor size_t fgetwln_z_buffer_size; 63235267Sgabor}; 64235267Sgabor 65235267Sgabortypedef void *bwstring_iterator; 66235267Sgabor 67265160Spfg#define BWSLEN(s) ((s)->len) 68235267Sgabor 69235267Sgaborstruct bwstring *bwsalloc(size_t sz); 70235267Sgabor 71235267Sgaborsize_t bwsrawlen(const struct bwstring *bws); 72235267Sgaborconst void* bwsrawdata(const struct bwstring *bws); 73235267Sgaborvoid bws_setlen(struct bwstring *bws, size_t newlen); 74235267Sgaborsize_t bws_memsize(const struct bwstring *bws); 75235267Sgabordouble bwstod(struct bwstring *s0, bool *empty); 76235267Sgaborint bws_month_score(const struct bwstring *s0); 77235267Sgabor 78235267Sgaborstruct bwstring *ignore_leading_blanks(struct bwstring *str); 79235267Sgaborstruct bwstring *ignore_nonprinting(struct bwstring *str); 80235267Sgaborstruct bwstring *dictionary_order(struct bwstring *str); 81235267Sgaborstruct bwstring *ignore_case(struct bwstring *str); 82235267Sgabor 83235267Sgaborvoid bwsprintf(FILE*, struct bwstring*, const char *prefix, const char *suffix); 84235267Sgaborvoid bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos); 85235267Sgabor 86235267Sgaborstruct bwstring *bwsdup(const struct bwstring *s); 87235267Sgaborstruct bwstring *bwssbdup(const wchar_t *str, size_t size); 88235267Sgaborstruct bwstring *bwscsbdup(const unsigned char *str, size_t size); 89235267Sgaborvoid bwsfree(const struct bwstring *s); 90235267Sgaborsize_t bwscpy(struct bwstring *dst, const struct bwstring *src); 91235267Sgaborstruct bwstring *bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size); 92235267Sgaborstruct bwstring *bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size); 93235267Sgaborint bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset); 94235267Sgaborint bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len); 95235267Sgaborint bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset); 96242430Sgaborsize_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended); 97235267Sgaborstruct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb); 98235267Sgabor 99235267Sgaborstatic inline bwstring_iterator 100235267Sgaborbws_begin(struct bwstring *bws) 101235267Sgabor{ 102235267Sgabor 103235267Sgabor return (bwstring_iterator) (&(bws->data)); 104235267Sgabor} 105235267Sgabor 106235267Sgaborstatic inline bwstring_iterator 107235267Sgaborbws_end(struct bwstring *bws) 108235267Sgabor{ 109235267Sgabor 110235267Sgabor return ((MB_CUR_MAX == 1) ? 111235267Sgabor (bwstring_iterator) (bws->data.cstr + bws->len) : 112235267Sgabor (bwstring_iterator) (bws->data.wstr + bws->len)); 113235267Sgabor} 114235267Sgabor 115235267Sgaborstatic inline bwstring_iterator 116235267Sgaborbws_iterator_inc(bwstring_iterator iter, size_t pos) 117235267Sgabor{ 118235267Sgabor 119235267Sgabor if (MB_CUR_MAX == 1) 120235267Sgabor return ((unsigned char *) iter) + pos; 121235267Sgabor else 122235267Sgabor return ((wchar_t*) iter) + pos; 123235267Sgabor} 124235267Sgabor 125235267Sgaborstatic inline wchar_t 126235267Sgaborbws_get_iter_value(bwstring_iterator iter) 127235267Sgabor{ 128235267Sgabor 129235267Sgabor if (MB_CUR_MAX == 1) 130235267Sgabor return *((unsigned char *) iter); 131235267Sgabor else 132235267Sgabor return *((wchar_t*) iter); 133235267Sgabor} 134235267Sgabor 135235267Sgaborint 136235267Sgaborbws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len); 137235267Sgabor 138265160Spfg#define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)]) 139235267Sgabor 140235267Sgaborvoid initialise_months(void); 141235267Sgabor 142235267Sgabor#endif /* __BWSTRING_H__ */ 143