Narthex

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

nclean.c (3988B)


      1 /*
      2  *    nclean - Narthex cleaner 
      3  *
      4  *  By Michael Constantine Dimopoulos https://mcdim.xyz	<mk@mcdim.xyz>
      5  *  License: GNU GPL v3
      6  *
      7  *  nclean will iterate over stdin and  print only the lines have met
      8  *  your set criteria. For example, it can print only lines that have
      9  *  both a number and a special character that are more than six chars
     10  *  in length.
     11  *
     12  * * * * * * * *
     13  *
     14  *  This program is free software: you can redistribute it and/or modify
     15  *  it under the terms of the GNU General Public License as published by
     16  *  the Free Software Foundation, either version 3 of the License, or
     17  *  (at your option) any later version.
     18  *
     19  *  This program is distributed in the hope that it will be useful,
     20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22  *  GNU General Public License for more details.
     23  *
     24  *  You should have received a copy of the GNU General Public License
     25  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
     26  *
     27  */
     28 
     29 #include <stdlib.h>
     30 #include <stdio.h>
     31 #include <string.h>
     32 #include <ctype.h>
     33 #include <unistd.h>
     34 
     35 #define VERSION "v1.1.2"
     36 #define BUFFER_SIZE 256
     37 
     38 static inline void
     39 usage(char *exename)
     40 {
     41 	fprintf(stderr, "Usage:	cat [FILENAME]"
     42 		"| %s [-c] [-n] [-s] [-l] 10\n",
     43 		exename);
     44 }
     45 
     46 static void
     47 help(char *exename)
     48 {
     49 	printf( "nclean - Narthex cleaner %s\n"
     50 		"By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n"
     51 
     52 		"-c  must have capital letters\n"
     53 		"-n  must have numbers\n"
     54 		"-s  must have symbols\n"
     55 		"-l  must have min length specified\n"
     56 		"-h  print this panel & exit\n"
     57 		"-v  print current version & exit\n\n"
     58 
     59 		"Usage:	cat [FILENAME] | %s [-c] [-n] [-s] [-l] 10\n",
     60 		VERSION, exename);
     61 	exit(EXIT_SUCCESS);
     62 }
     63 
     64 static inline void
     65 die(char *str)
     66 {
     67 	printf("%s\n", str);
     68 	exit(EXIT_SUCCESS);
     69 }
     70 
     71 static int
     72 case_check(char *str)
     73 {
     74 	for (int i = 0; str[i] != '\0'; i++) {
     75 		if (isupper(str[i]) != 0)
     76 			return 1;
     77 	}
     78 	return 0;
     79 }
     80 
     81 static int
     82 numb_check(char *str)
     83 {
     84 	for (int i = 0; str[i] != '\0'; i++) {
     85 		if (isdigit(str[i]) != 0)
     86 			return 1;
     87 	}
     88 	return 0;
     89 }
     90 
     91 static int
     92 symb_check(char *str)
     93 {
     94 	for (int i = 0; str[i] != '\0'; i++) {
     95 		if (str[i] == '!' || str[i] == '@' || str[i] == '#'
     96 		 || str[i] == '$' || str[i] == '%' || str[i] == '^'
     97 		 || str[i] == '&' || str[i] == '*' || str[i] == '('
     98 		 || str[i] == ')' || str[i] == '-' || str[i] == '{'
     99 		 || str[i] == '}' || str[i] == '[' || str[i] == ']'
    100 		 || str[i] == ':' || str[i] == ';' || str[i] == '"'
    101 		 || str[i] == '\''|| str[i] == '<' || str[i] == '>'
    102 		 || str[i] == '.' || str[i] == '/' || str[i] == '?'
    103 		 || str[i] == '~' || str[i] == '`' )
    104 			return 1;
    105 	}
    106 	return 0;
    107 }
    108 
    109 static inline int
    110 len_check(char *str, int len)
    111 {
    112 	return strlen(str) >= len ? 1 : 0; 
    113 }
    114 
    115 static void
    116 clean(FILE *f, int c, int n, int s, int l)
    117 {
    118 	char buffer[BUFFER_SIZE];
    119 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
    120 		strtok(buffer, "\n");
    121 		int fc = 1, fn = 1, fs = 1, fl = 1;
    122 
    123 		if (c == 1) fc = case_check(buffer);
    124 		if (n == 1) fn = numb_check(buffer);
    125 		if (s == 1) fs = symb_check(buffer);
    126 		if (l > 0)  fl = len_check(buffer, l);
    127 
    128 		/* for troubleshooting:
    129 		 * printf("fc:%d\nfn:%d\nfs:%d\nfl:%d\n",fc, fn, fs, fl);
    130 		 */
    131 
    132 		if (fc==1 && fn==1 && fs==1 && fl==1)
    133 			printf("%s\n",buffer);
    134 	}
    135 }
    136 
    137 void
    138 main(int argc, char *argv[])
    139 {
    140 	int c1, n, s, l, lvalue;
    141 	c1 = n = s = l = lvalue = 0;
    142 	char *cvalue = NULL;
    143 	int index;
    144 	int c;
    145 	extern char *optarg;
    146 	int optind = 0;
    147 
    148 	while ((c = getopt(argc, argv, "cnsvhl:")) != -1 ) {
    149 		switch (c) {
    150 		case 'v':
    151 			die(VERSION);
    152 		case 'h':
    153 			help(argv[0]);
    154 		case 'c':
    155 			c1=1;
    156 			break;
    157 		case 'n':
    158 			n=1;
    159 			break;
    160 		case 's':
    161 			s=1;
    162 			break;
    163 		case 'l':
    164 			l=1;
    165 			lvalue=atoi(optarg);
    166 			break;
    167 		case '?':
    168 			usage(argv[0]);
    169 			exit(EXIT_FAILURE);
    170 			break;
    171 		}
    172 	}
    173 
    174 	if (c1 == 1 || n == 1 || s == 1 || l == 1) {
    175 		clean(stdin, c1, n, s, lvalue);
    176 	} else {
    177 		usage(argv[0]);
    178 		exit(EXIT_FAILURE);
    179 	}
    180 
    181 	exit(EXIT_SUCCESS);
    182 }