lib/C/iec_std_lib.h
changeset 1105 8128cad6a6e3
parent 1104 0bc1f5a4f975
equal deleted inserted replaced
1103:f3c6c938d5ff 1105:8128cad6a6e3
   468 static inline LINT __pstring_to_sint(STRING* IN) {
   468 static inline LINT __pstring_to_sint(STRING* IN) {
   469     LINT res = 0;
   469     LINT res = 0;
   470     __strlen_t l;
   470     __strlen_t l;
   471     unsigned int shift = 0;
   471     unsigned int shift = 0;
   472 
   472 
   473     if(IN->body[0]=='2' && IN->body[1]=='#'){
   473     if(IN->len < 1){
       
   474         /* empty string */
       
   475         return 0;
       
   476     }else if(IN->len > 1 && IN->body[0]=='2' && IN->body[1]=='#'){
   474         /* 2#0101_1010_1011_1111 */
   477         /* 2#0101_1010_1011_1111 */
   475         for(l = IN->len - 1; l >= 2 && shift < 64; l--)
   478         for(l = IN->len - 1; l >= 2 && shift < 64; l--)
   476         {
   479         {
   477             char c = IN->body[l];
   480             char c = IN->body[l];
   478             if( c >= '0' && c <= '1'){
   481             if( c >= '0' && c <= '1'){
   479                 res |= ( c - '0') << shift;
   482                 res |= ( c - '0') << shift;
   480                 shift += 1;
   483                 shift += 1;
   481             }
   484             }
   482         }
   485         }
   483     }else if(IN->body[0]=='8' && IN->body[1]=='#'){
   486     }else if(IN->len > 1 && IN->body[0]=='8' && IN->body[1]=='#'){
   484         /* 8#1234_5665_4321 */
   487         /* 8#1234_5665_4321 */
   485         for(l = IN->len - 1; l >= 2 && shift < 64; l--)
   488         for(l = IN->len - 1; l >= 2 && shift < 64; l--)
   486         {
   489         {
   487             char c = IN->body[l];
   490             char c = IN->body[l];
   488             if( c >= '0' && c <= '7'){
   491             if( c >= '0' && c <= '7'){
   489                 res |= ( c - '0') << shift;
   492                 res |= ( c - '0') << shift;
   490                 shift += 3;
   493                 shift += 3;
   491             }
   494             }
   492         }
   495         }
   493     }else if(IN->body[0]=='1' && IN->body[1]=='6' && IN->body[2]=='#'){
   496     }else if(IN->len > 2 && IN->body[0]=='1' && IN->body[1]=='6' && IN->body[2]=='#'){
   494         /* 16#1234_5678_9abc_DEFG */
   497         /* 16#1234_5678_9abc_DEFG */
   495         for(l = IN->len - 1; l >= 3 && shift < 64; l--)
   498         for(l = IN->len - 1; l >= 3 && shift < 64; l--)
   496         {
   499         {
   497             char c = IN->body[l];
   500             char c = IN->body[l];
   498             if( c >= '0' && c <= '9'){
   501             if( c >= '0' && c <= '9'){
   507             }
   510             }
   508         }
   511         }
   509     }else{
   512     }else{
   510         /* -123456789 */
   513         /* -123456789 */
   511         LINT fac = IN->body[0] == '-' ? -1 : 1;
   514         LINT fac = IN->body[0] == '-' ? -1 : 1;
   512         for(l = IN->len - 1; l >= 0 && shift < 20; l--)
   515         for(l = IN->len; l > 0 && shift < 20; l--)
   513         {
   516         {
   514             char c = IN->body[l];
   517             char c = IN->body[l-1];
   515             if( c >= '0' && c <= '9'){
   518             if( c >= '0' && c <= '9'){
   516                 res += ( c - '0') * fac;
   519                 res += ( c - '0') * fac;
   517                 fac *= 10;
   520                 fac *= 10;
   518                 shift += 1;
   521                 shift += 1;
   519             }else if( c >= '.' ){ /* reset value */
   522             }else if( c >= '.' ){ /* reset value */
   531 static inline ULINT __string_to_uint(STRING IN) {return (ULINT)__pstring_to_sint(&IN);}
   534 static inline ULINT __string_to_uint(STRING IN) {return (ULINT)__pstring_to_sint(&IN);}
   532 static inline LREAL __string_to_real(STRING IN) {
   535 static inline LREAL __string_to_real(STRING IN) {
   533     __strlen_t l;
   536     __strlen_t l;
   534     l = IN.len;
   537     l = IN.len;
   535     /* search the dot */
   538     /* search the dot */
   536     while(--l > 0 && IN.body[l] != '.');
   539     while(l > 0 && IN.body[--l] != '.');
   537     if(l != 0){
   540     if(l != 0){
   538         return atof((const char *)&IN.body);
   541         return atof((const char *)&IN.body);
   539     }else{
   542     }else{
   540         return (LREAL)__pstring_to_sint(&IN);
   543         return (LREAL)__pstring_to_sint(&IN);
   541     }
   544     }
   569      *
   572      *
   570      */
   573      */
   571     /* Quick hack : only transform seconds */
   574     /* Quick hack : only transform seconds */
   572     /* search the dot */
   575     /* search the dot */
   573     l = IN.len;
   576     l = IN.len;
   574     while(--l > 0 && IN.body[l] != '.');
   577     while(l > 0 && IN.body[--l] != '.');
   575     if(l != 0){
   578     if(l != 0){
   576         LREAL IN_val = atof((const char *)&IN.body);
   579         LREAL IN_val = atof((const char *)&IN.body);
   577         return  (TIME){(long)IN_val, (long)(IN_val - (LINT)IN_val)*1000000000};
   580         return  (TIME){(long)IN_val, (long)(IN_val - (LINT)IN_val)*1000000000};
   578     }else{
   581     }else{
   579         return  (TIME){(long)__pstring_to_sint(&IN), 0};
   582         return  (TIME){(long)__pstring_to_sint(&IN), 0};