Text Parser problem

Sep 29, 2014 at 10:04pm
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 Sep 30, 2014 at 11:56pm
Sep 30, 2014 at 1:30am
Sep 30, 2014 at 3:18am
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 Sep 30, 2014 at 3:31am
Sep 30, 2014 at 3:23am
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 Sep 30, 2014 at 3:23am
Sep 30, 2014 at 3:32am
> Did you mean:

Yes, thanks!.
Sep 30, 2014 at 1:02pm
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.