From 14a419380f7e0d9e24cb663d5ae27f18449b26dc Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Wed, 1 Dec 2021 01:19:20 +1100 Subject: Add config file, refactor classes to be cleaner with macros --- config.h | 10 ++++++++++ passgen.c | 28 ++++++++-------------------- 2 files changed, 18 insertions(+), 20 deletions(-) create mode 100644 config.h diff --git a/config.h b/config.h new file mode 100644 index 0000000..1160580 --- /dev/null +++ b/config.h @@ -0,0 +1,10 @@ +#define DEFAULT_GRAMMAR "Cvccvc!##" + +// For vowels: +// i, o excluded due to potentially confusing 1/l/i + 0/o +// y included as a vowel because it kinda is one +#define CLASSES \ + CLASS('v', "aeuy") \ + CLASS('c', "bcdfghkmnprstvwxz") \ + CLASS('#', "1234567890") \ + CLASS('!', "@#$%^&*_-+=()[]{}") diff --git a/passgen.c b/passgen.c index 3b3bdd9..bdcd04a 100644 --- a/passgen.c +++ b/passgen.c @@ -1,3 +1,5 @@ +#include "config.h" + #include #include #include @@ -5,14 +7,12 @@ #include #include -#define DEFAULT_GRAMMAR "Cvccvc!##" +#define CLASS(c, chars) \ + case c: \ + class = chars; \ + class_size = sizeof(chars)-1; \ + break; -// i, o excluded due to potentially confusing 1/l/i + 0/o -// y included as a vowel because it kinda is one -#define VOWELS "aeuy" -#define CONSONANTS "bcdfghkmnprstvwxz" -#define NUMBERS "1234567890" -#define SYMBOLS "@#$%^&*_-+=()[]{}" int main(int argc, char *argv[]) { @@ -64,20 +64,8 @@ int main(int argc, char *argv[]) char *class; int class_size = 0; -#define setClass(cl) \ - class = cl; \ - class_size = sizeof(cl)-1; \ - break; - switch (c) { - case 'c': - setClass(CONSONANTS); - case 'v': - setClass(VOWELS); - case '!': - setClass(SYMBOLS); - case '#': - setClass(NUMBERS); + CLASSES default: printf("ERROR: Invalid grammar character '%c'.\n", c); if (grammar != (char*)DEFAULT_GRAMMAR) -- cgit