From 4fd21e3125c3003ecd0ae28c50c8656b62faefba Mon Sep 17 00:00:00 2001 From: katherine Date: Wed, 18 Dec 2019 08:39:17 -0700 Subject: allow some two-char c string escape codes \a, \b, \f, \n, \r, \t, \v --- doc/interface.md | 5 +++-- simple-opt.h | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/doc/interface.md b/doc/interface.md index 978ecb3..0882acd 100644 --- a/doc/interface.md +++ b/doc/interface.md @@ -115,8 +115,9 @@ floating point number that can be read by the standard library `strtod` function and stored in a `double` type. this includes arguments like "4.9", "-1.2e20", "infinity", or "nan". -arguments acceptable to type `SIMPLE_OPT_CHAR` may be any single-byte -character. +arguments acceptable to type `SIMPLE_OPT_CHAR` may be any single-byte character +or one of the following two-character escape sequences: `\a`, `\b`, `\f`, `\n`, +`\r`, `\t`, `\v`. arguments acceptable to type `SIMPLE_OPT_STRING` may be any string of characters the user passes. diff --git a/simple-opt.h b/simple-opt.h index d85ee3d..b298975 100644 --- a/simple-opt.h +++ b/simple-opt.h @@ -195,8 +195,42 @@ strmatch_out: return true; case SIMPLE_OPT_CHAR: - if (strlen(s) != 1) + if (strlen(s) == 2 && s[0] == '\\') { + switch (s[1]) { + case '0': + o->val.v_char = '\0'; + break; + case 'a': + o->val.v_char = '\a'; + break; + case 'b': + o->val.v_char = '\b'; + break; + case 't': + o->val.v_char = '\t'; + break; + case 'n': + o->val.v_char = '\n'; + break; + case 'v': + o->val.v_char = '\v'; + break; + case 'f': + o->val.v_char = '\f'; + break; + case 'r': + o->val.v_char = '\r'; + break; + case '\\': + o->val.v_char = '\\'; + break; + default: + return false; + } + return true; + } else if (strlen(s) != 1) { return false; + } o->val.v_char = s[0]; return true; -- cgit v1.2.3