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

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

bsv_t *init_bsv(char *source, char delim) {
  bsv_t *bsv = safe_calloc(1, sizeof(bsv_t *));
  bsv->source = source;
  bsv->i = 0;
  bsv->c = bsv->source[bsv->i];
  bsv->delim = delim;
  return bsv;
}

void bsv_move(bsv_t *bsv) {
  if (bsv->c != '\0') {
    bsv->i++;
    bsv->c = bsv->source[bsv->i];
  }
}

string_t *bsv_next(bsv_t *bsv) {
  string_t *s = init_string(NULL);
  bool escape = false;
  while (bsv->c != bsv->delim && !escape) {
    if (bsv->c == '\0') break;
    string_push(s, bsv->c);
    bsv_move(bsv);
  }
  return s;
}