source: trunk/orbital/CommandParser.h @ 1360

Last change on this file since 1360 was 1360, checked in by sam, 9 years ago

orbital: fix a few compilation warnings.

File size: 2.7 KB
Line 
1//
2// Orbital
3//
4// Copyright: (c) 2012 Various People
5//
6
7#if !defined __COMMANDPARSER_H__
8#define __COMMANDPARSER_H__
9
10class CommandParser
11{
12public:
13    CommandParser()
14    {}
15
16    virtual void SwitchCommand(char const *&command)
17    {
18        (void)command;
19    }
20
21    bool CheckCommand(char const *keyword, char const *&command)
22    {
23        return (!strncmp(command, keyword, strlen(keyword))
24          && (command[strlen(keyword)] < 'a' || command[strlen(keyword)] > 'z')
25          && (command += strlen(keyword)));
26    }
27
28    void SendCommand(char const *command)
29    {
30        for (char const *p = command; p && *p; )
31        {
32            SwitchCommand(p);
33
34            /* Skip all remaining letters */
35            while (*p && *p >= 'a' && *p <= 'z')
36                p++;
37            /* ... and skip everything until next letter */
38            while (*p && (*p < 'a' || *p > 'z'))
39                p++;
40        }
41    }
42
43    static char const *GetArg(char const *p, float &f)
44    {
45        float sign = 1.0f;
46        f = 0.0f;
47        /* 1) Skip commas until the first valid character, 2) read all
48         * the minus signs, 3) read all the digits, 4) skip all the dots,
49         * if any, 5) read all the remaining digits, if any. */
50        for ( ; *p == ','; p++)
51            ;
52        for ( ; *p == '-'; p++)
53            sign = -sign;
54        for ( ; *p >= '0' && *p <= '9'; p++)
55            f = f * 10.f + (*p - '0');
56        for ( ; *p == '.'; p++)
57            ;
58        for (float div = 10.f; *p >= '0' && *p <= '9'; div *= 10.f)
59            f += (*p++ - '0') / div;
60        f *= sign;
61        return p;
62    }
63
64    static char const *GetArg(char const *p, float &f1, float &f2)
65    {
66        return GetArg(GetArg(p, f1), f2);
67    }
68
69    static char const *GetArg(char const *p, float &f1, float &f2, float &f3)
70    {
71        return GetArg(GetArg(p, f1, f2), f3);
72    }
73
74    static char const *GetArg(char const *p, float &f1, float &f2,
75                                             float &f3, float &f4)
76    {
77        return GetArg(GetArg(p, f1, f2, f3), f4);
78    }
79
80    static char const *GetArg(char const *p, float &f1, float &f2, float &f3,
81                                             float &f4, float &f5)
82    {
83        return GetArg(GetArg(p, f1, f2, f3, f4), f5);
84    }
85
86    static char const *GetArg(char const *p, vec3 &v)
87    {
88        return GetArg(p, v.x, v.y, v.z);
89    }
90
91    static char const *GetArg(char const *p, vec3 &v, float &f)
92    {
93        return GetArg(p, v.x, v.y, v.z, f);
94    }
95
96    static char const *GetArg(char const *p, vec4 &v)
97    {
98        return GetArg(p, v.x, v.y, v.z, v.w);
99    }
100
101    virtual void Flush()
102    {
103    }
104
105private:
106};
107
108#endif /* __COMMANDPARSER_H__ */
109
Note: See TracBrowser for help on using the repository browser.