Hi,
wollte mir für mein Webinterface einen Parser schreiben der die ganzen EPG-Informationen verarbeitet und eine Timeline (html) generiert. Das ganze war mir in PHP aber manchmal viel zu langsam deshalb hab ichs in C geschrieben und habe aber nun ein Problem bei der Konvertierung der Zeit
Eine printf-Ausgabe (Zeile 98 ) der Variablen die zur Timekonvertierung verwendet werden:
start: 1296248400, end: 1296255600, sStart: 22:00, sEnd: 22:00
warum steht in sStart und sEnd das selbe drinnen?
in tmStart und tmEnd steht auch das gleiche!
der Code:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
typedef int bool;
#define FALSE (0)
#define TRUE (1)
typedef struct TIME_container {
char* duration;
char* start;
} TIME;
void printChannel(char* buffer)
{
int count = 1;
int length = strlen(buffer);
while(buffer[count] != ' '){ count++; }
count++;
while(buffer[count] != ' '){ count++; }
count++;
char* channel = strndup(buffer + count, length - count - 1);
printf("<div class=\"epgchannel\">%s</div>\n", channel);
}
void parseTime(char* buffer, TIME* time)
{
int counter_start = 6;
int counter_middle = 7;
int counter_end;
while(buffer[counter_start] != ' ') { counter_start++; }
counter_start++;
counter_middle = counter_start;
while(buffer[counter_middle] != ' ') { counter_middle++; }
counter_middle++;
counter_end = counter_middle;
while(buffer[counter_end] != ' ') { counter_end++; }
char* start = strndup(buffer + counter_start, counter_middle - counter_start - 1);
char* duration = strndup(buffer + counter_middle, counter_end - counter_middle);
//printf("s: _%s_, d: _%s_, b: %s", start, duration, buffer);
time->duration = duration;
time->start = start;
}
char* parseTitle(char * buffer)
{
int length = strlen(buffer);
char* name = strndup(buffer + 6, length - 7 );
return name;
}
void printProgram(char* title, TIME* times, int selectedtime, int mwidth, int* awidth, int buffertime)
{
int start = atoi(times->start);
int duration = atoi(times->duration);
int width = 0;
if( duration + start > selectedtime && (*awidth) > 0)
{
if(start < selectedtime)
{
duration = (start + duration) - selectedtime;
}
width = duration/(buffertime/100) * duration/100;
//DEBUG:
//printf("w: %i, d: %i, b: %i, s: %i, a: %i\n", width, duration, buffertime, start, *awidth);
if(width > *awidth) { width = *awidth; }
*awidth = *awidth - width;
if(*awidth < 10) { width+= *awidth - 1; *awidth = 0; } else { width -= 2; }
if(width > 0){
time_t timeStart = (time_t)start;
int end = start + duration;
time_t timeEnd = (time_t)end;
struct tm *tmStart;
struct tm *tmEnd;
tmEnd = localtime ( &timeEnd );
tmStart = localtime ( &timeStart );
char* sStart = malloc(80 * sizeof(char));
char* sEnd = malloc(80 * sizeof(char));
strftime(sStart, sizeof(sStart)*2, "%H:%M", tmStart);
strftime(sEnd, sizeof(sEnd)*2, "%H:%M", tmEnd);
//DEBUG:
printf("start: %i, end: %i, sStart: %s, sEnd: %s\n", start, end, sStart, sEnd);
printf("<div class=\"epgentry\" style=\"width:%ipx;\" title=\"%s, %s - %s\">%s</div>\n", width, title, sStart, sEnd, title);
free(sStart);
free(sEnd);
}
}
}
int main(int argc, char* argv[])
{
FILE *in;
char *buffer = malloc( 4096 * sizeof ( char ) );
TIME *time = NULL;
int maxwidth = 780; //total width available for programs in one epg-line
int availablewidth = maxwidth; //variable to store the currrently available width in the epg line
int buffertime = 60 * 60 * 1; //one hour in seconds
int selectedtime = 0;
bool first = TRUE;
if(argc < 2)
{
printf("First argument should contain the selected time!\n");
return 1;
}
selectedtime = atoi(argv[1]);
system("sudo svdrpsend LSTE > /tmp/webui_epg.tmp");
in = fopen("/tmp/webui_epg.tmp", "r");
while( fgets(buffer, 4096, in) != NULL )
{
if( buffer[2] == '5' && buffer[3] == '-' )
{
switch(buffer[4])
{
case 'C': if(first == TRUE){ first = FALSE; } else { printf("</div><div class=\"clearfloating\"></div>\n"); }
printf("<div class=\"epgline\">");
printChannel(buffer);
availablewidth = maxwidth;
break;
case 'E': if(time != NULL) { free(time); }
time = malloc(sizeof(TIME));
parseTime(buffer, time);
break;
case 'T': printProgram(parseTitle(buffer), time, selectedtime, maxwidth, &availablewidth, buffertime);
break;
}
}
}
printf("</div>\n");
fclose(in);
//cleanup
if(time != NULL) { free(time); }
system("rm /tmp/webui_epg.tmp");
return 0;
}
Alles anzeigen
muss allerdings dazu sagen: ich bin kein erfahrener C-Programmierer also kann auch an was ganz simplen liegen...
thanks and best regards
aelo