aboutsummaryrefslogtreecommitdiff
path: root/src/track_remove_parser.c
blob: 61c2375e8ead7b1290c6b5c19ead81813c3676ec (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
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
#include "track_remove_parser.h"
#include <stdio.h>

typedef enum {
    TR_TOK_RANGE,
    TR_TOK_END_OF_FILE,
    TR_TOK_INVALID
} TrackRemoveToken;

typedef struct {
    const char *str;
    size_t size;
    size_t offset;
    Range range;
} TrackRemoveTokenizer;

static int is_number(char c) {
    return c >= '0' && c <= '9';
}

/* Returns non-0 value on overflow */
static int string_to_num_unchecked(const char *str, size_t size, int *number) {
    int result = 0;
    for(size_t i = 0; i < size; ++i) {
        const int result_before = result;
        result *= 10;
        result += (str[i] - '0');
        /* overflow */
        if(result <= result_before)
            return 1;
    }
    *number = result;
    return 0;
}

static void track_remover_tokenizer_init(TrackRemoveTokenizer *self, const char *str, size_t size) {
    self->str = str;
    self->size = size;
    self->offset = 0;
    self->range.start = 0;
    self->range.end = 0;
}

static int track_remover_tokenizer_get_char(TrackRemoveTokenizer *self) {
    if(self->offset < self->size)
        return self->str[self->offset];
    else
        return '\0';
}

static TrackRemoveToken track_remover_tokenizer_next(TrackRemoveTokenizer *self) {
    char c;
    for(;;) {
        c = track_remover_tokenizer_get_char(self);
        if(c != ' ' && c != '\t')
            break;
        ++self->offset;
    }

    if(c >= '1' && c <= '9') {
        size_t number_start = self->offset;
        ++self->offset;

        for(;;) {
            c = track_remover_tokenizer_get_char(self);
            if(!is_number(c))
                break;
            ++self->offset;
        }

        if(string_to_num_unchecked(self->str + number_start, self->offset - number_start, &self->range.start) != 0)
            return TR_TOK_INVALID;
        
        self->range.end = self->range.start;
        if(c != '-')
            return TR_TOK_RANGE;

        ++self->offset;
        c = track_remover_tokenizer_get_char(self);
        if(!is_number(c))
            return TR_TOK_INVALID;

        number_start = self->offset;
        ++self->offset;

        for(;;) {
            c = track_remover_tokenizer_get_char(self);
            if(!is_number(c))
                break;
            ++self->offset;
        }

        if(string_to_num_unchecked(self->str + number_start, self->offset - number_start, &self->range.end) != 0)
            return TR_TOK_INVALID;

        return TR_TOK_RANGE;
    } else if(c == '\0') {
        return TR_TOK_END_OF_FILE;
    } else {
        return TR_TOK_INVALID;
    }
}

int track_remove_parser_parse(const char *str, size_t size, Buffer /*Range*/ *ranges) {
    int num_ranges = 0;
    TrackRemoveTokenizer tokenizer;
    track_remover_tokenizer_init(&tokenizer, str, size);

    for(;;) {
        TrackRemoveToken token = track_remover_tokenizer_next(&tokenizer);
        if(token == TR_TOK_RANGE) {
            if(tokenizer.range.start > tokenizer.range.end) {
                fprintf(stderr, "Invalid number range. The start range can't be past the end range\n");
                return 1;
            }

            buffer_append(ranges, &tokenizer.range, sizeof(tokenizer.range));
            ++num_ranges;
        } else if(token == TR_TOK_END_OF_FILE) {
            break;
        } else {
            fprintf(stderr, "Invalid input. Expected numbers and/or number ranges\n");
            return 1;
        }
    }

    if(num_ranges == 0) {
        fprintf(stderr, "Invalid input. Expected numbers and/or number ranges\n");
        return 1;
    }

    return 0;
}