Code:
#include <stdio.h>
#include <string.h>
#define DEBUG 0
typedef struct
{
int year;
int month;
int day;
int start_hour;
int start_min;
int ampm;
int weekday;
int duration;
int channel;
} info_t;
typedef struct
{
char *name;
int ch;
} ch_t;
ch_t ch_list[]=
{
{"NHK総合", 1},
{"NHK教育", 3},
{"日本テレビ", 14},
{"TBS", 16},
{"フジテレビ", 18},
{"テレビ朝日", 20},
{"テレビ東京", 22},
{"MXテレビ", 5},
{"放送大学", 67},
{"NHK衛星第一", 8},
{"NHK衛星第二", 9}
};
#define MAX 100
// years ending with 00 have to be divisible by 400 to leap
// note the "&&" is a DaniWeb problem and should be a double & for AND
#define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
//
// return 1 if date is valid, 0 otherwise.
//
int isdatevalid(int month, int day, int year)
{
if (day <= 0) return 0 ;
switch( month )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
if (day > 31) return 0 ; else return 1 ;
case 4 :
case 6 :
case 9 :
case 11 :
if (day > 30) return 0 ; else return 1 ;
case 2 :
if ( day > 29 ) return 0 ;
if ( day < 29 ) return 1 ;
if (isleapyear(year)) return 1 ; // leap year
else return 0 ;
}
return 0 ;
}
//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// tested for 1901 to 2099 (seems to work from 1800 on too)
//
int weekday(int month, int day, int year)
{
int ix, tx, vx;
switch (month) {
case 2 :
case 6 :
vx = 0; break;
case 8 :
vx = 4; break;
case 10 :
vx = 8; break;
case 9 :
case 12 :
vx = 12; break;
case 3 :
case 11 :
vx = 16; break;
case 1 :
case 5 :
vx = 20; break;
case 4 :
case 7 :
vx = 24; break;
}
if (year > 1900) // 1900 was not a leap year
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
int parse_tvpi( FILE *fi, char *buf, info_t *info )
{
char work[50];
int i, ret = 0;
int pos, start, end;
char *pos_ptr;
while( fgets(buf, MAX, fi) != NULL )
{
if( (pos_ptr = strchr( buf, (int)':')) == NULL )
continue;
pos = (int)(pos_ptr - buf);
for( i = 0; i < pos; i++ )
work[i] = buf[i];
work[i] = 0;
if( strcmp( work, "year") == 0 )
info->year = atoi( buf + pos + 1 );
else if( strcmp( work, "date") == 0 )
info->day = atoi( buf + pos + 1 );
else if( strcmp( work, "month") == 0 )
info->month = atoi( buf + pos + 1 );
else if( strcmp( work, "start") == 0 )
{
info->start_hour = atoi( buf + pos + 2 );
info->start_min = atoi( buf + pos + 5 );
start = info->start_hour * 60 + info->start_min;
if( info->start_hour > 11 )
info->ampm = 1;
else
info->ampm = 0;
if( info->start_hour > 12 )
info->start_hour -= 12;
}
else if( strcmp( work, "end") == 0 )
{
end = atoi( buf + pos + 2 ) * 60 + atoi( buf + pos + 5 );
}
else if( strcmp( work, "station") == 0 )
{
for( i = 0; i < sizeof(ch_list)/sizeof(ch_t); i++ )
{
if( strncmp( buf + pos + 2, ch_list[i].name, strlen(ch_list[i].name)) == 0 )
info->channel = ch_list[i].ch;
}
#if !DEBUG
if( info->channel == 0 )
#endif
{
printf("%s\n", buf);
}
}
}
if( info->channel == 0 )
{
ret = -1;
goto bail2;
}
if( isdatevalid( info->month, info->day, info->year ) == 0 )
{
ret = -1;
goto bail2;
}
else
info->weekday = (weekday( info->month, info->day, info->year ) + 1) % 7;
if( end > start )
info->duration = end - start;
else
info->duration = end + 24*60 - start;
bail2:
return ret;
}
int main(int argc, char* argv[])
{
FILE *fi, *fo;
char buf[MAX], saveto[MAX];
int i, val1, val2, val3, ret, found = 0;
info_t info;
for( i = 0; i < sizeof(info_t); i++ )
*((char *)(&info) + i) = 0;
if( (fi = fopen("\\progra~1\\AT-LAR~1\\rconfigs.ini", "r")) == NULL )
{
printf("input file open failed\n");
ret = -1;
goto bail;
}
if( (fo = fopen("\\progra~1\\AT-LAR~1\\rconfigs.tmp", "w")) == NULL )
{
printf("output file open failed\n");
ret = -1;
goto bail;
}
while( fgets(buf, MAX, fi) != NULL )
{
val1 = strncmp( buf, "[", 1 );
val2 = strncmp( buf, "[iEPG]", 6 );
val3 = strncmp( buf, "[LAST_USED", 10 );
if( val2 == 0 )
found = 1;
else if( val3 == 0 )
found = 2;
else if( val1 == 0 )
found = 0;
if( found != 1 )
fputs(buf, fo);
if( (found == 2) && (strncmp(buf, "SaveToDir", 9) == 0) )
{
strncpy( saveto, buf, strlen(buf) );
saveto[strlen(buf)+1] = 0;
}
}
fclose( fi );
fclose( fo );
if( (fi = fopen("\\progra~1\\AT-LAR~1\\rconfigs.tmp", "r")) == NULL )
{
printf("input file open failed\n");
ret = -1;
goto bail;
}
if( (fo = fopen("\\progra~1\\AT-LAR~1\\rconfigs.ini", "w")) == NULL )
{
printf("output file open failed\n");
ret = -1;
goto bail;
}
while( fgets(buf, MAX, fi) != NULL )
fputs(buf, fo);
fclose( fi );
if( (fi = fopen( argv[1], "r")) == NULL )
{
printf("input file open failed\n");
ret = -1;
goto bail;
}
if( (ret = parse_tvpi( fi, buf, &info ) ) != 0 )
{
printf("tvpi parse failed\n");
ret = -1;
goto bail;
}
fprintf( fo, "[iEPG]\n" );
fprintf( fo, saveto );
fprintf( fo, "Time=%02d:%02d %s\n", info.start_hour, info.start_min, (info.ampm == 0)? "AM":"PM");
fprintf( fo, "Channel=%d\n", info.channel);
fprintf( fo, "Day=%d\n", info.weekday);
fprintf( fo, "StartNow=0\n");
fprintf( fo, "Repeat=0\n");
fprintf( fo, "DurationValue=%d\n", info.duration);
fprintf( fo, "RepeatTimes=1\n");
fprintf( fo, "DurationType=0\n");
fprintf( fo, "Use=0\n");
fprintf( fo, "VideoSize=1\n");
fprintf( fo, "UseVideoOutput=0\n");
fprintf( fo, "VideoBitrate=300\n");
fprintf( fo, "FrameRate=30\n");
fprintf( fo, "VideoSmoothness=50\n");
fprintf( fo, "AudioBitrate=32\n");
fprintf( fo, "IFrame=15\n");
fclose( fi );
fclose( fo );
bail:
#if !DEBUG
if( ret != 0 )
#endif
{
printf("Hit ENTER to exit");
gets( buf );
}
return ret;
}
Bookmarks