File: tparse.cpp

package info (click to toggle)
viewcvs 0.9.2%2Bcvs.1.0.dev.2004.07.28-1.5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,436 kB
  • ctags: 1,355
  • sloc: python: 10,094; cpp: 840; ansic: 763; yacc: 526; sh: 168; makefile: 114
file content (351 lines) | stat: -rw-r--r-- 8,098 bytes parent folder | download | duplicates (3)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
# Copyright (C) 2000-2002 The ViewCVS Group. All Rights Reserved.
# This file has been rewritten in C++ from the rcsparse.py file by
# Lucas Bruand <lucas.bruand@ecl2002.ec-lyon.fr>
#
# By using this file, you agree to the terms and conditions set forth in
# the LICENSE.html file which can be found at the top level of the ViewCVS
# distribution or at http://viewcvs.sourceforge.net/license-1.html.
#
# Contact information:
#   Greg Stein, PO Box 760, Palo Alto, CA, 94302
#   gstein@lyra.org, http://viewcvs.sourceforge.net/
#
# -----------------------------------------------------------------------
#
# This software is being maintained as part of the ViewCVS project.
# Information is available at:
#    http://viewcvs.sourceforge.net/
#
# This file was originally based on portions of the blame.py script by
# Curt Hagenlocher.
#
# -----------------------------------------------------------------------
#
*/ 
/*
   This C++ library offers an API to a performance oriented RCSFILE parser.
   It does little syntax checking.
 
   Version: $Id: tparse.cpp,v 1.7 2004/04/06 02:52:07 cmpilato Exp $	
*/

#include "tparse.h"
#define __USE_XOPEN
#include <time.h>

#define Whitespace(c) (c == ' ' || c == '\t' || c == '\014' || c == '\n' || c == '\r')
#define Token_term(c) (c == ' ' || c == '\t' || c == '\014' || c == '\n' || \
    c == '\r' || c == ';')
#define isdigit(c) ((c-'0')<10)

/*--------- Tokenparser class -----------*/
char * TokenParser::get()
{
  ostrstream ost;
  if (backget)
  {
    char *ret;
    ret = backget;
    backget = NULL;
    return ret;
  }

  while (1)
  {
    if (idx == buflength)
    {
      input->read(buf, CHUNK_SIZE);
      if ( (buflength = input->gcount()) == 0 )
        return NULL;
      idx = 0;
    }
    if (!Whitespace(buf[idx]))
      break;
    idx++;
  }
  if (buf[idx] == ';')
  {
    idx++;
    return semicol;
  }

  if (buf[idx] != '@')
  {
    int end = idx + 1;
    while (1)
    {
      while ( (end < buflength) && !(Token_term(buf[end])) )
        end++;
      ost.write(buf + idx, end - idx);
      if (end < buflength)
      {
        idx = end;
        ost.put('\0');
        return ost.str();
      }
      input->read(buf, CHUNK_SIZE);
      buflength = input->gcount();
      idx = 0;
      end = 0;
    }
  }
  idx++;
  while (1)
  {
    int i;
    if (idx == buflength)
    {
      idx = 0;
      input->read(buf, CHUNK_SIZE);
      if ( (buflength = input->gcount()) == 0 )
        throw RCSIllegalCharacter("Unterminated string: @ missing!");
    }
    //i=strchr(buf+idx,'@');
    for (i = idx;i < buflength && (buf[i] != '@');i++)
      ;
    if (i == buflength)
    {
      if ((buflength - idx) > 0)
        ost.write(buf + idx, buflength - idx);
      idx = buflength;
      continue;
    }
    if ( i == buflength - 1)
    {
      ost.write(buf + idx, i - idx);
      idx = 0;
      buf[0] = '@';
      input->read(buf + 1, CHUNK_SIZE - 1);
      if ( (buflength = input->gcount()) == 0 )
        throw RCSIllegalCharacter("Unterminated string: @ missing!");
      buflength++;
      continue;
    }
    if (buf[i + 1] == '@')
    {
      ost.write(buf + idx, i - idx + 1);
      idx = i + 2;
      continue;
    }
    if ((i - idx) > 0)
      ost.write(buf + idx, i - idx);
    idx = i + 1;
    ost.put('\0');
    return ost.str();
  }
};

void TokenParser::unget(char *token)
{
  if (backget)
  {
    throw RCSParseError("Ungetting a token while already having an ungetted token.");
  }
  backget = token;
}

/*--------- tparseParser class -----------*/
int tparseParser::parse_rcs_admin()
{
  while (1)
  {
    char *token = tokenstream->get();
    if (isdigit(token[0]))
    {
      tokenstream->unget(token);
      return 0;
    }
    if (strcmp(token, "head") == 0)
    {
      if (sink->set_head_revision(tokenstream->get()))
      {
        delstr(token);
        return 1;
      }
      tokenstream->matchsemicol();
      delstr(token);
      continue;
    }
    if (strcmp(token, "branch") == 0)
    {
      if (sink->set_principal_branch(tokenstream->get()))
      {
        delstr(token);
        return 1;
      }
      tokenstream->matchsemicol();
      delstr(token);
      continue;
    }
    if (strcmp(token, "symbols") == 0)
    {
      while (1)
      {
        char *tag = tokenstream->get();
        char *second;
        if (tag == tokenstream->semicol)
          break;
        second = index(tag, ':');
        second[0] = '\0';
        second++;
        if (sink->define_tag(tag, second))
        {
          delstr(token);
          return 1;
        }
      }
      delstr(token);
      continue;
    }
    if (strcmp(token, "comment") == 0)
    {
      if (sink->set_comment(tokenstream->get()))
      {
        delstr(token);
        return 1;
      }
      tokenstream->matchsemicol();
      delstr(token);
      continue;
    }
    if ((strcmp(token, "locks") == 0) ||
        (strcmp(token, "strict") == 0) ||
        (strcmp(token, "expand") == 0) ||
        (strcmp(token, "access") == 0))
    {
      while (1)
      {
        char *tag = tokenstream->get();
        if (tag == tokenstream->semicol)
          break;
        delstr(tag);
      }
      delstr(token);
      continue;
    }
    delstr(token);
  }
};

int tparseParser::parse_rcs_tree()
{
  while (1)
  {
    char *revision;
    char *date;
    long timestamp;
    char *author;
    ostrstream *state;
    char *hstate;
    char *next;
    Branche *branches = NULL;
    struct tm tm;
    revision = tokenstream->get();
    if (strcmp(revision, "desc") == 0)
    {
      tokenstream->unget(revision);
      return 0;
    }
    // Parse date
    tokenstream->match("date");
    date = tokenstream->get();
    tokenstream->matchsemicol();
    memset ((void *) &tm, 0, sizeof(struct tm));
    if (strptime(date, "%y.%m.%d.%H.%M.%S", &tm) == NULL)
      strptime(date, "%Y.%m.%d.%H.%M.%S", &tm);
    timestamp = mktime(&tm);
    delstr(date);
    tokenstream->match("author");
    author = tokenstream->get();
    tokenstream->matchsemicol();
    tokenstream->match("state");
    state = new ostrstream();
    while (1)
    {
      char *token = tokenstream->get();
      if (token == tokenstream->semicol)
      {
        break;
      }
      if (state->pcount())
        state->put(' ');
      (*state) << token;
      delstr(token);
    }
    state->put('\0');
    hstate = state->str();
    delete state;
    state = NULL;
    tokenstream->match("branches");
    while (1)
    {
      char *token = tokenstream->get();
      if (token == tokenstream->semicol)
      {
        break;
      }
      if (branches == NULL)
        branches = new Branche(token, NULL);
      else
        branches = new Branche(token, branches);
    }
    tokenstream->match("next");
    next = tokenstream->get();
    if (next == tokenstream->semicol)
      next = NULL;
    else
      tokenstream->matchsemicol();
    /**
         * 	there are some files with extra tags in them. for example:
         *	owner	640;
      	 *	group	15;
      	 *	permissions	644;
      	 *	hardlinks	@configure.in@;
      	 *	this is "newphrase" in RCSFILE(5). we just want to skip over these.
    **/

    while (1)
    {
      char *token = tokenstream->get();
      if ( (strcmp(token, "desc") == 0) || isdigit(token[0]) )
      {
        tokenstream->unget(token);
        break;
      };
      delstr(token);
      while ( (token = tokenstream->get()) != tokenstream->semicol)
        delstr(token);
    }

    if (sink->define_revision(revision, timestamp, author, hstate, branches, next))
      return 1;
  }
  return 0;
}

int tparseParser::parse_rcs_description()
{
  tokenstream->match("desc");
  return (this->sink->set_description(tokenstream->get()));
}

int tparseParser::parse_rcs_deltatext()
{
  char *revision;
  char *log;
  char *text;
  while (1)
  {
    revision = tokenstream->get();
    if (revision == NULL)
      break;
    tokenstream->match("log");
    log = tokenstream->get();
    tokenstream->match("text");
    text = tokenstream->get();
    if (sink->set_revision_info(revision, log, text))
      return 1;
  }
  return 0;
}