From 1b21cf5c265bf19cf2486b4f648b654ad1fe3bff Mon Sep 17 00:00:00 2001 From: Harsh Kumar Date: Sun, 4 May 2014 20:57:34 +0530 Subject: Simple Split 2-iter loop: msg_show_console_dialog Simply spliting the 2-iter loop into code for computing size of memory & for copying to allocated memory --- src/message.c | 190 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 95 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/src/message.c b/src/message.c index 402b899dd8..897c6a21ff 100644 --- a/src/message.c +++ b/src/message.c @@ -2846,7 +2846,6 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl char_u *msgp = NULL; char_u *hotkp = NULL; char_u *r; - int copy; #define HAS_HOTKEY_LEN 30 char_u has_hotkey[HAS_HOTKEY_LEN]; int first_hotkey = FALSE; /* first char of button is hotkey */ @@ -2854,110 +2853,111 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl has_hotkey[0] = FALSE; - /* - * First loop: compute the size of memory to allocate. - * Second loop: copy to the allocated memory. - */ - for (copy = 0; copy <= 1; ++copy) { - r = buttons; - idx = 0; - while (*r) { - if (*r == DLG_BUTTON_SEP) { - if (copy) { - *msgp++ = ','; - *msgp++ = ' '; /* '\n' -> ', ' */ - - /* advance to next hotkey and set default hotkey */ - if (has_mbyte) - hotkp += STRLEN(hotkp); - else - ++hotkp; - hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; - if (dfltbutton) - --dfltbutton; - - /* If no hotkey is specified first char is used. */ - if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) - first_hotkey = TRUE; - } else { - len += 3; /* '\n' -> ', '; 'x' -> '(x)' */ - lenhotkey += HOTK_LEN; /* each button needs a hotkey */ - if (idx < HAS_HOTKEY_LEN - 1) - has_hotkey[++idx] = FALSE; - } - } else if (*r == DLG_HOTKEY_CHAR || first_hotkey) { - if (*r == DLG_HOTKEY_CHAR) - ++r; - first_hotkey = FALSE; - if (copy) { - if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */ - *msgp++ = *r; - else { - /* '&a' -> '[a]' */ - *msgp++ = (dfltbutton == 1) ? '[' : '('; - msgp += copy_char(r, msgp, FALSE); - *msgp++ = (dfltbutton == 1) ? ']' : ')'; - - /* redefine hotkey */ - hotkp[copy_char(r, hotkp, TRUE)] = NUL; - } - } else { - ++len; /* '&a' -> '[a]' */ - if (idx < HAS_HOTKEY_LEN - 1) - has_hotkey[idx] = TRUE; - } - } else { - /* everything else copy literally */ - if (copy) - msgp += copy_char(r, msgp, FALSE); - } - - /* advance to the next character */ - mb_ptr_adv(r); + // Compute the size of memory to allocate. + r = buttons; + idx = 0; + while (*r) { + if (*r == DLG_BUTTON_SEP) { + len += 3; /* '\n' -> ', '; 'x' -> '(x)' */ + lenhotkey += HOTK_LEN; /* each button needs a hotkey */ + if (idx < HAS_HOTKEY_LEN - 1) + has_hotkey[++idx] = FALSE; + } else if (*r == DLG_HOTKEY_CHAR || first_hotkey) { + if (*r == DLG_HOTKEY_CHAR) + ++r; + first_hotkey = FALSE; + ++len; /* '&a' -> '[a]' */ + if (idx < HAS_HOTKEY_LEN - 1) + has_hotkey[idx] = TRUE; } - if (copy) { - *msgp++ = ':'; - *msgp++ = ' '; - *msgp = NUL; - } else { - len += (int)(STRLEN(message) - + 2 /* for the NL's */ - + STRLEN(buttons) - + 3); /* for the ": " and NUL */ - lenhotkey++; /* for the NUL */ - - /* If no hotkey is specified first char is used. */ - if (!has_hotkey[0]) { - first_hotkey = TRUE; - len += 2; /* "x" -> "[x]" */ - } + /* advance to the next character */ + mb_ptr_adv(r); + } - /* - * Now allocate and load the strings - */ - free(confirm_msg); - confirm_msg = alloc(len); - *confirm_msg = NUL; - hotk = alloc(lenhotkey); + len += (int)(STRLEN(message) + + 2 /* for the NL's */ + + STRLEN(buttons) + + 3); /* for the ": " and NUL */ + lenhotkey++; /* for the NUL */ - *confirm_msg = '\n'; - STRCPY(confirm_msg + 1, message); + /* If no hotkey is specified first char is used. */ + if (!has_hotkey[0]) { + first_hotkey = TRUE; + len += 2; /* "x" -> "[x]" */ + } - msgp = confirm_msg + 1 + STRLEN(message); - hotkp = hotk; + /* + * Now allocate and load the strings + */ + free(confirm_msg); + confirm_msg = alloc(len); + *confirm_msg = NUL; + hotk = alloc(lenhotkey); + + *confirm_msg = '\n'; + STRCPY(confirm_msg + 1, message); + + msgp = confirm_msg + 1 + STRLEN(message); + hotkp = hotk; + + /* Define first default hotkey. Keep the hotkey string NUL + * terminated to avoid reading past the end. */ + hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; + + /* Remember where the choices start, displaying starts here when + * "hotkp" typed at the more prompt. */ + confirm_msg_tail = msgp; + *msgp++ = '\n'; + + // Copy to the allocated memory. + r = buttons; + idx = 0; + while (*r) { + if (*r == DLG_BUTTON_SEP) { + *msgp++ = ','; + *msgp++ = ' '; /* '\n' -> ', ' */ + + /* advance to next hotkey and set default hotkey */ + if (has_mbyte) + hotkp += STRLEN(hotkp); + else + ++hotkp; + hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; + if (dfltbutton) + --dfltbutton; - /* Define first default hotkey. Keep the hotkey string NUL - * terminated to avoid reading past the end. */ - hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; + /* If no hotkey is specified first char is used. */ + if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) + first_hotkey = TRUE; + } else if (*r == DLG_HOTKEY_CHAR || first_hotkey) { + if (*r == DLG_HOTKEY_CHAR) + ++r; + first_hotkey = FALSE; + if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */ + *msgp++ = *r; + else { + /* '&a' -> '[a]' */ + *msgp++ = (dfltbutton == 1) ? '[' : '('; + msgp += copy_char(r, msgp, FALSE); + *msgp++ = (dfltbutton == 1) ? ']' : ')'; - /* Remember where the choices start, displaying starts here when - * "hotkp" typed at the more prompt. */ - confirm_msg_tail = msgp; - *msgp++ = '\n'; + /* redefine hotkey */ + hotkp[copy_char(r, hotkp, TRUE)] = NUL; + } + } else { + /* everything else copy literally */ + msgp += copy_char(r, msgp, FALSE); } + + /* advance to the next character */ + mb_ptr_adv(r); } + *msgp++ = ':'; + *msgp++ = ' '; + *msgp = NUL; + display_confirm_msg(); return hotk; } -- cgit From af05207ce73c628175670f3d093d741a2382464e Mon Sep 17 00:00:00 2001 From: Harsh Kumar Date: Mon, 5 May 2014 01:02:13 +0530 Subject: Add new functions for memory alloction & data copy From msg_show_console_dialog(), a new function was created for memory allocation for hotkeys & dialog string. Similarly, another function created for copying hotkeys & dialog string. --- src/message.c | 111 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/message.c b/src/message.c index 897c6a21ff..4ad79d7968 100644 --- a/src/message.c +++ b/src/message.c @@ -61,6 +61,34 @@ static int do_more_prompt(int typed_char); static void msg_screen_putchar(int c, int attr); static int msg_check_screen(void); static void redir_write(char_u *s, int maxlen); + +/// Allocates memory for dialog string & for storing hotkeys +/// +/// Finds the size of memory required for the confirm_msg & for storing hotkeys +/// and then allocates the memory for them. +/// has_hotkey array is also filled-up. +/// +/// @param message Message which will be part of the confirm_msg +/// @param buttons String containing button names +/// @param has_hotkey A element in this array is TRUE if corresponding button +/// has a hotkey +/// +/// @return Pointer to memory allocated for storing hotkeys +static char_u * console_dialog_alloc(const char_u *message, + char_u *buttons, + char_u has_hotkey[]); + +/// Copies hotkeys & dialog message into the memory allocated for it +/// +/// @param message Message which will be part of the confirm_msg +/// @param buttons String containing button names +/// @param dfltbutton Number of default button +/// @param hotkp Pointer to the memory location where hotkeys will be copied +/// @param has_hotkey A element in this array is TRUE if corresponding button +/// has a hotkey +static void cpy_hotkeys_and_msg(const char_u *message, char_u *buttons, + int dfltbutton, char_u *hotkp, const char_u has_hotkey[]); + static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton); static int confirm_msg_used = FALSE; /* displaying confirm_msg */ @@ -2828,44 +2856,29 @@ copy_char ( } } -/* - * Format the dialog string, and display it at the bottom of - * the screen. Return a string of hotkey chars (if defined) for - * each 'button'. If a button has no hotkey defined, the first character of - * the button is used. - * The hotkeys can be multi-byte characters, but without combining chars. - * - * Returns an allocated string with hotkeys, or NULL for error. - */ -static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton) +#define HAS_HOTKEY_LEN 30 +#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1) + +static char_u * console_dialog_alloc(const char_u *message, + char_u *buttons, + char_u has_hotkey[]) { - int len = 0; -# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1) int lenhotkey = HOTK_LEN; /* count first button */ - char_u *hotk = NULL; - char_u *msgp = NULL; - char_u *hotkp = NULL; - char_u *r; -#define HAS_HOTKEY_LEN 30 - char_u has_hotkey[HAS_HOTKEY_LEN]; - int first_hotkey = FALSE; /* first char of button is hotkey */ - int idx; has_hotkey[0] = FALSE; // Compute the size of memory to allocate. - r = buttons; - idx = 0; + int len = 0; + int idx = 0; + char_u *r = buttons; while (*r) { if (*r == DLG_BUTTON_SEP) { len += 3; /* '\n' -> ', '; 'x' -> '(x)' */ lenhotkey += HOTK_LEN; /* each button needs a hotkey */ if (idx < HAS_HOTKEY_LEN - 1) has_hotkey[++idx] = FALSE; - } else if (*r == DLG_HOTKEY_CHAR || first_hotkey) { - if (*r == DLG_HOTKEY_CHAR) - ++r; - first_hotkey = FALSE; + } else if (*r == DLG_HOTKEY_CHAR) { + ++r; ++len; /* '&a' -> '[a]' */ if (idx < HAS_HOTKEY_LEN - 1) has_hotkey[idx] = TRUE; @@ -2883,23 +2896,46 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl /* If no hotkey is specified first char is used. */ if (!has_hotkey[0]) { - first_hotkey = TRUE; len += 2; /* "x" -> "[x]" */ } /* - * Now allocate and load the strings + * Now allocate space for the strings */ free(confirm_msg); confirm_msg = alloc(len); *confirm_msg = NUL; - hotk = alloc(lenhotkey); + return alloc(lenhotkey); +} + +/* + * Format the dialog string, and display it at the bottom of + * the screen. Return a string of hotkey chars (if defined) for + * each 'button'. If a button has no hotkey defined, the first character of + * the button is used. + * The hotkeys can be multi-byte characters, but without combining chars. + * + * Returns an allocated string with hotkeys, or NULL for error. + */ +static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton) +{ + char_u has_hotkey[HAS_HOTKEY_LEN]; + char_u *hotk = console_dialog_alloc(message, buttons, has_hotkey); + + cpy_hotkeys_and_msg(message, buttons, dfltbutton, hotk, has_hotkey); + + display_confirm_msg(); + return hotk; +} + +static void cpy_hotkeys_and_msg(const char_u *message, char_u *buttons, + int dfltbutton, char_u *hotkp, const char_u has_hotkey[]) +{ *confirm_msg = '\n'; STRCPY(confirm_msg + 1, message); - msgp = confirm_msg + 1 + STRLEN(message); - hotkp = hotk; + char_u *msgp = confirm_msg + 1 + STRLEN(message); /* Define first default hotkey. Keep the hotkey string NUL * terminated to avoid reading past the end. */ @@ -2910,9 +2946,13 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl confirm_msg_tail = msgp; *msgp++ = '\n'; - // Copy to the allocated memory. - r = buttons; - idx = 0; + int first_hotkey = FALSE; /* Is the first char of button a hotkey */ + if (!has_hotkey[0]) { + first_hotkey = TRUE; /* If no hotkey is specified, first char is used */ + } + + int idx = 0; + char_u *r = buttons; while (*r) { if (*r == DLG_BUTTON_SEP) { *msgp++ = ','; @@ -2957,9 +2997,6 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl *msgp++ = ':'; *msgp++ = ' '; *msgp = NUL; - - display_confirm_msg(); - return hotk; } /* -- cgit From cfe57fdb609f6a66315f6fec67f29fad5c5599bf Mon Sep 17 00:00:00 2001 From: Harsh Kumar Date: Mon, 5 May 2014 02:23:05 +0530 Subject: Style corrections in the two new functions Style changes in the two new function: cpy_hotkeys_and_msg() & console_dialog_alloc() --- src/message.c | 137 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/message.c b/src/message.c index 4ad79d7968..feb1226e6d 100644 --- a/src/message.c +++ b/src/message.c @@ -70,24 +70,25 @@ static void redir_write(char_u *s, int maxlen); /// /// @param message Message which will be part of the confirm_msg /// @param buttons String containing button names -/// @param has_hotkey A element in this array is TRUE if corresponding button -/// has a hotkey +/// @param[out] has_hotkey A element in this array is set to true if +/// corresponding button has a hotkey /// /// @return Pointer to memory allocated for storing hotkeys static char_u * console_dialog_alloc(const char_u *message, char_u *buttons, - char_u has_hotkey[]); + bool has_hotkey[]); /// Copies hotkeys & dialog message into the memory allocated for it /// /// @param message Message which will be part of the confirm_msg /// @param buttons String containing button names -/// @param dfltbutton Number of default button -/// @param hotkp Pointer to the memory location where hotkeys will be copied -/// @param has_hotkey A element in this array is TRUE if corresponding button +/// @param default_button_idx Number of default button +/// @param has_hotkey A element in this array is true if corresponding button /// has a hotkey -static void cpy_hotkeys_and_msg(const char_u *message, char_u *buttons, - int dfltbutton, char_u *hotkp, const char_u has_hotkey[]); +/// @param[out] hotkeys_ptr Pointer to the memory location where hotkeys will be copied +static void copy_hotkeys_and_msg(const char_u *message, char_u *buttons, + int default_button_idx, const bool has_hotkey[], + char_u *hotkeys_ptr); static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton); @@ -2861,11 +2862,10 @@ copy_char ( static char_u * console_dialog_alloc(const char_u *message, char_u *buttons, - char_u has_hotkey[]) + bool has_hotkey[]) { - int lenhotkey = HOTK_LEN; /* count first button */ - - has_hotkey[0] = FALSE; + int lenhotkey = HOTK_LEN; // count first button + has_hotkey[0] = false; // Compute the size of memory to allocate. int len = 0; @@ -2873,40 +2873,41 @@ static char_u * console_dialog_alloc(const char_u *message, char_u *r = buttons; while (*r) { if (*r == DLG_BUTTON_SEP) { - len += 3; /* '\n' -> ', '; 'x' -> '(x)' */ - lenhotkey += HOTK_LEN; /* each button needs a hotkey */ - if (idx < HAS_HOTKEY_LEN - 1) - has_hotkey[++idx] = FALSE; + len += 3; // '\n' -> ', '; 'x' -> '(x)' + lenhotkey += HOTK_LEN; // each button needs a hotkey + if (idx < HAS_HOTKEY_LEN - 1) { + has_hotkey[++idx] = false; + } } else if (*r == DLG_HOTKEY_CHAR) { - ++r; - ++len; /* '&a' -> '[a]' */ - if (idx < HAS_HOTKEY_LEN - 1) - has_hotkey[idx] = TRUE; + r++; + len++; // '&a' -> '[a]' + if (idx < HAS_HOTKEY_LEN - 1) { + has_hotkey[idx] = true; + } } - /* advance to the next character */ + // Advance to the next character mb_ptr_adv(r); } len += (int)(STRLEN(message) - + 2 /* for the NL's */ + + 2 // for the NL's + STRLEN(buttons) - + 3); /* for the ": " and NUL */ - lenhotkey++; /* for the NUL */ + + 3); // for the ": " and NUL + lenhotkey++; // for the NUL - /* If no hotkey is specified first char is used. */ + // If no hotkey is specified, first char is used. if (!has_hotkey[0]) { - len += 2; /* "x" -> "[x]" */ + len += 2; // "x" -> "[x]" } - /* - * Now allocate space for the strings - */ + + // Now allocate space for the strings free(confirm_msg); - confirm_msg = alloc(len); + confirm_msg = xmalloc(len); *confirm_msg = NUL; - return alloc(lenhotkey); + return xmalloc(lenhotkey); } /* @@ -2920,35 +2921,36 @@ static char_u * console_dialog_alloc(const char_u *message, */ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton) { - char_u has_hotkey[HAS_HOTKEY_LEN]; + bool has_hotkey[HAS_HOTKEY_LEN]; char_u *hotk = console_dialog_alloc(message, buttons, has_hotkey); - cpy_hotkeys_and_msg(message, buttons, dfltbutton, hotk, has_hotkey); + copy_hotkeys_and_msg(message, buttons, dfltbutton, has_hotkey, hotk); display_confirm_msg(); return hotk; } -static void cpy_hotkeys_and_msg(const char_u *message, char_u *buttons, - int dfltbutton, char_u *hotkp, const char_u has_hotkey[]) +static void copy_hotkeys_and_msg(const char_u *message, char_u *buttons, + int default_button_idx, const bool has_hotkey[], + char_u *hotkeys_ptr) { *confirm_msg = '\n'; STRCPY(confirm_msg + 1, message); char_u *msgp = confirm_msg + 1 + STRLEN(message); - /* Define first default hotkey. Keep the hotkey string NUL - * terminated to avoid reading past the end. */ - hotkp[copy_char(buttons, hotkp, TRUE)] = NUL; + // Define first default hotkey. Keep the hotkey string NUL + // terminated to avoid reading past the end. + hotkeys_ptr[copy_char(buttons, hotkeys_ptr, TRUE)] = NUL; - /* Remember where the choices start, displaying starts here when - * "hotkp" typed at the more prompt. */ + // Remember where the choices start, displaying starts here when + // "hotkeys_ptr" typed at the more prompt. confirm_msg_tail = msgp; *msgp++ = '\n'; - int first_hotkey = FALSE; /* Is the first char of button a hotkey */ + bool first_hotkey = false; // Is the first char of button a hotkey if (!has_hotkey[0]) { - first_hotkey = TRUE; /* If no hotkey is specified, first char is used */ + first_hotkey = true; // If no hotkey is specified, first char is used } int idx = 0; @@ -2956,41 +2958,44 @@ static void cpy_hotkeys_and_msg(const char_u *message, char_u *buttons, while (*r) { if (*r == DLG_BUTTON_SEP) { *msgp++ = ','; - *msgp++ = ' '; /* '\n' -> ', ' */ + *msgp++ = ' '; // '\n' -> ', ' + + // Advance to next hotkey and set default hotkey + hotkeys_ptr += (has_mbyte) ? STRLEN(hotkeys_ptr): 1; + hotkeys_ptr[copy_char(r + 1, hotkeys_ptr, TRUE)] = NUL; + + if (default_button_idx) { + default_button_idx--; + } + + // If no hotkey is specified, first char is used. + if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) { + first_hotkey = true; + } - /* advance to next hotkey and set default hotkey */ - if (has_mbyte) - hotkp += STRLEN(hotkp); - else - ++hotkp; - hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL; - if (dfltbutton) - --dfltbutton; - - /* If no hotkey is specified first char is used. */ - if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx]) - first_hotkey = TRUE; } else if (*r == DLG_HOTKEY_CHAR || first_hotkey) { - if (*r == DLG_HOTKEY_CHAR) + if (*r == DLG_HOTKEY_CHAR) { ++r; - first_hotkey = FALSE; - if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */ + } + + first_hotkey = false; + if (*r == DLG_HOTKEY_CHAR) { // '&&a' -> '&a' *msgp++ = *r; - else { - /* '&a' -> '[a]' */ - *msgp++ = (dfltbutton == 1) ? '[' : '('; + } else { + // '&a' -> '[a]' + *msgp++ = (default_button_idx == 1) ? '[' : '('; msgp += copy_char(r, msgp, FALSE); - *msgp++ = (dfltbutton == 1) ? ']' : ')'; + *msgp++ = (default_button_idx == 1) ? ']' : ')'; - /* redefine hotkey */ - hotkp[copy_char(r, hotkp, TRUE)] = NUL; + // redefine hotkey + hotkeys_ptr[copy_char(r, hotkeys_ptr, TRUE)] = NUL; } } else { - /* everything else copy literally */ + // everything else copy literally msgp += copy_char(r, msgp, FALSE); } - /* advance to the next character */ + // advance to the next character mb_ptr_adv(r); } -- cgit