Imported Upstream version 2.0.0
[platform/upstream/fdupes.git] / wcs.c
1 /* Copyright (c) 2018 Adrian Lopez
2
3    This software is provided 'as-is', without any express or implied
4    warranty. In no event will the authors be held liable for any damages
5    arising from the use of this software.
6
7    Permission is granted to anyone to use this software for any purpose,
8    including commercial applications, and to alter it and redistribute it
9    freely, subject to the following restrictions:
10
11    1. The origin of this software must not be misrepresented; you must not
12       claim that you wrote the original software. If you use this software
13       in a product, an acknowledgment in the product documentation would be
14       appreciated but is not required.
15    2. Altered source versions must be plainly marked as such, and must not be
16       misrepresented as being the original software.
17    3. This notice may not be removed or altered from any source distribution. */
18
19 #include <stdlib.h>
20 #include "wcs.h"
21 #include "mbstowcs_escape_invalid.h"
22
23 /* compare wide and multibyte strings */
24 int wcsmbcscmp(wchar_t *s1, char *s2)
25 {
26   wchar_t *s2w;
27   size_t size;
28   int result;
29
30   size = mbstowcs_escape_invalid(0, s2, 0) + 1;
31
32   s2w = (wchar_t*) malloc(size * sizeof(wchar_t));
33   if (s2w == 0)
34     return -1;
35
36   mbstowcs_escape_invalid(s2w, s2, size);
37
38   result = wcscmp(s1, s2w);
39
40   free(s2w);
41
42   return result;
43 }
44
45 /* wide character needle is contained in multibyte haystack */
46 int wcsinmbcs(char *haystack, wchar_t *needle)
47 {
48   wchar_t *haystackw;
49   size_t size;
50   int result;
51
52   size = mbstowcs_escape_invalid(0, haystack, 0) + 1;
53
54   haystackw = (wchar_t*) malloc(size * sizeof(wchar_t));
55   if (haystackw == 0)
56     return -1;
57
58   mbstowcs_escape_invalid(haystackw, haystack, size);
59
60   if (wcsstr(haystackw, needle) == 0)
61     result = 0;
62   else
63     result = 1;
64
65   free(haystackw);
66
67   return result;
68 }
69
70 /* wide character needle at beginning of multibyte haystack */
71 int wcsbeginmbcs(char *haystack, wchar_t *needle)
72 {
73   wchar_t *haystackw;
74   size_t size;
75   int result;
76
77   size = mbstowcs_escape_invalid(0, haystack, 0);
78
79   haystackw = (wchar_t*) malloc(size * sizeof(wchar_t));
80   if (haystackw == 0)
81     return -1;
82
83   mbstowcs_escape_invalid(haystackw, haystack, size);
84
85   if (wcsncmp(haystackw, needle, wcslen(needle)) == 0)
86     result = 1;
87   else
88     result = 0;
89
90   free(haystackw);
91
92   return result;
93 }
94
95 /* wide character needle at end of multibyte haystack */
96 int wcsendsmbcs(char *haystack, wchar_t *needle)
97 {
98   wchar_t *haystackw;
99   size_t size;
100   int result;
101
102   size = mbstowcs_escape_invalid(0, haystack, 0) + 1;
103
104   haystackw = (wchar_t*) malloc(size * sizeof(wchar_t));
105   if (haystackw == 0)
106     return -1;
107
108   mbstowcs_escape_invalid(haystackw, haystack, size);
109
110   if (wcsrstr(haystackw, needle) != 0 && wcscmp(wcsrstr(haystackw, needle), needle) == 0)
111     result = 1;
112   else
113     result = 0;
114
115   free(haystackw);
116
117   return result;
118 }
119
120 /* wide character reverse string search */
121 wchar_t *wcsrstr(wchar_t *haystack, wchar_t *needle)
122 {
123   wchar_t *found = 0;
124   wchar_t *next = 0;
125
126   found = wcsstr(haystack, needle);
127   if (found)
128   {
129     do {
130       next = wcsstr(found + 1, needle);
131       if (next != 0)
132         found = next;
133     } while (next);
134
135     return found;
136   }
137
138   return 0;
139 }