Initial commit (sort of)
[matches/swarm.git] / src / task.c
1 #include "task.h"
2 #include "log.h"
3
4 static int number = 0; // number of created tasks
5
6 Task * Task_Append(Task * p, char * message, int bufsiz, int repetitions, char * outfile)
7 {
8         Task * t = (Task*)(calloc(1, sizeof(Task)));
9         t->message = message;
10         t->bufsiz = bufsiz;
11         t->outsiz = bufsiz;
12         t->output = (char*)(calloc(t->outsiz, sizeof(char)));
13         t->outlen = 0;
14         t->next = NULL;
15         t->prev = NULL;
16         t->number = number;
17         t->repetitions = repetitions;
18         if (outfile == NULL || outfile[0] == '\0')
19                 t->outfile = NULL;
20         else
21                 t->outfile = strdup(outfile);
22
23
24         if (p != NULL)  
25         {
26                 t->next = p->next;
27                 p->next = t;
28                 t->prev = p;
29                 if (t->next != NULL)
30                         t->next->prev = t;
31         }
32
33         ++number;
34         return t;
35 }
36
37 Task * Task_Prepend(Task * n, char * message, int bufsiz, int repetitions, char * outfile)
38 {
39         Task * t = (Task*)(calloc(1, sizeof(Task)));
40         t->message = message;
41         t->bufsiz = bufsiz;
42         t->outsiz = bufsiz;
43         t->output = (char*)(calloc(t->outsiz, sizeof(char)));
44         t->outlen = 0;
45         t->next = NULL;
46         t->prev = NULL;
47         t->number = number;
48         t->repetitions = repetitions;
49         if (outfile == NULL || outfile[0] == '\0')
50                 t->outfile = NULL;
51         else
52                 t->outfile = strdup(outfile);
53         
54         if (n != NULL)  
55         {
56                 t->prev = n->prev;
57                 n->prev = t;
58                 t->next = n;
59                 if (t->prev != NULL)
60                         t->prev->next = t;
61         }
62         ++number;
63         return t;
64 }
65
66 void Task_Extract(Task * t)
67 {
68         Task * n = t->next;
69         if (n != NULL)
70                 n->prev = t->prev;
71         Task * p = t->prev;
72         if (p != NULL)
73                 p->next = t->next;
74         
75 }
76
77 void Task_Destroy(Task * t)
78 {
79         Task * n = t->next;
80         if (n != NULL)
81                 Task_Destroy(n);
82         free(t->message);
83         free(t->output);
84         free(t);
85 }
86
87 void Task_DebugPrint(Task * t)
88 {
89         if (t == NULL)
90                 log_print(3, "Task_DebugPrint", "no tasks");
91
92         int count = 0;
93         while (t != NULL)
94         {
95                 log_print(3, "Task_DebugPrint", "%d %p <- %p -> %p: %s", count,(void*)(t->prev), (void*)t, (void*)(t->next), t->message);
96                 t = t->next;
97         }
98 }

UCC git Repository :: git.ucc.asn.au