]> git.sesse.net Git - plocate/blob - lib.cpp
Remove dependency on non-POSIX header error.h.
[plocate] / lib.cpp
1 /* Common functions.
2
3 Copyright (C) 2005, 2007 Red Hat, Inc. All rights reserved.
4 This copyrighted material is made available to anyone wishing to use, modify,
5 copy, or redistribute it subject to the terms and conditions of the GNU General
6 Public License v.2.
7
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License along with
13 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
14 Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
16 Author: Miloslav Trmac <mitr@redhat.com>
17
18 plocate modifications: Copyright (C) 2020 Steinar H. Gunderson.
19 plocate parts and modifications are licensed under the GPLv2 or, at your option,
20 any later version.
21 */
22
23 #include "lib.h"
24
25 #include "db.h"
26
27 #include <algorithm>
28 #include <arpa/inet.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 using namespace std;
40
41 /* Compare two path names using the database directory order. This is not
42    exactly strcmp () order: "a" < "a.b", so "a/z" < "a.b". */
43 int dir_path_cmp(const string &a, const string &b)
44 {
45         auto [ai, bi] = mismatch(a.begin(), a.end(), b.begin(), b.end());
46         if (ai == a.end() && bi == b.end()) {
47                 return 0;
48         }
49         if (ai == a.end()) {
50                 return -1;
51         }
52         if (bi == b.end()) {
53                 return 1;
54         }
55         if (*ai == *bi) {
56                 return 0;
57         }
58         if (*ai == '/') {
59                 return -1;
60         }
61         if (*bi == '/') {
62                 return 1;
63         }
64         return int((unsigned char)*ai) - int((unsigned char)*bi);
65 }
66
67 /* Sort LIST using dir_path_cmp () */
68 void string_list_dir_path_sort(vector<string> *list)
69 {
70         sort(list->begin(), list->end(), [](const string &a, const string &b) {
71                 return dir_path_cmp(a, b) < 0;
72         });
73 }
74
75 /* Is PATH included in LIST?  Update *IDX to move within LIST.
76
77    LIST is assumed to be sorted using dir_path_cmp (), successive calls to this
78    function are assumed to use PATH values increasing in dir_path_cmp (). */
79 bool string_list_contains_dir_path(const vector<string> *list, size_t *idx,
80                                    const string &path)
81 {
82         int cmp = 0;
83         while (*idx < list->size() && (cmp = dir_path_cmp((*list)[*idx], path)) < 0) {
84                 (*idx)++;
85         }
86         if (*idx < list->size() && cmp == 0) {
87                 (*idx)++;
88                 return true;
89         }
90         return false;
91 }