root/lib/search/lib.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_search__recode_str
  2. mc_search__get_one_symbol
  3. mc_search__tolower_case_str
  4. mc_search__toupper_case_str
  5. mc_search_get_types_strings_array

   1 /*
   2    Search text engine.
   3    Common share code for module.
   4 
   5    Copyright (C) 2009-2019
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009, 2011
  10    Andrew Borodin <aborodin@vmail.ru>, 2013
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26  */
  27 
  28 #include <config.h>
  29 
  30 #include <stdlib.h>
  31 #include <sys/types.h>
  32 
  33 #include "lib/global.h"
  34 #include "lib/strutil.h"
  35 #include "lib/search.h"
  36 #ifdef HAVE_CHARSET
  37 #include "lib/charsets.h"
  38 #endif
  39 
  40 #include "internal.h"
  41 
  42 /*** global variables ****************************************************************************/
  43 
  44 const char *STR_E_NOTFOUND = N_("Search string not found");
  45 const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
  46 const char *STR_E_RPL_NOT_EQ_TO_FOUND =
  47 N_("Num of replace tokens not equal to num of found tokens");
  48 const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
  49 
  50 /*** file scope macro definitions ****************************************************************/
  51 
  52 /*** file scope type declarations ****************************************************************/
  53 
  54 /*** file scope variables ************************************************************************/
  55 
  56 /*** file scope functions ************************************************************************/
  57 
  58 /*** public functions ****************************************************************************/
  59 
  60 gchar *
  61 mc_search__recode_str (const char *str, gsize str_len,
     /* [previous][next][first][last][top][bottom][index][help]  */
  62                        const char *charset_from, const char *charset_to, gsize * bytes_written)
  63 {
  64     gchar *ret = NULL;
  65 
  66     if (charset_from != NULL && charset_to != NULL
  67         && g_ascii_strcasecmp (charset_to, charset_from) != 0)
  68     {
  69         GIConv conv;
  70 
  71         conv = g_iconv_open (charset_to, charset_from);
  72         if (conv != INVALID_CONV)
  73         {
  74             gsize bytes_read;
  75 
  76             ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
  77             g_iconv_close (conv);
  78         }
  79     }
  80 
  81     if (ret == NULL)
  82     {
  83         *bytes_written = str_len;
  84         ret = g_strndup (str, str_len);
  85     }
  86 
  87     return ret;
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 gchar *
  93 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
     /* [previous][next][first][last][top][bottom][index][help]  */
  94                            gboolean * just_letters)
  95 {
  96     gchar *converted_str;
  97     const gchar *next_char;
  98 
  99     gsize tmp_len;
 100 #ifdef HAVE_CHARSET
 101     gsize converted_str_len;
 102     gchar *converted_str2;
 103 
 104     if (charset == NULL)
 105         charset = cp_source;
 106 
 107     converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
 108 #else
 109     (void) charset;
 110 
 111     converted_str = g_strndup (str, str_len);
 112 #endif
 113 
 114     next_char = str_cget_next_char (converted_str);
 115 
 116     tmp_len = next_char - converted_str;
 117 
 118     converted_str[tmp_len] = '\0';
 119 
 120 #ifdef HAVE_CHARSET
 121     converted_str2 =
 122         mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
 123 #endif
 124     if (just_letters != NULL)
 125         *just_letters = str_isalnum (converted_str) && !str_isdigit (converted_str);
 126 #ifdef HAVE_CHARSET
 127     g_free (converted_str);
 128     return converted_str2;
 129 #else
 130     return converted_str;
 131 #endif
 132 }
 133 
 134 /* --------------------------------------------------------------------------------------------- */
 135 
 136 GString *
 137 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 138 {
 139     GString *ret;
 140 #ifdef HAVE_CHARSET
 141     gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
 142     gsize converted_str_len;
 143     gsize tmp_len;
 144 
 145     if (charset == NULL)
 146         charset = cp_source;
 147 
 148     tmp_str2 = converted_str =
 149         mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
 150 
 151     tmp_len = converted_str_len + 1;
 152 
 153     tmp_str3 = tmp_str1 = g_strdup (converted_str);
 154 
 155     while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
 156         tmp_str1 += str_length_char (tmp_str1);
 157 
 158     g_free (tmp_str3);
 159     tmp_str2 =
 160         mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
 161     g_free (converted_str);
 162 
 163     ret = g_string_new_len (tmp_str2, tmp_len);
 164     g_free (tmp_str2);
 165     return ret;
 166 #else
 167     const gchar *tmp_str1 = str;
 168     gchar *converted_str, *tmp_str2;
 169     gsize converted_str_len = str_len + 1;
 170 
 171     (void) charset;
 172 
 173     tmp_str2 = converted_str = g_strndup (str, str_len);
 174 
 175     while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
 176         tmp_str1 += str_length_char (tmp_str1);
 177 
 178     ret = g_string_new_len (converted_str, str_len);
 179     g_free (converted_str);
 180     return ret;
 181 #endif
 182 }
 183 
 184 /* --------------------------------------------------------------------------------------------- */
 185 
 186 GString *
 187 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     GString *ret;
 190 #ifdef HAVE_CHARSET
 191     gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
 192     gsize converted_str_len;
 193     gsize tmp_len;
 194 
 195     if (charset == NULL)
 196         charset = cp_source;
 197 
 198     tmp_str2 = converted_str =
 199         mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
 200 
 201     tmp_len = converted_str_len + 1;
 202 
 203     tmp_str3 = tmp_str1 = g_strdup (converted_str);
 204 
 205     while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
 206         tmp_str1 += str_length_char (tmp_str1);
 207 
 208     g_free (tmp_str3);
 209 
 210     tmp_str2 =
 211         mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
 212     g_free (converted_str);
 213 
 214     ret = g_string_new_len (tmp_str2, tmp_len);
 215     g_free (tmp_str2);
 216     return ret;
 217 #else
 218 
 219     const gchar *tmp_str1 = str;
 220     gchar *converted_str, *tmp_str2;
 221     gsize converted_str_len = str_len + 1;
 222 
 223     (void) charset;
 224 
 225     tmp_str2 = converted_str = g_strndup (str, str_len);
 226 
 227     while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
 228         tmp_str1 += str_length_char (tmp_str1);
 229 
 230     ret = g_string_new_len (converted_str, str_len);
 231     g_free (converted_str);
 232     return ret;
 233 #endif
 234 }
 235 
 236 /* --------------------------------------------------------------------------------------------- */
 237 
 238 gchar **
 239 mc_search_get_types_strings_array (size_t * num)
     /* [previous][next][first][last][top][bottom][index][help]  */
 240 {
 241     gchar **ret;
 242     int lc_index;
 243     size_t n;
 244 
 245     const mc_search_type_str_t *type_str;
 246     const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
 247 
 248     ret = g_try_new0 (char *, n + 1);
 249     if (ret == NULL)
 250         return NULL;
 251 
 252     for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
 253         ret[lc_index] = g_strdup (type_str->str);
 254 
 255     /* don't count last NULL item */
 256     if (num != NULL)
 257         *num = (size_t) lc_index;
 258 
 259     return ret;
 260 }
 261 
 262 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */