Text Parser problem

I have a code that I have to rewrite or rework and not really sure where to start

I know I have to work in text phaser but unsure of the direction



Last edited on
You could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
enum keyword_t { LOCAL_TIME, WEATHER, TEMP_STRING, HUMIDITY, WIND, PRESSURE, PRESSURE_TREND, HEAT_INDEX, NOWCAST, UNKNOWN };
keyword_t keyword ;
bool getNowCast = true ;

void noteKeyword( const std::string& token )
{
    static const std::unordered_map< std::string, keyword_t > lookup = // #include <unordered_map>
    {
        { "local_time_rfc822", LOCAL_TIME },
        { "weather", WEATHER },
        { "temperature_string", TEMP_STRING },
        { "relative_humidity", HUMIDITY },
        { "wind_string", WIND },
        { "pressure_in", PRESSURE },
        { "pressure_trend", PRESSURE_TREND },
        { "heat_index_string", HEAT_INDEX },
        { "nowcast", NOWCAST },
    };

    const auto iter = lookup.find(token) ;
    if( iter != lookup.end() ) keyword = iter->second ;
    else keyword = UNKNOWN ;

    if( keyword == NOWCAST && !getNowCast ) keyword = UNKNOWN ;
}



Adding an overload for std::string would be the simpler solution.
+ Guarantees behaviour identical to the current code; regressions are not required
- C++ code is easier to maintain.

1
2
3
4
void noteKeyword ( char* token ) { /* current code */ } ;

// EDIT: corrected by firedraco
void noteKeyword( const std::string& token ) { return noteKeyword( token.c_str() ) ; }
Last edited on
void noteKeyword( const std::string& token ) { return token.c_str() ; }

Did you mean:

void noteKeyword( const std::string& token ) { noteKeyword(token.c_str()); }
Last edited on
> Did you mean:

Yes, thanks!.
Wow I like that solution but other than text parser isn't there an easier way cause really it just looks like the strcmp is wrong but unsure what to put there instead
Topic archived. No new replies allowed.