File: tokenizer.c

package info (click to toggle)
genometools 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 57,988 kB
  • ctags: 45,574
  • sloc: ansic: 475,937; ruby: 24,092; python: 4,519; sh: 3,014; perl: 2,523; makefile: 1,839; java: 158; haskell: 37; xml: 6; sed: 5
file content (209 lines) | stat: -rw-r--r-- 5,179 bytes parent folder | download
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
/*
  Copyright (c) 2006-2009 Gordon Gremme <gordon@gremme.org>
  Copyright (c) 2006-2008 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <string.h>
#include "core/ensure.h"
#include "core/fa.h"
#include "core/ma.h"
#include "core/tokenizer.h"
#include "core/xansi_api.h"

struct GtTokenizer {
  GtIO *io;
  bool skip_comment_lines;
  GtStr *token; /* the current token */
};

GtTokenizer* gt_tokenizer_new(GtIO *io)
{
  GtTokenizer *t;
  gt_assert(io);
  t = gt_calloc(1, sizeof *t);
  t->io = io;
  return t;
}

void gt_tokenizer_skip_comment_lines(GtTokenizer *t)
{
  gt_assert(t);
  t->skip_comment_lines = true;
}

GtStr* gt_tokenizer_get_token(GtTokenizer *t)
{
  char c;
  bool has_eof = true;
  int rval = 0;
  gt_assert(t);

  /* if we have no current token, get it if possible */
  if (!t->token) {
    if (t->skip_comment_lines && gt_io_line_start(t->io)) {
      for (;;) {
        gt_assert(gt_io_line_start(t->io));
        if ((gt_io_get_char(t->io, (char*) &c) != -1)) {
          if ((char) c == '#') {
            /* skip line */
            while ((gt_io_get_char(t->io, &c) != -1) && c != '\n');
            has_eof = true;
          }
          else {
            gt_io_unget_char(t->io, c);
            has_eof = false;
            break;
          }
        }
        else {
          c = EOF;
          break;
        }
      }
    } /* skip blanks */
    while (((rval = gt_io_get_char(t->io, &c)) != -1) && c == ' ');
    if (rval == -1)
      has_eof = true;
    else
      has_eof = false;
    do {
      if (!has_eof) {
        if (!t->token)
          t->token = gt_str_new();
        if (c == '\n')
          break;
        gt_str_append_char(t->token, c);
      }
    } while (((rval = gt_io_get_char(t->io, &c)) != -1)
              && c != ' ' && c != '\n');
    if (rval == -1)
      has_eof = true;
    else
      has_eof = false;
    if (c == '\n' && !has_eof) {
      gt_assert(t->token);
      gt_str_append_char(t->token, c);
    }
  }
  /* return token */
  if (t->token)
    return gt_str_ref(t->token);
  return NULL;
}

bool gt_tokenizer_has_token(GtTokenizer *t)
{
  bool has_token = false;
  GtStr *token;
  gt_assert(t);
  token = gt_tokenizer_get_token(t);
  if (token) {
    has_token = true;
    gt_str_delete(token);
  }
  return has_token;
}

bool gt_tokenizer_line_start(const GtTokenizer *t)
{
  gt_assert(t);
  return gt_io_line_start(t->io);
}

void gt_tokenizer_next_token(GtTokenizer *t)
{
  gt_assert(t);
  gt_str_delete(t->token);
  t->token = NULL;
}

GtUword gt_tokenizer_get_line_number(const GtTokenizer *t)
{
  gt_assert(t);
  return gt_io_get_line_number(t->io);
}

const char* gt_tokenizer_get_filename(const GtTokenizer *t)
{
  gt_assert(t);
  return gt_io_get_filename(t->io);
}

int gt_tokenizer_unit_test(GtError *err)
{
  GtStr *tmpfilename;
  GtTokenizer *t;
  FILE *tmpfp;
  GtStr *token;
  int had_err = 0;
  gt_error_check(err);

  /* empty file (except comment line) */
  tmpfilename = gt_str_new();
  tmpfp = gt_xtmpfp(tmpfilename);
  fprintf(tmpfp, "# comment line\n");
  gt_fa_xfclose(tmpfp);
  t = gt_tokenizer_new(gt_io_new(gt_str_get(tmpfilename), "r"));
  gt_tokenizer_skip_comment_lines(t);
  gt_ensure(!gt_tokenizer_has_token(t));
  gt_tokenizer_delete(t);
  gt_xremove(gt_str_get(tmpfilename));

  /* larger test */
  tmpfp = gt_xtmpfp(tmpfilename);
  fprintf(tmpfp, " a bb ccc\ndddd -5");
  gt_fa_xfclose(tmpfp);
  t = gt_tokenizer_new(gt_io_new(gt_str_get(tmpfilename), "r"));

  token = gt_tokenizer_get_token(t);
  gt_ensure(!strcmp(gt_str_get(token), "a"));
  gt_str_delete(token);

  gt_tokenizer_next_token(t);
  token = gt_tokenizer_get_token(t);
  gt_ensure(!strcmp(gt_str_get(token), "bb"));
  gt_str_delete(token);

  gt_tokenizer_next_token(t);
  token = gt_tokenizer_get_token(t);
  gt_ensure(!strcmp(gt_str_get(token), "ccc\n"));
  gt_str_delete(token);

  gt_tokenizer_next_token(t);
  token = gt_tokenizer_get_token(t);
  gt_ensure(!strcmp(gt_str_get(token), "dddd"));
  gt_str_delete(token);

  gt_tokenizer_next_token(t);
  token = gt_tokenizer_get_token(t);
  gt_ensure(!strcmp(gt_str_get(token), "-5"));
  gt_str_delete(token);

  gt_tokenizer_next_token(t);
  gt_ensure(!gt_tokenizer_has_token(t));
  gt_tokenizer_delete(t);
  gt_xremove(gt_str_get(tmpfilename));
  gt_str_delete(tmpfilename);

  return had_err;
}

void gt_tokenizer_delete(GtTokenizer *t)
{
  if (!t) return;
  gt_io_delete(t->io);
  gt_str_delete(t->token);
  gt_free(t);
}