m_req_name.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /****************************************************************************
  2. * Copyright (c) 1998-2005,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. /***************************************************************************
  32. * Module m_request_name *
  33. * Routines to handle external names of menu requests *
  34. ***************************************************************************/
  35. #include "menu.priv.h"
  36. MODULE_ID("$Id: m_req_name.c,v 1.20 2008/09/13 18:59:17 tom Exp $")
  37. static const char *request_names[MAX_MENU_COMMAND - MIN_MENU_COMMAND + 1] =
  38. {
  39. "LEFT_ITEM",
  40. "RIGHT_ITEM",
  41. "UP_ITEM",
  42. "DOWN_ITEM",
  43. "SCR_ULINE",
  44. "SCR_DLINE",
  45. "SCR_DPAGE",
  46. "SCR_UPAGE",
  47. "FIRST_ITEM",
  48. "LAST_ITEM",
  49. "NEXT_ITEM",
  50. "PREV_ITEM",
  51. "TOGGLE_ITEM",
  52. "CLEAR_PATTERN",
  53. "BACK_PATTERN",
  54. "NEXT_MATCH",
  55. "PREV_MATCH"
  56. };
  57. #define A_SIZE (sizeof(request_names)/sizeof(request_names[0]))
  58. /*---------------------------------------------------------------------------
  59. | Facility : libnmenu
  60. | Function : const char * menu_request_name (int request);
  61. |
  62. | Description : Get the external name of a menu request.
  63. |
  64. | Return Values : Pointer to name - on success
  65. | NULL - on invalid request code
  66. +--------------------------------------------------------------------------*/
  67. NCURSES_EXPORT(const char *)
  68. menu_request_name(int request)
  69. {
  70. T((T_CALLED("menu_request_name(%d)"), request));
  71. if ((request < MIN_MENU_COMMAND) || (request > MAX_MENU_COMMAND))
  72. {
  73. SET_ERROR(E_BAD_ARGUMENT);
  74. returnCPtr((const char *)0);
  75. }
  76. else
  77. returnCPtr(request_names[request - MIN_MENU_COMMAND]);
  78. }
  79. /*---------------------------------------------------------------------------
  80. | Facility : libnmenu
  81. | Function : int menu_request_by_name (const char *str);
  82. |
  83. | Description : Search for a request with this name.
  84. |
  85. | Return Values : Request Id - on success
  86. | E_NO_MATCH - request not found
  87. +--------------------------------------------------------------------------*/
  88. NCURSES_EXPORT(int)
  89. menu_request_by_name(const char *str)
  90. {
  91. /* because the table is so small, it doesn't really hurt
  92. to run sequentially through it.
  93. */
  94. unsigned int i = 0;
  95. char buf[16];
  96. T((T_CALLED("menu_request_by_name(%s)"), _nc_visbuf(str)));
  97. if (str)
  98. {
  99. strncpy(buf, str, sizeof(buf));
  100. while ((i < sizeof(buf)) && (buf[i] != '\0'))
  101. {
  102. buf[i] = toupper(UChar(buf[i]));
  103. i++;
  104. }
  105. for (i = 0; i < A_SIZE; i++)
  106. {
  107. if (strncmp(request_names[i], buf, sizeof(buf)) == 0)
  108. returnCode(MIN_MENU_COMMAND + (int)i);
  109. }
  110. }
  111. RETURN(E_NO_MATCH);
  112. }
  113. /* m_req_name.c ends here */