OSDN Git Service

[Refactor] #40413 Moved angband_str*() from util.c/h to string-processor.c/h
[hengband/hengband.git] / src / player / process-name.c
1 #include "player/process-name.h"
2 #include "autopick/autopick-reader-writer.h"
3 #include "core/asking-player.h"
4 #include "io/files-util.h"
5 #include "player/player-personality.h"
6 #include "term/screen-processor.h"
7 #include "term/term-color-types.h"
8 #include "util/angband-files.h"
9 #include "util/string-processor.h"
10 #include "world/world.h"
11
12 /*!
13  * @brief プレイヤーの名前をチェックして修正する
14  * Process the player name.
15  * @param player_ptr プレーヤーへの参照ポインタ
16  * @param sf セーブファイル名に合わせた修正を行うならばTRUE
17  * @return なし
18  * @details
19  * Extract a clean "base name".
20  * Build the savefile name if needed.
21  */
22 void process_player_name(player_type *creature_ptr, bool sf)
23 {
24         char old_player_base[32] = "";
25         if (current_world_ptr->character_generated)
26                 strcpy(old_player_base, creature_ptr->base_name);
27
28         for (int i = 0; creature_ptr->name[i]; i++)
29         {
30 #ifdef JP
31                 if (iskanji(creature_ptr->name[i]))
32                 {
33                         i++;
34                         continue;
35                 }
36
37                 if (iscntrl((unsigned char)creature_ptr->name[i]))
38 #else
39                 if (iscntrl(creature_ptr->name[i]))
40 #endif
41                 {
42                         quit_fmt(_("'%s' という名前は不正なコントロールコードを含んでいます。", "The name '%s' contains control chars!"), creature_ptr->name);
43                 }
44         }
45
46         int k = 0;
47         for (int i = 0; creature_ptr->name[i]; i++)
48         {
49 #ifdef JP
50                 unsigned char c = creature_ptr->name[i];
51 #else
52                 char c = creature_ptr->name[i];
53 #endif
54
55 #ifdef JP
56                 if (iskanji(c)) {
57                         if (k + 2 >= (int)sizeof(creature_ptr->base_name) || !creature_ptr->name[i + 1])
58                                 break;
59
60                         creature_ptr->base_name[k++] = c;
61                         i++;
62                         creature_ptr->base_name[k++] = creature_ptr->name[i];
63                 }
64 #ifdef SJIS
65                 else if (iskana(c)) creature_ptr->base_name[k++] = c;
66 #endif
67                 else
68 #endif
69                         if (!strncmp(PATH_SEP, creature_ptr->name + i, strlen(PATH_SEP)))
70                         {
71                                 creature_ptr->base_name[k++] = '_';
72                                 i += strlen(PATH_SEP);
73                         }
74 #if defined(WINDOWS)
75                         else if (angband_strchr("\"*,/:;<>?\\|", c))
76                                 creature_ptr->base_name[k++] = '_';
77 #endif
78                         else if (isprint(c))
79                                 creature_ptr->base_name[k++] = c;
80         }
81
82         creature_ptr->base_name[k] = '\0';
83         if (!creature_ptr->base_name[0])
84                 strcpy(creature_ptr->base_name, "PLAYER");
85
86 #ifdef SAVEFILE_MUTABLE
87         sf = TRUE;
88 #endif
89         if (!savefile_base[0] && savefile[0])
90         {
91                 concptr s = savefile;
92                 while (TRUE)
93                 {
94                         concptr t;
95                         t = angband_strstr(s, PATH_SEP);
96                         if (!t)
97                                 break;
98                         s = t + 1;
99                 }
100
101                 strcpy(savefile_base, s);
102         }
103
104         if (!savefile_base[0] || !savefile[0])
105                 sf = TRUE;
106
107         if (sf)
108         {
109                 char temp[128];
110                 strcpy(savefile_base, creature_ptr->base_name);
111
112 #ifdef SAVEFILE_USE_UID
113                 /* Rename the savefile, using the creature_ptr->player_uid and creature_ptr->base_name */
114                 (void)sprintf(temp, "%d.%s", creature_ptr->player_uid, creature_ptr->base_name);
115 #else
116                 /* Rename the savefile, using the creature_ptr->base_name */
117                 (void)sprintf(temp, "%s", creature_ptr->base_name);
118 #endif
119                 path_build(savefile, sizeof(savefile), ANGBAND_DIR_SAVE, temp);
120         }
121
122         if (current_world_ptr->character_generated && !streq(old_player_base, creature_ptr->base_name))
123         {
124                 autopick_load_pref(creature_ptr, FALSE);
125         }
126 }
127
128
129 /*!
130  * @brief プレイヤーの名前を変更するコマンドのメインルーチン
131  * Gets a name for the character, reacting to name changes.
132  * @param creature_ptr プレーヤーへの参照ポインタ
133  * @return なし
134  * @details
135  * <pre>
136  * Assumes that "display_player()" has just been called
137  * Perhaps we should NOT ask for a name (at "birth()") on
138  * Unix machines?  XXX XXX
139  * What a horrible name for a global function.
140  * </pre>
141  */
142 void get_name(player_type *creature_ptr)
143 {
144         char tmp[64];
145         strcpy(tmp, creature_ptr->name);
146
147         if (get_string(_("キャラクターの名前を入力して下さい: ", "Enter a name for your character: "), tmp, 15))
148         {
149                 strcpy(creature_ptr->name, tmp);
150         }
151
152         if (strlen(creature_ptr->name) == 0)
153         {
154                 strcpy(creature_ptr->name, "PLAYER");
155         }
156
157         strcpy(tmp, ap_ptr->title);
158 #ifdef JP
159         if (ap_ptr->no == 1)
160                 strcat(tmp, "の");
161 #else
162         strcat(tmp, " ");
163 #endif
164         strcat(tmp, creature_ptr->name);
165
166         Term_erase(34, 1, 255);
167         c_put_str(TERM_L_BLUE, tmp, 1, 34);
168         clear_from(22);
169 }