Use strcasestr.

Since it's a GNU extension, move to strcasestr.h.
This commit is contained in:
Themaister 2013-12-10 20:10:19 +01:00
parent fde4532a33
commit 6fa3cb2f85
3 changed files with 76 additions and 1 deletions

View File

@ -202,6 +202,39 @@ int getopt_long(int argc, char *argv[],
#endif
#ifndef HAVE_STRCASESTR
// Pretty much strncasecmp.
static int casencmp(const char *a, const char *b, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
int a_lower = tolower(a[i]);
int b_lower = tolower(b[i]);
if (a_lower != b_lower)
return a_lower - b_lower;
}
return 0;
}
char *strcasestr_rarch__(const char *haystack, const char *needle)
{
size_t i;
size_t hay_len = strlen(haystack);
size_t needle_len = strlen(needle);
if (needle_len > hay_len)
return NULL;
size_t search_off = hay_len - needle_len;
for (i = 0; i <= search_off; i++)
if (!casencmp(haystack + i, needle, needle_len))
return (char*)haystack + i;
return NULL;
}
#endif
#ifndef HAVE_STRL
// Implementation of strlcpy()/strlcat() based on OpenBSD.

41
compat/strcasestr.h Normal file
View File

@ -0,0 +1,41 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RARCH_STRCASESTR_H
#define RARCH_STRCASESTR_H
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef HAVE_STRCASESTR
#ifdef __cplusplus
extern "C" {
#endif
// Avoid possible naming collisions during link since we prefer to use the actual name.
#define strcasestr(haystack, needle) strcasestr_rarch__(haystack, needle)
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif
#endif
#endif

View File

@ -17,6 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include "file_list.h"
#include "compat/strcasestr.h"
struct item_file
{
@ -128,7 +129,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *index
file_list_get_alt_at_offset(list, i, &alt);
if (!alt)
continue;
if (strstr(alt, needle))
if (strcasestr(alt, needle)) // GNU, but compat version in posix_string.h.
{
*index = i;
return true;