/* De oplossing voor het irritante probleem van de * "new line" karakters die voor mac, unix en windows * verschillen. Formateer nu uw teksten snel naar het * gewenste formaat... (kan veel makkelijker met een * regular expression in vi maar dat zal ons een zorg * wezen... deze code identificeerd welk formaat een * bestand is... * * ANYWAY ... IT KICKS ASS WHETHER YOU LIKE IT OR NOT. * * Plan: * * Prog neemt als argument de naam van een ascii text * bestand en zoekt naar een newline karakter: * * 0Dh0Ah - Windows * 0Dh - Mac * 0Ah - UNIX * * Als tweede argument geeft men aan voor welk formaat * men het tekst bestand wil formateren. (windows, mac * of UNIX) : * * -m: Formateer voor mac * -u: Formateer voor UNIX * -w: Formateer voor windows * * Dan wordt een nieuw bestand aangemaakt met de naam * van het originele bestand voorafgegaan door: * * - * * en daarin zijn dan natuurlijk de nieuwe lijnen naar * het juiste ding omgezet. * * Voor zover ik weet draagbaar over verschillende * platformen (van C op mac weet ik niets) * * De code is handig en ze werkt ... HAHAHAHA! */ #include #include #include typedef unsigned short format; #define UNKNOWN 0 #define UNIX 1 #define MAC 2 #define WINDOWS 3 struct task { char * filename; char * new_filename; FILE * src; FILE * dest; format src_fmt; format dest_fmt; }; int usage(char * filename); int configure(struct task *, int argc, char * argv[]); int identify_src_fmt(struct task *); int write_new_file(struct task *); int write_from_UNIX(struct task *); int write_from_MAC(struct task *); int write_from_WINDOWS(struct task *); void put_unix_line(FILE *); void put_mac_line(FILE *); void put_windows_line(FILE *); int usage(char * filename) { printf("Usage: %s \n\n", filename); printf("Format:\n"); printf("\t-u : Convert to UNIX format (\"\\n\")\n"); printf("\t-m : Convert to macintosh format (\"\\n\")\n"); printf("\t-w : Convert to windows format (\"\\r\\n\")\n"); printf("\n-h : Show this message\n"); exit(1); } /* configure() * * Argumenten bekijken. * bronbestand openen, en handelen bij fouten hierbij. */ int configure(struct task * the_task, int argc, char * argv[]) { int i; char type[8]; if(argc < 3) usage(argv[0]); for(i=1;i<=2;i++) { if(argv[i][0] == '-') { switch(argv[i][1]) { case 'u': the_task->dest_fmt = UNIX; sprintf(type, "unix"); break; case 'm': the_task->dest_fmt = MAC; sprintf(type, "mac"); break; case 'w': the_task->dest_fmt = WINDOWS; sprintf(type, "windows"); break; case 'h': usage(argv[0]); break; default: usage(argv[0]); break; } /* EO switch */ } /* if(argv[i][0] == '-') */ else { /* Assume it's a filename, open the file, check for errors */ the_task->filename = argv[i]; the_task->src = fopen(the_task->filename, "r"); if(the_task->src == NULL) { perror("fopen"); exit(1); } } /* else */ } /* for(i=1;i<=2;i++) */ if(the_task->dest_fmt == UNKNOWN || the_task->filename == NULL) { usage(argv[0]); } the_task->new_filename = (char *)malloc( (strlen(the_task->filename)+1) + 9); sprintf(the_task->new_filename, "%s.%s", type, the_task->filename); identify_src_fmt(the_task); return 0; } /* configure() */ /* identify_src_fmt() * * Zoekt in een bestand naar een nieuw lijn character en onderzoekt * in welk formaat het bestand is geschreven. */ int identify_src_fmt(struct task * the_task) { int c; while( (c = fgetc(the_task->src)) != EOF) { if(c == 10) { the_task->src_fmt = UNIX; } if(c == 13) { if( (c = fgetc(the_task->src)) == 10) { the_task->src_fmt = WINDOWS; } else if(c != EOF) { the_task->src_fmt = MAC; break; /* damn ... */ } else { if(feof(the_task->src) == 1) { printf("ERROR:\nAn EOF occured with 13 as last character.\n"); printf("We were unable to determine which format the file has\n"); printf("Abotring!"); exit(1); } else { printf("ERROR:\nAn error occured while reading the source file.\n"); printf("Aborting!"); exit(1); } } /* if( (c = fgetc(the_task->src)) == 10) */ } /* if(c == 13) */ } /* while( (c = fgetc(the_task->src)) != EOF) */ rewind(the_task->src); write_new_file(the_task); fclose(the_task->src); fclose(the_task->dest); return 0; } int write_new_file(struct task * the_task) { the_task->dest = fopen(the_task->new_filename, "w"); if(the_task->dest == NULL) { perror("fopen"); exit(1); } switch(the_task->src_fmt) { case UNIX: write_from_UNIX(the_task); break; case MAC: write_from_MAC(the_task); break; case WINDOWS: write_from_WINDOWS(the_task); break; } /* switch */ } /* Onderstaande reeks (tot en zonder main()) dienen voor het formatten * wat makkelijker te maken */ int write_from_UNIX(struct task * the_task) { int c; while( (c = fgetc(the_task->src)) != EOF) { if(c == 10) { if(the_task->dest_fmt == MAC) { put_mac_line(the_task->dest); } if(the_task->dest_fmt == WINDOWS) { put_windows_line(the_task->dest); } } else { fputc(c, the_task->dest); } } } int write_from_MAC(struct task * the_task) { int c; while( (c = fgetc(the_task->src)) != EOF) { if(c == 13) { if(the_task->dest_fmt == UNIX) { put_unix_line(the_task->dest); } if(the_task->dest_fmt == WINDOWS) { put_windows_line(the_task->dest); } } else { fputc(c, the_task->dest); } } } int write_from_WINDOWS(struct task * the_task) { int c; while( (c = fgetc(the_task->src)) != EOF) { if(c == 13) { if( (c = fgetc(the_task->src)) != EOF) { if(c == 10) { if(the_task->dest_fmt == UNIX) { put_unix_line(the_task->dest); } if(the_task->dest_fmt == MAC) { put_mac_line(the_task->dest); } } /* if(c == 10) */ else { fputc(c, the_task->dest); } } /* if( (c = fgetc(the_task->src)) != EOF) */ } /* if(c == 13) */ else { fputc(c, the_task->dest); } } } void put_unix_line(FILE * dest) { fputc(10, dest); } void put_mac_line(FILE * dest) { fputc(13, dest); } void put_windows_line(FILE * dest) { fputc(13, dest); fputc(10, dest); } int main(int argc, char * argv[]) { struct task the_task; the_task.filename = NULL; the_task.src = NULL; the_task.dest = NULL; the_task.src_fmt = UNKNOWN; the_task.dest_fmt = UNKNOWN; configure(&the_task, argc, argv); switch(the_task.src_fmt) { case UNKNOWN: printf("Unknown format\n"); break; case UNIX: printf("UNIX format\n"); break; case MAC: printf("Macintosh format\n"); break; case WINDOWS: printf("Windows format\n"); break; default: printf("Unknown format\n"); break; } printf("to\n"); switch(the_task.dest_fmt) { case UNIX: printf("UNIX format\n"); break; case MAC: printf("Macintosh format\n"); break; case WINDOWS: printf("Windows format\n"); break; default: printf("'t Is naar de kloten\n"); break; } printf("Bestand opgeslaan als: %s\n", the_task.new_filename); return 0; }