aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/config.c9
-rw-r--r--source/identifier.c68
2 files changed, 75 insertions, 2 deletions
diff --git a/source/config.c b/source/config.c
index f1aabf8..6e213cb 100644
--- a/source/config.c
+++ b/source/config.c
@@ -3,9 +3,14 @@
typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]);
+/*
+ * Per configuration option actions
+ * Used to print help messages and parse arg strings and set env values
+ */
typedef struct {
- int32_t flags;
- char match, *help_option_argument, *help_message;
+ int32_t flags; // flags to set on env->flags
+ char match; // must be set, used in printing help message
+ char *help_option_argument, *help_message; // help_option_argument, string type if the option takes an arg
kpl_config_option_parse_fn *parse_fn;
} kpl_config_option;
diff --git a/source/identifier.c b/source/identifier.c
new file mode 100644
index 0000000..f6a7279
--- /dev/null
+++ b/source/identifier.c
@@ -0,0 +1,68 @@
+
+#include "kpl.h"
+
+#define KPL_IDENTIFIER_NUMBER_RANGE_START 1
+
+#define KPL_IDENTIFIER_UPPER_RANGE_START 12
+
+#define KPL_IDENTIFIER_LOWER_RANGE_START 38
+
+static uint64_t kpl_identifier_char_to_value(char c, uint64_t value_index) {
+ uint64_t value = 0;
+ if (c >= '0' && c <= '9')
+ value = c - '0' + KPL_IDENTIFIER_NUMBER_RANGE_START;
+ else if (c >= 'A' && c <= 'Z')
+ value = c - 'A' + KPL_IDENTIFIER_UPPER_RANGE_START;
+ else if (c >= 'a' && c <= 'z')
+ value = c - 'a' + KPL_IDENTIFIER_LOWER_RANGE_START;
+ return value << value_index * KPL_IDENTIFIER_BITS_PER_VALUE;
+}
+
+bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length) {
+ if (c_string_length > KPL_IDENTIFIER_MAX_LENGTH)
+ return false;
+ for (size_t string_index = 0; string_index < c_string_length; string_index++)
+ if (!isalnum(c_string[string_index]) && c_string[string_index] != '_')
+ return false;
+ for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++)
+ identifier->parts[part_index] = 0;
+ uint32_t string_index = 0, part_index = 0;
+ while (string_index < c_string_length) {
+ uint64_t value_index = 0;
+ while (value_index < KPL_IDENTIFIER_VALUES_PER_PART && string_index < c_string_length) {
+ identifier->parts[part_index] |= kpl_identifier_char_to_value(c_string[string_index], value_index);
+ value_index++;
+ string_index++;
+ }
+ identifier->parts[part_index] |= value_index << KPL_IDENTIFIER_VALUES_TOTAL_BITS;
+ part_index++;
+ }
+ return true;
+}
+
+static char kpl_identifier_value_to_char(uint64_t part) {
+ uint8_t value = part & (1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0);
+ if (!value)
+ return '_';
+ if (value < KPL_IDENTIFIER_UPPER_RANGE_START)
+ return value - KPL_IDENTIFIER_NUMBER_RANGE_START + '0';
+ if (value < KPL_IDENTIFIER_LOWER_RANGE_START)
+ return value - KPL_IDENTIFIER_UPPER_RANGE_START + 'A';
+ return value - KPL_IDENTIFIER_LOWER_RANGE_START + 'a';
+}
+
+void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]) {
+ uint32_t string_index = 0;
+ for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) {
+ uint64_t part = identifier->parts[part_index];
+ uint64_t value_length = part >> KPL_IDENTIFIER_VALUES_TOTAL_BITS;
+ if (!value_length)
+ break;
+ while (value_length) {
+ c_string[string_index] = kpl_identifier_value_to_char(part);
+ part >>= KPL_IDENTIFIER_BITS_PER_VALUE;
+ value_length--;
+ string_index++;
+ }
+ }
+}