Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*============================================================================
- ----------------------------------------------------------------------------
- myIntegralScanf.c - A scanf alternative (for integers only).
- (c) Damion 'Phr0z3n.Dev' Tapper, 2013.
- Email: Phr0z3n.Dev@Gmail.com
- NOTE: Consider it secure.
- ----------------------------------------------------------------------------
- ============================================================================*/
- #define USE_SEC_API /* Comment this line if you do not have the secure libraries. */
- #include <stdio.h> /* For the printf function. */
- #include <conio.h> /* For the getch function. */
- #include <ctype.h> /* For the isdigit function. */
- #include <math.h> /* For the pow function. */
- /* Variable name definitions. */
- #define POSITION pmss->position
- #define INCREMENT pmss->increment
- #define BUFFER pmss->charArray
- #define ENTRY pmss->entry
- #define INPUT pmss->entry = getch()
- #define OFFSET BUFFER[0]
- #define ENTER_IS_NOT_PRESSED ENTRY != 13
- #define RESET_POSITION POSITION = 1
- /* Type definitions. */
- typedef struct myScanfStruct MSS;
- typedef struct myScanfStruct* PMSS; /* Hungarian notation. Ummmm... */
- struct myScanfStruct
- {
- int entry;
- int increment;
- int position;
- char charArray[]; /* Unconventional but allowed. */
- };
- /* Function prototypes definitions. */
- static void myScanf(struct myScanfStruct *); /* ...with limited scope (static).*/
- int main(void)
- {
- /* NOTE: Pointers are always perfect to reduce pushing and popping on the
- * stack.
- * The variable is also declared local for maximum security.
- */
- PMSS pmss;
- /*
- * No return value is needed because a pointer is used.
- * Such is the nature of functions in the secure library.
- * ...unless there is an (unlikely) error...
- */
- myScanf(pmss);
- /* TEST */
- #ifdef USE_SEC_API
- printf_s("\n%d", ENTRY - 10); /* The secure printf function (good programming practice). */
- /*
- * Notice how you entered digits (characters) but a number was subtracted
- * from the result.
- */
- #else
- /* If you are lacking the secure libraries. */
- printf("\n%d", ENTRY - 10);
- #endif
- return 0;
- }
- static void myScanf(struct myScanfStruct *pmss)
- {
- RESET_POSITION; /* Each time the function is called. */
- /*
- * A 32-bit integer (or long) allows only 10 digits so provisions
- * must be made accordingly. The code can be altered to accommodate
- * entry for a 64-bit integer (long long) (19 digits).
- */
- for(INCREMENT = 0; INCREMENT <= 10; INCREMENT++)
- BUFFER[INCREMENT] = '\0';
- /* Accept entry... */
- do
- {
- INPUT;
- /* While an entry is being made... */
- if((isdigit(ENTRY) || (POSITION == 1 && ENTRY == '-')) && POSITION <= 10)
- {
- printf("%c", ENTRY);
- BUFFER[POSITION] = ENTRY;
- POSITION++;
- }
- /* While backspace is pressed... */
- if(ENTRY == 8 && POSITION > 1)
- {
- BUFFER[POSITION] = '\0';
- printf("\b \b");
- POSITION--;
- }
- }
- while(ENTER_IS_NOT_PRESSED);
- /*
- * A terminating character is assigned. '\0' would be an unwise choice since
- * any number ending with 0 (multiple of 10) entered could be misinterpreted.
- * Besides 'ENTER' usually invokes the new line character.
- */
- BUFFER[POSITION] = '\n';
- /* If a negative number is entered... */
- if(BUFFER[1] == '-')
- {
- OFFSET = '-';
- BUFFER[1] = '0';
- }
- /* Entry conversion: from characters to digits (integers). */
- for(POSITION = 0; BUFFER[POSITION] != '\n'; POSITION++)
- {
- switch(BUFFER[POSITION])
- {
- case '0':
- BUFFER[POSITION] = 0;
- break;
- case '1':
- BUFFER[POSITION] = 1;
- break;
- case '2':
- BUFFER[POSITION] = 2;
- break;
- case '3':
- BUFFER[POSITION] = 3;
- break;
- case '4':
- BUFFER[POSITION] = 4;
- break;
- case '5':
- BUFFER[POSITION] = 5;
- break;
- case '6':
- BUFFER[POSITION] = 6;
- break;
- case '7':
- BUFFER[POSITION] = 7;
- break;
- case '8':
- BUFFER[POSITION] = 8;
- break;
- case '9':
- BUFFER[POSITION] = 9;
- break;
- }
- }
- /*
- * If for some 'curious' reason zeroes are entered before the number...
- * Such a number is still valid (by law) so...
- */
- for(POSITION = 1; BUFFER[POSITION] == 0; POSITION++)
- ;
- /* ...remove leading zeroes. */
- for(INCREMENT = 1; BUFFER[POSITION] != '\n'; INCREMENT++, POSITION++)
- BUFFER[INCREMENT] = BUFFER[POSITION];
- /* Convert the entry to one integer. */
- for(ENTRY = 0, POSITION = 1, INCREMENT -= 2; INCREMENT >= 0; POSITION++, INCREMENT--)
- ENTRY += BUFFER[POSITION] * pow(10, INCREMENT);
- /* ...if the entry is negative. */
- if(OFFSET == '-')
- ENTRY = -ENTRY;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement