OSDN Git Service

fix compile problem
[tjqt4port/tj2qt4.git] / taskjuggler / FileToken.cpp
1 /*
2  * FileToken.cpp - TaskJuggler
3  *
4  * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * $Id: $
11  */
12 #include "FileToken.h"
13
14 #include <stdlib.h>
15
16 #include "tjlib-internal.h"
17
18 FileToken::FileToken(const QString& file, const QString& tp) :
19     m_file(file),
20     m_fh(0),
21     m_f(0),
22     m_currLine(0),
23     m_macroStack(),
24     m_lineBuf(),
25     m_ungetBuf(),
26     m_tokenTypeBuf(INVALID),
27     m_tokenBuf(),
28     m_taskPrefix(tp)
29 { }
30
31 FileToken::FileToken() :
32     m_file(),
33     m_fh(0),
34     m_f(0),
35     m_currLine(0),
36     m_macroStack(),
37     m_lineBuf(),
38     m_ungetBuf(),
39     m_tokenTypeBuf(INVALID),
40     m_tokenBuf(),
41     m_taskPrefix()
42 { }
43
44 QString FileToken::getPath() const
45 {
46     if (m_file.find('/') >= 0)
47         return m_file.left(m_file.findRev('/') + 1);
48     else
49         return "";
50 }
51
52 void FileToken::returnToken(TokenType tt, const QString& buf)
53 {
54     if (m_tokenTypeBuf != INVALID)
55     {
56         qFatal("Internal Error: Token buffer overflow!");
57         return;
58     }
59     m_tokenTypeBuf = tt;
60     m_tokenBuf = buf;
61 }
62
63 QString FileToken::cleanupLine(const QString& line)
64 {
65     QString res;
66     for (int i = 0; i < line.length(); ++i)
67         if (line[i] != QChar(EOMacro))
68             res += line[i];
69
70     return res;
71 }
72
73 bool FileToken::readEnvironment()
74 {
75     QString id;
76
77     if (nextToken(id) != ID)
78     {
79         errorMessage(i18n("Environment name expected"));
80         return false;
81     }
82
83     QString token;
84     if (nextToken(token) != RBRACKET)
85     {
86         errorMessage(i18n("')' expected"));
87         return false;
88     }
89
90     char *value = getenv (id.ascii());
91     id = ( value ? value : "" );
92
93     // push expanded macro reverse into ungetC buffer.
94     for (int i = id.length() - 1; i >= 0; --i)
95         m_ungetBuf.append(id[i].latin1());
96
97     return true;
98 }
99
100 bool FileToken::getDateFragment(QString& token, QChar& c)
101 {
102     token += c;
103     c = getC();
104     // c must be a digit
105     if (!c.isDigit())
106     {
107         errorMessage(i18n("Corrupted date"));
108         return false;
109     }
110     token += c;
111     // read other digits
112     while ((c = getC()).unicode() != EOFile && c.isDigit())
113         token += c;
114
115     return true;
116 }
117
118 bool FileToken::readMacroCall()
119 {
120     QString id;
121     TokenType tt;
122     /* For error reporting we need to replace the macro call with the macro
123      * text. So we save a copy of the current line buf (the ${ has already
124      * been removed) and copy it over the lineBuf again after we have read the
125      * complete macro call. */
126     QString lineBufCopy = m_lineBuf;
127     QString prefix;
128     if ((tt = nextToken(id)) == QUESTIONMARK)
129     {
130         prefix = "?";
131     }
132     else
133         returnToken(tt, id);
134
135     if ((tt = nextToken(id)) != ID && tt != INTEGER)
136     {
137         errorMessage(i18n("Macro ID expected"));
138         return false;
139     }
140     id = prefix + id;
141
142     QString token;
143     // Store all arguments in a newly created string list.
144     QStringList sl(id);
145     while ((tt = nextToken(token)) == STRING || tt == ID)
146         sl.append(token);
147     if (tt != RBRACE)
148     {
149         errorMessage(i18n("'}' expected"));
150         return false;
151     }
152
153     // expand the macro
154     setLocation(m_file, m_currLine);
155     QString macro = resolve(&sl);
156
157     if (macro.isNull() && prefix.isEmpty())
158         return false;
159
160     m_lineBuf = lineBufCopy;
161
162     // Push pointer to macro on stack. Needed for error handling.
163     m_macroStack.append(getMacro(id));
164
165     // mark end of macro
166     m_ungetBuf.append(QChar(EOMacro));
167     // push expanded macro reverse into ungetC buffer.
168     for (int i = macro.length() - 1; i >= 0; --i)
169         m_ungetBuf.append(macro[i]);
170     return true;
171 }