| Close | Back |
1 /*
2 * getnum.h
3 *
4 *****************************************************************************
5 *
6 * Scan a int / unsigned int from stdin for C
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *****************************************************************************
9 *
10 * Author : Rajesh _ThE gReAt
11 * Built on : Saturday, January 27, 2002, 9:30:14 AM
12 * Last Modified on : Friday , February 08, 2002, 4:00:44 PM
13 *
14 * Known errors : ---===={none}====---
15 *
16 *****************************************************************************
17 * DISCLAIMER:
18 *****************************************************************************
19 *
20 * Programmers may incorporate any or all code into their programs,
21 * giving proper credit within the source. Publication of the
22 * source routines is permitted so long as proper credit is given
23 * to Rajesh _ThE gReAt.
24 *
25 * Copyright (C) 2002, 2003 by Rajesh _ThE gReAt. You may use this program, or
26 * code or tables extracted from it, as desired without restriction.
27 * I can not and will not be held responsible for any damage caused from
28 * the use of this software.
29 *
30 * Read license.txt from http://www.rajeshgoli.com/license.txt
31 * or http://www.rajeshgoli.com/license.htm
32 * before using / distributing / or any sort of use of my source
33 * Also read my disclaimer http://www.rajeshgoli.com/disclaimer.htm
34 *
35 * YOU AGREE TO BE BOUND BY MY DISCLAIMER AND LICENSE BY IN ANY WAY
36 * USING THIS FILE .
37 *
38 *****************************************************************************
39 * This source works with Turbo C 2.0 and Turbo C++ 3.0 and above.
40 *****************************************************************************
41 * Rajesh _ThE gReAt is a synonym of Rajesh Goli
42 * Source freely distributable
43 *****************************************************************************/
44
45 #ifndef __GETNUM_H
46 #define __GETNUM_H
47 #endif
48
49 /* Not reliable yet
50 */
51 #define CHECK_FOR_OVERFLOW 0
52
53 #ifndef EMPTY_STRING
54 #define EMPTY_STRING -3
55 #endif
56
57 #ifndef MT
58 #define MT EMPTY_STRING
59 #endif
60
61 #ifndef ESC_KEY_HIT
62 #define ESC_KEY_HIT -2
63 #endif
64
65 #ifndef EK
66 #define EK ESC_KEY_HIT
67 #endif
68
69 #ifndef OVERFLOW
70 #define OVERFLOW -2
71 #endif
72
73 #ifndef ORF
74 #define ORF OVERFLOW
75 #endif
76
77 #ifndef __STDIO_H
78 #include<stdio.h>
79 #endif
80
81 #ifndef __CONIO_H
82 #include<conio.h>
83 #endif
84
85 #ifndef __CTYPE_H
86 #include<ctype.h>
87 #endif
88
89 #ifndef __STRING_H
90 #include<string.h>
91 #endif
92
93 /* For compatibility */
94 #define getnum(x) getint(x)
95 #define getunum(x) getuint(x)
96
97 int getint(int *);
98 int getuint(unsigned *);
99
100 int getint(int *no)
101 {
102 char ch=0,num[100];
103 #if( CHECK_FOR_OVERFLOW == 1 )
104 int len;
105 char intmax[8],intmin[8];
106 #endif
107 short flag;
108 int i;
109 i=0;
110 flag = 0;
111
112 while(ch!='\r')
113 {
114 fflush(stdin);
115 if( i == 0)
116 {
117 do
118 {
119 ch = getch();
120 if(!isdigit(ch) && ch != '+' && ch != '-')
121 flag = 0;
122 else
123 {
124 num[i++] = ch;
125 putch(ch);
126 flag = 1;
127 }
128 }
129 while(!flag);
130 continue;
131 }
132 ch = getch();
133 if(isdigit(ch))
134 {
135 num[i++] = ch;
136 putch(ch);
137 }
138 else if(ch == '\b')
139 {
140 printf("\b \b");
141 if(i>0)
142 i--;
143 }
144 else if(ch == 0x1b)
145 return ESC_KEY_HIT;
146 }
147 num[i] = 0;
148 if(num[0] == 0)
149 {
150 return EMPTY_STRING;
151 }
152
153 *no = atoi(num);
154
155 #if( CHECK_FOR_OVERFLOW == 1 )
156 sprintf(intmax,"%d",INT_MAX);
157 sprintf(intmin,"%d",INT_MIN);
158 len = (strlen(intmax) >= strlen(intmin))?strlen(intmax):strlen(intmin);
159 if( (strlen(num) > len) || ((strlen(num) == len) && ((num[0] == '-' && atol(num) < INT_MIN) || (num[0] == '+' && atol(num) > INT_MIN) || (isdigit(num[0]) && atol(num) > INT_MIN) ))
160 return OVERFLOW;
161 #endif
162 return 1;
163 }
164
165 int getuint(unsigned *no)
166 {
167 char ch=0,num[100];
168 #if( CHECK_FOR_OVERFLOW == 1 )
169 int len;
170 char unsignedmax[18];
171 #endif
172 short flag;
173 int i;
174 i=0;
175 flag = 0;
176
177 while(ch!='\r')
178 {
179 fflush(stdin);
180 if( i == 0)
181 {
182 do
183 {
184 ch = getch();
185 if(!isdigit(ch) && ch != '+')
186 flag = 0;
187 else
188 {
189 num[i++] = ch;
190 putch(ch);
191 flag = 1;
192 }
193 }
194 while(!flag);
195 continue;
196 }
197 ch = getch();
198 if(isdigit(ch))
199 {
200 num[i++] = ch;
201 putch(ch);
202 }
203 else if(ch == '\b')
204 {
205 printf("\b \b");
206 if(i>0)
207 i--;
208 }
209 else if(ch == 0x1b)
210 return ESC_KEY_HIT;
211 }
212 num[i] = 0;
213 if(num[0] == 0)
214 {
215 return EMPTY_STRING;
216 }
217
218 sscanf(num,"%u",no);
219
220 #if( CHECK_FOR_OVERFLOW == 1 )
221 sprintf(unsignedmax,"%d",UINT_MAX);
222 len = strlen(unsignedmax);
223 if( (strlen(num) > len) || ((strlen(num) == len) && ((num[0] == '+' && atol(num) > UINT_MIN) || (isdigit(num[0]) && atol(num) > UINT_MIN) ))
224 return OVERFLOW;
225 #endif
226 return 1;
227 }