Massive moved some dirs from $(srcdir)/src into $(srcdir)/lib
[midnight-commander.git] / lib / search / hex.c
blob7675d0be197510f7e5270575b920fddab3a35837
1 /*
2 Search text engine.
3 HEX-style pattern matching
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
30 #include <stdio.h>
32 #include "../src/global.h"
33 #include "../src/search/search.h"
34 #include "../src/search/internal.h"
35 #include "../src/strutil.h"
36 #include "../src/strescape.h"
37 #include "../src/charsets.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 /*** file scope functions ************************************************************************/
49 static GString *
50 mc_search__hex_translate_to_regex (gchar * str, gsize * len)
52 GString *buff = g_string_new ("");
53 gchar *tmp_str = g_strndup (str, *len);
54 gchar *tmp_str2;
55 gsize loop = 0;
56 int val, ptr;
58 g_strchug (tmp_str); /* trim leadind whitespaces */
60 while (loop < *len) {
61 if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) {
62 if (val < -128 || val > 255) {
63 loop++;
64 continue;
66 tmp_str2 = g_strdup_printf ("\\x%02X", (unsigned char) val);
67 g_string_append (buff, tmp_str2);
68 g_free (tmp_str2);
69 loop += ptr;
70 continue;
73 if (*(tmp_str + loop) == '"') {
74 gsize loop2 = 0;
75 loop++;
76 while (loop + loop2 < *len) {
77 if (*(tmp_str + loop + loop2) == '"' &&
78 !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
79 break;
80 loop2++;
82 g_string_append_len (buff, tmp_str + loop, loop2 - 1);
83 loop += loop2;
84 continue;
86 loop++;
88 *len = buff->len;
89 return buff;
92 /*** public functions ****************************************************************************/
94 void
95 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
96 mc_search_cond_t * mc_search_cond)
98 GString *tmp =
99 mc_search__hex_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len);
101 g_string_free (mc_search_cond->str, TRUE);
102 mc_search_cond->str = tmp;
104 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
108 /* --------------------------------------------------------------------------------------------- */
110 gboolean
111 mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
112 gsize start_search, gsize end_search, gsize * found_len)
114 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
117 /* --------------------------------------------------------------------------------------------- */
119 GString *
120 mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
122 (void) lc_mc_search;
123 return g_string_new_len (replace_str->str, replace_str->len);
126 /* --------------------------------------------------------------------------------------------- */