+ char buf[256];
+ int ret;
+
+ // cheat funcs are destructive, need a copy..
+ strncpy(buf, code, sizeof(buf));
+ buf[sizeof(buf) - 1] = 0;
+
+ if (index < NumCheats)
+ ret = EditCheat(index, "", buf);
+ else
+ ret = AddCheat("", buf);
+
+ if (ret != 0)
+ SysPrintf("Failed to set cheat %#u\n", index);
+ else if (index < NumCheats)
+ Cheats[index].Enabled = enabled;
+}
+
+/* multidisk support */
+static bool disk_ejected;
+static unsigned int disk_current_index;
+static unsigned int disk_count;
+static struct disks_state {
+ char *fname;
+ int internal_index; // for multidisk eboots
+} disks[8];
+
+static bool disk_set_eject_state(bool ejected)
+{
+ // weird PCSX API..
+ SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
+ LidInterrupt();
+
+ disk_ejected = ejected;
+ return true;
+}
+
+static bool disk_get_eject_state(void)
+{
+ /* can't be controlled by emulated software */
+ return disk_ejected;
+}
+
+static unsigned int disk_get_image_index(void)
+{
+ return disk_current_index;
+}
+
+static bool disk_set_image_index(unsigned int index)
+{
+ if (index >= sizeof(disks) / sizeof(disks[0]))
+ return false;
+
+ CdromId[0] = '\0';
+ CdromLabel[0] = '\0';
+
+ if (disks[index].fname == NULL) {
+ SysPrintf("missing disk #%u\n", index);
+ CDR_shutdown();
+
+ // RetroArch specifies "no disk" with index == count,
+ // so don't fail here..
+ disk_current_index = index;
+ return true;
+ }
+
+ SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
+ disks[index].fname, disks[index].internal_index);
+
+ cdrIsoMultidiskSelect = disks[index].internal_index;
+ set_cd_image(disks[index].fname);
+ if (ReloadCdromPlugin() < 0) {
+ SysPrintf("failed to load cdr plugin\n");
+ return false;
+ }
+ if (CDR_open() < 0) {
+ SysPrintf("failed to open cdr plugin\n");
+ return false;
+ }
+
+ if (!disk_ejected) {
+ SetCdOpenCaseTime(time(NULL) + 2);
+ LidInterrupt();
+ }
+
+ disk_current_index = index;
+ return true;
+}
+
+static unsigned int disk_get_num_images(void)
+{
+ return disk_count;
+}
+
+static bool disk_replace_image_index(unsigned index,
+ const struct retro_game_info *info)
+{
+ char *old_fname;
+ bool ret = true;
+
+ if (index >= sizeof(disks) / sizeof(disks[0]))
+ return false;
+
+ old_fname = disks[index].fname;
+ disks[index].fname = NULL;
+ disks[index].internal_index = 0;
+
+ if (info != NULL) {
+ disks[index].fname = strdup(info->path);
+ if (index == disk_current_index)
+ ret = disk_set_image_index(index);
+ }
+
+ if (old_fname != NULL)
+ free(old_fname);
+
+ return ret;
+}
+
+static bool disk_add_image_index(void)
+{
+ if (disk_count >= 8)
+ return false;
+
+ disk_count++;
+ return true;
+}
+
+static struct retro_disk_control_callback disk_control = {
+ .set_eject_state = disk_set_eject_state,
+ .get_eject_state = disk_get_eject_state,
+ .get_image_index = disk_get_image_index,
+ .set_image_index = disk_set_image_index,
+ .get_num_images = disk_get_num_images,
+ .replace_image_index = disk_replace_image_index,
+ .add_image_index = disk_add_image_index,
+};
+
+// just in case, maybe a win-rt port in the future?
+#ifdef _WIN32
+#define SLASH '\\'
+#else
+#define SLASH '/'
+#endif
+
+static char base_dir[PATH_MAX];
+
+static bool read_m3u(const char *file)
+{
+ char line[PATH_MAX];
+ char name[PATH_MAX];
+ FILE *f = fopen(file, "r");
+ if (!f)
+ return false;
+
+ while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
+ if (line[0] == '#')
+ continue;
+ char *carrige_return = strchr(line, '\r');
+ if (carrige_return)
+ *carrige_return = '\0';
+ char *newline = strchr(line, '\n');
+ if (newline)
+ *newline = '\0';
+
+ if (line[0] != '\0')
+ {
+ snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
+ disks[disk_count++].fname = strdup(name);
+ }
+ }
+
+ fclose(f);
+ return (disk_count != 0);
+}
+
+static void extract_directory(char *buf, const char *path, size_t size)
+{
+ char *base;
+ strncpy(buf, path, size - 1);
+ buf[size - 1] = '\0';
+
+ base = strrchr(buf, '/');
+ if (!base)
+ base = strrchr(buf, '\\');
+
+ if (base)
+ *base = '\0';
+ else
+ {
+ buf[0] = '.';
+ buf[1] = '\0';
+ }
+}
+
+#ifdef __QNX__
+/* Blackberry QNX doesn't have strcasestr */
+
+/*
+ * Find the first occurrence of find in s, ignore case.
+ */
+char *
+strcasestr(const char *s, const char*find)
+{
+ char c, sc;
+ size_t len;
+
+ if ((c = *find++) != 0) {
+ c = tolower((unsigned char)c);
+ len = strlen(find);
+ do {
+ do {
+ if ((sc = *s++) == 0)
+ return (NULL);
+ } while ((char)tolower((unsigned char)sc) != c);
+ } while (strncasecmp(s, find, len) != 0);
+ s--;
+ }
+ return ((char *)s);