.
This commit is contained in:
parent
f361a02852
commit
2f145eb879
2 changed files with 43 additions and 39 deletions
66
kyaa.md
66
kyaa.md
|
@ -11,6 +11,9 @@ standard library headers:
|
||||||
* `stdio.h`
|
* `stdio.h`
|
||||||
* `string.h`
|
* `string.h`
|
||||||
|
|
||||||
|
in addition, `kyaa_extra.h` requires `limits.h`,
|
||||||
|
or at least `LONG_MIN` to be defined.
|
||||||
|
|
||||||
## tutorial
|
## tutorial
|
||||||
|
|
||||||
ensure `argc` and `argv` are defined.
|
ensure `argc` and `argv` are defined.
|
||||||
|
@ -119,36 +122,39 @@ kyaa prints error messages to `stderr`, and help text to `stdout`.
|
||||||
|
|
||||||
### kyaa.h
|
### kyaa.h
|
||||||
|
|
||||||
```
|
return type or type | name | arguments or default value
|
||||||
KYAA_OKAY DEFINE int
|
--- | --- | ---
|
||||||
KYAA_ERROR DEFINE int
|
define | KYAA\_OKAY | 0
|
||||||
KYAA_ITER DEFINE int
|
define | KYAA\_ERROR | 1
|
||||||
KYAA_SETUP MACRO
|
define | KYAA\_ITER | i
|
||||||
KYAA_LOOP MACRO
|
||
|
||||||
KYAA_BEGIN MACRO
|
macro | KYAA\_SETUP | *none*
|
||||||
KYAA_END MACRO
|
macro | KYAA\_LOOP | *none*
|
||||||
KYAA_DESCRIBE MACRO
|
macro | KYAA\_BEGIN | *none*
|
||||||
KYAA_FLAG MACRO
|
macro | KYAA\_END | *none*
|
||||||
KYAA_FLAG_ARG MACRO
|
macro | KYAA\_DESCRIBE | c, name, description
|
||||||
KYAA_HELP MACRO
|
macro | KYAA\_FLAG | c, name, description
|
||||||
|
macro | KYAA\_FLAG\_ARG | c, name, description
|
||||||
kyaa_name char *
|
macro | KYAA\_HELP | description
|
||||||
kyaa_read_stdin bool
|
||
|
||||||
kyaa_flag char
|
`char *` | kyaa\_name | *n/a*
|
||||||
kyaa_keep_parsing bool
|
`bool` | kyaa\_read\_stdin | *n/a*
|
||||||
kyaa_parse_next bool
|
`char *` | kyaa\_arg | *n/a*
|
||||||
kyaa_arg char *
|
|| **internal use** ||
|
||||||
kyaa_no_more bool
|
`char` | kyaa\_flag | *n/a*
|
||||||
kyaa_helping bool
|
`bool` | kyaa\_keep\_parsing | *n/a*
|
||||||
kyaa_any bool
|
`bool` | kyaa\_parse\_next | *n/a*
|
||||||
kyaa_flag_arg char *
|
`bool` | kyaa\_no\_more | *n/a*
|
||||||
```
|
`bool` | kyaa\_helping | *n/a*
|
||||||
|
`bool` | kyaa\_any | *n/a*
|
||||||
|
|
||||||
### kyaa\_extra.h
|
### kyaa\_extra.h
|
||||||
|
|
||||||
```
|
return value or type | name | arguments or default value
|
||||||
KYAA_FLAG_LONG MACRO
|
--- | --- | ---
|
||||||
|
macro | KYAA\_FLAG\_LONG | c, name, description
|
||||||
char *kyaa_skip_spaces(char *str)
|
||
|
||||||
const char *kyaa_str_to_long(char *str, long *p_out)
|
`char *` | kyaa\_flag\_arg | *n/a*
|
||||||
```
|
||
|
||||||
|
`char *` | kyaa\_skip\_spaces | `char *` str
|
||||||
|
`const char *` | kyaa\_str\_to\_long | `char *` str, `long *` p\_out
|
||||||
|
|
16
kyaa_extra.h
16
kyaa_extra.h
|
@ -17,8 +17,7 @@ static char *kyaa_skip_spaces(char *str) {
|
||||||
static const char *kyaa__base_2(char **p_str, long *p_out) {
|
static const char *kyaa__base_2(char **p_str, long *p_out) {
|
||||||
char *str = *p_str;
|
char *str = *p_str;
|
||||||
long out = *p_out;
|
long out = *p_out;
|
||||||
char c;
|
for (char c; (c = *str) != '\0'; str++) {
|
||||||
while ((c = *str++) != '\0') {
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '0': case '1': {
|
case '0': case '1': {
|
||||||
long digit = (long)(c - '0');
|
long digit = (long)(c - '0');
|
||||||
|
@ -46,8 +45,7 @@ exit:
|
||||||
static const char *kyaa__base_8(char **p_str, long *p_out) {
|
static const char *kyaa__base_8(char **p_str, long *p_out) {
|
||||||
char *str = *p_str;
|
char *str = *p_str;
|
||||||
long out = *p_out;
|
long out = *p_out;
|
||||||
char c;
|
for (char c; (c = *str) != '\0'; str++) {
|
||||||
while ((c = *str++) != '\0') {
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '0': case '1': case '2': case '3':
|
case '0': case '1': case '2': case '3':
|
||||||
case '4': case '5': case '6': case '7': {
|
case '4': case '5': case '6': case '7': {
|
||||||
|
@ -75,8 +73,7 @@ exit:
|
||||||
static const char *kyaa__base_10(char **p_str, long *p_out) {
|
static const char *kyaa__base_10(char **p_str, long *p_out) {
|
||||||
char *str = *p_str;
|
char *str = *p_str;
|
||||||
long out = *p_out;
|
long out = *p_out;
|
||||||
char c;
|
for (char c; (c = *str) != '\0'; str++) {
|
||||||
while ((c = *str++) != '\0') {
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
case '5': case '6': case '7': case '8': case '9': {
|
case '5': case '6': case '7': case '8': case '9': {
|
||||||
|
@ -103,8 +100,7 @@ exit:
|
||||||
static const char *kyaa__base_16(char **p_str, long *p_out) {
|
static const char *kyaa__base_16(char **p_str, long *p_out) {
|
||||||
char *str = *p_str;
|
char *str = *p_str;
|
||||||
long out = *p_out;
|
long out = *p_out;
|
||||||
char c;
|
for (char c; (c = *str) != '\0'; str++) {
|
||||||
while ((c = *str++) != '\0') {
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
case '5': case '6': case '7': case '8': case '9': {
|
case '5': case '6': case '7': case '8': case '9': {
|
||||||
|
@ -177,7 +173,8 @@ static const char *kyaa_str_to_long(char *str, long *p_out) {
|
||||||
|
|
||||||
if (*str == '\0') return "no number given";
|
if (*str == '\0') return "no number given";
|
||||||
|
|
||||||
// TODO: comment on how we go towards negatives instead of positives
|
// NOTE: we actually subtract each digit from the result instead of summing.
|
||||||
|
// this lets us represent LONG_MIN without overflowing.
|
||||||
const char *err;
|
const char *err;
|
||||||
switch (base) {
|
switch (base) {
|
||||||
case 2: { err = kyaa__base_2( &str, &out); } break;
|
case 2: { err = kyaa__base_2( &str, &out); } break;
|
||||||
|
@ -191,6 +188,7 @@ static const char *kyaa_str_to_long(char *str, long *p_out) {
|
||||||
str = kyaa_skip_spaces(str);
|
str = kyaa_skip_spaces(str);
|
||||||
if (*str != '\0') return "unexpected character";
|
if (*str != '\0') return "unexpected character";
|
||||||
|
|
||||||
|
// NOTE: out is negative here; see above comment.
|
||||||
// assuming two's complement
|
// assuming two's complement
|
||||||
if (!negated && out == LONG_MIN) return "out of range for long integer";
|
if (!negated && out == LONG_MIN) return "out of range for long integer";
|
||||||
*p_out = negated ? out : -out;
|
*p_out = negated ? out : -out;
|
||||||
|
|
Loading…
Reference in a new issue