OSDN Git Service

[Fix] 自動拾いエディタで最大行数を超えて編集しないようにする
authorshimitei <shimitei@gmail.com>
Sat, 8 May 2021 00:25:19 +0000 (09:25 +0900)
committershimitei <shimitei@gmail.com>
Sat, 8 May 2021 01:04:36 +0000 (10:04 +0900)
最大行数のチェックを追加。

src/autopick/autopick-editor-command.cpp
src/autopick/autopick-editor-util.cpp
src/autopick/autopick-inserter-killer.cpp
src/autopick/autopick-inserter-killer.h
src/autopick/autopick-reader-writer.cpp
src/autopick/autopick-util.cpp
src/autopick/autopick-util.h

index b3b7b34..ee56f32 100644 (file)
@@ -70,7 +70,8 @@ ape_quittance do_editor_command(player_type *player_ptr, text_body_type *tb, int
             tb->dirty_flags |= DIRTY_ALL;
         }
 
-        insert_return_code(tb);
+        if (!insert_return_code(tb))
+            break;
         tb->cy++;
         tb->cx = 0;
         tb->dirty_flags |= DIRTY_ALL;
@@ -269,7 +270,8 @@ ape_quittance do_editor_command(player_type *player_ptr, text_body_type *tb, int
             buf[i] = '\0';
             chain = chain->next;
             if (chain || tb->yank_eol) {
-                insert_return_code(tb);
+                if (!insert_return_code(tb))
+                    break;
                 string_free(tb->lines_list[tb->cy]);
                 tb->lines_list[tb->cy] = string_make(buf);
                 tb->cx = 0;
@@ -506,7 +508,8 @@ ape_quittance do_editor_command(player_type *player_ptr, text_body_type *tb, int
         }
 
         tb->cx = 0;
-        insert_return_code(tb);
+        if (!insert_return_code(tb))
+            break;
         string_free(tb->lines_list[tb->cy]);
         tb->lines_list[tb->cy] = autopick_line_from_entry_kill(entry);
         tb->dirty_flags |= DIRTY_SCREEN;
@@ -517,7 +520,8 @@ ape_quittance do_editor_command(player_type *player_ptr, text_body_type *tb, int
             break;
 
         tb->cx = 0;
-        insert_return_code(tb);
+        if (!insert_return_code(tb))
+            break;
         string_free(tb->lines_list[tb->cy]);
         tb->lines_list[tb->cy] = string_make(tb->last_destroyed);
         tb->dirty_flags |= DIRTY_ALL;
@@ -525,6 +529,8 @@ ape_quittance do_editor_command(player_type *player_ptr, text_body_type *tb, int
         break;
     }
     case EC_INSERT_BLOCK: {
+        if (!can_insert_line(tb, 2))
+            break;
         char expression[80];
         sprintf(expression, "?:[AND [EQU $RACE %s] [EQU $CLASS %s] [GEQ $LEVEL %02d]]",
 #ifdef JP
index 638212f..09a0a33 100644 (file)
@@ -188,12 +188,8 @@ void add_keyword(text_body_type *tb, BIT_FLAGS flg)
  */
 bool add_empty_line(text_body_type *tb)
 {
-    int num_lines;
-    for (num_lines = 0; tb->lines_list[num_lines]; num_lines++)
-        ;
+    int num_lines = count_line(tb);
 
-    if (num_lines >= MAX_LINES - 2)
-        return FALSE;
     if (!tb->lines_list[num_lines - 1][0])
         return FALSE;
 
index c19e5ef..5a0b581 100644 (file)
@@ -23,6 +23,19 @@ void check_expression_line(text_body_type *tb, int y)
     }
 }
 
+/*!
+ * @brief 行を追加可能かチェックする
+ * @param tb text_body_type
+ * @param add_num 追加する行数
+ * @retval true 追加可能
+ * @retval false 最大行数を超えるため追加不可
+ */
+bool can_insert_line(text_body_type *tb, int add_num)
+{
+    const int count = count_line(tb);
+    return !is_greater_autopick_max_line(count + add_num);
+}
+
 /*
  * Insert return code and split the line
  */
@@ -31,11 +44,10 @@ bool insert_return_code(text_body_type *tb)
     char buf[MAX_LINELEN];
     int i, j, num_lines;
 
-    for (num_lines = 0; tb->lines_list[num_lines]; num_lines++)
-        ;
+    num_lines = count_line(tb);
+    if (is_greater_autopick_max_line(num_lines))
+        return false;
 
-    if (num_lines >= MAX_LINES - 2)
-        return FALSE;
     num_lines--;
 
     for (; tb->cy < num_lines; num_lines--) {
@@ -65,6 +77,8 @@ bool insert_return_code(text_body_type *tb)
  */
 bool insert_macro_line(text_body_type *tb)
 {
+    if (!can_insert_line(tb, 2))
+        return false;
     int i, n = 0;
     flush();
     inkey_base = TRUE;
@@ -109,6 +123,8 @@ bool insert_macro_line(text_body_type *tb)
  */
 bool insert_keymap_line(text_body_type *tb)
 {
+    if (!can_insert_line(tb, 2))
+        return false;
     BIT_FLAGS mode;
     if (rogue_like_commands) {
         mode = KEYMAP_MODE_ROGUE;
index a41e348..33119a2 100644 (file)
@@ -2,6 +2,7 @@
 
 typedef struct text_body_type text_body_type;
 void check_expression_line(text_body_type *tb, int y);
+bool can_insert_line(text_body_type *tb, int add_num = 1);
 bool insert_return_code(text_body_type *tb);
 bool insert_macro_line(text_body_type *tb);
 bool insert_keymap_line(text_body_type *tb);
index ab97110..035d54f 100644 (file)
@@ -73,7 +73,7 @@ static concptr *read_text_lines(concptr filename)
     C_MAKE(lines_list, MAX_LINES, concptr);
     while (angband_fgets(fff, buf, sizeof(buf)) == 0) {
         lines_list[lines++] = string_make(buf);
-        if (lines >= MAX_LINES - 1)
+        if (is_greater_autopick_max_line(lines))
             break;
     }
 
index 30d8d9b..13d853c 100644 (file)
@@ -95,3 +95,17 @@ void add_autopick_list(autopick_type *entry)
     autopick_list[max_autopick] = *entry;
     max_autopick++;
 }
+
+/*!
+ * @brief 行数をカウントする
+ * @param tb text_body_type
+ * @return 行数
+ */
+int count_line(text_body_type *tb)
+{
+    int num_lines;
+    for (num_lines = 0; tb->lines_list[num_lines]; num_lines++)
+        ;
+
+    return num_lines;
+}
index 3d38e77..532eee9 100644 (file)
@@ -84,3 +84,15 @@ void free_text_lines(concptr *lines_list);
 int get_com_id(char key);
 void auto_inscribe_item(player_type *player_ptr, object_type *o_ptr, int idx);
 void add_autopick_list(autopick_type *entry);
+int count_line(text_body_type *tb);
+
+/*!
+ * @brief 最大行数を超えるかチェックする
+ * @param count 行数
+ * @retval true 最大行数を超える
+ * @retval false 最大行数を超えない
+ */
+inline bool is_greater_autopick_max_line(int count)
+{
+    return (count > MAX_LINES - 3);
+}