Changeset 1049 for XIOS/trunk


Ignore:
Timestamp:
02/03/17 14:26:51 (7 years ago)
Author:
rlacroix
Message:

Fix: Do not allow redefining the same duration unit.

For example "4d 7d" used to be parsed as "7d", now an error will be thrown.

Fixes ticket #115.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/duration.cpp

    r1028 r1049  
    3838        char   c = '/'; 
    3939        bool   invalidUnit = false; 
     40        CDuration sentinel = NoneDu; 
     41 
     42#define setDuration(unit, value)                                                  \ 
     43        {                                                                         \ 
     44          if (sentinel.unit)                                                      \ 
     45            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", \ 
     46                  << "Bad duration format: " #unit " has already been set.");      \ 
     47                                                                                  \ 
     48          duration.unit = value;                                                  \ 
     49          sentinel.unit = 1.0;                                                    \ 
     50        } 
    4051 
    4152        do 
     
    4859          switch (c) 
    4960          { 
    50             case 'y': duration.year   = v; break; 
    51             case 'd': duration.day    = v; break; 
    52             case 'h': duration.hour   = v; break; 
    53             case 's': duration.second = v; break; 
     61            case 'y': setDuration(year, v) break; 
     62            case 'd': setDuration(day, v) break; 
     63            case 'h': setDuration(hour, v) break; 
     64            case 's': setDuration(second, v) break; 
    5465            case 'm': 
    5566            { 
    5667              in >> c; 
    57               if      (c == 'i') duration.minute = v; 
    58               else if (c == 'o') duration.month  = v; 
     68              if      (c == 'i') setDuration(minute, v) 
     69              else if (c == 'o') setDuration(month, v) 
    5970              else invalidUnit = true; 
    6071              break; 
     
    6374            { 
    6475              in >> c; 
    65               if (c == 's') duration.timestep = v; 
     76              if (c == 's') setDuration(timestep, v) 
    6677              else invalidUnit = true; 
    6778              break; 
     
    7687                  << "Bad duration format: invalid unit, unexpected '" << c << "' character."); 
    7788        } while (in.peek() != EOF); // check whether there is a next character to read 
     89 
     90#undef setDuration 
    7891 
    7992        return in; 
Note: See TracChangeset for help on using the changeset viewer.