-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.c
95 lines (67 loc) · 1.86 KB
/
token.c
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
/*
$NiH: token.c,v 1.4 2002/04/16 22:46:17 wiz Exp $
token.c -- token handling
Copyright (C) 2002 Dieter Baron and Thomas Klausner
This file is part of cg, a program to assemble and decode binary Usenet
postings. The authors can be contacted at <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdarg.h>
#include <stdlib.h>
#include "stream.h"
#include "util.h"
token *
token_new(enum token_type type, char *line)
{
return token_set(xmalloc(sizeof(token)), type, line);
}
token *
token_printf3(token *t, enum token_type type, int n, char *fmt, ...)
{
char *s;
va_list argp;
va_start(argp, fmt);
vasprintf(&s, fmt, argp);
va_end(argp);
token_set3(t, type, n, s);
t->alloced = 1;
return t;
}
token *
token_set(token *t, enum token_type type, char *line)
{
return token_set3(t, type, 0, line);
}
token *
token_set3(token *t, enum token_type type, int n, char *line)
{
t->alloced = 0;
t->type = type;
t->n = n;
t->line = line;
return t;
}
void
token_free(token *t)
{
if (t == NULL)
return;
if (t->alloced)
free(t->line);
free(t);
}
token *
token_copy(token *d, token *s)
{
return token_set3(d, s->type, s->n, s->line);
}