Narthex

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

nhan.c (3101B)


      1 /*
      2  *     nhance - Narthex enhancer
      3  *    
      4  *  By Michael Constantine Dimopoulos https://mcdim.xyz <mk@mcdim.xyz>
      5  *  License: GNU GPL v3
      6  *  
      7  *  nhance iterates over stdin and, after printing the dictionary as is
      8  *  it will reprint it this time with the first letter capitalized. Ir
      9  *  can also append full capitalizations at the end of the dictionary
     10  *  with the -f flag.
     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 <ctype.h>
     31 #include <stdio.h>
     32 #include <errno.h>
     33 #include <string.h>
     34 
     35 #define VERSION "v1.3.1"
     36 #define BUFFER_SIZE 256
     37 
     38 
     39 static void
     40 help(char *exename)
     41 {
     42 	printf( "nhance - Narthex enhancer %s\n"
     43 		"By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n"
     44 
     45 		"-f  append full capitalization\n"
     46 		"-h  print this panel & exit\n"
     47 		"-v  print current version & exit\n\n"
     48 
     49 		"Usage:	cat [FILENAME] | %s [-f]\n"
     50 		"	%s [-f] [FILENAME]\n",
     51 		VERSION, exename, exename);
     52 	exit(EXIT_SUCCESS);
     53 }
     54 
     55 static inline void
     56 die(char *str)
     57 {
     58 	printf("%s\n", str);
     59 	exit(EXIT_SUCCESS);
     60 }
     61 
     62 static FILE *
     63 save_stdin(FILE *f)
     64 {
     65 	FILE *f2 = tmpfile();
     66 	char buffer[BUFFER_SIZE];
     67 	while (fgets(buffer, sizeof(buffer), f) != NULL)  {
     68 		fprintf(f2, "%s", buffer);
     69 	}
     70 	fclose(f);
     71 	return f2;
     72 }
     73 
     74 void
     75 cap(char string[])
     76 {  
     77 	int i;
     78 	int x = strlen(string);
     79 	for (i=0; i<x-1; i++){
     80 		if (isalpha(string[i])){ 
     81 			string[i]= toupper(string[i]);
     82 		}
     83 	}
     84 }
     85 
     86 void
     87 print_only(FILE *f)
     88 {
     89 	char buffer[BUFFER_SIZE];
     90 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
     91 		printf("%s",buffer);
     92 	}
     93 }
     94 
     95 static void
     96 enhance(FILE *f, int full_upper)
     97 {
     98 	char buffer[BUFFER_SIZE];
     99 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
    100 		if (isalpha(buffer[0])) {
    101 			if (full_upper == 0)
    102 				buffer[0] = toupper(buffer[0]);	
    103 			else
    104 				cap(buffer);
    105 
    106 			printf("%s",buffer);	
    107 		}
    108 
    109 	}
    110 }
    111 
    112 void
    113 main(int argc, char *argv[])
    114 {
    115 	int full_upper, file = 0, i;
    116 	for (i = 0; i < argc; i++) {
    117 		if (strcmp(argv[i],"-f")==0) 
    118 			full_upper = 1;
    119 		else if (strcmp(argv[i],"-v")==0)
    120 			die(VERSION); 
    121 		else if (strcmp(argv[i],"-h")==0)
    122 			help(argv[0]);
    123 		else
    124 			file = i;
    125 	}
    126 
    127 	FILE *f2;
    128 	if (file != 0) {
    129 		FILE *f = fopen(argv[file], "r");
    130 
    131 		if (f) {
    132 			f2 = save_stdin(f);
    133 		} else {
    134 			fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
    135 			exit(EXIT_FAILURE);
    136 		}
    137 	} else {
    138 		f2 = save_stdin(stdin);
    139 	}
    140 
    141 	rewind(f2);
    142 	print_only(f2);
    143 	rewind(f2);
    144 	enhance(f2,0);
    145 
    146 	if (full_upper == 1) {
    147 		rewind(f2);
    148 		enhance(f2,1);
    149 	}
    150 
    151 	fclose(f2);
    152 
    153 	exit(EXIT_SUCCESS);
    154 }