Commit c5163d26 authored by Romain Bignon's avatar Romain Bignon

store data in a linked list

parent 06018f2e
FILES=main.o
all: $(FILES)
gcc -o irssleep -Wall -Wextra $(FILES)
clean:
rm -rf *.o
all:
gcc -o irssleep -Wall -Wextra -ggdb main.c
......@@ -62,12 +62,12 @@ int str2mon(const char *str)
#define TS_START 1
#define TS_END 0
static int write_ts(int fd, int start, time_t ts)
static int write_ts(int fd, time_t start, time_t end)
{
int ret = 0;
ret |= write(fd, &start, sizeof(start));
ret |= write(fd, &ts, sizeof(ts));
return ret;
static time_t ts[2];
ts[0] = start;
ts[1] = end;
return write(fd, &ts, sizeof(ts));
}
static int parse_file(int fd, const char **nicknames, const char *filename)
......@@ -76,7 +76,7 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
size_t len = 0;
ssize_t read;
FILE* fp = fopen(filename, "r");
time_t last = 0, current = 0;
time_t last = 0, current = 0, start = 0;
struct tm lt;
if (!fp)
......@@ -89,18 +89,18 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
{
if (line[0] == '-')
{
char *start = NULL, *tmp;
char *p = NULL, *tmp;
if (!strncmp(line, "--- Log ", 8))
{
/* --- Log opened DDD MMM dd hh:mm:ss yyyy
* --- Log closed DDD MMM dd hh:mm:ss yyyy
*/
start = strchr(line + 15, ' ') + 1; /* don't care about the day of the week */
p = strchr(line + 15, ' ') + 1; /* don't care about the day of the week */
}
else if (!strncmp(line, "--- Day changed ", 16))
{
/* --- Day changed DDD MMM dd yyyy */
start = strchr(line + 16, ' ') + 1;
p = strchr(line + 16, ' ') + 1;
}
else
......@@ -108,25 +108,25 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
localtime_r(&current, &lt);
tmp = strchr(start, ' ');
tmp = strchr(p, ' ');
*tmp++ = '\0';
lt.tm_mon = str2mon(start);
lt.tm_mon = str2mon(p);
start = tmp;
lt.tm_mday = atoi(start);
p = tmp;
lt.tm_mday = atoi(p);
start = strchr(start, ' ') + 1;
if (strchr(start, ':'))
p = strchr(p, ' ') + 1;
if (strchr(p, ':'))
{ /* There is a time */
lt.tm_hour = atoi(start);
lt.tm_hour = atoi(p);
start = strchr(start, ':') + 1;
lt.tm_min = atoi(start);
p = strchr(p, ':') + 1;
lt.tm_min = atoi(p);
start = strchr(start, ':') + 1;
lt.tm_sec = atoi(start);
p = strchr(p, ':') + 1;
lt.tm_sec = atoi(p);
start = strchr(start, ' ') + 1;
p = strchr(p, ' ') + 1;
}
else
{
......@@ -135,14 +135,11 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
lt.tm_sec = 0;
}
lt.tm_year = atoi(start) - 1900;
lt.tm_year = atoi(p) - 1900;
current = mktime(&lt);
if (!last)
{
last = current;
write_ts(fd, TS_START, last);
}
start = last = current;
/*
char s[300];
......@@ -156,34 +153,34 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
/* Assume that the line is in form:
* hh:mm [@+<]nick[> ][message]
*/
char *start = strchr(line, ' ');
char *p = strchr(line, ' ');
char *end;
time_t t;
int hh, mm;
/* Get the nickname. */
while (strchr(" %@+<", *start))
++start; /* skip chan status */
while (strchr(" %@+<", *p))
++p; /* skip chan status */
if (strchr(".:-", *start))
if (strchr(".:-", *p))
continue; /* probably a status line */
end = strchr(start, '>');
end = strchr(p, '>');
if (!end)
end = strchr(start, ' ');
end = strchr(p, ' ');
if (!end)
continue;
*end = '\0';
/* Check it's me. */
if (*start != '\0' && !is_in_list(start, nicknames))
if (*p != '\0' && !is_in_list(p, nicknames))
continue;
/* Get timestamp */
start = strchr(line, ':') + 1;
p = strchr(line, ':') + 1;
hh = atoi(line);
mm = atoi(start);
mm = atoi(p);
localtime_r(&current, &lt);
lt.tm_min = mm;
......@@ -192,8 +189,8 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
if (t - last > IDLE_TIME)
{
write_ts(fd, TS_END, last);
write_ts(fd, TS_START, t);
write_ts(fd, start, last);
start = t;
}
current = last = t;
}
......@@ -203,7 +200,7 @@ static int parse_file(int fd, const char **nicknames, const char *filename)
free(line);
if (last)
write_ts(fd, TS_END, last);
write_ts(fd, start, last);
fclose(fp);
return 0;
......@@ -222,8 +219,9 @@ int start_job(const char *filename, const char **nicks, int* hsock, fd_set *fdse
/* child */
close(notify_pipe[0]);
parse_file(notify_pipe[1], nicks, filename);
//printf("Finished %s\n", filename);
exit(0);
printf("Finished %s\n", filename);
close(notify_pipe[1]);
exit(127);
}
printf("Started a new job on %s\n", filename);
close(notify_pipe[1]);
......@@ -250,6 +248,114 @@ void usage(const char* progname, int is_help)
}
}
struct period
{
time_t start;
time_t end;
struct period* prev;
struct period* next;
};
struct data
{
struct period* periods;
};
static struct period* insert_period(struct data* data, struct period* prev, struct period* next, time_t start, time_t end)
{
struct period* p = calloc(sizeof(struct period), 1);
p->start = start;
p->end = end;
p->prev = prev;
p->next = next;
if (next)
next->prev = p;
if (prev)
prev->next = p;
else
data->periods = p;
return p;
}
static void process_data(struct data* data, time_t start, time_t end)
{
struct period* p;
if (!data->periods)
{
insert_period(data, NULL, NULL, start, end);
return;
}
for (p = data->periods; p; p = p->next)
{
if (start <= p->end + IDLE_TIME)
{
/* p->prev p p->next
* +---------+<> <>+-------------+<> <>+-------+
* |<=====================|
* start
*/
if (end < p->start - IDLE_TIME)
{
/* p->prev p
* +-------+<> <>+----------+
* |<====>|
* period
*/
insert_period(data, p->prev, p, start, end);
return;
}
/* p->prev p p->next
* +-------+<> <>+-----------+<> <>+-------+
* |<=============================---
* period
*/
if (start < p->start)
p->start = start;
if (end > p->end)
{
struct period* next;
p->end = end;
for(next = p->next;
next && end >= next->start - IDLE_TIME;
next = p->next)
{
/* p next
* +-----+<> <>+--------+<>
* --=====================>
*/
p->next = next->next;
if (p->next)
p->next->prev = p;
if (next->end > end)
p->end = next->end;
free(next);
}
}
return;
}
/* Last entry */
if (!p->next)
{
/* p
* +--------+<>
* |<=============---
* start
*/
insert_period(data, p, NULL, start, end);
return;
}
}
}
int main(int argc, char **argv)
{
static struct option long_options[] =
......@@ -260,9 +366,10 @@ int main(int argc, char **argv)
};
int option_index = 0, i;
int nprocess = 1;
int hsock;
int hsock = 0;
fd_set fdset;
const char *nicks[] = {"romain", "Progs", NULL};
struct data data;
const char *nicks[] = {"romain", "Progs", "vaginus", NULL};
while((i = getopt_long(argc, argv, "j:h", long_options, &option_index)) != -1)
switch(i)
......@@ -318,16 +425,17 @@ int main(int argc, char **argv)
{
if (FD_ISSET(i, &tmp_fdset))
{
time_t t;
int start;
time_t ts[2];
ssize_t r;
r = read(i, &start, sizeof(start));
if (r > 0)
r = read(i, &t, sizeof(t));
r = read(i, &ts, sizeof(ts));
if (r > 0 && r < (ssize_t)sizeof(ts))
printf("Pas content (%ld < %ld)\n", r, sizeof(ts));
if (r <= 0)
{
if (errno)
fprintf(stderr, "Close[%d/%d]: %s\n", i, hsock, strerror(errno));
close(i);
FD_CLR(i, &fdset);
......@@ -335,7 +443,7 @@ int main(int argc, char **argv)
{
if (start_job(argv[optind++], nicks, &hsock, &fdset) != 0)
{
fprintf(stderr, "Error: %s\n", strerror(errno));
fprintf(stderr, "Error on start_job(): %s\n", strerror(errno));
continue;
}
}
......@@ -345,11 +453,14 @@ int main(int argc, char **argv)
continue;
}
if (start && 0)
printf("%s: %ld\n", start ? "START" : "END", t);
process_data(&data, ts[0], ts[1]);
}
}
}
struct period* p;
for (p = data.periods; p; p = p->next)
printf("From %ld to %ld\n", p->start, p->end);
exit(EXIT_SUCCESS);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment