c6242015b726cd0dfb7e3069db0467d4a97cfa33
[vuplus_bitbake] / lib / bb / parse / parse_c / token.h
1 /*
2 Copyright (C) 2005 Holger Hans Peter Freyther
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
20 THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 */
23
24 #ifndef TOKEN_H
25 #define TOKEN_H
26
27 #include <ctype.h>
28 #include <string.h>
29
30 #define PURE_METHOD
31
32
33 /**
34  * Special Value for End Of File Handling. We set it to
35  * 1001 so we can have up to 1000 Terminal Symbols on
36  * grammar. Currenlty we have around 20
37  */
38 #define T_EOF    1001
39
40 struct token_t {
41     const char* string()const PURE_METHOD;
42
43     static char* concatString(const char* l, const char* r);
44     void assignString(char* str);
45     void copyString(const char* str);
46
47     void release_this();
48
49 private:
50     char  *m_string;
51     size_t m_stringLen;
52 };
53
54 inline const char* token_t::string()const
55 {
56     return m_string;
57 }
58
59 /*
60  * append str to the current string
61  */
62 inline char* token_t::concatString(const char* l, const char* r)
63 {
64     size_t cb = (l ? strlen (l) : 0) + strlen (r) + 1;
65     char *r_sz = new char[cb];
66     *r_sz = 0;
67
68     if (l)
69         strcat (r_sz, l);
70     strcat (r_sz, r);
71
72     return r_sz;
73 }
74
75 inline void token_t::assignString(char* str)
76 {
77     m_string = str;
78     m_stringLen = str ? strlen(str) : 0;
79 }
80
81 inline void token_t::copyString(const char* str)
82 {
83     if( str ) {
84         m_stringLen = strlen(str);
85         m_string = new char[m_stringLen+1];
86         strcpy(m_string, str);
87     }
88 }
89
90 inline void token_t::release_this()
91 {
92     delete m_string;
93     m_string = 0;
94 }
95
96 #endif