-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq.c
91 lines (73 loc) · 1.24 KB
/
seq.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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "seq.h"
#include "serial.h"
#include "util.h"
static bool parse_args(const char* str, unsigned short* a, unsigned short* b);
void seq_main(const char* str)
{
unsigned short a;
unsigned short b;
if (!parse_args(str, &a, &b))
{
char buf[9];
sprintf(buf, "bad args");
serial_write(buf, strlen(buf));
serial_write_newline();
return;
}
unsigned short i;
char buf[8];
for (i = a; i <= b; i++)
{
sprintf(buf, "%u", i);
serial_write(buf, strlen(buf));
serial_write_newline();
}
}
static bool parse_args(const char* str, unsigned short* a, unsigned short* b)
{
char c;
*a = 0;
*b = 0;
// Keep going until we get a numeric char.
while (!util_is_numeric(c = *str))
{
if (c == 0x00)
{
return false;
}
str++;
}
// Parse "a".
while (util_is_numeric(c = *str))
{
*a *= 10;
*a += (c - 0x30);
str++;
}
// Expecting space char next.
if (c != ' ')
{
return false;
}
// Keep going until we run out of spaces.
while ((c = *str) == ' ')
{
str++;
}
// Expecting numeric char next.
if (!util_is_numeric(c))
{
return false;
}
// Parse "b".
while (util_is_numeric(c = *str))
{
*b *= 10;
*b += (c - 0x30);
str++;
}
return true;
}