1235267Sgabor/*	$FreeBSD: stable/10/usr.bin/sort/coll.h 318152 2017-05-10 20:29:01Z marius $	*/
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(__COLL_H__)
31265160Spfg#define	__COLL_H__
32235267Sgabor
33235267Sgabor#include "bwstring.h"
34235267Sgabor#include "sort.h"
35235267Sgabor
36235267Sgabor/*
37235267Sgabor * Sort hint data for -n
38235267Sgabor */
39235267Sgaborstruct n_hint
40235267Sgabor{
41235267Sgabor	unsigned long long	 n1;
42235267Sgabor	unsigned char		 si;
43235267Sgabor	bool			 empty;
44235267Sgabor	bool			 neg;
45235267Sgabor};
46235267Sgabor
47235267Sgabor/*
48235267Sgabor * Sort hint data for -g
49235267Sgabor */
50235267Sgaborstruct g_hint
51235267Sgabor{
52235267Sgabor	double			 d;
53235267Sgabor	bool			 nan;
54235267Sgabor	bool			 notnum;
55235267Sgabor};
56235267Sgabor
57235267Sgabor/*
58235267Sgabor * Sort hint data for -M
59235267Sgabor */
60235267Sgaborstruct M_hint
61235267Sgabor{
62235267Sgabor	int			 m;
63235267Sgabor};
64235267Sgabor
65235267Sgabor/*
66235267Sgabor * Status of a sort hint object
67235267Sgabor */
68235267Sgabortypedef enum
69235267Sgabor{
70235267Sgabor	HS_ERROR = -1, HS_UNINITIALIZED = 0, HS_INITIALIZED = 1
71235267Sgabor} hint_status;
72235267Sgabor
73235267Sgabor/*
74235267Sgabor * Sort hint object
75235267Sgabor */
76235267Sgaborstruct key_hint
77235267Sgabor{
78235267Sgabor	hint_status		status;
79235267Sgabor	union
80235267Sgabor	{
81235267Sgabor		struct n_hint		nh;
82235267Sgabor		struct g_hint		gh;
83235267Sgabor		struct M_hint		Mh;
84235267Sgabor	}			v;
85235267Sgabor};
86235267Sgabor
87235267Sgabor/*
88235267Sgabor * Key value
89235267Sgabor */
90235267Sgaborstruct key_value
91235267Sgabor{
92235267Sgabor	struct bwstring		*k; /* key string */
93235267Sgabor	struct key_hint		 hint[0]; /* key sort hint */
94318152Smarius} __packed;
95235267Sgabor
96235267Sgabor/*
97235267Sgabor * Set of keys container object.
98235267Sgabor */
99235267Sgaborstruct keys_array
100235267Sgabor{
101235267Sgabor	struct key_value	 key[0];
102235267Sgabor};
103235267Sgabor
104235267Sgabor/*
105235267Sgabor * Parsed -k option data
106235267Sgabor */
107235267Sgaborstruct key_specs
108235267Sgabor{
109235267Sgabor	struct sort_mods	 sm;
110235267Sgabor	size_t			 c1;
111235267Sgabor	size_t			 c2;
112235267Sgabor	size_t			 f1;
113235267Sgabor	size_t			 f2;
114235267Sgabor	bool			 pos1b;
115235267Sgabor	bool			 pos2b;
116235267Sgabor};
117235267Sgabor
118235267Sgabor/*
119235267Sgabor * Single entry in sort list.
120235267Sgabor */
121235267Sgaborstruct sort_list_item
122235267Sgabor{
123235267Sgabor	struct bwstring		*str;
124235267Sgabor	struct keys_array	 ka;
125235267Sgabor};
126235267Sgabor
127235267Sgabor/*
128235267Sgabor * Function type, used to compare two list objects
129235267Sgabor */
130235267Sgabortypedef int (*listcoll_t)(struct sort_list_item **ss1, struct sort_list_item **ss2);
131235267Sgabor
132235267Sgaborextern struct key_specs *keys;
133235267Sgaborextern size_t keys_num;
134235267Sgabor
135235267Sgabor/*
136242430Sgabor * Main localised symbols. These must be wint_t as they may hold WEOF.
137235267Sgabor */
138242430Sgaborextern wint_t symbol_decimal_point;
139242430Sgaborextern wint_t symbol_thousands_sep;
140242430Sgaborextern wint_t symbol_negative_sign;
141242430Sgaborextern wint_t symbol_positive_sign;
142235267Sgabor
143235267Sgabor/* funcs */
144235267Sgabor
145235267Sgaborcmpcoll_t get_sort_func(struct sort_mods *sm);
146235267Sgabor
147235267Sgaborstruct keys_array *keys_array_alloc(void);
148235267Sgaborsize_t keys_array_size(void);
149318152Smariusstruct key_value *get_key_from_keys_array(struct keys_array *ka, size_t ind);
150235267Sgaborvoid set_key_on_keys_array(struct keys_array *ka, struct bwstring *s, size_t ind);
151235267Sgaborvoid clean_keys_array(const struct bwstring *s, struct keys_array *ka);
152235267Sgabor
153235267Sgaborstruct sort_list_item *sort_list_item_alloc(void);
154235267Sgaborvoid sort_list_item_set(struct sort_list_item *si, struct bwstring *str);
155235267Sgaborvoid sort_list_item_clean(struct sort_list_item *si);
156235267Sgaborsize_t sort_list_item_size(struct sort_list_item *si);
157235267Sgabor
158235267Sgaborint preproc(struct bwstring *s, struct keys_array *ka);
159235267Sgaborint top_level_str_coll(const struct bwstring *, const struct bwstring *);
160235267Sgaborint key_coll(struct keys_array *ks1, struct keys_array *ks2, size_t offset);
161235267Sgaborint str_list_coll(struct bwstring *str1, struct sort_list_item **ss2);
162235267Sgaborint list_coll_by_str_only(struct sort_list_item **ss1, struct sort_list_item **ss2);
163235267Sgaborint list_coll(struct sort_list_item **ss1, struct sort_list_item **ss2);
164235267Sgaborint list_coll_offset(struct sort_list_item **ss1, struct sort_list_item **ss2, size_t offset);
165235267Sgabor
166235267Sgaborlistcoll_t get_list_call_func(size_t offset);
167235267Sgabor
168235267Sgabor#endif /* __COLL_H__ */
169