File indexing completed on 2024-05-05 05:54:12
0001 // a silly quotation utitility 0002 0003 /* 0004 This code was written by Lars Doelle <lars.doelle@on-line.de>. 0005 This code is in the public domain. 0006 */ 0007 0008 #include <stdio.h> 0009 #include <strings.h> 0010 0011 int skip = 0; 0012 int empty = 1; 0013 0014 void pchr(int c, int indent) 0015 { 0016 if (skip) { 0017 skip = (c != '\n'); 0018 return; 0019 } 0020 switch (c) { 0021 case '\n': 0022 if (!empty) 0023 printf("\\n\"\n%*s\"", indent, ""); 0024 empty = 1; 0025 break; 0026 case '#': 0027 skip = 1; 0028 break; 0029 case '"': 0030 case '\\': 0031 printf("\\"); 0032 // fallthrough 0033 default: 0034 printf("%c", c); 0035 empty = 0; 0036 break; 0037 } 0038 } 0039 0040 #define INDENT 2 0041 0042 int main(int argc, char *argv[]) 0043 { 0044 int cc; 0045 FILE *sysin; 0046 if (argc < 2) { 0047 fprintf(stderr, "usage: %s filename\n", argv[0]); 0048 return 1; 0049 } 0050 sysin = fopen(argv[1], "r"); 0051 if (!sysin) { 0052 fprintf(stderr, "cannot open %s\n", argv[1]); 0053 perror("reason: "); 0054 return 1; 0055 } 0056 printf("%*s/* generated by '%s %s' */\n\n", INDENT, "", argv[0], argv[1]); 0057 printf("%*s\"", INDENT, ""); 0058 while ((cc = fgetc(sysin)) > 0) { 0059 pchr(cc, INDENT); 0060 } 0061 printf("\"\n"); 0062 fclose(sysin); 0063 }