sbin/mount_hammer: Use calloc(3) and cleanups
[dragonfly.git] / usr.bin / sort / coll.h
blob3f2a3560ff68d121ea3c5e015cffc928385c6382
1 /* $FreeBSD: head/usr.bin/sort/coll.h 264744 2014-04-21 22:52:18Z pfg $ */
3 /*-
4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #if !defined(__COLL_H__)
31 #define __COLL_H__
33 #include "bwstring.h"
34 #include "sort.h"
37 * Sort hint data for -n
39 struct n_hint
41 unsigned long long n1;
42 unsigned char si;
43 bool empty;
44 bool neg;
48 * Sort hint data for -g
50 struct g_hint
52 double d;
53 bool nan;
54 bool notnum;
58 * Sort hint data for -M
60 struct M_hint
62 int m;
66 * Status of a sort hint object
68 typedef enum
70 HS_ERROR = -1, HS_UNINITIALIZED = 0, HS_INITIALIZED = 1
71 } hint_status;
74 * Sort hint object
76 struct key_hint
78 hint_status status;
79 union
81 struct n_hint nh;
82 struct g_hint gh;
83 struct M_hint Mh;
84 } v;
88 * Key value
90 struct key_value
92 struct bwstring *k; /* key string */
93 struct key_hint hint[0]; /* key sort hint */
97 * Set of keys container object.
99 struct keys_array
101 struct key_value key[0];
105 * Parsed -k option data
107 struct key_specs
109 struct sort_mods sm;
110 size_t c1;
111 size_t c2;
112 size_t f1;
113 size_t f2;
114 bool pos1b;
115 bool pos2b;
119 * Single entry in sort list.
121 struct sort_list_item
123 struct bwstring *str;
124 struct keys_array ka;
128 * Function type, used to compare two list objects
130 typedef int (*listcoll_t)(struct sort_list_item **ss1, struct sort_list_item **ss2);
132 extern struct key_specs *keys;
133 extern size_t keys_num;
136 * Main localised symbols. These must be wint_t as they may hold WEOF.
138 extern wint_t symbol_decimal_point;
139 extern wint_t symbol_thousands_sep;
140 extern wint_t symbol_negative_sign;
141 extern wint_t symbol_positive_sign;
143 /* funcs */
145 cmpcoll_t get_sort_func(struct sort_mods *sm);
147 struct keys_array *keys_array_alloc(void);
148 size_t keys_array_size(void);
149 void set_key_on_keys_array(struct keys_array *ka, struct bwstring *s, size_t ind);
150 void clean_keys_array(const struct bwstring *s, struct keys_array *ka);
152 struct sort_list_item *sort_list_item_alloc(void);
153 void sort_list_item_set(struct sort_list_item *si, struct bwstring *str);
154 void sort_list_item_clean(struct sort_list_item *si);
155 size_t sort_list_item_size(struct sort_list_item *si);
157 int preproc(struct bwstring *s, struct keys_array *ka);
158 int top_level_str_coll(const struct bwstring *, const struct bwstring *);
159 int key_coll(struct keys_array *ks1, struct keys_array *ks2, size_t offset);
160 int str_list_coll(struct bwstring *str1, struct sort_list_item **ss2);
161 int list_coll_by_str_only(struct sort_list_item **ss1, struct sort_list_item **ss2);
162 int list_coll(struct sort_list_item **ss1, struct sort_list_item **ss2);
163 int list_coll_offset(struct sort_list_item **ss1, struct sort_list_item **ss2, size_t offset);
165 listcoll_t get_list_call_func(size_t offset);
167 #endif /* __COLL_H__ */