From 39df7646014104fea6da9cce6fbc45a9fe7a2272 Mon Sep 17 00:00:00 2001 From: theblacksquid Date: Wed, 21 Oct 2020 02:12:15 +0800 Subject: [PATCH] Implement option switches. --- basics.h | 4 +-- numbers.h | 2 -- sigutils.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++------------- spec.org | 14 ++++++----- 4 files changed, 77 insertions(+), 27 deletions(-) rewrite sigutils.c (67%) diff --git a/basics.h b/basics.h index 2c3966c..9b1f0a4 100644 --- a/basics.h +++ b/basics.h @@ -7,10 +7,10 @@ #include #include -char* absorb_words (int count, char** string_list) +char* absorb_words (int count, char** string_list, int start_index) { char* result = calloc(sizeof(char), (count * 20)); - for (int i = 1; i < count; i++) + for (int i = start_index; i < count; i++) strcat(result, string_list[i]); return result; diff --git a/numbers.h b/numbers.h index be9603d..5421243 100644 --- a/numbers.h +++ b/numbers.h @@ -2,8 +2,6 @@ #ifndef NUMBERS_H #define NUMBERS_H -#include "basics.h" - typedef struct { char id; diff --git a/sigutils.c b/sigutils.c dissimilarity index 67% index 7152f0a..2a753b9 100644 --- a/sigutils.c +++ b/sigutils.c @@ -1,17 +1,67 @@ - -#include "basics.h" -#include "numbers.h" -#include - -int main(int argc, char** argv) -{ - char* raw = absorb_words(argc, argv); - char* example = string_process(raw); - int val = string_value(example, LETTERS); - printf("Digested Intent: %s\nNumerical Value: %d\n", - example, val); - - printf("%d\n", digital_root(val, 10)); - return 0; -} - + +#include "basics.h" +#include "numbers.h" +#include + +#define VERSION 0.01 + +char name[] = "Sigil Utilities, version"; +char contact[] = "(C) 2020 theblaqcksquid \n"; +char license[] = "Licensed under GNU GPL v3\n"; + +char help0[] = "usage: sigutils [options] \n"; +char help1[] = "-h Print this message and quit.\n"; +char help2[] = "-v Print the version number and quit.\n"; + +int main(int argc, char** argv) +{ + int numeric_flag = 0; + int version_flag = 0; + int help_flag = 0; + int c; + + while ((c = getopt(argc, argv, "nvh")) != -1) + switch (c) + { + case 'n': numeric_flag = 1; + break; + + case 'v': version_flag = 1; + break; + + case 'h': help_flag = 1; + break; + + } + + if (version_flag) + { + printf("%s %.2f\n%s%s", name, VERSION, contact, license); + return 0; + } + + else if (help_flag) + { + printf("%s%s%s", help0, help1, help2); + return 0; + } + + char* raw = absorb_words(argc, argv, optind); + char* result = string_process(raw); + + printf("%s", result); + + if (numeric_flag) + { + int value = string_value(result, LETTERS); + int root = digital_root(value, 10); + printf(" %d %d", value, root); + } + + printf("\n"); + + free(result); + free(raw); + return 0; +} + diff --git a/spec.org b/spec.org index 975da69..ecf576d 100644 --- a/spec.org +++ b/spec.org @@ -6,7 +6,6 @@ ** DONE Process a Statement of Intent (SOI) ** DONE Return the Numeric value of a given SOI ** TODO Create a Word Square from SOI -** TODO Create Number Square from SOI * Usage sigutils [options] @@ -23,10 +22,8 @@ ** -W return an N by N square from the statement of intent. -** -N - * Architectural TODO List -** TODO replace absorb_words() +** DONE replace absorb_words() Right now the binary is only able to take raw input from the command line arguments that are passed to it. We need to replace this with a way to process command switches, and implement a @@ -34,5 +31,10 @@ associative array that has the switches and their corresponding values. - - [ ] Learn how to use getopt - - [ ] Revise code to use getopt + - [X] Learn how to use getopt + - [X] Revise code to use getopt + + [2020-10-21 Wed] + + I didn't exactly go and replace absorb_words(), but instead changed + it to start at where getopt() ends. -- 2.11.4.GIT