RetroArch/libretro-common/playlists/label_sanitization.c

199 lines
5.2 KiB
C
Raw Normal View History

2020-01-31 15:43:42 +01:00
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
2020-01-30 17:14:45 +01:00
* The following license statement only applies to this file (label_sanitization.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <playlists/label_sanitization.h>
#include <compat/strl.h>
2019-07-28 15:52:48 +02:00
#include <retro_miscellaneous.h>
#include <string/stdstring.h>
#include <string.h>
2020-08-26 15:13:48 +02:00
#define DISC_STRINGS_LENGTH 3
#define REGION_STRINGS_LENGTH 20
2020-08-26 15:13:48 +02:00
const char *disc_strings[DISC_STRINGS_LENGTH] = {
"(CD",
"(Disc",
"(Disk"
};
/*
* We'll use the standard No-Intro regions for now.
*/
2020-08-26 15:13:48 +02:00
const char *region_strings[REGION_STRINGS_LENGTH] = {
"(Australia)", /* Dont use with Europe */
"(Brazil)",
"(Canada)", /* Dont use with USA */
"(China)",
"(France)",
"(Germany)",
"(Hong Kong)",
"(Italy)",
"(Japan)",
"(Korea)",
"(Netherlands)",
"(Spain)",
"(Sweden)",
"(USA)", /* Includes Canada */
"(World)",
"(Europe)", /* Includes Australia */
"(Asia)",
"(Japan, USA)",
"(Japan, Europe)",
"(USA, Europe)"
};
/*
* Does not work with nested blocks.
*/
void label_sanitize(char *label, bool (*left)(char*), bool (*right)(char*))
{
bool copy = true;
int rindex = 0;
int lindex = 0;
2019-07-28 15:52:48 +02:00
char new_label[PATH_MAX_LENGTH];
for (; lindex < PATH_MAX_LENGTH && label[lindex] != '\0'; lindex++)
{
if (copy)
{
/* check for the start of the range */
if ((*left)(&label[lindex]))
copy = false;
if (copy)
new_label[rindex++] = label[lindex];
}
else if ((*right)(&label[lindex]))
copy = true;
}
new_label[rindex] = '\0';
strlcpy(label, new_label, PATH_MAX_LENGTH);
}
static bool left_parens(char *left)
{
return left[0] == '(';
}
static bool right_parens(char *right)
{
return right[0] == ')';
}
static bool left_brackets(char *left)
{
return left[0] == '[';
}
static bool right_brackets(char *right)
{
return right[0] == ']';
}
static bool left_parens_or_brackets(char *left)
{
return left[0] == '(' || left[0] == '[';
}
static bool right_parens_or_brackets(char *right)
{
return right[0] == ')' || right[0] == ']';
}
static bool left_exclusion(char *left,
2019-08-15 13:51:07 +02:00
const char **strings, const size_t strings_count)
{
2019-08-15 13:51:07 +02:00
unsigned i;
char exclusion_string[32];
char comparison_string[32];
2020-08-26 15:13:48 +02:00
strlcpy(exclusion_string, left, sizeof(exclusion_string));
string_to_upper(exclusion_string);
2019-08-15 13:51:07 +02:00
for (i = 0; i < (unsigned)strings_count; i++)
{
2020-08-26 15:13:48 +02:00
strlcpy(comparison_string, strings[i], sizeof(comparison_string));
string_to_upper(comparison_string);
2021-03-26 12:22:10 +00:00
if (string_starts_with(exclusion_string,
2020-08-26 15:13:48 +02:00
comparison_string))
return true;
}
return false;
}
static bool left_parens_or_brackets_excluding_region(char *left)
{
return left_parens_or_brackets(left)
2020-08-26 15:13:48 +02:00
&& !left_exclusion(left, region_strings, REGION_STRINGS_LENGTH);
}
static bool left_parens_or_brackets_excluding_disc(char *left)
{
return left_parens_or_brackets(left)
2020-08-26 15:13:48 +02:00
&& !left_exclusion(left, disc_strings, DISC_STRINGS_LENGTH);
}
static bool left_parens_or_brackets_excluding_region_or_disc(char *left)
{
return left_parens_or_brackets(left)
2020-08-26 15:13:48 +02:00
&& !left_exclusion(left, region_strings, REGION_STRINGS_LENGTH)
&& !left_exclusion(left, disc_strings, DISC_STRINGS_LENGTH);
}
void label_remove_parens(char *label)
{
label_sanitize(label, left_parens, right_parens);
}
void label_remove_brackets(char *label)
{
label_sanitize(label, left_brackets, right_brackets);
}
void label_remove_parens_and_brackets(char *label)
{
2020-08-26 15:13:48 +02:00
label_sanitize(label, left_parens_or_brackets,
right_parens_or_brackets);
}
void label_keep_region(char *label)
{
2020-08-26 15:13:48 +02:00
label_sanitize(label, left_parens_or_brackets_excluding_region,
right_parens_or_brackets);
}
void label_keep_disc(char *label)
{
2020-08-26 15:13:48 +02:00
label_sanitize(label, left_parens_or_brackets_excluding_disc,
right_parens_or_brackets);
}
void label_keep_region_and_disc(char *label)
{
2020-08-26 15:13:48 +02:00
label_sanitize(label, left_parens_or_brackets_excluding_region_or_disc,
right_parens_or_brackets);
}