home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 420.lha / winformat_v1.0 / winformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-02  |  4.8 KB  |  219 lines

  1. #include "stdio.h"
  2. #include "ctype.h"
  3. #include "string.h"
  4.  
  5. #define MAXTABLE 24
  6. #define QUOTE 0x22
  7.  
  8. int col_ctr = 1, line_ctr = 0;
  9. int len_of_text[MAXTABLE];
  10. int beg_col[MAXTABLE];
  11. int xpos[MAXTABLE];
  12. int ypos[MAXTABLE];
  13.  
  14. char byte;
  15. char *sub1;
  16. char text[MAXTABLE][81];
  17.  
  18. void compresstext (sub1,beg_col,len)
  19. char *sub1;
  20. int *beg_col, *len;
  21. {
  22.    char *sub2, *sub3;
  23.    for (sub2 = sub1; isspace (*sub2); sub2++);
  24.    if (*sub2 != '\0')
  25.    {
  26.       *beg_col = sub2 - sub1 + 1;
  27.       for (sub3 = sub2; *sub3 != '\0'; sub3++);
  28.       for (sub3--; isspace (*sub3); sub3--);
  29.       sub3++;
  30.       *sub3 = '\0';
  31.       *len = sub3 - sub2;
  32.       if (sub2 != sub1)
  33.       {
  34.          for (; sub2 <= sub3; sub2++, sub1++)
  35.             *sub1 = *sub2;
  36.       }
  37.    }
  38. }
  39.  
  40. void nextline()
  41. {
  42.    line_ctr++;
  43.    if (line_ctr >= MAXTABLE)
  44.       byte = NULL;
  45.    else
  46.    {
  47.       col_ctr = 1;
  48.       sub1 = &text[line_ctr];
  49.    }
  50. }
  51.  
  52. void getdata (fname)
  53. char *fname;
  54.  
  55. {
  56.    FILE *infile;
  57.    int a;
  58.  
  59.    infile = fopen (fname, "r");
  60.    if (infile == NULL)
  61.    {
  62.       printf ("Input Open Error\n");
  63.       printf ("Error Code %d\n", infile);
  64.       exit (1);
  65.    }
  66.  
  67.  
  68.    sub1 = &text[0];
  69.    do
  70.    {
  71.       a = fread (&byte, sizeof (byte), 1,infile);
  72.       if (a < 1)
  73.          byte = NULL;
  74.  
  75.       switch (byte)
  76.       {
  77.          case NULL:
  78.             *sub1 = '\0';
  79.             compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
  80.             break;
  81.          case '\n':
  82.             *sub1 = '\0';
  83.             compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
  84.             nextline ();
  85.             break;
  86.          default:
  87.             *sub1 = byte;
  88.             sub1++;
  89.             col_ctr++;
  90.             if (col_ctr > 80)
  91.             {
  92.                compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
  93.                nextline ();
  94.             }
  95.       }
  96.  
  97.    }  while (byte != NULL);
  98.  
  99.    fclose (infile);
  100. }
  101.  
  102. void calcxy (font)
  103. char *font;
  104. {
  105.    int indx1 = 0;
  106.    int xorg = 0, yorg, baseline, txheight, txwidth;
  107.  
  108.    if (strcmpi (font, "topaz.8") == 0)
  109.    {
  110.       xorg = 4;
  111.       yorg = 11;
  112.       baseline = 0;     /* is really 6, but is not needed for itext struct */
  113.       txheight = 8;
  114.       txwidth = 8;
  115.    }
  116.    else
  117.    {
  118.       if (strcmpi (font, "topaz.9") == 0)
  119.       {
  120.          xorg = 4;
  121.          yorg = 12;
  122.          baseline = 0;  /* is really 6 but not need for itext structures */
  123.          txheight = 9;
  124.          txwidth = 10;
  125.       }
  126.    }
  127.  
  128.    if (xorg == 0)
  129.    {
  130.          printf ("%s font is not supported.\nOutput File was not created.\n", font);
  131.          exit (1);
  132.    }
  133.  
  134.    for (indx1 = 0; indx1 < MAXTABLE; indx1++)
  135.    {
  136.       if (len_of_text[indx1] > 0)
  137.       {
  138.          xpos[indx1] = xorg + (txwidth * (beg_col[indx1] - 1));
  139.          ypos[indx1] = yorg + baseline + (txheight * indx1);
  140.       }
  141.    }
  142. }
  143.  
  144. void givedata (outfile)
  145. char  *outfile;
  146. {
  147.    FILE *fp;
  148.    int indx1;
  149.    int lines_of_text = 0;
  150.    int first_time = 0;
  151.  
  152.    for (indx1 = 0; indx1 < MAXTABLE; indx1++)
  153.    {
  154.       if (len_of_text[indx1] > 0)
  155.          lines_of_text++;
  156.    }
  157.  
  158.    if (lines_of_text == 0)
  159.    {
  160.       printf ("Input File contains no Text\nOutput File is not created\n");
  161.       exit (1);
  162.    }
  163.  
  164.    fp = fopen (outfile, "w");
  165.    if (fp == NULL)
  166.    {
  167.       printf ("Output Open Error\n");
  168.       printf ("Error Code %d\n", fp);
  169.       exit (1);
  170.    }
  171.  
  172.    for (indx1 = MAXTABLE - 1; indx1 >= 0; indx1--)
  173.    {
  174.       if (len_of_text[indx1] > 0)
  175.       {
  176.          fprintf (fp, "struct IntuiText itext%d = {\n", lines_of_text);
  177.          fprintf (fp, "             3,0,JAM2,          /* front and back text pens, drawmode and fill byte */\n");
  178.          fprintf (fp, "             %3d,%3d,           /* XY origin relateive to conatiner TOPLEFT */\n", xpos[indx1], ypos[indx1]);
  179.          fprintf (fp, "             NULL,              /* font pointer or NULL for default */\n");
  180.          fprintf (fp, "             %c%s%c,            /* pointer to text */\n", QUOTE, text[indx1], QUOTE);
  181.          if (first_time == 0)
  182.             fprintf (fp, "             NULL               /* next InutiText structure */\n");
  183.          else
  184.             fprintf (fp, "             &itext%d            /* next InutiText structure */\n", lines_of_text + 1);
  185.          fprintf (fp, "};\n\n");
  186.          lines_of_text--;
  187.          first_time = 1;
  188.       }
  189.    }
  190.  
  191.    fprintf (fp, "#define IntuiTextList1 itext1\n");
  192.  
  193.    fclose (fp);
  194. }
  195.  
  196.  
  197. void main(argc, argv)
  198. int argc;
  199. char *argv[];
  200. {
  201.  
  202.    if (argc < 3 || argc > 4)
  203.    {
  204.       printf ("Invalid number of arguments received!\nArguments are:\n  1) The name of the file to read (required)\n  2) The name of the file to output (required)\n  3) The font to use (option) (ie:topaz.9) (default = topaz.8)\n");
  205.       exit(1);
  206.    }
  207.  
  208.    getdata (argv[1]);
  209.  
  210.    if (argc == 4)
  211.          calcxy (argv[3]);
  212.       else
  213.          calcxy ("topaz.8");
  214.  
  215.    givedata (argv[2]);
  216.  
  217. }
  218.  
  219.