1 /**
2  * @namespace   biewlib
3  * @file        biewlib/sysdep/ia32/win32/misc.c
4  * @brief       Misc. functions for Win32 (optional!!!)
5  * @version     -
6  * @author      Nickols_K
7  * @date        2003
8 **/
9 /* for cygwin - remove unnecessary includes */
10 #define _OLE_H
11 #define _OLE2_H
12 #include <windows.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #include "biewlib/biewlib.h"
17 
18 extern HANDLE hIn;
19 
__inputRawInfo(char * head,char * text)20 int __FASTCALL__ __inputRawInfo(char *head, char *text)
21 {
22   int rval=1;
23   DWORD nread;
24   INPUT_RECORD ir;
25   ReadConsoleInput(hIn,&ir,1,&nread);
26     switch(ir.EventType)
27     {
28       case WINDOW_BUFFER_SIZE_EVENT:
29       {
30 	strcpy(head,"Type dwSize");
31 	sprintf(text,"Size %08lX"
32                 ,ir.Event.WindowBufferSizeEvent.dwSize
33                 );
34       }
35       break;
36       case FOCUS_EVENT:
37       {
38 	strcpy(head,"Type bSetFocus");
39 	sprintf(text,"Focs %i"
40                 ,ir.Event.FocusEvent.bSetFocus
41                 );
42       }
43       break;
44       case MENU_EVENT:
45       {
46 	strcpy(head,"Type dwCommandId");
47 	sprintf(text,"Menu %08lX"
48                 ,ir.Event.MenuEvent.dwCommandId
49                 );
50       }
51       break;
52       case MOUSE_EVENT:
53       {
54 	strcpy(head,"Type Position ButtonState ControlKeyState Flags");
55 	sprintf(text,"Mous %08lX %-11lX %-15lX %-5lX"
56                 ,ir.Event.MouseEvent.dwMousePosition
57                 ,ir.Event.MouseEvent.dwButtonState
58                 ,ir.Event.MouseEvent.dwControlKeyState
59                 ,ir.Event.MouseEvent.dwEventFlags
60                 );
61       }
62       break;
63       case KEY_EVENT:
64       {
65 	strcpy(head,"Type KDown RepeatCnt KeyCode ScanCode Ascii ControlKeyState");
66 	sprintf(text,"KEY  %-5i %-9i %-7X %-7X  %-5c %-15lX"
67                 ,ir.Event.KeyEvent.bKeyDown
68                 ,ir.Event.KeyEvent.wRepeatCount
69                 ,ir.Event.KeyEvent.wVirtualKeyCode
70                 ,ir.Event.KeyEvent.wVirtualScanCode
71                 ,ir.Event.KeyEvent.uChar.AsciiChar?ir.Event.KeyEvent.uChar.AsciiChar:' '
72                 ,ir.Event.KeyEvent.dwControlKeyState
73                 );
74 
75         if( ir.Event.KeyEvent.wVirtualKeyCode == 27 && ir.Event.KeyEvent.wVirtualScanCode == 1 &&
76             !ir.Event.KeyEvent.uChar.AsciiChar )
77             ir.Event.KeyEvent.uChar.AsciiChar = ir.Event.KeyEvent.wVirtualKeyCode;
78         rval=ir.Event.KeyEvent.wVirtualKeyCode==27&&!ir.Event.KeyEvent.bKeyDown?0:1;
79       }
80       break;
81       default: break;
82     }
83   return rval;
84 }
85 
86 #ifdef __GNUC__
87 #include <limits.h>
88 #include <ctype.h>
89 #include <errno.h>
90 #include <stdlib.h>
91 
92 /* constants used in Solaris */
93 #define LLONG_MIN       LONG_LONG_MIN
94 #define LLONG_MAX       LONG_LONG_MAX
95 #define ULLONG_MAX      ULONG_LONG_MAX
96 
97 #define unconst(__v, __t) __extension__ ({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p;})
98 
99 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
100 
101 /*
102  * Convert a string to an unsigned long long integer.
103  *
104  * Ignores `locale' stuff.  Assumes that the upper and lower case
105  * alphabets and digits are each contiguous.
106  */
107 unsigned long long
strtoull(const char * nptr,char ** endptr,int base)108 strtoull(const char *nptr, char **endptr, int base)
109 {
110   const char *s = nptr;
111   unsigned long long acc;
112   int c;
113   unsigned long long cutoff;
114   int neg = 0, any, cutlim;
115 
116   /*
117    * See strtol for comments as to the logic used.
118    */
119   do {
120     c = *s++;
121   } while (isspace(c));
122   if (c == '-')
123   {
124     neg = 1;
125     c = *s++;
126   }
127   else if (c == '+')
128     c = *s++;
129   if ((base == 0 || base == 16) &&
130       c == '0' && (*s == 'x' || *s == 'X'))
131   {
132     c = s[1];
133     s += 2;
134     base = 16;
135   }
136   if (base == 0)
137     base = c == '0' ? 8 : 10;
138   cutoff = (unsigned long long)ULLONG_MAX / base;
139   cutlim = (unsigned long long)ULLONG_MAX % base;
140   for (acc = 0, any = 0;; c = *s++)
141   {
142     if (isdigit(c))
143       c -= '0';
144     else if (isalpha(c))
145       c -= isupper(c) ? 'A' - 10 : 'a' - 10;
146     else
147       break;
148     if (c >= base)
149       break;
150     if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
151       any = -1;
152     else {
153       any = 1;
154       acc *= base;
155       acc += c;
156     }
157   }
158   if (any < 0)
159   {
160     acc = ULLONG_MAX;
161     errno = ERANGE;
162   }
163   else if (neg)
164     acc = -acc;
165   if (endptr != 0)
166     *endptr = any ? unconst(s, char *) - 1 : unconst(nptr, char *);
167   return acc;
168 }
169 
170 long long
strtoll(const char * nptr,char ** endptr,int base)171 strtoll(const char *nptr, char **endptr, int base)
172 {
173   const char *s = nptr;
174   unsigned long long acc;
175   int c;
176   unsigned long long cutoff;
177   int neg = 0, any, cutlim;
178 
179   /*
180    * See strtol for comments as to the logic used.
181    */
182   do {
183     c = *s++;
184   } while (isspace(c));
185   if (c == '-')
186   {
187     neg = 1;
188     c = *s++;
189   }
190   else if (c == '+')
191     c = *s++;
192   if ((base == 0 || base == 16) &&
193       c == '0' && (*s == 'x' || *s == 'X'))
194   {
195     c = s[1];
196     s += 2;
197     base = 16;
198   }
199   if (base == 0)
200     base = c == '0' ? 8 : 10;
201 
202 /* to prevent overflow, we take max-1 and add 1 after division */
203   cutoff = neg ? -(LLONG_MIN+1) : LLONG_MAX-1;
204   cutlim = cutoff % base;
205   cutoff /= base;
206   if (++cutlim == base)
207   {
208     cutlim = 0;
209     cutoff++;
210   }
211   for (acc = 0, any = 0;; c = *s++)
212   {
213     if (isdigit(c))
214       c -= '0';
215     else if (isalpha(c))
216       c -= isupper(c) ? 'A' - 10 : 'a' - 10;
217     else
218       break;
219     if (c >= base)
220       break;
221     if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
222       any = -1;
223     else
224     {
225       any = 1;
226       acc *= base;
227       acc += c;
228     }
229   }
230   if (any < 0)
231   {
232     acc = neg ? LLONG_MIN : LLONG_MAX;
233     errno = ERANGE;
234   }
235   else if (neg)
236     acc = -acc;
237   if (endptr != 0)
238     *endptr = any ? unconst(s, char *) - 1 : unconst(nptr, char *);
239   return acc;
240 }
241 #endif
242