home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / TCH2AVI.LZH / TCH.C < prev    next >
C/C++ Source or Header  |  1996-06-12  |  7KB  |  302 lines

  1. #pragma hdrstop
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "tch.h"
  7.  
  8. TimeChart    *timechart;
  9. //;[TIMECHARTMAX];
  10. int            timecharts;
  11. char tchdir[256];
  12.  
  13.  
  14. int tchinsert(TimeChart *tch, int point)
  15. {
  16.     int i;
  17.     if (point < 0 || timecharts < point || timecharts == TIMECHARTMAX) {
  18.         return FALSE;
  19.     }
  20.     for (i = timecharts - 1; i > point; --i) {
  21.         timechart[i] = timechart[i-1];
  22.     }
  23.  
  24.     timechart[point] = *tch;
  25.     timecharts++;
  26.     return TRUE;
  27. }
  28.  
  29. int tchdelete(TimeChart *tch, int point)
  30. {
  31.     int i;
  32.     if (point < 0 || timecharts <= point || timecharts == TIMECHARTMAX) {
  33.         return FALSE;
  34.     }
  35.     for (i = point; i < timecharts - 1; ++i) {
  36.         timechart[i] = timechart[i+1];
  37.     }
  38.     timecharts--;
  39.     return TRUE;
  40. }
  41.  
  42. static int insertpic(TimeChart *tch, char *name, int point, FILE *fp)
  43. {
  44.     char str[256];
  45.     int i, c, frame;
  46.  
  47.     if (name[0] == '\\' || name[0] && name[1] == ':') {
  48.         strcpy(str, name);
  49.     } else {
  50.         strcpy(str, tchdir);
  51.         strcat(str, name);
  52.     }
  53.  
  54.     skipspace(fp);
  55.     c = fgetc(fp);
  56.     if (c != '[') {
  57.         ungetc(c, fp);
  58.         removepic(str);
  59.         frame = removenumber(str);
  60.         if (frame != 0) {
  61.             strcpy(tch->filename, str);
  62.             tch->startframe = tch->endframe = frame;
  63.         } else {
  64.             strcpy(tch->filename, str);
  65.             tch->startframe = tch->endframe = 0;
  66.         }
  67.         if (tchinsert(tch, point) == FALSE) {
  68.             return FALSE;
  69.         }
  70.     } else {
  71.         i = 0;
  72.         frame = 0;
  73.         skipspace(fp);
  74.         while ( c = fgetc(fp), c != EOF) {
  75.             if (isdigit(c)) {
  76.                 frame = frame * 10 + (c - '0');
  77.             } else {
  78.                 break;
  79.             }
  80.         }
  81.         if (c == EOF) {
  82.             return FALSE;
  83.         }
  84.         ungetc(c, fp);
  85.         tch->startframe = frame;
  86.         skipspace(fp);
  87.         if (c = fgetc(fp), c == '-') {
  88.             skipspace(fp);
  89.             frame = 0;
  90.             while ( c = fgetc(fp), c != EOF) {
  91.                 if (isdigit(c)) {
  92.                     frame = frame * 10 + (c - '0');
  93.                 } else {
  94.                     break;
  95.                 }
  96.             }
  97.         }
  98.         if (c == EOF) {
  99.             return FALSE;
  100.         }
  101.         ungetc(c, fp);
  102.         tch->endframe = frame;
  103.         if (tch->startframe > tch->endframe) {
  104.             tch->step = -1;
  105.         } else {
  106.             tch->step = 1;
  107.         }
  108.         skipspace(fp);
  109.         if ((c = fgetc(fp)) == ':') {
  110.             int tmp = 0;
  111.             while (c = fgetc(fp), c != EOF) {
  112.                 if (isdigit(c)) {
  113.                     tmp = tmp * 10 + (c - '0');
  114.                 } else {
  115.                     break;
  116.                 }
  117.             }
  118.             if (tmp > 0) {
  119.                 tch->step *= tmp;
  120.             }
  121.         }
  122.         do {
  123.             if (c == ']') {
  124.                 break;
  125.             }
  126.         } while (c = fgetc(fp), c != EOF);
  127.         if (c == EOF) {
  128.             return FALSE;
  129.         }
  130.         strcpy(tch->filename, str);
  131.         if (tchinsert(tch, point) == FALSE) {
  132.             return FALSE;
  133.         }
  134.     }
  135.     return TRUE;
  136. }
  137.  
  138. int mergetimechart(char *fname, int point)
  139. {
  140.     FILE *fp;
  141.     int    defwait = 1, defspeed = 2;
  142.     int i;
  143.     TimeChart    tch;
  144.     char str[32];
  145.     int c;
  146.     char *p, name[128];
  147.     strcpy(tchdir, fname);
  148.     if ((p = strrchr(tchdir, '\\')) != NULL) {
  149.         p[1] = '\0';
  150.     } else if (tchdir[0] && tchdir[1] == ':') {
  151.         tchdir[2] = '\0';
  152.     } else {
  153.         tchdir[0] = '\0';
  154.     }
  155.     strcpy(name, fname);
  156.     addtch(name);
  157.     if (fp = fopen(name, "rt"), fp == NULL) {
  158.         printf("cannot open %s\n", name);
  159.         return FALSE;
  160.     }
  161.     while (c = gettoken(str, fp), c != EOF) {
  162.         if (str[0] == '.' && isalpha(str[1])) {
  163.             if (strcmpi(str, ".timechart") == 0) {
  164.             } else if (strcmpi(str, ".endchart") == 0) {
  165.                 break;
  166.             } else if (strcmpi(str, ".voice") == 0
  167.              || ( tolower(str[1]) == 'v' && isdigit(str[2]))) {
  168.                 if ((c = gettoken(str, fp)) != EOF) {
  169.                     tch.mode = PCM;
  170.                     tch.wait = defwait;
  171.                     tch.speed = defspeed;
  172.                     switch(str[1]) {
  173.                     case '0':
  174.                         tch.pcmoutputmode = 0;
  175.                         break;
  176.                     case '1':
  177.                         tch.pcmoutputmode = 1;
  178.                         break;
  179.                     case '2':
  180.                         tch.pcmoutputmode = 2;
  181.                         break;
  182.                     case '3':
  183.                         tch.pcmoutputmode = 3;
  184.                         break;
  185.                     default :
  186.                         tch.pcmoutputmode = 4;
  187.                     }
  188.                     strcpy(tch.filename, str);
  189.                     if (tchinsert(&tch, point++) == FALSE) {
  190.                         break;
  191.                     }
  192.                 }
  193.             } else if (strcmpi(str, ".rep") == 0) {
  194.                 if (gettoken(str, fp) != EOF && (c = atoi(str)) != 0) {
  195.                     tch.mode = REP;
  196.                     tch.speed = defspeed;
  197.                     tch.wait = defwait;
  198.                     tch.repeatcount = c;
  199.                     if (tchinsert(&tch, point++) == FALSE) {
  200.                         break;
  201.                     }
  202.                 }
  203.             } else if (strcmpi(str, ".endrep") == 0) {
  204.                     tch.mode = ENDREP;
  205.                     tch.speed = defspeed;
  206.                     tch.wait = defwait;
  207.                     if (tchinsert(&tch, point++) == FALSE) {
  208.                         break;
  209.                     }
  210.             } else if (strcmpi(str, ".opm") == 0) {
  211.                 if ((c = gettoken(str, fp)) != EOF) {
  212.                 }
  213.             } else if (strcmpi(str, ".wait") == 0) {
  214.                 if ((c = gettoken(str, fp)) != EOF && (i = atoi(str)) != 0) {
  215.                     tch.wait = defwait = i;
  216.                     tch.speed = defspeed;
  217.                 }
  218.             } else if (strcmpi(str, ".speed") == 0) {
  219.                 if ((c = gettoken(str, fp)) != EOF && (i = atoi(str)) != 0) {
  220.                     tch.speed = defspeed = i;
  221.                     tch.wait = defwait;
  222.                 }
  223.             }
  224.         } else {
  225.             tch.mode = PIC;
  226.             tch.speed = defspeed;
  227.             tch.wait = defwait;
  228.             tch.step = 1;
  229.             insertpic(&tch, str, point++, fp);
  230.         }
  231.     }
  232.     fclose(fp);
  233.     return TRUE;
  234. }
  235.  
  236. int loadtimechart(char *name)
  237. {
  238.     int retcode;
  239.     if (timechart == NULL) {
  240.         timechart = malloc(sizeof(TimeChart) * TIMECHARTMAX);
  241.     }
  242.  
  243.     timecharts = 0;
  244.     retcode = mergetimechart(name, 0);
  245.     return retcode;
  246. }
  247.  
  248. int savetimechart(char *fname)
  249. {
  250.     int    defwait = 1, defspeed = 3;
  251.     int i;
  252.     FILE *fp;
  253.     char name[128];
  254.     strcpy(name, fname);
  255.     addtch(name);
  256.     if (fp = fopen(name, "wt"), fp == NULL) {
  257.         printf("cannot open %s\n", name);
  258.         return FALSE;
  259.     }
  260.     fprintf(fp, ".timechart\n");
  261.     for (i = 0; i < timecharts; ++i) {
  262.         if (timechart[i].speed != defspeed) {
  263.             fprintf(fp, ".speed %d\n", defspeed = timechart[i].speed);
  264.         }
  265.         if (timechart[i].wait != defwait) {
  266.             fprintf(fp, ".wait %d\n", defwait= timechart[i].wait);
  267.         }
  268.         switch (timechart[i].mode) {
  269.         case PCM :
  270.             fprintf(fp, ".v%1d %s\n", timechart[i].pcmoutputmode, timechart[i].filename);
  271.             break;
  272.         case OPM :
  273.             break;
  274.         case PIC :
  275.             if (timechart[i].startframe == 0) {
  276.                 fprintf(fp, "\t%s\n", timechart[i].filename);
  277.             } else if (timechart[i].startframe == timechart[i].endframe) {
  278.                 fprintf(fp, "\t%s%03d\n", timechart[i].filename, timechart[i].startframe);
  279.             } else {
  280.                 fprintf(fp, "\t%s[%d-%d]\n", timechart[i].filename,
  281.                     timechart[i].startframe, timechart[i].endframe);
  282.             }
  283.             break;
  284.         case REP :
  285.             fprintf(fp, "rep %d\n", timechart[i].repeatcount);
  286.             break;
  287.         case ENDREP :
  288.             fprintf(fp, "endrep\n");
  289.             break;
  290.         }
  291.     }
  292.     if (defwait != 1) {
  293.         fprintf(fp, ".wait %d\n", 1);
  294.     }
  295.     if (defspeed != 3) {
  296.         fprintf(fp, ".speed %d\n", 3);
  297.     }
  298.     fprintf(fp, ".endchart\n");
  299.     fclose(fp);
  300.     return TRUE;
  301. }
  302.