lib/charsets.[ch]: clarify usage of const qualifier.
[midnight-commander.git] / lib / search / hex.c
blob9e3dbcdf97449ab1048f8e5bd2a58e02082bce22
1 /*
2 Search text engine.
3 HEX-style pattern matching
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include <stdio.h>
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/search.h"
34 #include "lib/strescape.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
48 static GString *
49 mc_search__hex_translate_to_regex (const GString * astr)
51 GString *buff;
52 gchar *tmp_str, *tmp_str2;
53 gsize tmp_str_len;
54 gsize loop = 0;
56 buff = g_string_sized_new (64);
57 tmp_str = g_strndup (astr->str, astr->len);
58 tmp_str2 = tmp_str;
60 /* remove 0x prefices */
61 while (TRUE)
63 tmp_str2 = strstr (tmp_str2, "0x");
64 if (tmp_str2 == NULL)
65 break;
67 *tmp_str2++ = ' ';
68 *tmp_str2++ = ' ';
71 g_strchug (tmp_str); /* trim leadind whitespaces */
72 tmp_str_len = strlen (tmp_str);
74 while (loop < tmp_str_len)
76 int val, ptr;
78 /* cppcheck-suppress invalidscanf */
79 if (sscanf (tmp_str + loop, "%x%n", &val, &ptr))
81 if (val < -128 || val > 255)
82 loop++;
83 else
85 g_string_append_printf (buff, "\\x%02X", (unsigned char) val);
86 loop += ptr;
89 else if (*(tmp_str + loop) == '"')
91 gsize loop2 = 0;
93 loop++;
94 while (loop + loop2 < tmp_str_len)
96 if (*(tmp_str + loop + loop2) == '"' &&
97 !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
98 break;
99 loop2++;
102 g_string_append_len (buff, tmp_str + loop, loop2 - 1);
103 loop += loop2;
105 else
106 loop++;
109 g_free (tmp_str);
111 return buff;
114 /*** public functions ****************************************************************************/
116 void
117 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
118 mc_search_cond_t * mc_search_cond)
120 GString *tmp;
122 g_string_ascii_down (mc_search_cond->str);
123 tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
124 g_string_free (mc_search_cond->str, TRUE);
125 mc_search_cond->str = tmp;
127 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
130 /* --------------------------------------------------------------------------------------------- */
132 gboolean
133 mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
134 gsize start_search, gsize end_search, gsize * found_len)
136 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
139 /* --------------------------------------------------------------------------------------------- */
141 GString *
142 mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
144 (void) lc_mc_search;
145 return g_string_new_len (replace_str->str, replace_str->len);
148 /* --------------------------------------------------------------------------------------------- */