aboutsummaryrefslogtreecommitdiff
path: root/src/common/tsv.c
blob: c06163b88df709bf651e1f336eaa2e508afb2803 (plain)
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
#include <stdlib.h>
#include <stdbool.h>

#include "../include/tsv.h"
#include "../include/helpers.h"
#include "../include/better_string.h"

tsv_t *init_tsv(char *source) {
  tsv_t *tsv = safe_calloc(1, sizeof(tsv_t *));
  tsv->source = source;
  tsv->i = 0;
  tsv->c = tsv->source[tsv->i];
  return tsv;
}

void tsv_move(tsv_t *tsv) {
  if (tsv->c != '\0') {
    tsv->i++;
    tsv->c = tsv->source[tsv->i];
  }
}

string_t *tsv_next(tsv_t *tsv) {
  string_t *s = init_string(NULL);
  bool escape = false;
  while (tsv->c != '\t' && !escape) {
    if (tsv->c == '\0') break;
    string_push(s, tsv->c);
    tsv_move(tsv);
  }
  return s;
}