home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / chord3_6.zip / chord / a2crd.c next >
C/C++ Source or Header  |  1995-04-25  |  7KB  |  367 lines

  1. static char SccsId[] = "@(#)a2crd.c    3.6\t Mar. 1995";
  2. static char copyright[] = "Copyright 1991-1995 by Martin Leclerc & Mario Dorion";
  3.  
  4. #include <stdio.h>
  5. #include "chord.h"
  6.  
  7.  
  8. FILE
  9.     *source_fd;
  10.  
  11. int
  12.     p_put,
  13.     lchord,
  14.     ltext,
  15.     lbuf,
  16.     c,
  17.     debug_mode = 0,
  18.     dirty_chord=0,
  19.     dirty_text=0,
  20.     in_header,
  21.     in_title=0,
  22.     in_subtitle=0,
  23.     n_lines,
  24.     in_chorus=0;
  25.  
  26. char
  27.     chord_line[MAXLINE],
  28.     text_line[MAXLINE],
  29.     buf[MAXLINE],
  30.     *command_name,
  31.     source[MAXTOKEN],
  32.     *current_file,
  33.     mesgbuf[MAXLINE],
  34.     *mesg;
  35.  
  36. float
  37.     spaces=0,
  38.     not_space=0;
  39.  
  40. double 
  41.     ratio=1.0;
  42.  
  43. extern char *optarg;
  44. extern int optind, opterr;
  45.  
  46. /* --------------------------------------------------------------------------------*/
  47. void print_2lines(chord_line, text_line)
  48. char chord_line[], text_line[];
  49.  
  50. {
  51. int i, mini=0, max=0;
  52. int p_out=0;
  53.  
  54. max = (ltext > lchord) ? ltext : lchord;
  55.  
  56. sprintf (mesg, "   print_2lines text_line=[%s]", text_line); debug(mesg);
  57. sprintf (mesg, "   print_2lines chord_line=[%s]", chord_line); debug(mesg);
  58.  
  59.  
  60. for (i = 0; i < max ; i++)
  61.     {
  62.     if ( (i <= lchord) && ( chord_line[i] != ' ' )
  63.         && (chord_line[i] != '\0') && ( i >= mini ) )
  64.         {
  65.         printf ("[");
  66.         for (mini=i; (chord_line[mini] != ' ') && (mini<lchord); mini++)
  67.             {
  68.             printf("%c", chord_line[mini]);
  69.             p_out++;
  70.             }
  71.         printf ("]");
  72.         p_out += 2;
  73.         }
  74.     if (i < ltext)
  75.         {
  76.         printf ("%c", text_line[i]);
  77.         p_out++;
  78.         }
  79.     }
  80.  
  81. printf("\n");
  82. p_out=0;
  83.  
  84.  
  85. ltext=0;
  86. dirty_text=0;
  87. for (i=0;i<MAXLINE;i++)
  88.     text_line[i]='\0';
  89.  
  90. lchord=0;
  91. dirty_chord=0;
  92. for (i=0;i<MAXLINE;i++)
  93.     chord_line[i]='\0';
  94. }
  95.  
  96. /* --------------------------------------------------------------------------------*/
  97. void do_help (command)
  98. char *command;
  99.         {
  100.         fprintf (stderr, "Usage: %s [options] file\n", command);
  101.         fprintf (stderr, "Options:\n");
  102.         fprintf (stderr, "      -D                 : Debug mode\n");
  103.     exit(0);
  104.         }
  105.  
  106. /* --------------------------------------------------------------------------------*/
  107. void do_sig()
  108.     {
  109.     char sig_file[MAXTOKEN];
  110.     FILE *sig_fd;
  111.  
  112.     strcpy (sig_file, getenv ("HOME"));
  113.     strcat (sig_file,"/.a2crdsig\0");
  114.  
  115.     sig_fd = fopen (sig_file, "r");
  116.     if (sig_fd != NULL)
  117.         {
  118.         while ((c=getc(sig_fd)) != EOF)
  119.             printf("%c", c);
  120.         fclose(sig_fd);
  121.         }
  122.     }
  123.  
  124. /* --------------------------------------------------------------------------------*/
  125. void process_file(source_fd)
  126. FILE *source_fd;
  127. {
  128.  
  129. int i;
  130.  
  131. do_sig();
  132.  
  133. debug ("start of process_file");
  134. n_lines=0;
  135. lbuf=0;
  136.  
  137. while ((c=getc(source_fd)) != EOF)
  138.     {
  139.  
  140.     switch (c)
  141.         {
  142.     
  143.         case '\n':
  144.     
  145.         n_lines++;
  146.         buf[lbuf]='\0';
  147.  
  148.         /* Handle exceptions */
  149.         if ( n_lines == 1)
  150.             {
  151.             if (! strncmp(text_line, "From ", 5))
  152.                 in_header = TRUE;
  153.             else    in_title  = TRUE;
  154.             }
  155.  
  156.         if ( in_header && lbuf == 0)
  157.             in_header = FALSE;
  158.  
  159.         if (in_header )
  160.             printf ("# %s\n", text_line);
  161.  
  162.         else if (not_space != 0)
  163.             {
  164. sprintf (mesg, "   space=%d not_space=%d", (int)spaces,(int)not_space); debug(mesg);
  165.     
  166.             if ( ((spaces / not_space) > ratio) || ((spaces==0) && (lbuf<3)) )
  167.  
  168.             /* this must be a chord line */
  169.                 {
  170. sprintf (mesg, "chord is \"%s\"",buf); debug(mesg);
  171.     
  172.                 if (dirty_chord)
  173.                     {
  174.                     print_2lines (chord_line, text_line);
  175.                     }
  176.     
  177.                 strcpy (chord_line, buf);
  178.                 lchord=lbuf;
  179.                 dirty_chord++;
  180.                 in_title=0; in_subtitle=0;
  181.                 }
  182.             else
  183.             /* so it is a text line ! */
  184.                 {
  185.     
  186. sprintf (mesg, "text is \"%s\"",buf); debug(mesg);
  187.     
  188.                 if (dirty_text)
  189.                     {
  190.                     print_2lines (chord_line, text_line);
  191.                     }
  192.     
  193.  
  194.                 if (in_title)
  195.                     {
  196.                     printf("{title:%s}\n", buf);
  197.                     in_title=FALSE;
  198.                     in_subtitle=TRUE;
  199.                     }
  200.                 else
  201.                     if (in_subtitle)
  202.                         {
  203.                         printf("{subtitle:%s}\n", buf);
  204.                         }
  205.                     else 
  206.                         {
  207.                         strcpy(text_line, buf);
  208.                         ltext=lbuf;
  209.                         dirty_text++;
  210.                         }
  211.     
  212.                 }
  213.                         
  214.             lbuf=0;
  215.             spaces=0;
  216.             not_space=0;
  217.             }
  218.         else
  219.             {
  220.             if (dirty_chord || dirty_text)
  221.                 print_2lines(chord_line, text_line);
  222.             printf ("\n");
  223.             if (in_chorus)
  224.                 {
  225.                 printf("{end_of_chorus}\n");
  226.                 in_chorus=0;
  227.                 }
  228.             in_subtitle=FALSE;
  229.             in_title=FALSE;
  230.             }
  231.  
  232.         for (i=0;i<MAXLINE;i++)
  233.             buf[i]='\0';
  234.         break;
  235.     
  236.         case '{':
  237.             if (lbuf == 0)
  238.             /* directive */
  239.                 {
  240.                 debug("got a directive");
  241.                 if (dirty_chord || dirty_text)
  242.                     {
  243.                     debug("FLUSHING");
  244.                     print_2lines(chord_line, text_line);
  245.                     }
  246.                 buf[0]='{';
  247.                 for (lbuf=1; (c=getc(source_fd)) != '\n'; lbuf++)
  248.                     buf[lbuf]=c;
  249.                 strcpy(text_line, buf);
  250.                 ltext=lbuf;
  251.                 print_2lines(chord_line, text_line);
  252.                 lbuf=0;
  253.                 }
  254.             break;
  255.  
  256.         case '#':
  257.             if (lbuf == 0)
  258.             /* comment */
  259.                 {
  260.                 debug("got a comment");
  261.                 if (dirty_chord || dirty_text)
  262.                     {
  263.                     debug("FLUSHING");
  264.                     print_2lines(chord_line, text_line);
  265.                     }
  266.                 buf[0]='#';
  267.                 for (lbuf=1; (c=getc(source_fd)) != '\n'; lbuf++)
  268.                     buf[lbuf]=c;
  269.                 strcpy(text_line, buf);
  270.                 ltext=lbuf;
  271.                 print_2lines(chord_line, text_line);
  272.                 lbuf=0;
  273.                 }
  274.             break;
  275.  
  276.         case '\t':
  277.             for (i=0; (i<8) && (lbuf % 8 != 0); i++)
  278.                 buf[lbuf++] = ' ';
  279.             spaces += 8;
  280.             break;
  281.             
  282.     
  283.         case '|':
  284.         case ' ':
  285.             spaces++;
  286.             buf[lbuf++] = ' ';
  287.             break;
  288.     
  289.         default:
  290.             not_space++;
  291.             buf[lbuf++] = c;
  292.             break;
  293.         }
  294.     }
  295. if (dirty_chord || dirty_text)
  296.     print_2lines(chord_line, text_line);
  297. chord_line[0]='\0';
  298. text_line[0]='\0';
  299. buf[0]='\0';
  300. }
  301. /* --------------------------------------------------------------------------------*/
  302.  
  303. void main (argc, argv)
  304. int argc;
  305. char **argv;
  306.  
  307. {
  308. double f;
  309.  
  310. command_name=argv[0];
  311. mesg=&mesgbuf[0];
  312.  
  313. while (( c = getopt(argc, argv, "Dr:o:")) != -1)
  314.     switch (c) {
  315.  
  316.     case 'D':
  317.         debug_mode++;
  318.         break;
  319.     
  320.     case 'r':
  321.         f=atof(optarg);
  322.             if (f ==0)
  323.                 error ("Invalid value for ratio");
  324.             else
  325.                 ratio=f;
  326.             break;
  327.  
  328.     case 'o':
  329.         if ( freopen(optarg, "w", stdout) == NULL)
  330.             {
  331.             fprintf (stderr, "Unable to open \"%s\" for output\n", optarg);
  332.             exit(1);
  333.             }
  334.         break;
  335.         
  336.     
  337.     case '?':
  338.         do_help (argv[0]);
  339.         break;
  340.     }
  341.     
  342.  
  343.         if (optind  == argc)
  344.                 {
  345.                 debug ("Reading stdin");
  346.                 strcpy(source, "stdin");
  347.                 process_file (stdin);
  348.                 }
  349.         else
  350.                 {
  351.                 for ( ; optind < argc; optind++ )
  352.                         {
  353.             sprintf (mesg, "Ready to process file \"%s\"\n", argv[optind]);
  354.                         debug (mesg);
  355.  
  356.                         strcpy(source, argv[optind]);
  357.  
  358.             sprintf (mesg, "Calling read_input_file on \"%s\"\n", source);
  359.                         debug (mesg);
  360.  
  361.                         read_input_file(source, source_fd);
  362.                         }
  363.                 }
  364.  
  365.  
  366. }
  367.