OSDN Git Service

Null pointer check added
[hengband/hengband.git] / src / util.c
index 0743aa7..b626282 100644 (file)
@@ -293,7 +293,11 @@ static errr path_temp(char *buf, int max)
        if (!s) return (-1);
 
        /* Format to length */
+#ifndef WIN32
        (void)strnfmt(buf, max, "%s", s);
+#else
+       (void)strnfmt(buf, max, ".%s", s);
+#endif
 
        /* Success */
        return (0);
@@ -356,19 +360,19 @@ FILE *my_fopen(cptr file, cptr mode)
 {
        char buf[1024];
 
-#if defined(MACINTOSH) && defined(MAC_MPW)
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
        FILE *tempfff;
 #endif
 
        /* Hack -- Try to parse the path */
        if (path_parse(buf, 1024, file)) return (NULL);
 
-#if defined(MACINTOSH) && defined(MAC_MPW)
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
        if (my_strchr(mode, 'w'))
        {
                /* setting file type/creator */
                tempfff = fopen(buf, mode);
-               fsetfileinfo(file, _fcreator, _ftype);
+               fsetfileinfo(buf, _fcreator, _ftype);
                fclose(tempfff);
        }
 #endif
@@ -702,16 +706,16 @@ int fd_make(cptr file, int mode)
 
 #else /* BEN_HACK */
 
-# if defined(MACINTOSH) && defined(MAC_MPW)
-
-       /* setting file type and creator -- AR */
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
        {
-               errr errr_tmp;
-               errr_tmp = open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
-               fsetfileinfo(file, _fcreator, _ftype);
-               return(errr_tmp);
+               int fdes;
+               /* Create the file, fail if exists, write-only, binary */
+               fdes = open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
+               /* Set creator and type if the file is successfully opened */
+               if (fdes >= 0) fsetfileinfo(buf, _fcreator, _ftype);
+               /* Return the descriptor */
+               return (fdes);
        }
-
 # else
        /* Create the file, fail if exists, write-only, binary */
        return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
@@ -2216,6 +2220,22 @@ char inkey(void)
  */
 
 /*
+ * Initialize the quark array
+ */
+void quark_init(void)
+{
+       /* Quark variables */
+       C_MAKE(quark__str, QUARK_MAX, cptr);
+
+       /* Prepare first quark, which is used when quark_add() is failed */
+       quark__str[1] = string_make("");
+
+       /* There is one quark (+ NULL) */
+       quark__num = 2;
+}
+
+
+/*
  * Add a new "quark" to the set of quarks.
  */
 s16b quark_add(cptr str)
@@ -2229,8 +2249,8 @@ s16b quark_add(cptr str)
                if (streq(quark__str[i], str)) return (i);
        }
 
-       /* Paranoia -- Require room */
-       if (quark__num == QUARK_MAX) return (0);
+       /* Return "" when no room is available */
+       if (quark__num == QUARK_MAX) return 1;
 
        /* New maximal quark */
        quark__num = i + 1;
@@ -2250,8 +2270,8 @@ cptr quark_str(s16b i)
 {
        cptr q;
 
-       /* Verify */
-       if ((i < 0) || (i >= quark__num)) i = 0;
+       /* Return NULL for an invalid index */
+       if ((i < 1) || (i >= quark__num)) return NULL;
 
        /* Access the quark */
        q = quark__str[i];
@@ -2435,7 +2455,7 @@ void message_add(cptr str)
                }
 
                /* Limit the multiplier to 1000 */
-               if (buf && streq(buf, str) && (j < 1000))
+               if (streq(buf, str) && (j < 1000))
                {
                        j++;
 
@@ -3180,7 +3200,7 @@ void clear_from(int row)
  * ESCAPE clears the buffer and the window and returns FALSE.
  * RETURN accepts the current buffer contents and returns TRUE.
  */
-bool askfor_aux(char *buf, int len)
+bool askfor_aux(char *buf, int len, bool numpad_cursor)
 {
        int y, x;
        int pos = 0;
@@ -3221,7 +3241,7 @@ bool askfor_aux(char *buf, int len)
                Term_gotoxy(x + pos, y);
 
                /* Get a special key code */
-               skey = inkey_special();
+               skey = inkey_special(numpad_cursor);
 
                /* Analyze the key */
                switch (skey)
@@ -3423,6 +3443,17 @@ bool askfor_aux(char *buf, int len)
 
 
 /*
+ * Get some string input at the cursor location.
+ *
+ * Allow to use numpad keys as cursor keys.
+ */
+bool askfor(char *buf, int len)
+{
+       return askfor_aux(buf, len, TRUE);
+}
+
+
+/*
  * Get a string from the user
  *
  * The "prompt" should take the form "Prompt: "
@@ -3443,7 +3474,7 @@ bool get_string(cptr prompt, char *buf, int len)
        prt(prompt, 0, 0);
 
        /* Ask the user for a string */
-       res = askfor_aux(buf, len);
+       res = askfor(buf, len);
 
        /* Clear prompt */
        prt("", 0, 0);
@@ -3595,7 +3626,10 @@ bool get_com(cptr prompt, char *command, bool z_escape)
        prt(prompt, 0, 0);
 
        /* Get a key */
-       *command = inkey();
+       if (get_com_no_macros)
+               *command = inkey_special(FALSE);
+       else
+               *command = inkey();
 
        /* Clear the prompt */
        prt("", 0, 0);
@@ -3616,10 +3650,9 @@ bool get_com(cptr prompt, char *command, bool z_escape)
  */
 s16b get_quantity(cptr prompt, int max)
 {
+       bool res;
        int amt;
-
        char tmp[80];
-
        char buf[80];
 
 
@@ -3661,7 +3694,7 @@ s16b get_quantity(cptr prompt, int max)
        {
                /* Build a prompt */
 #ifdef JP
-                       sprintf(tmp, "¤¤¤¯¤Ä¤Ç¤¹¤« (1-%d): ", max);
+               sprintf(tmp, "¤¤¤¯¤Ä¤Ç¤¹¤« (1-%d): ", max);
 #else
                sprintf(tmp, "Quantity (1-%d): ", max);
 #endif
@@ -3671,6 +3704,11 @@ s16b get_quantity(cptr prompt, int max)
                prompt = tmp;
        }
 
+       /* Paranoia XXX XXX XXX */
+       msg_print(NULL);
+
+       /* Display prompt */
+       prt(prompt, 0, 0);
 
        /* Default to one */
        amt = 1;
@@ -3678,8 +3716,17 @@ s16b get_quantity(cptr prompt, int max)
        /* Build the default */
        sprintf(buf, "%d", amt);
 
-       /* Ask for a quantity */
-       if (!get_string(prompt, buf, 6)) return (0);
+       /*
+        * Ask for a quantity
+        * Don't allow to use numpad as cursor key.
+        */
+       res = askfor_aux(buf, 6, FALSE);
+
+       /* Clear prompt */
+       prt("", 0, 0);
+
+       /* Cancelled */
+       if (!res) return 0;
 
        /* Extract a number */
        amt = atoi(buf);
@@ -3878,7 +3925,7 @@ menu_naiyou menu_info[10][10] =
                {"Items(other)", 4, FALSE},
                {"Equip", 5, FALSE},
                {"Door/Box", 6, FALSE},
-               {"Infomations", 7, FALSE},
+               {"Informations", 7, FALSE},
                {"Options", 8, FALSE},
                {"Other commands", 9, FALSE},
                {"", 0, FALSE},
@@ -3905,7 +3952,7 @@ menu_naiyou menu_info[10][10] =
                {"Target(*)", '*', TRUE},
                {"Dig(T/^t)", 'T', TRUE},
                {"Go up stairs(<)", '<', TRUE},
-               {"Go down staies(>)", '>', TRUE},
+               {"Go down stairs(>)", '>', TRUE},
                {"Command pets(p)", 'p', TRUE},
                {"Search mode ON/OFF(S/#)", 'S', TRUE}
        },
@@ -3970,7 +4017,7 @@ menu_naiyou menu_info[10][10] =
                {"Identify symbol(/)", '/', TRUE},
                {"Show prev messages(^p)", KTRL('P'), TRUE},
                {"Current time(^t/')", KTRL('T'), TRUE},
-               {"Various infomations(~)", '~', TRUE},
+               {"Various informations(~)", '~', TRUE},
                {"Play record menu(|)", '|', TRUE},
                {"", 0, FALSE}
        },
@@ -4020,9 +4067,13 @@ special_menu_naiyou special_menu_info[] =
 {
        {"ĶǽÎÏ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_MINDCRAFTER},
        {"¤â¤Î¤Þ¤Í/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_IMITATOR},
+       {"²Î/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_BARD},
        {"ɬ»¦µ»/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_SAMURAI},
        {"Îýµ¤½Ñ/ËâË¡/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_FORCETRAINER},
+       {"µ»/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_BERSERKER},
+       {"µ»½Ñ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_SMITH},
        {"¶ÀËâË¡/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_MIRROR_MASTER},
+       {"Ǧ½Ñ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_NINJA},
        {"¹­°è¥Þ¥Ã¥×(<)", 2, 6, MENU_WILD, FALSE},
        {"Ä̾ï¥Þ¥Ã¥×(>)", 2, 7, MENU_WILD, TRUE},
        {"", 0, 0, 0, 0},
@@ -4032,9 +4083,13 @@ special_menu_naiyou special_menu_info[] =
 {
        {"MindCraft/Special", 0, 0, MENU_CLASS, CLASS_MINDCRAFTER},
        {"Imitation/Special", 0, 0, MENU_CLASS, CLASS_IMITATOR},
+       {"Song/Special", 0, 0, MENU_CLASS, CLASS_BARD},
        {"Technique/Special", 0, 0, MENU_CLASS, CLASS_SAMURAI},
        {"Mind/Magic/Special", 0, 0, MENU_CLASS, CLASS_FORCETRAINER},
+       {"BrutalPower/Special", 0, 0, MENU_CLASS, CLASS_BERSERKER},
+       {"Technique/Special", 0, 0, MENU_CLASS, CLASS_SMITH},
        {"MirrorMagic/Special", 0, 0, MENU_CLASS, CLASS_MIRROR_MASTER},
+       {"Ninjutsu/Special", 0, 0, MENU_CLASS, CLASS_NINJA},
        {"Enter global map(<)", 2, 6, MENU_WILD, FALSE},
        {"Enter local map(>)", 2, 7, MENU_WILD, TRUE},
        {"", 0, 0, 0, 0},
@@ -4798,14 +4853,9 @@ static void swap(tag_type *a, tag_type *b)
 {
        tag_type temp;
 
-       temp.tag = a->tag;
-       temp.pointer = a->pointer;
-
-       a->tag = b->tag;
-       a->pointer = b->pointer;
-
-       b->tag = temp.tag;
-       b->pointer = temp.pointer;
+       temp = *a;
+       *a = *b;
+       *b = temp;
 }
 
 
@@ -5166,26 +5216,29 @@ size_t my_strcpy(char *buf, const char *src, size_t bufsize)
        const char *s = src;
        size_t len = 0;
 
-       /* reserve for NUL termination */
-       bufsize--;
+       if (bufsize > 0) {
+               /* reserve for NUL termination */
+               bufsize--;
 
-       /* Copy as many bytes as will fit */
-       while (len < bufsize)
-       {
-               if (iskanji(*s))
+               /* Copy as many bytes as will fit */
+               while (*s && (len < bufsize))
                {
-                       if (len + 1 >= bufsize || !*(s+1)) break;
-                       *d++ = *s++;
-                       *d++ = *s++;
-                       len += 2;
-               }
-               else
-               {
-                       *d++ = *s++;
-                       len++;
+                       if (iskanji(*s))
+                       {
+                               if (len + 1 >= bufsize || !*(s+1)) break;
+                               *d++ = *s++;
+                               *d++ = *s++;
+                               len += 2;
+                       }
+                       else
+                       {
+                               *d++ = *s++;
+                               len++;
+                       }
                }
+               *d = '\0';
        }
-       *d = '\0';
+
        while(*s++) len++;
 
        return len;
@@ -5270,7 +5323,7 @@ char *my_strstr(const char *haystack, const char *needle)
 
 
 /*
- * A copy of ANSI my_strchr()
+ * A copy of ANSI strchr()
  *
  * my_strchr() can handle Kanji strings correctly.
  */
@@ -5290,13 +5343,33 @@ char *my_strchr(const char *ptr, char ch)
 
 
 /*
+ * Convert string to lower case
+ */
+void str_tolower(char *str)
+{
+       /* Force to be lower case string */
+       for (; *str; str++)
+       {
+#ifdef JP
+               if (iskanji(*str))
+               {
+                       str++;
+                       continue;
+               }
+#endif
+               *str = tolower(*str);
+       }
+}
+
+
+/*
  * Get a keypress from the user.
  * And interpret special keys as internal code.
  *
  * This function is a Mega-Hack and depend on pref-xxx.prf's.
  * Currently works on Linux(UNIX), Windows, and Macintosh only.
  */
-int inkey_special(void)
+int inkey_special(bool numpad_cursor)
 {
        static const struct {
                cptr keyname;
@@ -5346,6 +5419,12 @@ int inkey_special(void)
        int i;
        size_t trig_len;
 
+       /*
+        * Forget macro trigger ----
+        * It's important if we are already expanding macro action
+        */
+       inkey_macro_trigger_string[0] = '\0';
+
        /* Get a keypress */
        key = inkey();
 
@@ -5395,10 +5474,13 @@ int inkey_special(void)
                        if (!modifier_key_list[i].keyname) break;
                }
 
+               /* numpad_as_cursorkey option force numpad keys to input numbers */
+               if (!numpad_as_cursorkey) numpad_cursor = FALSE;
+
                /* Get a special key code */
                for (i = 0; special_key_list[i].keyname; i++)
                {
-                       if ((!special_key_list[i].numpad || numpad_as_cursorkey) &&
+                       if ((!special_key_list[i].numpad || numpad_cursor) &&
                            streq(str, special_key_list[i].keyname))
                        {
                                skey = special_key_list[i].keycode;