/* * getnum.h * ***************************************************************************** * * Scan a int / unsigned int from stdin for C * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ***************************************************************************** * * Author : Rajesh _ThE gReAt * Built on : Saturday, January 27, 2002, 9:30:14 AM * Last Modified on : Friday , February 08, 2002, 4:00:44 PM * * Known errors : ---===={none}====--- * ***************************************************************************** * DISCLAIMER: ***************************************************************************** * * Programmers may incorporate any or all code into their programs, * giving proper credit within the source. Publication of the * source routines is permitted so long as proper credit is given * to Rajesh _ThE gReAt. * * Copyright (C) 2002, 2003 by Rajesh _ThE gReAt. You may use this program, or * code or tables extracted from it, as desired without restriction. * I can not and will not be held responsible for any damage caused from * the use of this software. * * Read license.txt from http://www.rajeshgoli.com/license.txt * or http://www.rajeshgoli.com/license.htm * before using / distributing / or any sort of use of my source * Also read my disclaimer http://www.rajeshgoli.com/disclaimer.htm * * YOU AGREE TO BE BOUND BY MY DISCLAIMER AND LICENSE BY IN ANY WAY * USING THIS FILE . * ***************************************************************************** * This source works with Turbo C 2.0 and Turbo C++ 3.0 and above. ***************************************************************************** * Rajesh _ThE gReAt is a synonym of Rajesh Goli * Source freely distributable *****************************************************************************/ #ifndef __GETNUM_H #define __GETNUM_H #endif /* Not reliable yet */ #define CHECK_FOR_OVERFLOW 0 #ifndef EMPTY_STRING #define EMPTY_STRING -3 #endif #ifndef MT #define MT EMPTY_STRING #endif #ifndef ESC_KEY_HIT #define ESC_KEY_HIT -2 #endif #ifndef EK #define EK ESC_KEY_HIT #endif #ifndef OVERFLOW #define OVERFLOW -2 #endif #ifndef ORF #define ORF OVERFLOW #endif #ifndef __STDIO_H #include #endif #ifndef __CONIO_H #include #endif #ifndef __CTYPE_H #include #endif #ifndef __STRING_H #include #endif /* For compatibility */ #define getnum(x) getint(x) #define getunum(x) getuint(x) int getint(int *); int getuint(unsigned *); int getint(int *no) { char ch=0,num[100]; #if( CHECK_FOR_OVERFLOW == 1 ) int len; char intmax[8],intmin[8]; #endif short flag; int i; i=0; flag = 0; while(ch!='\r') { fflush(stdin); if( i == 0) { do { ch = getch(); if(!isdigit(ch) && ch != '+' && ch != '-') flag = 0; else { num[i++] = ch; putch(ch); flag = 1; } } while(!flag); continue; } ch = getch(); if(isdigit(ch)) { num[i++] = ch; putch(ch); } else if(ch == '\b') { printf("\b \b"); if(i>0) i--; } else if(ch == 0x1b) return ESC_KEY_HIT; } num[i] = 0; if(num[0] == 0) { return EMPTY_STRING; } *no = atoi(num); #if( CHECK_FOR_OVERFLOW == 1 ) sprintf(intmax,"%d",INT_MAX); sprintf(intmin,"%d",INT_MIN); len = (strlen(intmax) >= strlen(intmin))?strlen(intmax):strlen(intmin); 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) )) return OVERFLOW; #endif return 1; } int getuint(unsigned *no) { char ch=0,num[100]; #if( CHECK_FOR_OVERFLOW == 1 ) int len; char unsignedmax[18]; #endif short flag; int i; i=0; flag = 0; while(ch!='\r') { fflush(stdin); if( i == 0) { do { ch = getch(); if(!isdigit(ch) && ch != '+') flag = 0; else { num[i++] = ch; putch(ch); flag = 1; } } while(!flag); continue; } ch = getch(); if(isdigit(ch)) { num[i++] = ch; putch(ch); } else if(ch == '\b') { printf("\b \b"); if(i>0) i--; } else if(ch == 0x1b) return ESC_KEY_HIT; } num[i] = 0; if(num[0] == 0) { return EMPTY_STRING; } sscanf(num,"%u",no); #if( CHECK_FOR_OVERFLOW == 1 ) sprintf(unsignedmax,"%d",UINT_MAX); len = strlen(unsignedmax); if( (strlen(num) > len) || ((strlen(num) == len) && ((num[0] == '+' && atol(num) > UINT_MIN) || (isdigit(num[0]) && atol(num) > UINT_MIN) )) return OVERFLOW; #endif return 1; }