From: Hourier Date: Sun, 14 Jun 2020 10:58:57 +0000 (+0900) Subject: [Refactor] #40413 Moved angband_str*() from util.c/h to string-processor.c/h X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=d99745e625cfbbc28367f678c2e1782e2274bfd7;p=hengband%2Fhengband.git [Refactor] #40413 Moved angband_str*() from util.c/h to string-processor.c/h --- diff --git a/src/autopick/autopick-describer.c b/src/autopick/autopick-describer.c index cc9f5f2d4..ae3fd21bd 100644 --- a/src/autopick/autopick-describer.c +++ b/src/autopick/autopick-describer.c @@ -10,6 +10,7 @@ #include "autopick/autopick-key-flag-process.h" #include "autopick/autopick-methods-table.h" #include "system/angband.h" +#include "util/string-processor.h" typedef struct { diff --git a/src/autopick/autopick-destroyer.c b/src/autopick/autopick-destroyer.c index 601d63c7a..29c7581c7 100644 --- a/src/autopick/autopick-destroyer.c +++ b/src/autopick/autopick-destroyer.c @@ -20,6 +20,7 @@ #include "sv-definition/sv-wand-types.h" #include "player/player-move.h" #include "player/player-race-types.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/autopick/autopick-entry.c b/src/autopick/autopick-entry.c index cdf25c049..8afa28fed 100644 --- a/src/autopick/autopick-entry.c +++ b/src/autopick/autopick-entry.c @@ -15,6 +15,7 @@ #include "object/object-info.h" #include "perception/object-perception.h" #include "util/quarks.h" +#include "util/string-processor.h" #ifdef JP static char kanji_colon[] = ":"; diff --git a/src/autopick/autopick-finder.c b/src/autopick/autopick-finder.c index 2ec9183d8..c2c95293a 100644 --- a/src/autopick/autopick-finder.c +++ b/src/autopick/autopick-finder.c @@ -17,6 +17,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" /* * @brief —^‚¦‚ç‚ꂽƒAƒCƒeƒ€‚ªŽ©“®E‚¢‚̃ŠƒXƒg‚É“o˜^‚³‚ê‚Ä‚¢‚é‚©‚Ç‚¤‚©‚ðŒŸõ‚·‚é diff --git a/src/autopick/autopick-matcher.c b/src/autopick/autopick-matcher.c index 8a817951f..6fa3fb99e 100644 --- a/src/autopick/autopick-matcher.c +++ b/src/autopick/autopick-matcher.c @@ -17,6 +17,7 @@ #include "object/object-value.h" #include "object/object-info.h" #include "object-enchant/special-object-flags.h" +#include "util/string-processor.h" /* * A function for Auto-picker/destroyer diff --git a/src/autopick/autopick-reader-writer.c b/src/autopick/autopick-reader-writer.c index 70799f31c..6dd8e4709 100644 --- a/src/autopick/autopick-reader-writer.c +++ b/src/autopick/autopick-reader-writer.c @@ -4,6 +4,7 @@ #include "io/files-util.h" #include "io/read-pref-file.h" #include "util/angband-files.h" +#include "util/string-processor.h" #include "view/display-messages.h" /* diff --git a/src/cmd-io/cmd-gameoption.c b/src/cmd-io/cmd-gameoption.c index 530776555..51e323e7b 100644 --- a/src/cmd-io/cmd-gameoption.c +++ b/src/cmd-io/cmd-gameoption.c @@ -17,6 +17,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/cmd-io/cmd-lore.c b/src/cmd-io/cmd-lore.c index 8d66ec3fc..1d1cbe45f 100644 --- a/src/cmd-io/cmd-lore.c +++ b/src/cmd-io/cmd-lore.c @@ -11,6 +11,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-lore.h" diff --git a/src/cmd-item/cmd-eat.c b/src/cmd-item/cmd-eat.c index 5bbb2d0b9..d43c25b10 100644 --- a/src/cmd-item/cmd-eat.c +++ b/src/cmd-item/cmd-eat.c @@ -31,6 +31,7 @@ #include "spell/spells-status.h" #include "sv-definition/sv-food-types.h" #include "sv-definition/sv-other-types.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" #include "view/object-describer.h" diff --git a/src/cmd/cmd-draw.c b/src/cmd/cmd-draw.c index 3dcbdd43c..cb0393b01 100644 --- a/src/cmd/cmd-draw.c +++ b/src/cmd/cmd-draw.c @@ -11,6 +11,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-main-window.h" // 暫定。後で消す. #include "view/display-messages.h" #include "view/display-player.h" // 暫定。後で消す. diff --git a/src/core/asking-player.c b/src/core/asking-player.c index 9a4d1a185..222210c81 100644 --- a/src/core/asking-player.c +++ b/src/core/asking-player.c @@ -9,6 +9,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-messages.h" /* diff --git a/src/core/scores.c b/src/core/scores.c index 308a0e11e..0717d8e14 100644 --- a/src/core/scores.c +++ b/src/core/scores.c @@ -34,6 +34,7 @@ #include "term/term-color-types.h" #include "util/angband-files.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/core/show-file.c b/src/core/show-file.c index 08a60dda8..93044b108 100644 --- a/src/core/show-file.c +++ b/src/core/show-file.c @@ -9,6 +9,7 @@ #include "term/term-color-types.h" #include "util/angband-files.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/artifact-reader.c b/src/info-reader/artifact-reader.c index f576ceb91..83dab297c 100644 --- a/src/info-reader/artifact-reader.c +++ b/src/info-reader/artifact-reader.c @@ -3,6 +3,7 @@ #include "object-enchant/artifact.h" #include "object-enchant/tr-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/dungeon-reader.c b/src/info-reader/dungeon-reader.c index 1722d7760..edabc3331 100644 --- a/src/info-reader/dungeon-reader.c +++ b/src/info-reader/dungeon-reader.c @@ -5,6 +5,7 @@ #include "info-reader/parse-error-types.h" #include "info-reader/race-info-tokens-table.h" #include "io/tokenizer.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/ego-reader.c b/src/info-reader/ego-reader.c index f1c1b2ac9..7bcf53dfa 100644 --- a/src/info-reader/ego-reader.c +++ b/src/info-reader/ego-reader.c @@ -3,6 +3,7 @@ #include "object-enchant/object-ego.h" #include "object-enchant/tr-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/feature-reader.c b/src/info-reader/feature-reader.c index 4f4e78e0c..4fd8dee62 100644 --- a/src/info-reader/feature-reader.c +++ b/src/info-reader/feature-reader.c @@ -7,6 +7,7 @@ #include "room/rooms.h" #include "term/gameterm.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" diff --git a/src/info-reader/fixed-map-parser.c b/src/info-reader/fixed-map-parser.c index 1b5bc2b37..8d7d478fa 100644 --- a/src/info-reader/fixed-map-parser.c +++ b/src/info-reader/fixed-map-parser.c @@ -15,6 +15,7 @@ #include "io/files-util.h" #include "system/system-variables.h" #include "util/angband-files.h" +#include "util/string-processor.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/info-reader/general-parser.c b/src/info-reader/general-parser.c index ff90bef02..fcab79947 100644 --- a/src/info-reader/general-parser.c +++ b/src/info-reader/general-parser.c @@ -12,6 +12,7 @@ #include "system/building-type-definition.h" #include "system/system-variables.h" #include "util/angband-files.h" +#include "util/string-processor.h" /*! * @brief パース関数に基づいてデータファイルからデータを読み取る / diff --git a/src/info-reader/kind-reader.c b/src/info-reader/kind-reader.c index c028c88d4..4c9130a65 100644 --- a/src/info-reader/kind-reader.c +++ b/src/info-reader/kind-reader.c @@ -4,6 +4,7 @@ #include "object/object-kind.h" #include "term/gameterm.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/magic-reader.c b/src/info-reader/magic-reader.c index 5ab22e44d..e6baa2de4 100644 --- a/src/info-reader/magic-reader.c +++ b/src/info-reader/magic-reader.c @@ -1,4 +1,5 @@ #include "info-reader/magic-reader.h" +#include "util/string-processor.h" /*! * @brief 職業魔法情報(m_info)のパース関数 / diff --git a/src/info-reader/race-reader.c b/src/info-reader/race-reader.c index 24a15e622..313b00dbf 100644 --- a/src/info-reader/race-reader.c +++ b/src/info-reader/race-reader.c @@ -1,6 +1,7 @@ #include "info-reader/race-reader.h" #include "info-reader/race-info-tokens-table.h" #include "term/gameterm.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/info-reader/vault-reader.c b/src/info-reader/vault-reader.c index af4ddacb9..afd0121b1 100644 --- a/src/info-reader/vault-reader.c +++ b/src/info-reader/vault-reader.c @@ -1,5 +1,6 @@ #include "info-reader/vault-reader.h" #include "room/rooms-vault.h" +#include "util/string-processor.h" /*! * @brief Vault情報(v_info)のパース関数 / diff --git a/src/inventory/inventory-curse.c b/src/inventory/inventory-curse.c index 6f8303b69..d790f1bf3 100644 --- a/src/inventory/inventory-curse.c +++ b/src/inventory/inventory-curse.c @@ -20,6 +20,7 @@ #include "spell-kind/spells-teleport.h" #include "spell/spells-summon.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "util/quarks.h" #include "view/display-messages.h" diff --git a/src/inventory/player-inventory.c b/src/inventory/player-inventory.c index 6e1f8114c..f87b47f04 100644 --- a/src/inventory/player-inventory.c +++ b/src/inventory/player-inventory.c @@ -26,6 +26,7 @@ #include "term/term-color-types.h" #include "util/int-char-converter.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" diff --git a/src/inventory/recharge-processor.c b/src/inventory/recharge-processor.c index 34539fc2b..6d1fcb386 100644 --- a/src/inventory/recharge-processor.c +++ b/src/inventory/recharge-processor.c @@ -5,6 +5,7 @@ #include "object/object-kind.h" #include "player/player-move.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/io/input-key-requester.c b/src/io/input-key-requester.c index 92688a848..7bb52f326 100644 --- a/src/io/input-key-requester.c +++ b/src/io/input-key-requester.c @@ -9,6 +9,7 @@ #include "term/screen-processor.h" // todo 相互依存している、後で何とかする. #include "util/int-char-converter.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "view/display-main-window.h" // todo panel_row_min が依存している。後で移設を検討する. #include "view/display-messages.h" diff --git a/src/io/pref-file-expressor.c b/src/io/pref-file-expressor.c index 5b276d6d7..3d672b6a2 100644 --- a/src/io/pref-file-expressor.c +++ b/src/io/pref-file-expressor.c @@ -1,6 +1,7 @@ #include "io/pref-file-expressor.h" #include "game-option/runtime-arguments.h" #include "system/system-variables.h" +#include "util/string-processor.h" /*! * @brief process_pref_fileのサブルーチンとして条件分岐処理の解釈と結果を返す diff --git a/src/knowledge/knowledge-monsters.c b/src/knowledge/knowledge-monsters.c index 663438544..5ee51c189 100644 --- a/src/knowledge/knowledge-monsters.c +++ b/src/knowledge/knowledge-monsters.c @@ -28,6 +28,7 @@ #include "term/term-color-types.h" #include "util/angband-files.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-main-window.h" // 暫定、後で消す. #include "view/display-lore.h" #include "view/display-monster-status.h" diff --git a/src/locale/english.c b/src/locale/english.c index 41e81a693..4281620f6 100644 --- a/src/locale/english.c +++ b/src/locale/english.c @@ -1,4 +1,5 @@ #include "locale/english.h" +#include "util/string-processor.h" /*! * @brief 英単語、句、説を複数形を変換する / Pluralize a monster name diff --git a/src/locale/japanese.c b/src/locale/japanese.c index cc607be72..2ef637151 100644 --- a/src/locale/japanese.c +++ b/src/locale/japanese.c @@ -5,6 +5,7 @@ */ #include "locale/japanese.h" +#include "util/string-processor.h" #include "view/display-messages.h" #ifdef JP diff --git a/src/market/building-monster.c b/src/market/building-monster.c index 871594cfb..88bd3c2bc 100644 --- a/src/market/building-monster.c +++ b/src/market/building-monster.c @@ -10,6 +10,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-lore.h" diff --git a/src/mind/monk-attack.c b/src/mind/monk-attack.c index c8517f237..a7c77e1c9 100644 --- a/src/mind/monk-attack.c +++ b/src/mind/monk-attack.c @@ -19,6 +19,7 @@ #include "monster-race/race-flags1.h" #include "monster-race/race-flags3.h" #include "monster/monster-status.h" +#include "util/string-processor.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/monster-floor/monster-generator.c b/src/monster-floor/monster-generator.c index ea7a7eb0d..27c2f1307 100644 --- a/src/monster-floor/monster-generator.c +++ b/src/monster-floor/monster-generator.c @@ -22,6 +22,7 @@ #include "monster/smart-learn-types.h" #include "mspell/summon-checker.h" #include "spell/spells-summon.h" +#include "util/string-processor.h" #include "view/display-messages.h" #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */ diff --git a/src/monster-race/monster-race-hook.c b/src/monster-race/monster-race-hook.c index 4f14aa07e..45e000316 100644 --- a/src/monster-race/monster-race-hook.c +++ b/src/monster-race/monster-race-hook.c @@ -17,7 +17,7 @@ #include "monster/monster-list.h" #include "monster/monster-util.h" #include "mspell/monster-spell.h" -#include "util/util.h" +#include "util/string-processor.h" /*! 通常pit生成時のモンスターの構成条件ID / Race index for "monster pit (clone)" */ int vault_aux_race; diff --git a/src/monster/monster-describer.c b/src/monster/monster-describer.c index 5356f2915..ce08f9cf8 100644 --- a/src/monster/monster-describer.c +++ b/src/monster/monster-describer.c @@ -7,6 +7,7 @@ #include "monster/monster-flag-types.h" #include "monster/monster-info.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/mspell/summon-checker.c b/src/mspell/summon-checker.c index e98beccef..a947ed951 100644 --- a/src/mspell/summon-checker.c +++ b/src/mspell/summon-checker.c @@ -9,6 +9,7 @@ #include "monster-race/race-indice-types.h" #include "monster/monster-util.h" #include "spell/spells-summon.h" +#include "util/string-processor.h" /*! * @brief 指定されたモンスター種族がsummon_specific_typeで指定された召喚条件に合うかどうかを返す diff --git a/src/object/object-flavor.c b/src/object/object-flavor.c index 103601a91..5dcab0061 100644 --- a/src/object/object-flavor.c +++ b/src/object/object-flavor.c @@ -36,6 +36,7 @@ #include "sv-definition/sv-lite-types.h" #include "util/bit-flags-calculator.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "world/world.h" /*! diff --git a/src/object/object-hook.c b/src/object/object-hook.c index cba847478..57fc0ae42 100644 --- a/src/object/object-hook.c +++ b/src/object/object-hook.c @@ -30,6 +30,7 @@ #include "sv-definition/sv-protector-types.h" #include "sv-definition/sv-weapon-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "world/world.h" diff --git a/src/player/player-damage.c b/src/player/player-damage.c index 14833a7e1..0ccd4ffca 100644 --- a/src/player/player-damage.c +++ b/src/player/player-damage.c @@ -46,6 +46,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/player/player-status.c b/src/player/player-status.c index 3bfbb6e06..8b2516aa8 100644 --- a/src/player/player-status.c +++ b/src/player/player-status.c @@ -75,6 +75,7 @@ #include "term/screen-processor.h" #include "util/bit-flags-calculator.h" #include "util/quarks.h" +#include "util/string-processor.h" #include "view/display-main-window.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/player/process-death.c b/src/player/process-death.c index d8a29f728..519c7af65 100644 --- a/src/player/process-death.c +++ b/src/player/process-death.c @@ -19,8 +19,9 @@ #include "store/store-util.h" #include "store/store.h" #include "term/gameterm.h" -#include "util/int-char-converter.h" #include "term/screen-processor.h" +#include "util/int-char-converter.h" +#include "util/string-processor.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/player/process-name.c b/src/player/process-name.c index 6c9d7babf..5043af16e 100644 --- a/src/player/process-name.c +++ b/src/player/process-name.c @@ -6,6 +6,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/angband-files.h" +#include "util/string-processor.h" #include "world/world.h" /*! diff --git a/src/spell-kind/spells-detection.c b/src/spell-kind/spells-detection.c index 19d96e988..4356a198e 100644 --- a/src/spell-kind/spells-detection.c +++ b/src/spell-kind/spells-detection.c @@ -16,6 +16,7 @@ #include "realm/realm-song-numbers.h" #include "realm/realm-song.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/spell/spells-summon.c b/src/spell/spells-summon.c index e992efc02..cf5980537 100644 --- a/src/spell/spells-summon.c +++ b/src/spell/spells-summon.c @@ -25,6 +25,7 @@ #include "spell/spells-status.h" #include "spell/spell-types.h" #include "sv-definition/sv-other-types.h" +#include "util/string-processor.h" #include "view/display-messages.h" /*! diff --git a/src/store/store-util.c b/src/store/store-util.c index 1939cb573..258f97d95 100644 --- a/src/store/store-util.c +++ b/src/store/store-util.c @@ -22,6 +22,7 @@ #include "sv-definition/sv-scroll-types.h" #include "sv-definition/sv-weapon-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "world/world-object.h" int cur_store_num = 0; diff --git a/src/term/gameterm.c b/src/term/gameterm.c index 1d34ce63e..baa7b1402 100644 --- a/src/term/gameterm.c +++ b/src/term/gameterm.c @@ -2,6 +2,7 @@ #include "system/system-variables.h" #include "term/term-color-types.h" #include "util/quarks.h" +#include "util/string-processor.h" /* * Convert an "attr"/"char" pair into a "pict" (P) diff --git a/src/util/string-processor.c b/src/util/string-processor.c index 7f4c3724c..31c8c77ab 100644 --- a/src/util/string-processor.c +++ b/src/util/string-processor.c @@ -352,3 +352,125 @@ void ascii_to_text(char *buf, concptr str) *s = '\0'; } + +/* + * The angband_strcpy() function copies up to 'bufsize'-1 characters from 'src' + * to 'buf' and NUL-terminates the result. The 'buf' and 'src' strings may + * not overlap. + * + * angband_strcpy() returns strlen(src). This makes checking for truncation + * easy. Example: if (angband_strcpy(buf, src, sizeof(buf)) >= sizeof(buf)) ...; + * + * This function should be equivalent to the strlcpy() function in BSD. + */ +size_t angband_strcpy(char *buf, concptr src, size_t bufsize) +{ +#ifdef JP + char *d = buf; + concptr s = src; + size_t len = 0; + + if (bufsize > 0) { + /* reserve for NUL termination */ + bufsize--; + + /* Copy as many bytes as will fit */ + while (*s && (len < bufsize)) { + if (iskanji(*s)) { + if (len + 1 >= bufsize || !*(s + 1)) + break; + *d++ = *s++; + *d++ = *s++; + len += 2; + } else { + *d++ = *s++; + len++; + } + } + *d = '\0'; + } + + while (*s++) + len++; + return len; + +#else + size_t len = strlen(src); + size_t ret = len; + if (bufsize == 0) + return ret; + + if (len >= bufsize) + len = bufsize - 1; + + (void)memcpy(buf, src, len); + buf[len] = '\0'; + return ret; +#endif +} + +/* + * The angband_strcat() tries to append a string to an existing NUL-terminated string. + * It never writes more characters into the buffer than indicated by 'bufsize' and + * NUL-terminates the buffer. The 'buf' and 'src' strings may not overlap. + * + * angband_strcat() returns strlen(buf) + strlen(src). This makes checking for + * truncation easy. Example: + * if (angband_strcat(buf, src, sizeof(buf)) >= sizeof(buf)) ...; + * + * This function should be equivalent to the strlcat() function in BSD. + */ +size_t angband_strcat(char *buf, concptr src, size_t bufsize) +{ + size_t dlen = strlen(buf); + if (dlen < bufsize - 1) { + return (dlen + angband_strcpy(buf + dlen, src, bufsize - dlen)); + } else { + return (dlen + strlen(src)); + } +} + +/* + * A copy of ANSI strstr() + * + * angband_strstr() can handle Kanji strings correctly. + */ +char *angband_strstr(concptr haystack, concptr needle) +{ + int l1 = strlen(haystack); + int l2 = strlen(needle); + + if (l1 >= l2) { + for (int i = 0; i <= l1 - l2; i++) { + if (!strncmp(haystack + i, needle, l2)) + return (char *)haystack + i; + +#ifdef JP + if (iskanji(*(haystack + i))) + i++; +#endif + } + } + + return NULL; +} + +/* + * A copy of ANSI strchr() + * + * angband_strchr() can handle Kanji strings correctly. + */ +char *angband_strchr(concptr ptr, char ch) +{ + for (; *ptr != '\0'; ptr++) { + if (*ptr == ch) + return (char *)ptr; + +#ifdef JP + if (iskanji(*ptr)) + ptr++; +#endif + } + + return NULL; +} diff --git a/src/util/string-processor.h b/src/util/string-processor.h index 730b6e32d..ddc6d8564 100644 --- a/src/util/string-processor.h +++ b/src/util/string-processor.h @@ -19,3 +19,7 @@ extern concptr macro_trigger_keycode[2][MAX_MACRO_TRIG]; void text_to_ascii(char *buf, concptr str); void ascii_to_text(char *buf, concptr str); +size_t angband_strcpy(char *buf, concptr src, size_t bufsize); +size_t angband_strcat(char *buf, concptr src, size_t bufsize); +char *angband_strstr(concptr haystack, concptr needle); +char *angband_strchr(concptr ptr, char ch); diff --git a/src/util/util.c b/src/util/util.c index 7d9f2f141..2b5c3e024 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -58,259 +58,122 @@ */ errr type_string(concptr str, uint len) { - errr err = 0; - term *old = Term; - if (!str) return -1; - if (!len) len = strlen(str); - - Term_activate(term_screen); - for (concptr s = str; s < str + len; s++) - { - if (*s == '\0') break; - - err = Term_keypress(*s); - if (err) break; - } - - Term_activate(old); - return err; + errr err = 0; + term *old = Term; + if (!str) + return -1; + if (!len) + len = strlen(str); + + Term_activate(term_screen); + for (concptr s = str; s < str + len; s++) { + if (*s == '\0') + break; + + err = Term_keypress(*s); + if (err) + break; + } + + Term_activate(old); + return err; } - void roff_to_buf(concptr str, int maxlen, char *tbuf, size_t bufsize) { - int read_pt = 0; - int write_pt = 0; - int line_len = 0; - int word_punct = 0; - char ch[3]; - ch[2] = '\0'; - - while (str[read_pt]) - { + int read_pt = 0; + int write_pt = 0; + int line_len = 0; + int word_punct = 0; + char ch[3]; + ch[2] = '\0'; + + while (str[read_pt]) { #ifdef JP - bool kinsoku = FALSE; - bool kanji; + bool kinsoku = FALSE; + bool kanji; #endif - int ch_len = 1; - ch[0] = str[read_pt]; - ch[1] = '\0'; + int ch_len = 1; + ch[0] = str[read_pt]; + ch[1] = '\0'; #ifdef JP - kanji = iskanji(ch[0]); + kanji = iskanji(ch[0]); - if (kanji) - { - ch[1] = str[read_pt + 1]; - ch_len = 2; + if (kanji) { + ch[1] = str[read_pt + 1]; + ch_len = 2; - if (strcmp(ch, "。") == 0 || - strcmp(ch, "、") == 0 || - strcmp(ch, "ィ") == 0 || - strcmp(ch, "ー") == 0) - kinsoku = TRUE; - } - else if (!isprint(ch[0])) - ch[0] = ' '; + if (strcmp(ch, "。") == 0 || strcmp(ch, "、") == 0 || strcmp(ch, "ィ") == 0 || strcmp(ch, "ー") == 0) + kinsoku = TRUE; + } else if (!isprint(ch[0])) + ch[0] = ' '; #else - if (!isprint(ch[0])) - ch[0] = ' '; -#endif - - if (line_len + ch_len > maxlen - 1 || str[read_pt] == '\n') - { - int word_len = read_pt - word_punct; -#ifdef JP - if (kanji && !kinsoku) - /* nothing */; - else -#endif - if (ch[0] == ' ' || word_len >= line_len / 2) - read_pt++; - else - { - read_pt = word_punct; - if (str[word_punct] == ' ') - read_pt++; - write_pt -= word_len; - } - - tbuf[write_pt++] = '\0'; - line_len = 0; - word_punct = read_pt; - continue; - } - - if (ch[0] == ' ') - word_punct = read_pt; - -#ifdef JP - if (!kinsoku) word_punct = read_pt; -#endif - - if ((size_t)(write_pt + 3) >= bufsize) break; - - tbuf[write_pt++] = ch[0]; - line_len++; - read_pt++; -#ifdef JP - if (kanji) - { - tbuf[write_pt++] = ch[1]; - line_len++; - read_pt++; - } + if (!isprint(ch[0])) + ch[0] = ' '; #endif - } - tbuf[write_pt] = '\0'; - tbuf[write_pt + 1] = '\0'; - return; -} - - -/* - * The angband_strcpy() function copies up to 'bufsize'-1 characters from 'src' - * to 'buf' and NUL-terminates the result. The 'buf' and 'src' strings may - * not overlap. - * - * angband_strcpy() returns strlen(src). This makes checking for truncation - * easy. Example: if (angband_strcpy(buf, src, sizeof(buf)) >= sizeof(buf)) ...; - * - * This function should be equivalent to the strlcpy() function in BSD. - */ -size_t angband_strcpy(char *buf, concptr src, size_t bufsize) -{ + if (line_len + ch_len > maxlen - 1 || str[read_pt] == '\n') { + int word_len = read_pt - word_punct; #ifdef JP - char *d = buf; - concptr s = src; - size_t len = 0; - - if (bufsize > 0) { - /* reserve for NUL termination */ - bufsize--; - - /* Copy as many bytes as will fit */ - while (*s && (len < bufsize)) - { - if (iskanji(*s)) - { - if (len + 1 >= bufsize || !*(s + 1)) break; - *d++ = *s++; - *d++ = *s++; - len += 2; - } - else - { - *d++ = *s++; - len++; - } - } - *d = '\0'; - } - - while (*s++) len++; - return len; - -#else - size_t len = strlen(src); - size_t ret = len; - if (bufsize == 0) return ret; - - if (len >= bufsize) len = bufsize - 1; - - (void)memcpy(buf, src, len); - buf[len] = '\0'; - return ret; + if (kanji && !kinsoku) + /* nothing */; + else #endif -} - - -/* - * The angband_strcat() tries to append a string to an existing NUL-terminated string. - * It never writes more characters into the buffer than indicated by 'bufsize' and - * NUL-terminates the buffer. The 'buf' and 'src' strings may not overlap. - * - * angband_strcat() returns strlen(buf) + strlen(src). This makes checking for - * truncation easy. Example: - * if (angband_strcat(buf, src, sizeof(buf)) >= sizeof(buf)) ...; - * - * This function should be equivalent to the strlcat() function in BSD. - */ -size_t angband_strcat(char *buf, concptr src, size_t bufsize) -{ - size_t dlen = strlen(buf); - if (dlen < bufsize - 1) - { - return (dlen + angband_strcpy(buf + dlen, src, bufsize - dlen)); - } - else - { - return (dlen + strlen(src)); - } -} - - -/* - * A copy of ANSI strstr() - * - * angband_strstr() can handle Kanji strings correctly. - */ -char *angband_strstr(concptr haystack, concptr needle) -{ - int l1 = strlen(haystack); - int l2 = strlen(needle); - - if (l1 >= l2) - { - for (int i = 0; i <= l1 - l2; i++) - { - if (!strncmp(haystack + i, needle, l2)) - return (char *)haystack + i; + if (ch[0] == ' ' || word_len >= line_len / 2) + read_pt++; + else { + read_pt = word_punct; + if (str[word_punct] == ' ') + read_pt++; + write_pt -= word_len; + } + + tbuf[write_pt++] = '\0'; + line_len = 0; + word_punct = read_pt; + continue; + } + + if (ch[0] == ' ') + word_punct = read_pt; #ifdef JP - if (iskanji(*(haystack + i))) i++; + if (!kinsoku) + word_punct = read_pt; #endif - } - } - - return NULL; -} + if ((size_t)(write_pt + 3) >= bufsize) + break; -/* - * A copy of ANSI strchr() - * - * angband_strchr() can handle Kanji strings correctly. - */ -char *angband_strchr(concptr ptr, char ch) -{ - for (; *ptr != '\0'; ptr++) - { - if (*ptr == ch) return (char *)ptr; - + tbuf[write_pt++] = ch[0]; + line_len++; + read_pt++; #ifdef JP - if (iskanji(*ptr)) ptr++; + if (kanji) { + tbuf[write_pt++] = ch[1]; + line_len++; + read_pt++; + } #endif - } + } - return NULL; + tbuf[write_pt] = '\0'; + tbuf[write_pt + 1] = '\0'; + return; } - /* * Convert string to lower case */ void str_tolower(char *str) { - for (; *str; str++) - { + for (; *str; str++) { #ifdef JP - if (iskanji(*str)) - { - str++; - continue; - } + if (iskanji(*str)) { + str++; + continue; + } #endif - *str = (char)tolower(*str); - } + *str = (char)tolower(*str); + } } diff --git a/src/util/util.h b/src/util/util.h index 52a3a32e9..ccd855f28 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -35,8 +35,4 @@ struct alloc_entry errr type_string(concptr str, uint len); void roff_to_buf(concptr str, int wlen, char *tbuf, size_t bufsize); -size_t angband_strcpy(char *buf, concptr src, size_t bufsize); -size_t angband_strcat(char *buf, concptr src, size_t bufsize); -char *angband_strstr(concptr haystack, concptr needle); -char *angband_strchr(concptr ptr, char ch); void str_tolower(char *str); diff --git a/src/view/display-main-window.c b/src/view/display-main-window.c index 6bf224851..e1d22a884 100644 --- a/src/view/display-main-window.c +++ b/src/view/display-main-window.c @@ -62,6 +62,7 @@ #include "term/screen-processor.h" #include "term/term-color-types.h" #include "util/bit-flags-calculator.h" +#include "util/string-processor.h" #include "view/display-lore.h" #include "view/display-messages.h" #include "view/display-player.h"