home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / lj2up2.zip / STOI.C < prev    next >
Text File  |  1988-07-12  |  2KB  |  98 lines

  1. /* STOI.C   More powerful version of atoi.
  2.  *
  3.  *  Copyright (C) 1985 by Allen  Holub.  All rights reserved. 
  4.  *  This program may be copied for personal, non-profit use only.
  5.  */
  6.  
  7. /* Steve Coles - 7/1/88 Prototype added for MSC 5.1  */
  8. int stoi(char * *instr);
  9.  
  10. #define islower(c)  ( 'a' <= (c) && (c) <= 'z' )
  11. #define toupper(c)  ( islower(c) ? (c) - ('a' - 'A') : (c) )
  12.  
  13. int     stoi(instr)
  14. register char   **instr;
  15. {
  16.     /*  Convert string to integer. If string starts with 0x it is
  17.      *  interpreted as a hex number, else if it starts with a 0 it
  18.      *  is octal, else it is decimal. Conversion stops on encountering
  19.      *  the first character which is not a digit in the indicated
  20.      *  radix. *instr is updated to point past the end of the number.
  21.      */
  22.  
  23.     register int    num  = 0 ;
  24.     register char   *str     ;
  25.     int     sign = 1 ;
  26.  
  27.     str = *instr;
  28.  
  29.     while(*str == ' '  ||  *str == '\t'  ||  *str == '\n' )
  30.         str++ ;
  31.  
  32.     if( *str == '-' )
  33.     {
  34.         sign = -1 ;
  35.         str++;
  36.     }
  37.  
  38.     if(*str == '0')
  39.     {
  40.         ++str;
  41.         if (*str == 'x'  ||  *str == 'X')
  42.         {
  43.             str++;
  44.             while(  ('0'<= *str && *str <= '9') ||
  45.                 ('a'<= *str && *str <= 'f') ||
  46.                 ('A'<= *str && *str <= 'F')  )
  47.             {
  48.                 num *= 16;
  49.                 num += ('0'<= *str && *str <= '9') ?
  50.                     *str - '0'         :
  51.                     toupper(*str) - 'A' + 10   ;
  52.                 str++;
  53.             }
  54.         }
  55.         else
  56.         {
  57.             while( '0' <= *str  &&  *str <= '7' )
  58.             {
  59.                 num *= 8;
  60.                 num += *str++ - '0' ;
  61.             }
  62.         }
  63.     }
  64.     else
  65.     {
  66.         while( '0' <= *str  &&  *str <= '9' )
  67.         {
  68.             num *= 10;
  69.             num += *str++ - '0' ;
  70.         }
  71.     }
  72.  
  73.     *instr = str;
  74.     return( num * sign );
  75. }
  76.  
  77. #ifdef TEST
  78.  
  79. main()
  80. {
  81.     int num;
  82.     char    numbuf[80], *p;
  83.  
  84.     printf("Use ^C to exit\n");
  85.  
  86.     while( 1 )
  87.     {
  88.         printf("string ( 0x.. 0.. ... ): ");
  89.         gets( p = numbuf );
  90.         num = stoi( &p )  ;
  91.         printf( "<%s> = %d = 0x%x = %o octal   ", numbuf,num,num,num);
  92.         printf( "string tail = <%s>\n", p );
  93.     }
  94. }
  95.  
  96. #endif
  97. 
  98.