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 >
Wrap
C/C++ Source or Header
|
1996-06-12
|
7KB
|
302 lines
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tch.h"
TimeChart *timechart;
//;[TIMECHARTMAX];
int timecharts;
char tchdir[256];
int tchinsert(TimeChart *tch, int point)
{
int i;
if (point < 0 || timecharts < point || timecharts == TIMECHARTMAX) {
return FALSE;
}
for (i = timecharts - 1; i > point; --i) {
timechart[i] = timechart[i-1];
}
timechart[point] = *tch;
timecharts++;
return TRUE;
}
int tchdelete(TimeChart *tch, int point)
{
int i;
if (point < 0 || timecharts <= point || timecharts == TIMECHARTMAX) {
return FALSE;
}
for (i = point; i < timecharts - 1; ++i) {
timechart[i] = timechart[i+1];
}
timecharts--;
return TRUE;
}
static int insertpic(TimeChart *tch, char *name, int point, FILE *fp)
{
char str[256];
int i, c, frame;
if (name[0] == '\\' || name[0] && name[1] == ':') {
strcpy(str, name);
} else {
strcpy(str, tchdir);
strcat(str, name);
}
skipspace(fp);
c = fgetc(fp);
if (c != '[') {
ungetc(c, fp);
removepic(str);
frame = removenumber(str);
if (frame != 0) {
strcpy(tch->filename, str);
tch->startframe = tch->endframe = frame;
} else {
strcpy(tch->filename, str);
tch->startframe = tch->endframe = 0;
}
if (tchinsert(tch, point) == FALSE) {
return FALSE;
}
} else {
i = 0;
frame = 0;
skipspace(fp);
while ( c = fgetc(fp), c != EOF) {
if (isdigit(c)) {
frame = frame * 10 + (c - '0');
} else {
break;
}
}
if (c == EOF) {
return FALSE;
}
ungetc(c, fp);
tch->startframe = frame;
skipspace(fp);
if (c = fgetc(fp), c == '-') {
skipspace(fp);
frame = 0;
while ( c = fgetc(fp), c != EOF) {
if (isdigit(c)) {
frame = frame * 10 + (c - '0');
} else {
break;
}
}
}
if (c == EOF) {
return FALSE;
}
ungetc(c, fp);
tch->endframe = frame;
if (tch->startframe > tch->endframe) {
tch->step = -1;
} else {
tch->step = 1;
}
skipspace(fp);
if ((c = fgetc(fp)) == ':') {
int tmp = 0;
while (c = fgetc(fp), c != EOF) {
if (isdigit(c)) {
tmp = tmp * 10 + (c - '0');
} else {
break;
}
}
if (tmp > 0) {
tch->step *= tmp;
}
}
do {
if (c == ']') {
break;
}
} while (c = fgetc(fp), c != EOF);
if (c == EOF) {
return FALSE;
}
strcpy(tch->filename, str);
if (tchinsert(tch, point) == FALSE) {
return FALSE;
}
}
return TRUE;
}
int mergetimechart(char *fname, int point)
{
FILE *fp;
int defwait = 1, defspeed = 2;
int i;
TimeChart tch;
char str[32];
int c;
char *p, name[128];
strcpy(tchdir, fname);
if ((p = strrchr(tchdir, '\\')) != NULL) {
p[1] = '\0';
} else if (tchdir[0] && tchdir[1] == ':') {
tchdir[2] = '\0';
} else {
tchdir[0] = '\0';
}
strcpy(name, fname);
addtch(name);
if (fp = fopen(name, "rt"), fp == NULL) {
printf("cannot open %s\n", name);
return FALSE;
}
while (c = gettoken(str, fp), c != EOF) {
if (str[0] == '.' && isalpha(str[1])) {
if (strcmpi(str, ".timechart") == 0) {
} else if (strcmpi(str, ".endchart") == 0) {
break;
} else if (strcmpi(str, ".voice") == 0
|| ( tolower(str[1]) == 'v' && isdigit(str[2]))) {
if ((c = gettoken(str, fp)) != EOF) {
tch.mode = PCM;
tch.wait = defwait;
tch.speed = defspeed;
switch(str[1]) {
case '0':
tch.pcmoutputmode = 0;
break;
case '1':
tch.pcmoutputmode = 1;
break;
case '2':
tch.pcmoutputmode = 2;
break;
case '3':
tch.pcmoutputmode = 3;
break;
default :
tch.pcmoutputmode = 4;
}
strcpy(tch.filename, str);
if (tchinsert(&tch, point++) == FALSE) {
break;
}
}
} else if (strcmpi(str, ".rep") == 0) {
if (gettoken(str, fp) != EOF && (c = atoi(str)) != 0) {
tch.mode = REP;
tch.speed = defspeed;
tch.wait = defwait;
tch.repeatcount = c;
if (tchinsert(&tch, point++) == FALSE) {
break;
}
}
} else if (strcmpi(str, ".endrep") == 0) {
tch.mode = ENDREP;
tch.speed = defspeed;
tch.wait = defwait;
if (tchinsert(&tch, point++) == FALSE) {
break;
}
} else if (strcmpi(str, ".opm") == 0) {
if ((c = gettoken(str, fp)) != EOF) {
}
} else if (strcmpi(str, ".wait") == 0) {
if ((c = gettoken(str, fp)) != EOF && (i = atoi(str)) != 0) {
tch.wait = defwait = i;
tch.speed = defspeed;
}
} else if (strcmpi(str, ".speed") == 0) {
if ((c = gettoken(str, fp)) != EOF && (i = atoi(str)) != 0) {
tch.speed = defspeed = i;
tch.wait = defwait;
}
}
} else {
tch.mode = PIC;
tch.speed = defspeed;
tch.wait = defwait;
tch.step = 1;
insertpic(&tch, str, point++, fp);
}
}
fclose(fp);
return TRUE;
}
int loadtimechart(char *name)
{
int retcode;
if (timechart == NULL) {
timechart = malloc(sizeof(TimeChart) * TIMECHARTMAX);
}
timecharts = 0;
retcode = mergetimechart(name, 0);
return retcode;
}
int savetimechart(char *fname)
{
int defwait = 1, defspeed = 3;
int i;
FILE *fp;
char name[128];
strcpy(name, fname);
addtch(name);
if (fp = fopen(name, "wt"), fp == NULL) {
printf("cannot open %s\n", name);
return FALSE;
}
fprintf(fp, ".timechart\n");
for (i = 0; i < timecharts; ++i) {
if (timechart[i].speed != defspeed) {
fprintf(fp, ".speed %d\n", defspeed = timechart[i].speed);
}
if (timechart[i].wait != defwait) {
fprintf(fp, ".wait %d\n", defwait= timechart[i].wait);
}
switch (timechart[i].mode) {
case PCM :
fprintf(fp, ".v%1d %s\n", timechart[i].pcmoutputmode, timechart[i].filename);
break;
case OPM :
break;
case PIC :
if (timechart[i].startframe == 0) {
fprintf(fp, "\t%s\n", timechart[i].filename);
} else if (timechart[i].startframe == timechart[i].endframe) {
fprintf(fp, "\t%s%03d\n", timechart[i].filename, timechart[i].startframe);
} else {
fprintf(fp, "\t%s[%d-%d]\n", timechart[i].filename,
timechart[i].startframe, timechart[i].endframe);
}
break;
case REP :
fprintf(fp, "rep %d\n", timechart[i].repeatcount);
break;
case ENDREP :
fprintf(fp, "endrep\n");
break;
}
}
if (defwait != 1) {
fprintf(fp, ".wait %d\n", 1);
}
if (defspeed != 3) {
fprintf(fp, ".speed %d\n", 3);
}
fprintf(fp, ".endchart\n");
fclose(fp);
return TRUE;
}