~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to libannotate/parse.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
    Xplanet 0.94 - render an image of a planet into an X window
3
 
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
4
 
 
5
 
    This program is free software; you can redistribute it and/or modify
6
 
    it under the terms of the GNU General Public License as published by
7
 
    the Free Software Foundation; either version 2 of the License, or
8
 
    (at your option) any later version.
9
 
 
10
 
    This program is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
    GNU General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU General Public License
16
 
    along with this program; if not, write to the Free Software
17
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
****************************************************************************/
19
 
 
20
 
#include <cstdio>
21
 
#include <cstring>
22
 
#include <iostream>
23
 
using namespace std;
24
 
 
25
 
#include "keywords.h"
26
 
 
27
 
bool
28
 
isDelimiter(char c)
29
 
{
30
 
    return(c == ' ' || c == '\t' || c == ',' || c == '/');
31
 
}
32
 
 
33
 
bool
34
 
isEndOfLine(char c)
35
 
{
36
 
    // 13 is DOS end-of-line, 28 is the file separator
37
 
    return(c == '#' || c == '\0' || c == 13 || c == 28); 
38
 
}
39
 
 
40
 
static void
41
 
skipPastToken(int &i, const char *line)
42
 
{
43
 
    while (!isDelimiter(line[i]))
44
 
    {
45
 
        if (isEndOfLine(line[i])) return;
46
 
        i++;
47
 
    }
48
 
}
49
 
 
50
 
static bool
51
 
getValue(const char *line, int &i, const char *key, char *&returnstring)
52
 
{
53
 
    const unsigned int length = strlen(key);
54
 
    if (strncmp(line + i, key, length) == 0)
55
 
    {
56
 
        i += length;
57
 
        int istart = i;
58
 
        skipPastToken(i, line);
59
 
        returnstring = new char[i - istart + 1];
60
 
        strncpy(returnstring, (line + istart), i - istart);
61
 
        returnstring[i-istart] = '\0';
62
 
        return(true);
63
 
    }
64
 
    return(false);
65
 
}
66
 
 
67
 
// This routine returns the next token in the line and its type.
68
 
int 
69
 
parse(int &i, const char *line, char *&returnstring)
70
 
{
71
 
    if (i >= (int) strlen(line)) return(ENDOFLINE);
72
 
 
73
 
    if (returnstring != NULL) cerr << "returnstring is not NULL!\n";
74
 
 
75
 
    int returnval = UNKNOWN;
76
 
 
77
 
    if (isDelimiter(line[i]))
78
 
        returnval = DELIMITER;
79
 
    else if (isEndOfLine(line[i]))
80
 
        returnval = ENDOFLINE;
81
 
    else if (getValue(line, i, "align=", returnstring))
82
 
        returnval = ALIGN;
83
 
    else if (getValue(line, i, "altcirc=", returnstring))
84
 
        returnval = ALTCIRC;
85
 
    else if (getValue(line, i, "color=", returnstring))
86
 
        returnval = COLOR;
87
 
    else if (getValue(line, i, "font=", returnstring))
88
 
        returnval = FONT;
89
 
    else if (getValue(line, i, "fontsize=", returnstring))
90
 
        returnval = FONTSIZE;
91
 
    else if (getValue(line, i, "image=", returnstring))
92
 
        returnval = IMAGE;
93
 
    else if (getValue(line, i, "symbolsize=", returnstring))
94
 
        returnval = SYMBOLSIZE;
95
 
    else if (getValue(line, i, "position=", returnstring))
96
 
        returnval = POSITION;
97
 
    else if (getValue(line, i, "radius=", returnstring))
98
 
        returnval = RADIUS;
99
 
    else if (getValue(line, i, "spacing=", returnstring))
100
 
        returnval = SPACING;
101
 
    else if (line[i] == '"')
102
 
    {
103
 
        int istart = ++i;
104
 
        while (line[i] != '"') 
105
 
        {
106
 
            i++;
107
 
            if (i >= (int) strlen(line))
108
 
            {
109
 
                cerr << "Unterminated string in marker file!\n";
110
 
                return(UNKNOWN);
111
 
            }
112
 
        }
113
 
        returnstring = new char[i - istart + 1];
114
 
        strncpy(returnstring, (line + istart), i - istart);
115
 
        returnstring[i-istart] = '\0';
116
 
        returnval = NAME;
117
 
    }
118
 
    else if (line[i] == '{')
119
 
    {
120
 
        int istart = ++i;
121
 
        while (line[i] != '}') 
122
 
        {
123
 
            i++;
124
 
            if (i >= (int) strlen(line))
125
 
            {
126
 
                cerr << "Unterminated string in marker file!\n";
127
 
                return(UNKNOWN);
128
 
            }
129
 
        }
130
 
        returnstring = new char[i - istart + 1];
131
 
        strncpy(returnstring, (line + istart), i - istart);
132
 
        returnstring[i-istart] = '\0';
133
 
        returnval = NAME;
134
 
    }
135
 
    else if (strncmp(line+i, "timezone=", 9) == 0)
136
 
    {
137
 
        i += 9;
138
 
        int istart = i;
139
 
        while (line[i] == '/' || line[i] == ',' || !isDelimiter(line[i]))
140
 
        {
141
 
            if (isEndOfLine(line[i])) break;
142
 
            i++;
143
 
        }
144
 
        returnstring = new char[i - istart + 1];
145
 
        strncpy(returnstring, (line + istart), i - istart);
146
 
        returnstring[i-istart] = '\0';
147
 
        returnval = TIMEZONE;
148
 
    }
149
 
    else if (strncmp(line+i, "trail={", 7) == 0)
150
 
    {
151
 
        i += 7;
152
 
        int istart = i;
153
 
        while (line[i] != '}') 
154
 
        {
155
 
            i++;
156
 
            if (i >= (int) strlen(line))
157
 
            {
158
 
                cerr << "Unterminated trail value in satellite file!\n";
159
 
                return(UNKNOWN);
160
 
            }
161
 
        }
162
 
        returnstring = new char[i - istart + 1];
163
 
        strncpy(returnstring, (line + istart), i - istart);
164
 
        returnstring[i-istart] = '\0';
165
 
        returnval = TRAIL;
166
 
    }
167
 
    else if (strncmp(line+i, "transparent={", 13) == 0)
168
 
    {
169
 
        i += 13;
170
 
        int istart = i;
171
 
        while (line[i] != '}') 
172
 
        {
173
 
            i++;
174
 
            if (i >= (int) strlen(line))
175
 
            {
176
 
                cerr << "Unterminated pixel value in marker file!\n";
177
 
                return(UNKNOWN);
178
 
            }
179
 
        }
180
 
        returnstring = new char[i - istart + 1];
181
 
        strncpy(returnstring, (line + istart), i - istart);
182
 
        returnstring[i-istart] = '\0';
183
 
        returnval = TRANSPARENT;
184
 
    }
185
 
    else // assume it's a latitude/longitude value
186
 
    {
187
 
        int istart = i;
188
 
        skipPastToken(i, line);
189
 
        returnstring = new char[i - istart + 1];
190
 
        strncpy(returnstring, (line + istart), i - istart);
191
 
        returnstring[i-istart] = '\0';
192
 
 
193
 
        double temp;
194
 
        sscanf(returnstring, "%lf", &temp);
195
 
        if (temp > -360 && temp < 360)
196
 
        {
197
 
            returnval = LATLON; // else defaults to UNKNOWN
198
 
        }
199
 
    }
200
 
#if 0
201
 
    if (opts.debug)
202
 
    {
203
 
        string returntype;
204
 
        switch (returnval)
205
 
        {
206
 
        case UNKNOWN:
207
 
            returntype = "UNKNOWN";
208
 
            break;
209
 
        case DELIMITER:
210
 
            returntype = "DELIMITER";
211
 
            break;
212
 
        case ENDOFLINE:
213
 
            returntype = "ENDOFLINE";
214
 
            break;
215
 
        case ALIGN:
216
 
            returntype = "ALIGN";
217
 
            break;
218
 
        case COLOR:
219
 
            returntype = "COLOR";
220
 
            break;
221
 
        case FONT:
222
 
            returntype = "FONT";
223
 
            break;
224
 
        case FONTSIZE:
225
 
            returntype = "FONTSIZE";
226
 
            break;
227
 
        case IMAGE:
228
 
            returntype = "IMAGE";
229
 
            break;
230
 
        case POSITION:
231
 
            returntype = "POSITION";
232
 
            break;
233
 
        case SPACING:
234
 
            returntype = "SPACING";
235
 
            break;
236
 
        case NAME:
237
 
            returntype = "NAME";
238
 
            break;
239
 
        case TIMEZONE:
240
 
            returntype = "TIMEZONE";
241
 
        case TRANSPARENT:
242
 
            returntype = "TRANSPARENT";
243
 
            break;
244
 
        case LATLON:
245
 
            returntype = "LATLON";
246
 
            break;
247
 
        default:
248
 
            returntype = "?";
249
 
        }
250
 
 
251
 
        cout << "token is " 
252
 
             << (returnstring == NULL ? "NULL" : returnstring)
253
 
             << "\t(" << returntype << ")"
254
 
             << endl;
255
 
    }
256
 
#endif
257
 
    return(returnval);
258
 
}