Advertisement
Mary_99

Strtol see progress

May 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <assert.h>  
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5.  
  6. long strtol(char* nPtr, char **endPtr , int base)
  7. {
  8.    
  9. char *string;
  10. long returnedNumber = 0;
  11. int numberSign;
  12.  
  13. assert(nPtr); /* quits if string is empty */
  14. string = nPtr; /* string points to the same place as nPtr*/
  15.  
  16. while(isspace(* string)) /* omitt white numbers*/
  17. {
  18.     string++;
  19. }
  20.  
  21. if(*string =='-')
  22. {
  23.     numberSign = 1;
  24.     string++;
  25. }
  26. else
  27. {
  28.     if(*string =='+')
  29.     {
  30.         numberSign = 0;
  31.         string++;
  32.     }
  33.     else numberSign = 0;
  34. }
  35.  
  36.  
  37. if((base == 8) && (*string =='0'))
  38. {
  39.     string++;
  40. }
  41.  
  42. if((base == 16) && (*string =='0'))
  43. {
  44.     string++;
  45.    
  46.     if((*string =='x')||(*string =='X'))
  47.     {
  48.         string++;
  49.     }
  50.     else
  51.     {
  52.         string--;
  53.     }
  54. }
  55. if((*string =='0') && (base == 0))
  56. {
  57.     base = 8;
  58.     string++;
  59.    
  60.     if((*string =='x')||(*string =='X'))
  61.     {
  62.         base = 16;
  63.         string++;
  64.     }  
  65.    
  66. }
  67. 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*/
  68. {  
  69.     base = 10;
  70. }
  71. if(base < 2 || base > 36)
  72. {
  73.     errno = EINVAL;
  74.     return 0;
  75. }
  76. if((base == 8)&&((*string < '0')||(*string > '7')))
  77.  
  78. {
  79.     *endPtr = string;
  80.     return 0;
  81. }        
  82.  
  83. if((base == 16) && ((*string < '0')||((*string >'9')&&(*string < 'A'))||((*string > 'F')&&(*string < 'a'))||(*string > 'f')))
  84. {
  85.     string--;
  86.     *endPtr = string;
  87.     return 0;
  88. }
  89.  
  90.  
  91.  
  92. return returnedNumber;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement