//SPDX-FileCopyrightText: 2025 Ethan Masse // //SPDX-License-Identifier: CC-BY-SA-4.0 OR AGPL-3.0-or-later /* * WS4TOTXT: a converter from WordStar 4.0 files * to pleasant .TXT files. Strips the high bit * and inserts newlines. * * Copyright (C) 2025 Ethan Masse * * This program is free software: you can redistribute * it and/or modify it under the terms of the GNU * Affero General Public License as published by the * Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Affero General Public License for more details. * * You should have received a copy of the GNU * Affero General Public License along with this * program. If not, see * . */ #include #include #include int debug_convert_ws4_and_print(FILE* input_file) { int current_byte = EOF; /*Maybe not actually be a byte?*/ int stripped_byte = 0; do { if (input_file == NULL) { (void)(fprintf(stderr, "Conversion failed.\n")); return 1; } current_byte = fgetc(input_file); if (ferror(input_file) != 0) { (void)(fprintf(stderr, "Error reading file\n")); return 1; } if (current_byte != EOF) { stripped_byte = current_byte & 0x7F; if (stripped_byte != current_byte) { (void)(printf("\{")); } if ((stripped_byte & 0x1F) == stripped_byte) { (void)(printf("\<%d\>", stripped_byte)); } else { (void)(printf("%c", stripped_byte)); } if (stripped_byte != current_byte) { (void)(printf("\}")); } if ((stripped_byte == 13) || (stripped_byte == 10)) { (void)(printf("\n")); } } } while (current_byte != EOF); return 0; } int convert_ws4_and_print(FILE* input_file) { int current_byte = EOF; int stripped_byte = 0; do { if (input_file == NULL) { (void)(fprintf(stderr, "Conversion failed.\n")); return 1; } current_byte = fgetc(input_file); if (ferror(input_file) != 0) { (void)(fprintf(stderr, "Error reading file\n")); return 1; } if (current_byte != EOF) { stripped_byte = current_byte & 0x7F; if (stripped_byte == 13) { current_byte = fgetc(input_file); if (ferror(input_file) != 0) { (void)(fprintf(stderr, "Error reading file\n")); return 1; } if (current_byte == EOF) { return 0; } stripped_byte = current_byte & 0x7F; if (stripped_byte == 10) { (void)(printf("\n")); } } else { if ((stripped_byte & 0x1F) != stripped_byte) { (void)(printf("%c", stripped_byte)); } } } } while (current_byte != EOF); return 0; } int main(int argc, char** argv) { FILE* current_input_file = NULL; int index = 1; char* current_file_name = NULL; const int debug_flag = 0; for (; index < argc; index++) { current_file_name = argv[index]; if (current_file_name == NULL) { (void)(fprintf(stderr, "Error: CLI argument %d invalid\n", index)); } (void)(assert(current_file_name != NULL)); current_input_file = fopen(current_file_name, "rb"); if (current_input_file == NULL) { (void)(fprintf(stderr, "Error: file %s not found\n", current_file_name)); } (void)(assert(current_input_file != NULL)); (void)(printf("Printing file %s as txt:\n\n", current_file_name)); if (debug_flag != 0) { if (debug_convert_ws4_and_print(current_input_file) != 0) { (void)(fprintf(stderr, "Error: debug conversion failed on %s\n", current_file_name)); } } else { if (convert_ws4_and_print(current_input_file) != 0) { (void)(fprintf(stderr, "Error: txt conversion failed on %s\n", current_file_name)); } } (void)(assert(current_input_file != NULL)); if (fclose(current_input_file) != 0) { (void)(fprintf(stderr, "Error: couldn't close file %s\n", current_file_name)); return 1; } current_input_file = NULL; } return 0; }