]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_req_name.c
13c9e31fd8cfbc3c1b1a4faf74241c0220248738
[ncurses.git] / menu / m_req_name.c
1 /*-----------------------------------------------------------------------------+
2 |           The ncurses menu library is  Copyright (C) 1995-1997               |
3 |             by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de>                 |
4 |                          All Rights Reserved.                                |
5 |                                                                              |
6 | Permission to use, copy, modify, and distribute this software and its        |
7 | documentation for any purpose and without fee is hereby granted, provided    |
8 | that the above copyright notice appear in all copies and that both that      |
9 | copyright notice and this permission notice appear in supporting             |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the        |
12 | software without specific, written prior permission.                         | 
13 |                                                                              |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO  |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-  |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR   |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH    |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE.                                     |
21 +-----------------------------------------------------------------------------*/
22
23 /***************************************************************************
24 * Module menu_request_name                                                 *
25 * Routines to handle external names of menu requests                       *
26 ***************************************************************************/
27
28 #include "menu.priv.h"
29
30 MODULE_ID("$Id: m_req_name.c,v 1.8 1997/05/01 16:47:26 juergen Exp $")
31
32 static const char *request_names[ MAX_MENU_COMMAND - MIN_MENU_COMMAND + 1 ] = {
33   "LEFT_ITEM"    ,
34   "RIGHT_ITEM"   ,
35   "UP_ITEM"      ,
36   "DOWN_ITEM"    ,
37   "SCR_ULINE"    ,
38   "SCR_DLINE"    ,
39   "SCR_DPAGE"    ,
40   "SCR_UPAGE"    ,
41   "FIRST_ITEM"   ,
42   "LAST_ITEM"    ,
43   "NEXT_ITEM"    ,
44   "PREV_ITEM"    ,
45   "TOGGLE_ITEM"  ,
46   "CLEAR_PATTERN",
47   "BACK_PATTERN" ,
48   "NEXT_MATCH"   ,
49   "PREV_MATCH"          
50 };
51 #define A_SIZE (sizeof(request_names)/sizeof(request_names[0]))
52
53 /*---------------------------------------------------------------------------
54 |   Facility      :  libnmenu  
55 |   Function      :  const char * menu_request_name (int request);
56 |   
57 |   Description   :  Get the external name of a menu request.
58 |
59 |   Return Values :  Pointer to name      - on success
60 |                    NULL                 - on invalid request code
61 +--------------------------------------------------------------------------*/
62 const char *menu_request_name( int request )
63 {
64   if ( (request < MIN_MENU_COMMAND) || (request > MAX_MENU_COMMAND) )
65     {
66       SET_ERROR(E_BAD_ARGUMENT);
67       return (const char *)0;
68     }
69   else
70     return request_names[ request - MIN_MENU_COMMAND ];
71 }
72
73
74 /*---------------------------------------------------------------------------
75 |   Facility      :  libnmenu  
76 |   Function      :  int menu_request_by_name (const char *str);
77 |   
78 |   Description   :  Search for a request with this name.
79 |
80 |   Return Values :  Request Id       - on success
81 |                    E_NO_MATCH       - request not found
82 +--------------------------------------------------------------------------*/
83 int menu_request_by_name( const char *str )
84
85   /* because the table is so small, it doesn't really hurt
86      to run sequentially through it.
87   */
88   unsigned int i = 0;
89   char buf[16];
90   
91   if (str)
92     {
93       strncpy(buf,str,sizeof(buf));
94       while( (i<sizeof(buf)) && (buf[i] != '\0') )
95         {
96           buf[i] = toupper(buf[i]);
97           i++;
98         }
99       
100       for (i=0; i < A_SIZE; i++)
101         {
102           if (strncmp(request_names[i],buf,sizeof(buf))==0)
103             return MIN_MENU_COMMAND + i;
104         } 
105     }
106   RETURN(E_NO_MATCH);
107 }
108
109 /* m_req_name.c ends here */