Advertisement
Mary_99

stril oddany

May 29th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include <assert.h>
  2. #include <ctype.h>
  3. #include <errno.h>  
  4. #include <limits.h>
  5.  
  6.  
  7. long strtol( const char* nptr, char **endptr , int base)
  8. {
  9.     // char *string;  //points at the character being currently converted
  10.     long returnedNumber = 0;
  11.     int numberOutsideTheLimit = 0;
  12.     int sign;
  13.     int addedDigit;
  14.    
  15.    
  16.     assert(nptr); // quits if (nptr = NULL)
  17.     char * string = (char*)nptr;
  18.    
  19.     while(isspace(*string))
  20.     {
  21.         string++;
  22.     }
  23.  
  24.     if(*string =='-')
  25.     {
  26.         sign = 1;
  27.         string++;
  28.     }
  29.     else
  30.     {
  31.         if(*string =='+')
  32.         {
  33.             sign = 0;
  34.             string++;
  35.         }
  36.         else
  37.         {
  38.             sign = 0;
  39.         }
  40.     }
  41.    
  42.     if((base == 8) && (*string =='0'))
  43.     {
  44.         string++;
  45.     }
  46.    
  47.     if((base == 16) && (*string == '0'))
  48.     {
  49.         string++;
  50.        
  51.         if((*string =='x')||(*string =='X'))
  52.         {
  53.             string++;
  54.         }
  55.         else
  56.         {
  57.             string--;
  58.         }
  59.     }
  60.     if((*string =='0') && (base == 0))
  61.     {
  62.         base = 8;
  63.         string++;
  64.        
  65.         if((*string =='x')||(*string =='X'))
  66.         {
  67.             base = 16;
  68.             string++;
  69.         }  
  70.    
  71.     }
  72.     else if(base == 0) //if the base is 0 and the beginning of string is not 0, 0x or 0X then the base is 10
  73.     {
  74.         base = 10;
  75.     }
  76.    
  77.     if(base < 2 || base > 36)
  78.     {
  79.         errno = EINVAL;
  80.         return 0;
  81.     }
  82.    
  83.     if((base == 8) && ((*string < '0')||(*string >'7')))
  84.     {
  85.         *endptr = string;
  86.         return 0;
  87.     }  
  88.    
  89.     if((base == 16) && ((*string < '0') || ((*string > '9')&&(*string < 'A')) || ((*string > 'F') && (*string < 'a')) || (*string >'f')))
  90.     {
  91.         string--;
  92.         *endptr = string;
  93.         return 0;
  94.     }
  95.    
  96.     while(*string != '\0') //This loop terminates when string points to the end of the file
  97.     {  
  98.         if (isdigit(*string))
  99.         {
  100.             addedDigit = *string - '0'; //converting ASCII code to corresponding digit
  101.         }
  102.         else
  103.         {
  104.             if(isalpha(*string))
  105.             {
  106.                 if(islower(*string))
  107.                 {
  108.                     addedDigit = (*string - 'a') + 10;
  109.                 }
  110.                 else
  111.                 {
  112.                     addedDigit = (*string - 'A') + 10;
  113.                 }
  114.             }
  115.             else // loop is terminated if string does not point to digits or letters
  116.             {
  117.                 break;
  118.             }
  119.         }
  120.        
  121.         switch(sign)
  122.         {
  123.         case 0:
  124.         {
  125.             if (numberOutsideTheLimit < 0 || returnedNumber > ((LONG_MAX-addedDigit) / base))
  126.             {
  127.                 numberOutsideTheLimit =- 1; //limit for long int
  128.                 string++;
  129.             }  
  130.             else // if the number is inside the limits then numberOutsideTheLimit=1 and current digit is attached to the number
  131.             {
  132.                 numberOutsideTheLimit = 1;
  133.                 returnedNumber = (returnedNumber * base) + addedDigit;
  134.                 string++;
  135.             }
  136.             break;
  137.    
  138.         }
  139.         case 1:
  140.         {
  141.             if (numberOutsideTheLimit < 0 || returnedNumber < ((LONG_MIN + addedDigit) / base))
  142.             {
  143.                 numberOutsideTheLimit =-1;
  144.                 string++;
  145.             }  
  146.             else
  147.             {
  148.                 numberOutsideTheLimit = 1;
  149.                 returnedNumber = (returnedNumber * base)- addedDigit;
  150.                 string++;
  151.             }
  152.        
  153.             break;
  154.         }
  155.         }
  156.     }
  157.     if (numberOutsideTheLimit < 0)
  158.             {
  159.                 returnedNumber = sign ? LONG_MIN : LONG_MAX;
  160.                 errno = ERANGE;
  161.             }
  162.    
  163.     if (endptr)  
  164.         *endptr = numberOutsideTheLimit ? string : (char *) nptr;
  165.  
  166. return returnedNumber;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement