boot.library is no more, *.handler to *-handler.
[AROS.git] / compiler / alib / invertstring.c
blob16b6a2639f5c837d366ce5de075fc3c3ea668f98
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <libraries/commodities.h>
7 #include <proto/commodities.h>
8 #include <proto/exec.h>
9 #include <proto/alib.h>
10 #include <exec/memory.h>
11 #include <devices/keymap.h>
12 #include <devices/inputevent.h>
14 extern struct Library *CxBase;
16 /*****************************************************************************
18 NAME */
20 struct InputEvent *InvertString(
22 /* SYNOPSIS */
24 STRPTR str,
25 struct KeyMap *km
27 /* FUNCTION
28 Return a linked list of input events which would produce the string
29 'str' with the keymap 'km'.
31 INPUTS
32 str -- pointer to a (NULL-terminated) string that may contain
33 * ANSI character codes
34 * backslash-escaped characters:
35 \n -- carriage return
36 \r -- carriage return
37 \t -- tab
38 \\ -- backslash
39 * a description of an input event a la ParseIX() surrounded
40 by angle brackets
42 km -- keymap to use for the conversion or NULL to use the default
43 keymap
45 RESULT
46 A linked list of input events or NULL if something went wrong.
48 NOTES
50 EXAMPLE
51 An example string: "Hello <shift alt a>\n"
53 BUGS
55 SEE ALSO
56 commodities.library/ParseIX(), FreeIEvents()
58 INTERNALS
59 Ought to have an extra \< for < not starting an IX expression.
61 HISTORY
63 ******************************************************************************/
65 struct InputEvent *ieChain = NULL;
66 struct InputEvent *ie;
67 struct InputEvent *first = NULL;
68 UBYTE ansiCode;
69 UBYTE *start;
71 while(*str != '\0')
73 ie = AllocMem(sizeof(struct InputEvent), MEMF_PUBLIC | MEMF_CLEAR);
74 if (!ie)
76 if (first) FreeIEvents(first);
77 return NULL;
80 if (!first) first = ie;
82 if(ieChain != NULL)
83 ieChain->ie_NextEvent = ie;
85 ieChain = ie;
87 ie->ie_Class = IECLASS_RAWKEY;
88 ie->ie_EventAddress = NULL;
90 switch(*str)
92 case '\\' :
93 str++;
94 switch(*str)
96 case 't':
97 ansiCode = '\t';
98 break;
99 case 'r':
100 case 'n':
101 ansiCode = '\n';
102 break;
103 case '\\':
104 ansiCode = '\\';
105 break;
106 default :
107 /* FIXME: What to do if "\x" comes? */
108 /* stegerg: This? */
109 ansiCode = *str;
110 break;
113 if(InvertKeyMap(ansiCode, ie, km) == FALSE)
115 FreeIEvents(first);
116 return NULL;
119 break;
121 case '<' :
122 start = ++str;
124 while(*str && (*str != '>')) str++;
127 IX ix = {0};
128 LONG err;
130 *str = '\0';
131 err = ParseIX(start, &ix);
132 *str++ = '>';
134 if (err < 0)
136 FreeIEvents(first);
137 return NULL;
140 ie->ie_Class = ix.ix_Class;
141 ie->ie_Code = ix.ix_Code;
142 ie->ie_Qualifier = ix.ix_Qualifier;
144 break;
146 default :
147 if(InvertKeyMap(*str++, ie, km) == FALSE)
149 FreeIEvents(first);
150 return NULL;
153 break;
158 return first;
159 } /* InvertString */