Mary_99

strtol working on

May 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #include <assert.h>  
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <limits.h> // includs LONG_MIN LONG_MAX
  5.  
  6. long strtol(char* nPtr, char **endPtr , int base)
  7. {
  8.    
  9.     char *string;
  10.     long returnedNumber = 0;
  11.    
  12.     int outsideTheLimit;
  13.     int numberSign;
  14.     int addedDigit;
  15.    
  16.    
  17.     assert(nPtr); // quits if string is empty
  18.     string = nPtr; // string points to the same place as nPtr
  19.  
  20.     while(isspace(* string)) // omitt white numbers
  21.     {
  22.         string++;endPtr
  23.     }
  24.  
  25.     if(*string =='-')
  26.     {
  27.         numberSign = 1;
  28.         string++;
  29.     }
  30.     else
  31.     {
  32.         if(*string =='+')
  33.         {
  34.             numberSign = 0;
  35.             string++;
  36.         }
  37.         else numberSign = 0;
  38.     }
  39.  
  40.  
  41.     if((base == 8) && (*string =='0'))
  42.     {
  43.         string++;
  44.     }
  45.  
  46.     if((base == 16) && (*string =='0'))
  47.     {
  48.         string++;
  49.        
  50.         if((*string =='x')||(*string =='X'))
  51.         {
  52.             string++;
  53.         }
  54.         else
  55.         {
  56.             string--;
  57.         }
  58.     }
  59.     if((*string =='0') && (base == 0))
  60.     {
  61.         base = 8;
  62.         string++;
  63.        
  64.         if((*string =='x')||(*string =='X'))
  65.         {
  66.             base = 16;
  67.             string++;
  68.         }  
  69.        
  70.     }
  71.     else if(base == 0) //if the base is 0 and not 0x or 0X then the base is 10
  72.     {  
  73.         base = 10;
  74.     }
  75.     if(base < 2 || base > 36)
  76.     {
  77.         errno = EINVAL; // if base outside [2;36]
  78.         return 0;
  79.     }
  80.     if((base == 8)&&((*string < '0')||(*string > '7')))
  81.  
  82.     {
  83.         *endPtr = string;
  84.         return 0;
  85.     }        
  86.  
  87.     if((base == 16) && ((*string < '0')||((*string >'9')&&(*string < 'A'))||((*string > 'F')&&(*string < 'a'))||(*string > 'f')))
  88.     {
  89.         string--;
  90.         *endPtr = string;
  91.         return 0;
  92.     }
  93.  
  94.    
  95.  
  96.     while(*string!='\0') // pointing to end of file
  97.     {
  98.         if(isdigit(*string)) // to return digit or nothing
  99.         {
  100.                 addedDigit = *string - '0'; //ascci conversion
  101.         }
  102.         else
  103.         {
  104.             if(isalpha(*string)) //  0 or a letter
  105.             {
  106.                 if(islower(*string)) //points to the lower case letter
  107.                 {
  108.                     addedDigit = (*string - 'a') + 10; //converts letters ASCII to digits  
  109.                 }
  110.                 else
  111.                 {
  112.                     addedDigit = (*string - 'A') + 10;
  113.                 }
  114.             }
  115.             else
  116.             {
  117.                 break;
  118.             }
  119.         }
  120.     }
  121.        
  122.         switch(numberSign);
  123.         {
  124.             case 0:
  125.             {
  126.                 if(outsideTheLimit < 0  || returnedNumber < ((LONG_MAX - addedDigit)/ base)) // check if the number is outsie the limits
  127.                 {
  128.                     outsideTheLimit = - 1;
  129.                     string ++;
  130.                 }
  131.                 else
  132.                 {
  133.                     outsideTheLimit = 1;
  134.                     returnedNumber = (returnedNumber * base ) + addedDigit ;
  135.                     string++ ;
  136.                 }
  137.                 break ;
  138.             }
  139.            
  140.             case 1:
  141.             {
  142.                 if(outsideTheLimit > 0  || returnedNumber > ((LONG_MAX - addedDigit)/ base))
  143.                 {
  144.                     outsideTheLimit = - 1;
  145.                     string ++;
  146.                 }
  147.                 else
  148.                 {
  149.                     outsideTheLimit = 1;
  150.                     returnedNumber = (returnedNumber * base ) - addedDigit ;
  151.                     string++ ;
  152.                 }
  153.                   break;
  154.             }
  155.          
  156.            
  157.         }
  158.  
  159.  
  160.     }
  161.    
  162.     if(outsideTheLimit < 0) // to return max or min it depends on te sin
  163.     {
  164.          returnedNumber = numberSign ? LONG_MIN : LONG_MAX  //something like that ? checik it XD
  165.            
  166.             errno = ERANGE; // out of the range xD
  167.  
  168.     }
  169.    
  170.     if(endPtr
  171.  
  172.        
  173.        
  174.        
  175.    
  176.        
  177.  
  178.     return returnedNumber;
  179. }
Add Comment
Please, Sign In to add comment