source: titan/titan/channelslot.h @ 26727

Last change on this file since 26727 was 23206, checked in by nit, 11 years ago

[titan] revert

File size: 3.1 KB
Line 
1#ifndef CHANNELSLOT_H
2#define CHANNELSLOT_H
3
4struct channelslot* addchannelslot(char *line, int count, struct channelslot* last)
5{
6        struct channelslot *newnode = NULL, *prev = NULL, *node = channelslot;
7        int ret = 0;
8
9        newnode = (struct channelslot*)calloc(1, sizeof(struct channelslot));
10        if(newnode == NULL)
11        {
12                err("no memory");
13                return NULL;
14        }
15
16        ret = sscanf(line, "%llu#%d#%d", &newnode->transponderid, &newnode->serviceid, &newnode->slot);
17        if(ret != 3)
18        {
19                if(count > 0)
20                {
21                        err("channelslot line %d not ok", count);
22                }
23                else
24                {
25                        err("add channelslot");
26                }
27                free(newnode);
28                return NULL;
29        }
30
31        status.writechannelslot = 1;
32
33        if(last == NULL)
34        {
35                while(node != NULL)
36                {
37                        prev = node;
38                        node = node->next;
39                }
40        }
41        else
42        {
43                prev = last;
44                node = last->next;
45        }
46
47        if(prev == NULL)
48                channelslot = newnode;
49        else
50                prev->next = newnode;
51
52        newnode->next = node;
53
54        return newnode;
55}
56
57struct channelslot* getchannelslot(int serviceid, uint64_t transponderid)
58{
59        struct channelslot *node = channelslot;
60       
61        while(node != NULL)
62        {
63                if(node->serviceid == serviceid && node->transponderid == transponderid)
64                        return node;
65                node = node->next;     
66        }
67       
68        return NULL;
69}
70
71int readchannelslot(char* filename)
72{
73        FILE *fd = NULL;
74        char* fileline = NULL;
75        int linecount = 0, len = 0;
76        struct channelslot* last = NULL, *tmplast = NULL;
77       
78        fileline = malloc(MINMALLOC);
79        if(fileline == NULL)
80        {
81                err("no memory");
82                return 1;
83        }
84
85        fd = fopen(filename, "r");
86        if(fd == NULL)
87        {
88                perr("can't open %s", filename);
89                free(fileline);
90                return 1;
91        }
92
93        while(fgets(fileline, MINMALLOC, fd) != NULL)
94        {
95                if(fileline[0] == '#' || fileline[0] == '\n')
96                        continue;
97                len = strlen(fileline) - 1;
98                if(len >= 0 && fileline[len] == '\n')
99                        fileline[len] = '\0';
100                len--;
101                if(len >= 0 && fileline[len] == '\r')
102                        fileline[len] = '\0';
103
104                linecount++;
105
106                if(last == NULL) last = tmplast;
107                last = addchannelslot(fileline, linecount, last);
108                if(last != NULL) tmplast = last;
109        }
110
111  status.writechannelslot = 0;
112        free(fileline);
113        fclose(fd);
114        return 0;
115}
116
117int delchannelslot(int serviceid, uint64_t transponderid)
118{
119        int ret = 1;
120        struct channelslot *node = channelslot, *prev = channelslot;
121
122        while(node != NULL)
123        {
124                if(node->serviceid == serviceid && node->transponderid == transponderid)
125                {
126                        status.writechannelslot = 1;
127                        if(node == channelslot)
128                                channelslot = node->next;
129                        else
130                                prev->next = node->next;
131
132                        free(node);
133                        node = NULL;
134                        ret = 0;
135                        break;
136                }
137
138                prev = node;
139                node = node->next;
140        }
141        return ret;
142}
143
144void freechannelslot()
145{
146        struct channelslot *node = channelslot, *prev = channelslot;
147
148        while(node != NULL)
149        {
150                prev = node;
151                node = node->next;
152                if(prev != NULL)
153                        delchannelslot(prev->serviceid, prev->transponderid);
154        }
155}
156
157int writechannelslot(char *filename)
158{
159        FILE *fd = NULL;
160        struct channelslot *node = channelslot;
161        int ret = 0;
162
163        fd = fopen(filename, "w");
164        if(fd == NULL)
165        {
166                perr("can't open %s", filename);
167                return 1;
168        }
169
170        while(node != NULL)
171        {
172                ret = fprintf(fd, "%llu#%d#%d\n", node->transponderid, node->serviceid, node->slot);
173                if(ret < 0)
174                {
175                        perr("writting file %s", filename);
176                }
177                node = node->next;
178        }
179
180        fclose(fd);
181        return 0;
182}
183
184#endif
Note: See TracBrowser for help on using the repository browser.