Updated Russian translation.
[midnight-commander.git] / lib / search / hex.c
blob42416c6f9d9585560b493c3e4e7eee77170b931e
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 "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #include "lib/strescape.h"
37 #include "lib/charsets.h"
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 /*** file scope functions ************************************************************************/
51 static GString *
52 mc_search__hex_translate_to_regex (const GString * astr)
54 GString *buff;
55 gchar *tmp_str;
56 gsize tmp_str_len;
57 gsize loop = 0;
59 buff = g_string_sized_new (64);
60 tmp_str = g_strndup (astr->str, astr->len);
61 g_strchug (tmp_str); /* trim leadind whitespaces */
62 tmp_str_len = strlen (tmp_str);
64 while (loop < tmp_str_len)
66 int val, ptr;
68 if (sscanf (tmp_str + loop, "%i%n", &val, &ptr))
70 if (val < -128 || val > 255)
71 loop++;
72 else
74 g_string_append_printf (buff, "\\x%02X", (unsigned char) val);
75 loop += ptr;
78 else if (*(tmp_str + loop) == '"')
80 gsize loop2 = 0;
82 loop++;
83 while (loop + loop2 < tmp_str_len)
85 if (*(tmp_str + loop + loop2) == '"' &&
86 !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
87 break;
88 loop2++;
91 g_string_append_len (buff, tmp_str + loop, loop2 - 1);
92 loop += loop2;
94 else
95 loop++;
98 g_free (tmp_str);
100 return buff;
103 /*** public functions ****************************************************************************/
105 void
106 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
107 mc_search_cond_t * mc_search_cond)
109 GString *tmp;
111 tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
112 g_string_free (mc_search_cond->str, TRUE);
113 mc_search_cond->str = tmp;
115 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
118 /* --------------------------------------------------------------------------------------------- */
120 gboolean
121 mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
122 gsize start_search, gsize end_search, gsize * found_len)
124 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
127 /* --------------------------------------------------------------------------------------------- */
129 GString *
130 mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
132 (void) lc_mc_search;
133 return g_string_new_len (replace_str->str, replace_str->len);
136 /* --------------------------------------------------------------------------------------------- */