nact: Change tvar from char array to std::string

This commit is contained in:
kichikuou
2025-06-06 14:51:38 +09:00
parent aef2822bc4
commit 7f527da74f
12 changed files with 50 additions and 57 deletions
+2 -3
View File
@@ -12,7 +12,7 @@ void NACT::text_dialog()
if (!jni.env())
return;
std::string oldstr = encoding->toUtf8(tvar[tvar_index - 1]);
std::string oldstr = encoding->toUtf8(tvar[tvar_index - 1].c_str());
jstring joldstr = jni.env()->NewStringUTF(oldstr.c_str());
if (!joldstr) {
WARNING("Failed to allocate a string");
@@ -23,8 +23,7 @@ void NACT::text_dialog()
if (!jnewstr)
return;
const char* newstr_utf8 = jni.env()->GetStringUTFChars(jnewstr, NULL);
std::string newstr = encoding->fromUtf8(newstr_utf8);
strcpy_s(tvar[tvar_index - 1], 22, newstr.c_str());
tvar[tvar_index - 1] = encoding->fromUtf8(newstr_utf8);
jni.env()->ReleaseStringUTFChars(jnewstr, newstr_utf8);
}
-6
View File
@@ -38,12 +38,6 @@ void sys_warning(const char* format, ...);
#define _MAX_PATH PATH_MAX
#define sscanf_s sscanf
inline void strcpy_s(char* dst, size_t n, const char* src)
{
strncpy(dst, src, n);
dst[n - 1] = '\0';
}
#endif // !WIN32
#ifdef _MSC_VER
+1 -1
View File
@@ -417,7 +417,7 @@ private:
}
value = value.substr(1, value.size() - 2);
g_nact->set_string(index, g_nact->encoding->fromUtf8(value.c_str()).c_str());
g_nact->set_string(index, g_nact->encoding->fromUtf8(value.c_str()));
resp["success"] = true;
resp["body"] = {
+2 -2
View File
@@ -19,7 +19,7 @@ Uint32 custom_event_type = static_cast<Uint32>(-1);
void NACT::text_dialog()
{
static char buf[256];
std::string oldstr = encoding->toUtf8(tvar[tvar_index - 1]);
std::string oldstr = encoding->toUtf8(tvar[tvar_index - 1].c_str());
int ok = EM_ASM_({
var r = xsystem35.shell.inputString("文字列を入力してください", UTF8ToString($0), $1);
if (r) {
@@ -29,7 +29,7 @@ void NACT::text_dialog()
return 0;
}, oldstr.c_str(), tvar_maxlen, buf, sizeof buf);
if (ok) {
strcpy_s(tvar[tvar_index - 1], 22, encoding->fromUtf8(buf).c_str());
tvar[tvar_index - 1] = encoding->fromUtf8(buf);
}
}
+15
View File
@@ -5,6 +5,7 @@
*/
#include "fileio.h"
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -89,3 +90,17 @@ std::string FILEIO::gets()
}
return s;
}
std::string FILEIO::read_string(size_t size) {
std::vector<char> buf(size + 1);
if (fread(buf.data(), size, 1, fp) != 1) {
return std::string();
}
return std::string(buf.data());
}
bool FILEIO::write_string(const std::string& str, size_t size) {
std::vector<char> buf(size);
memcpy(buf.data(), str.c_str(), std::min(size, str.size()));
return fwrite(buf.data(), size, 1, fp) == 1;
}
+2
View File
@@ -40,6 +40,8 @@ public:
bool write(const void* buffer, size_t size) {
return fwrite(buffer, size, 1, fp) == 1;
}
std::string read_string(size_t size);
bool write_string(const std::string& str, size_t size);
int getc() { return fgetc(fp); }
int getw() {
int val = getc();
+5 -22
View File
@@ -474,7 +474,7 @@ void NACT::cmd_x()
TRACE("X %d:", index);
if(1 <= index && index <= 10) {
ags->draw_text(tvar[index - 1]);
ags->draw_text(tvar[index - 1].c_str());
}
}
@@ -554,11 +554,11 @@ bool NACT::load(int index)
}
ags->load(fio.get());
for (int i = 0; i < 10; i++) {
fio->read(tvar[i], 22);
tvar[i] = fio->read_string(22);
}
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 10; j++) {
fio->read(tvar_stack[i][j], 22);
tvar_stack[i][j] = fio->read_string(22);
}
}
for (int i = 0; i < 30; i++) {
@@ -597,11 +597,11 @@ bool NACT::save(int index, const char header[112])
}
ags->save(fio.get());
for (int i = 0; i < 10; i++) {
fio->write(tvar[i], 22);
fio->write_string(tvar[i], 22);
}
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 10; j++) {
fio->write(tvar_stack[i][j], 22);
fio->write_string(tvar_stack[i][j], 22);
}
}
for (int i = 0; i < 30; i++) {
@@ -755,23 +755,6 @@ void NACT::sys_sleep(int ms) {
#endif
}
void NACT::set_string(int index, const char* value)
{
const char *src = value;
char *dst = tvar[index];
int remaining = sizeof(tvar[0]) - 1;
while (*src) {
int len = encoding->mblen(*src);
if (len > remaining)
break;
memcpy(dst, src, len);
src += len;
dst += len;
remaining -= len;
}
*dst = '\0';
}
// WinMainとのインターフェース
int NACT::get_screen_height()
+4 -4
View File
@@ -56,8 +56,8 @@ protected:
// 変数
uint16 var[MAX_VAR] = {};
uint16 var_stack[30][20] = {};
char tvar[MAX_STRVAR][33] = {};
char tvar_stack[30][MAX_STRVAR][22] = {};
std::string tvar[MAX_STRVAR];
std::string tvar_stack[30][MAX_STRVAR];
int tvar_index = 0;
int tvar_maxlen;
@@ -189,8 +189,8 @@ public:
int get_scenario_page() const { return sco.page(); }
uint16 get_var(int index) const { return var[index]; }
void set_var(int index, uint16_t value) { var[index] = value; }
const char* get_string(int index) const { return tvar[index]; }
void set_string(int index, const char* value);
const char* get_string(int index) const { return tvar[index].c_str(); }
void set_string(int index, const std::string& value) { tvar[index] = value; }
private:
void pump_events();
+7 -7
View File
@@ -948,13 +948,13 @@ public:
NACT_DPS(const Config& config, const GameId& game_id)
: NACT_Sys1(config, game_id) {
text_refresh = false;
strcpy(tvar[0], strings.dps_custom.c_str());
strcpy(tvar[1], strings.dps_linus.c_str());
strcpy(tvar[2], strings.dps_katsumi.c_str());
strcpy(tvar[3], strings.dps_yumiko.c_str());
strcpy(tvar[4], strings.dps_itsumi.c_str());
strcpy(tvar[5], strings.dps_hitomi.c_str());
strcpy(tvar[6], strings.dps_mariko.c_str());
tvar[0] = strings.dps_custom;
tvar[1] = strings.dps_linus;
tvar[2] = strings.dps_katsumi;
tvar[3] = strings.dps_yumiko;
tvar[4] = strings.dps_itsumi;
tvar[5] = strings.dps_hitomi;
tvar[6] = strings.dps_mariko;
}
void cmd_y() override {
+3 -3
View File
@@ -554,7 +554,7 @@ void NACT_Sys2::cmd_m()
TRACE("M %s:", encoding->toUtf8(string).c_str());
if(1 <= tvar_index && tvar_index <= 10) {
memcpy(tvar[tvar_index - 1], string, 33);
tvar[tvar_index - 1] = string;
}
}
@@ -775,8 +775,8 @@ void NACT_Sys2::cmd_y()
case 228:
case 229:
if(1 <= param && param <= 10) {
ags->draw_text(tvar[param - 1]);
int padlen = cmd - 220 - encoding->mbslen(tvar[param - 1]);
ags->draw_text(tvar[param - 1].c_str());
int padlen = cmd - 220 - encoding->mbslen(tvar[param - 1].c_str());
if (padlen > 0) {
char pad[10] = " ";
pad[padlen] = '\0';
+7 -7
View File
@@ -485,7 +485,7 @@ void NACT_Sys3::cmd_l()
}
// M1 - M10
for(int i = 0; i < 10; i++) {
fio->read(tvar[i], 22);
tvar[i] = fio->read_string(22);
}
}
} else if (1 <= index && index <= 26) {
@@ -526,7 +526,7 @@ void NACT_Sys3::cmd_m()
TRACE("M %s:", encoding->toUtf8(string).c_str());
if(1 <= tvar_index && tvar_index <= 10) {
memcpy(tvar[tvar_index - 1], string, 22);
tvar[tvar_index - 1] = string;
} else if(tvar_index == 31) {
sco.open(string);
} else if(tvar_index == 32) {
@@ -651,7 +651,7 @@ void NACT_Sys3::cmd_q()
}
// M1 - M10
for(int i = 0; i < 10; i++) {
fio->write(tvar[i], 22);
fio->write_string(tvar[i], 22);
}
}
} else if (1 <= index && index <= 26) {
@@ -698,14 +698,14 @@ void NACT_Sys3::cmd_v()
var[21 + i] = var_stack[index - 1][i];
}
for(int i = 0; i < 10; i++) {
memcpy(tvar[i], tvar_stack[index - 1][i], 22);
tvar[i] = tvar_stack[index - 1][i];
}
} else {
for(int i = 0; i < 20; i++) {
var_stack[index - 1][i] = var[21 + i];
}
for(int i = 0; i< 10; i++) {
memcpy(tvar_stack[index - 1][i], tvar[i], 22);
tvar_stack[index - 1][i] = tvar[i];
}
}
}
@@ -965,8 +965,8 @@ void NACT_Sys3::cmd_y()
case 228:
case 229:
{
ags->draw_text(tvar[param - 1]);
int padlen = cmd - 220 - encoding->mbslen(tvar[param - 1]);
ags->draw_text(tvar[param - 1].c_str());
int padlen = cmd - 220 - encoding->mbslen(tvar[param - 1].c_str());
if (padlen > 0) {
char pad[10] = " ";
pad[padlen] = '\0';
+2 -2
View File
@@ -95,7 +95,7 @@ void NACT::text_dialog()
swprintf_s(wstring, 64, L"文字列を入力してください(最大%d文字)", g_nact->tvar_maxlen);
SetWindowTextW(GetDlgItem(hDlg, IDC_TEXT), wstring);
std::string oldstr = g_nact->encoding->toUtf8(g_nact->tvar[g_nact->tvar_index - 1]);
std::string oldstr = g_nact->encoding->toUtf8(g_nact->tvar[g_nact->tvar_index - 1].c_str());
MultiByteToWideChar(CP_UTF8, 0, oldstr.c_str(), -1, wstring, 64);
SetWindowTextW(GetDlgItem(hDlg, IDC_EDITBOX), wstring);
EnableWindow(GetDlgItem(hDlg, IDOK), oldstr[0] != '\0');
@@ -118,7 +118,7 @@ void NACT::text_dialog()
{
GetDlgItemTextW(hDlg, IDC_EDITBOX, wstring, 64);
WideCharToMultiByte(CP_UTF8, 0, wstring, -1, string, sizeof(string), NULL, NULL);
strcpy_s(g_nact->tvar[g_nact->tvar_index - 1], 22, g_nact->encoding->fromUtf8(string).c_str());
g_nact->tvar[g_nact->tvar_index - 1] = g_nact->encoding->fromUtf8(string);
EndDialog(hDlg, IDOK);
}
break;