/* This program converts between .bdf font files (bitmap distribution format) and a more human-readable format. The second format is easily edited with an ordinary text editor. Compile with gcc -o bdf2readable bdf2readable.c */ #include /* printf(), fopen(), fgets() */ #include /* exit() */ #include /* strcmp(), strcasecmp() */ #include /* toupper() */ #define Boolean char #define True 1 #define False 0 /* These strings are part of the bdf standard. */ #define BITMAP_START "BITMAP\n" #define BITMAP_END "ENDCHAR\n" /* These strings appear in the more human-readable format. */ #define ONE "X" #define ZERO "." #define ONE_C 'X' #define ZERO_C '.' #define MAX_LINE_LENGTH 1024 main(int argc, char *argv[]) { FILE * stream; char * filename = NULL; char buffer[MAX_LINE_LENGTH]; Boolean convertingBitmap = False; Boolean convertingFrom = False; char *pc; int value, count; if (argc != 2 && (argc != 3 || strcmp(argv[1],"r") != 0)) { printf("\nUsage: %s [r] file\n" "Echos a converted version of the file to stdout.\n" "Default behaviour is to convert a bdf (bitmap distribution format) font\n" "into a more human-readable format. The 'r' flag causes the reverse\n" "conversion to take place.\n\n", argv[0]); exit(0); } if (argc == 3) { convertingFrom = True; filename = argv[2]; } else filename = argv[1]; stream = fopen(filename,"r"); if (NULL == stream) { fprintf(stderr,"\nCannot open file %s.\n\n",filename); exit(-1); } while (NULL != fgets(buffer,MAX_LINE_LENGTH,stream)) { if (convertingBitmap) { if (strcasecmp(buffer,BITMAP_END) == 0) { convertingBitmap = False; printf(buffer); } else { if (convertingFrom) { /* convert readable->bdf */ pc = buffer; count = value = 0; while ('\0' != *pc) { switch (*pc) { case ZERO_C : value = 2*value; ++count; break; case ONE_C : value = 2*value+1; ++count; break; default : printf("%c",*pc); break; } if (count == 4) { printf("%c",value > 9 ? (char)('a'+value-10) : (char)('0'+value)); count = value = 0; } ++ pc; } /* while */ } else { /* perform the forward conversion bdf->readable */ pc = buffer; while ('\0' != *pc) { switch (toupper(*pc)) { case '0': printf(ZERO ZERO ZERO ZERO); break; case '1': printf(ZERO ZERO ZERO ONE); break; case '2': printf(ZERO ZERO ONE ZERO); break; case '3': printf(ZERO ZERO ONE ONE); break; case '4': printf(ZERO ONE ZERO ZERO); break; case '5': printf(ZERO ONE ZERO ONE); break; case '6': printf(ZERO ONE ONE ZERO); break; case '7': printf(ZERO ONE ONE ONE); break; case '8': printf( ONE ZERO ZERO ZERO); break; case '9': printf( ONE ZERO ZERO ONE); break; case 'A': printf( ONE ZERO ONE ZERO); break; case 'B': printf( ONE ZERO ONE ONE); break; case 'C': printf( ONE ONE ZERO ZERO); break; case 'D': printf( ONE ONE ZERO ONE); break; case 'E': printf( ONE ONE ONE ZERO); break; case 'F': printf( ONE ONE ONE ONE); break; default : printf("%c",*pc); break; } ++ pc; } /* while */ } } } else { printf(buffer); if (strcasecmp(buffer,BITMAP_START) == 0) { convertingBitmap = True; } } } fclose(stream); }