File: calckeys.c

package info (click to toggle)
ncurses-hexedit 0.9.7%2Borig-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,824 kB
  • sloc: ansic: 7,418; sh: 332; makefile: 45
file content (213 lines) | stat: -rw-r--r-- 6,397 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* calckeys.c by Adam Rogoyski <apoc@laker.net> Temperanc on EFNet irc
 * Copyright (C) 1998, 1999 Adam Rogoyski
 * --- GNU General Public License Disclaimer ---
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


#include "hexedit.h"


void
calcNumberKey (struct calcEntryBox *binbox, struct calcEntryBox *octbox,
               struct calcEntryBox *decbox, struct calcEntryBox *hexbox,
               wchar_t in, int mode)
{
   unsigned char *p = NULL;
   int bitoff = 0;

   memset (&bitoff, 0xFF, sizeof (int));
   p = (unsigned char *) &bitoff + (sizeof (int) - 1);
   switch (mode)
   {
      case BIN_BOX:
         switch (in)
         {
            case CONTROL_D:
            case BACKSPACE:
            case CONTROL_H:
            case KEY_BACKSPACE:
            case KEY_DC:
#ifdef __PDCURSES__
               case CTL_BKSP:
#endif
               binbox->value.i = binbox->value.i >> 1;
               *p &= 0x7F;
               binbox->value.i &= bitoff;
               octbox->value.i = decbox->value.i = hexbox->value.i =
                                                       binbox->value.i;
               break;

            default:
            {
               char *p = binbox->tokens;
               while (*p)
               {
                  if (*p == in)
                     break;
                  p++;
               }
               if (!*p)
                  return;
               binbox->value.i = (binbox->value.i << 1) + getHexValue (in);
               octbox->value.i = decbox->value.i = hexbox->value.i =
                                                       binbox->value.i;
            }
         }
         break;

      case OCT_BOX:
         switch (in)
         {
            case CONTROL_D:
            case BACKSPACE:
            case CONTROL_H:
            case KEY_BACKSPACE:
            case KEY_DC:
#ifdef __PDCURSES__
               case CTL_BKSP:
#endif
               octbox->value.i = octbox->value.i >> 3;
               *p = 0x1F;
               octbox->value.i &= bitoff;
               binbox->value.i = decbox->value.i = hexbox->value.i =
                                                       octbox->value.i;
               break;

            default:
            {
               char *p = octbox->tokens;
               while (*p)
               {
                  if (*p == in)
                     break;
                  p++;
               }
               if (!*p)
                  return;
               octbox->value.i = (octbox->value.i << 3) + getHexValue (in);
               binbox->value.i = decbox->value.i = hexbox->value.i =
                                                       octbox->value.i;
            }
         }
         break;
      
      case DEC_BOX:
         switch (in)
         {
            case CONTROL_D:
            case BACKSPACE:
            case CONTROL_H:
            case KEY_BACKSPACE:
            case KEY_DC:
#ifdef __PDCURSES__
               case CTL_BKSP:
#endif
               decbox->value.i /= 10;
               binbox->value.i = octbox->value.i = hexbox->value.i =
                                                     decbox->value.i;
               break;

            default:
            {
               char *p = hexbox->tokens;
               while (*p)
               {
                  if (*p == in)
                     break;
                  p++;
               }
               if (!*p)
                  return;
                  /* this doesn't really do what we want it to do */
               decbox->value.i *= 10;
               decbox->value.i += getHexValue (in);
               binbox->value.i = octbox->value.i = hexbox->value.i =
                                                     decbox->value.i;
               break;
            }
         }
         break;

      case HEX_BOX:
         switch (in)
         {
            case CONTROL_D:
            case BACKSPACE:
            case CONTROL_H:
            case KEY_BACKSPACE:
            case KEY_DC:
#ifdef __PDCURSES__
               case CTL_BKSP:
#endif
               hexbox->value.i = hexbox->value.i >> 4;
               *p &= 0x0F;
               hexbox->value.i &= bitoff;
               binbox->value.i = octbox->value.i = decbox->value.i =
                                                       hexbox->value.i;
               break;

            default:
            {
               char *p = hexbox->tokens;
               while (*p)
               {
                  if (*p == in)
                     break;
                  p++;
               }
               if (!*p)
                  return;
               hexbox->value.i = (hexbox->value.i << 4) + getHexValue (in);
               binbox->value.i = octbox->value.i = decbox->value.i =
                                                       hexbox->value.i;
            }
         }
         break;
   }
}


void
calcArrowKey  (struct calcEntryBox *binbox, struct calcEntryBox *octbox,
               struct calcEntryBox *decbox, struct calcEntryBox *hexbox,
               wchar_t in, int *mode)
{
   switch (*mode)
   {
      case BIN_BOX:
         if ((in == KEY_UP) || (toupper (in) == 'K'))
            *mode = HEX_BOX;
         else if ((in == KEY_DOWN) || (toupper (in) == 'J'))
            *mode = OCT_BOX;
         break;

      case OCT_BOX:
         if ((in == KEY_UP) || (toupper (in) == 'K'))
            *mode = BIN_BOX;
         else if ((in == KEY_DOWN) || (toupper (in) == 'J'))
            *mode = DEC_BOX;
         break;

      case DEC_BOX:
         if ((in == KEY_UP) || (toupper (in) == 'K'))
            *mode = OCT_BOX;
         else if ((in == KEY_DOWN) || (toupper (in) == 'J'))
            *mode = HEX_BOX;
         break;

      case HEX_BOX:
         if ((in == KEY_UP) || (toupper (in) == 'K'))
            *mode = DEC_BOX;
         else if ((in == KEY_DOWN) || (toupper (in) == 'J'))
            *mode = BIN_BOX;
         break;
   }
}