mirror of
https://github.com/joel16/NX-Shell.git
synced 2024-11-27 05:30:39 +00:00
Properly validate text entry in keyboard
Also fixed crash if string returned NULL
This commit is contained in:
parent
058acefa28
commit
fce41f2f77
6
include/keyboard.h
Normal file
6
include/keyboard.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef NX_SHELL_KEYBOARD_H
|
||||
#define NX_SHELL_KEYBOARD_H
|
||||
|
||||
const char *Keyboard_GetText(const char *guide_text, const char *initial_text);
|
||||
|
||||
#endif
|
@ -1,6 +0,0 @@
|
||||
#ifndef NX_SHELL_OSK_H
|
||||
#define NX_SHELL_OSK_H
|
||||
|
||||
const char *OSK_GetString(const char *guide_text, const char *initial_text);
|
||||
|
||||
#endif
|
41
source/keyboard.c
Normal file
41
source/keyboard.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <string.h>
|
||||
#include <switch.h>
|
||||
|
||||
// Empty strings are invalid.
|
||||
SwkbdTextCheckResult Keyboard_ValidateText(char *string, size_t size) {
|
||||
if (strcmp(string, "") == 0) {
|
||||
strncpy(string, "The name cannot be empty.", size);
|
||||
return SwkbdTextCheckResult_Bad;
|
||||
}
|
||||
|
||||
return SwkbdTextCheckResult_OK;
|
||||
}
|
||||
|
||||
const char *Keyboard_GetText(const char *guide_text, const char *initial_text) {
|
||||
Result ret = 0;
|
||||
SwkbdConfig swkbd;
|
||||
static char input_string[256];
|
||||
|
||||
if (R_FAILED(ret = swkbdCreate(&swkbd, 0))) {
|
||||
swkbdClose(&swkbd);
|
||||
return "";
|
||||
}
|
||||
|
||||
swkbdConfigMakePresetDefault(&swkbd);
|
||||
|
||||
if (strlen(guide_text) != 0)
|
||||
swkbdConfigSetGuideText(&swkbd, guide_text);
|
||||
|
||||
if (strlen(initial_text) != 0)
|
||||
swkbdConfigSetInitialText(&swkbd, initial_text);
|
||||
|
||||
swkbdConfigSetTextCheckCallback(&swkbd, Keyboard_ValidateText);
|
||||
|
||||
if (R_FAILED(ret = swkbdShow(&swkbd, input_string, sizeof(input_string)))) {
|
||||
swkbdClose(&swkbd);
|
||||
return "";
|
||||
}
|
||||
|
||||
swkbdClose(&swkbd);
|
||||
return input_string;
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
#include "fs.h"
|
||||
#include "progress_bar.h"
|
||||
#include "menu_options.h"
|
||||
#include "osk.h"
|
||||
#include "keyboard.h"
|
||||
#include "SDL_helper.h"
|
||||
#include "textures.h"
|
||||
#include "utils.h"
|
||||
@ -44,7 +44,7 @@ void FileOptions_ResetClipboard(void) {
|
||||
static Result FileOptions_CreateFolder(void) {
|
||||
Result ret = 0;
|
||||
char *buf = malloc(256);
|
||||
strcpy(buf, OSK_GetString("Enter folder name", "New folder"));
|
||||
strcpy(buf, Keyboard_GetText("Enter folder name", "New folder"));
|
||||
|
||||
if (!strncmp(buf, "", 1))
|
||||
return -1;
|
||||
@ -66,7 +66,7 @@ static Result FileOptions_CreateFolder(void) {
|
||||
static Result FileOptions_CreateFile(void) {
|
||||
Result ret = 0;
|
||||
char *buf = malloc(256);
|
||||
strcpy(buf, OSK_GetString("Enter file name", "New File.txt"));
|
||||
strcpy(buf, Keyboard_GetText("Enter file name", "New File.txt"));
|
||||
|
||||
if (!strncmp(buf, "", 1))
|
||||
return -1;
|
||||
@ -102,7 +102,7 @@ static Result FileOptions_Rename(void) {
|
||||
strcpy(newPath, cwd);
|
||||
strcat(oldPath, file->name);
|
||||
|
||||
strcpy(buf, OSK_GetString("Enter name", file->name));
|
||||
strcpy(buf, Keyboard_GetText("Enter name", file->name));
|
||||
strcat(newPath, buf);
|
||||
free(buf);
|
||||
|
||||
|
26
source/osk.c
26
source/osk.c
@ -1,26 +0,0 @@
|
||||
#include <string.h>
|
||||
#include <switch.h>
|
||||
|
||||
#include "osk.h"
|
||||
|
||||
const char *OSK_GetString(const char *guide_text, const char *initial_text) {
|
||||
SwkbdConfig swkbd;
|
||||
Result ret = 0;
|
||||
static char input_string[256];
|
||||
|
||||
if (R_SUCCEEDED(ret = swkbdCreate(&swkbd, 0))) {
|
||||
swkbdConfigMakePresetDefault(&swkbd);
|
||||
|
||||
if (strlen(guide_text) != 0)
|
||||
swkbdConfigSetGuideText(&swkbd, guide_text);
|
||||
|
||||
if (strlen(initial_text) != 0)
|
||||
swkbdConfigSetInitialText(&swkbd, initial_text);
|
||||
|
||||
if (R_FAILED(ret = swkbdShow(&swkbd, input_string, sizeof(input_string))))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
swkbdClose(&swkbd);
|
||||
return input_string;
|
||||
}
|
Loading…
Reference in New Issue
Block a user