Narthex

Modular dictionary generator
git clone git://mcdim.xyz/Narthex.git
Log | Files | Refs | README | LICENSE

ngen.c (2982B)


      1 /*
      2  *    ngen - Narthex generator
      3  *
      4  *  By Michael Constantine Dimopoulos https://mcdim.xyz <mk@mcdim.xyz>
      5  *  License: GNU GPL v3
      6  *
      7  *  ninc will iterate over stdin and  after printing the dictionary as is
      8  *  it will reprint it but will also multiply each line with the
      9  *  difference of max-min, and will append n to each line, where n is
     10  *  increased after every line from min to max inclusive. (I know, I know.
     11  *  Just try to use it and it will make more sense).
     12  *
     13  * * * * * * * *
     14  *
     15  *  This program is free software: you can redistribute it and/or modify
     16  *  it under the terms of the GNU General Public License as published by
     17  *  the Free Software Foundation, either version 3 of the License, or
     18  *  (at your option) any later version.
     19  *
     20  *  This program is distributed in the hope that it will be useful,
     21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     23  *  GNU General Public License for more details.
     24  *
     25  *  You should have received a copy of the GNU General Public License
     26  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
     27  *
     28  */
     29 
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 
     34 #define VERSION "v1.0"
     35 
     36 static void
     37 help(char *exename)
     38 {
     39 	printf( "ngen - Narthex generator %s\n"
     40 		"By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n"
     41 
     42 		"-h  print this panel & exit\n"
     43 		"-v  print current version & exit\n\n"
     44 
     45 		"Usage:	%s [MIN] [MAX] [CHARSET]\n"
     46 		"MAX must be greater than or equal to MIN\n",
     47 		VERSION, exename);
     48 	exit(EXIT_SUCCESS);
     49 }
     50 
     51 static inline void
     52 die(char *str)
     53 {
     54 	printf("%s\n", str);
     55 	exit(EXIT_SUCCESS);
     56 }
     57 
     58 int rec(char *chars, int l, int *c, int len, int exit)
     59 {
     60 	if (exit == 1) return 1;
     61 
     62 	for (int i=0; i<=len-1; i++) {
     63 		printf("%c", chars[c[i]]);
     64 	}
     65 	printf("\n");
     66 
     67 	for (int i=len-1; i>=0; i--) {
     68 		if (c[i] < l) {
     69 			c[i]++;
     70 			exit = rec(chars, l, c, len, exit);
     71 		} else {
     72 			if (i == 0) {
     73 				return 1;
     74 			}
     75 			c[i] = 0;
     76 		}
     77 	}
     78 }
     79 
     80 int main(int argc, char **argv)
     81 {
     82 	if (argc != 2 && argc != 4) {
     83 		help(argv[0]);
     84 	}
     85 
     86 	if (strcmp(argv[1], "-h") == 0) {
     87 		help(argv[0]);
     88 	}
     89 
     90 	if (strcmp(argv[1], "-v") == 0) {
     91 		printf("%s\n", VERSION);
     92 		exit(EXIT_SUCCESS);
     93 	}
     94 
     95 	if (argc != 4) {
     96 		help(argv[0]);
     97 	}
     98 
     99 	int min = atoi(argv[1]);
    100 	int max = atoi(argv[2]);
    101 
    102 	if (min > max) die("MAX must be greater than or equal to MIN");
    103 
    104 	/* get unique */
    105 	int len = strlen(argv[3]);
    106 	int size = 1;
    107 	char *uniq = (char *)malloc(size * sizeof(char));
    108 	uniq[0] = '\0';
    109 
    110 	for (int i=0; i<len; i++) {
    111 		int found = 0;
    112 		for (int j=0; j<strlen(uniq); j++) {
    113 			if (argv[3][i] == uniq[j]) {
    114 				found = 1;
    115 			}
    116 		}
    117 
    118 		if (found == 0) {
    119 			size++;
    120 			uniq = (char *)realloc(uniq, size * sizeof(char));
    121 			uniq[size-2] = argv[3][i];
    122 			uniq[size-1] = '\0';
    123 		}
    124 	}
    125 	/* ********** */
    126 
    127 	for (int l=min; l<=max; l++) {
    128 		int s = strlen(uniq)-1;
    129 		int c[l];
    130 		for (int i=0; i<l; i++) {
    131 			c[i] = 0;
    132 		}
    133 
    134 		rec(uniq, s, c, l, 0);
    135 	}
    136 	return 0;
    137 }