miftext.cpp


01: #include <stdio.h>
02: #include <stdlib.h>
03: 
04: void miftext(FILE *in, FILE *out);
05: 
06: int main(int argc, char *argv[])
07: {
08:         char *filename;
09: 
10:         if (argc<3) {
11:                 printf("usage: miftext input_file output_file\n");
12:                 return -1;
13:         }
14: 
15:         FILE *in, *out;
16:         errno_t err;
17:         filename = argv[1];
18:         err = fopen_s(&in,filename,"rt");
19:         if (err) {
20:                 printf("file: %s not found\n",filename);
21:                 return -1;
22:         }
23:         filename = argv[2];
24:         err = fopen_s(&out,filename,"wt");
25:         if (err) {
26:                 printf("file: %s not opened\n",filename);
27:                 return -1;
28:         }
29:         miftext(in,out);
30:         return 0;
31: }
32: 
33: void miftext(FILE *in, FILE *out)
34: {
35:         unsigned int data;
36:         unsigned int address, offset;
37:         unsigned int ndepth = 256;
38:         int nwidth = 8;
39:         fprintf(out,"DEPTH = %d;\n",ndepth);
40:         fprintf(out,"WIDTH = %d;\n\n",nwidth);
41:         fprintf(out,"ADDRESS_RADIX = HEX;\n");
42:         fprintf(out,"DATA_RADIX = HEX;\n");
43:         fprintf(out,"CONTENTS\n  BEGIN\n");
44:         fprintf(out,"[0..%x]   :  0;\n",ndepth-1);
45:         address = 0;
46:         offset = 0;
47:         while ( ( data = getc( in ) ) != EOF ) {
48:                 if (data=='\n') {
49:                         while (offset<16) {
50:                                 fprintf(out,"%04x : %02x;\n",address,' ');
51:                                 address++;
52:                                 offset++;
53:                         }
54:                         offset = 0;
55:                 }
56:                 else {
57:                         fprintf(out,"%04x  : %02x; %% %c %%\n",address,data,(data<32?32:data));
58:                         address++;
59:                         offset++;
60:                 }
61:                 if (address>=ndepth) break;
62:         }
63:         fprintf(out,"END;\n");
64:         fclose(in);
65:         fclose(out);
66: }


Results

The example input text is as follows:

0123456789ABCDEF
Testing RS232
Row, row, row
your boat,
gently down
the stream.

The output MIF file looks as follows:

DEPTH = 256;
WIDTH = 8;

ADDRESS_RADIX = HEX;
DATA_RADIX = HEX;
CONTENTS
  BEGIN
[0..ff]   :  0;
0000  : 30; % 0 %
0001  : 31; % 1 %
0002  : 32; % 2 %
0003  : 33; % 3 %
0004  : 34; % 4 %
0005  : 35; % 5 %
0006  : 36; % 6 %
0007  : 37; % 7 %
0008  : 38; % 8 %
0009  : 39; % 9 %
000a  : 41; % A %
000b  : 42; % B %
000c  : 43; % C %
000d  : 44; % D %
000e  : 45; % E %
000f  : 46; % F %
0010  : 54; % T %

The remainder of the file has been excluded from this listing.


Maintained by John Loomis, updated Mon Nov 09 22:59:30 2009