Properly validate text entry in keyboard

Also fixed crash if string returned NULL
This commit is contained in:
Joel16 2019-01-03 13:57:06 -06:00
parent 058acefa28
commit fce41f2f77
5 changed files with 51 additions and 36 deletions

6
include/keyboard.h Normal file
View 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

View File

@ -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
View 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;
}

View File

@ -7,7 +7,7 @@
#include "fs.h" #include "fs.h"
#include "progress_bar.h" #include "progress_bar.h"
#include "menu_options.h" #include "menu_options.h"
#include "osk.h" #include "keyboard.h"
#include "SDL_helper.h" #include "SDL_helper.h"
#include "textures.h" #include "textures.h"
#include "utils.h" #include "utils.h"
@ -44,7 +44,7 @@ void FileOptions_ResetClipboard(void) {
static Result FileOptions_CreateFolder(void) { static Result FileOptions_CreateFolder(void) {
Result ret = 0; Result ret = 0;
char *buf = malloc(256); 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)) if (!strncmp(buf, "", 1))
return -1; return -1;
@ -66,7 +66,7 @@ static Result FileOptions_CreateFolder(void) {
static Result FileOptions_CreateFile(void) { static Result FileOptions_CreateFile(void) {
Result ret = 0; Result ret = 0;
char *buf = malloc(256); 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)) if (!strncmp(buf, "", 1))
return -1; return -1;
@ -102,7 +102,7 @@ static Result FileOptions_Rename(void) {
strcpy(newPath, cwd); strcpy(newPath, cwd);
strcat(oldPath, file->name); strcat(oldPath, file->name);
strcpy(buf, OSK_GetString("Enter name", file->name)); strcpy(buf, Keyboard_GetText("Enter name", file->name));
strcat(newPath, buf); strcat(newPath, buf);
free(buf); free(buf);

View File

@ -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;
}