source: titan/titan/sat.h @ 40904

Last change on this file since 40904 was 24223, checked in by nit, 10 years ago

[titan] add flag to copy only provider/sat with channel

File size: 9.8 KB
Line 
1#ifndef SAT_H
2#define SAT_H
3
4void resetsatscan()
5{
6        struct sat *node = sat;
7
8        while(node != NULL)
9        {
10                node->scan = 0;
11                node = node->next;
12        }
13}
14
15struct sat* getsatbyorbitalpos(int orbitalpos)
16{
17        struct sat *node = sat;
18
19        while(node != NULL)
20        {
21                if(node->orbitalpos == orbitalpos)
22                        return node;
23
24                node = node->next;
25        }
26        debug(100, "sat not found (%d)", orbitalpos);
27        return NULL;
28}
29
30char* getorbitalposstring(char* feshortname, int fetype)
31{
32        int i = 0, orbitalpos = 0;
33        struct sat* node = sat;
34        char* str = NULL, *tmp = NULL, *tmpstr = NULL, *tmpnr = NULL;
35
36        if(feshortname != NULL)
37        {
38                tmpstr = ostrcat(feshortname, "_sat", 0, 0);
39                for(i = 1; i <= getmaxsat(feshortname); i++)
40                {
41                        tmpnr = oitoa(i);
42                        orbitalpos = getconfigint(tmpstr, tmpnr);
43                        free(tmpnr); tmpnr = NULL;
44
45                        node = getsatbyorbitalpos(orbitalpos);
46                        if(node != NULL)
47                        {
48                                tmp = oitoa(node->orbitalpos);
49                                str = ostrcat(str, tmp, 1, 1);
50                                str = ostrcat(str, "\n", 1, 0);
51                        }
52                }
53                free(tmpstr); tmpstr = NULL;
54
55        }
56        else
57        {
58                while(node != NULL)
59                {
60                        if(node->fetype == fetype)
61                        {
62                                tmp = oitoa(node->orbitalpos);
63                                str = ostrcat(str, tmp, 1, 1);
64                                str = ostrcat(str, "\n", 1, 0);
65                        }
66                        node = node->next;
67                }
68        }
69
70        return str;
71}
72
73char* getsatstring(char* feshortname, int fetype)
74{
75        int i = 0, orbitalpos = 0;
76        struct sat* node = sat;
77        char* str = NULL, *tmpstr = NULL, *tmpnr = NULL;
78
79        if(feshortname != NULL)
80        {
81                tmpstr = ostrcat(feshortname, "_sat", 0, 0);
82                for(i = 1; i <= getmaxsat(feshortname); i++)
83                {
84                        tmpnr = oitoa(i);
85                        orbitalpos = getconfigint(tmpstr, tmpnr);
86                        free(tmpnr); tmpnr = NULL;
87
88                        node = getsatbyorbitalpos(orbitalpos);
89                        if(node != NULL)
90                        {
91                                str = ostrcat(str, node->name, 1, 0);
92                                str = ostrcat(str, "\n", 1, 0);
93                        }
94                }
95                free(tmpstr); tmpstr = NULL;
96
97        }
98        else
99        {
100                while(node != NULL)
101                {
102                        if(node->fetype == fetype)
103                        {
104                                str = ostrcat(str, node->name, 1, 0);
105                                str = ostrcat(str, "\n", 1, 0);
106                        }
107                        node = node->next;
108                }
109        }
110
111        return str;
112}
113
114struct sat* getlastsat(struct sat* node)
115{
116        struct sat *prev = NULL;
117
118        while(node != NULL || sat == NULL)
119        {
120                prev = node;
121                node = node->next;
122        }
123
124        return prev;
125}
126
127int movesatblockdown(struct sat* node)
128{
129        int i = 0, ret = 0;
130        struct sat* prev = NULL;
131
132        if(node == NULL)
133        {
134                err("NULL detect");
135                return 1;
136        }
137
138        for(i = 0; i < status.moveblockcount; i++)
139        {
140                if(node == NULL) break;
141                node = node->next;
142        }
143
144        for(i = 0; i < status.moveblockcount + 1; i++)
145        {
146                if(node == NULL) break;
147                prev = node->prev;
148                ret = movesatdown(node);
149                node = prev;
150        }
151
152        return ret;
153}
154
155int movesatdown(struct sat* node)
156{
157        struct sat* prev = NULL, *next = NULL;
158
159        if(node == NULL || sat == NULL)
160        {
161                err("NULL detect");
162                return 1;
163        }
164       
165        //only one node
166        if(node->prev == NULL && node->next == NULL)
167                return 0;
168
169        //last node
170        if(node->next == NULL)
171        {
172                if(node->prev != NULL)
173                        node->prev->next = NULL;
174                node->prev = NULL;
175                node->next = sat;
176                sat->prev = node;
177                sat = node;
178                return 99;
179        }
180
181        //haenge node aus
182        if(node->prev != NULL)
183                node->prev->next = node->next;
184        else
185                sat = node->next;
186        node->next->prev = node->prev;
187
188        //save nodes next and prev
189        next = node->next;
190        prev = node->prev;
191
192        //haenge es eine pos nacher ein
193        node->next = next->next;
194        node->prev = next;
195       
196        if(next->next != NULL)
197                next->next->prev = node;
198        next->next = node;
199
200        status.writesat = 1;
201        return 0;
202}
203
204int movesatblockup(struct sat* node)
205{
206        int i = 0, ret = 0;
207        struct sat* next = NULL;
208
209        if(node == NULL)
210        {
211                err("NULL detect");
212                return 1;
213        }
214
215        for(i = 0; i < status.moveblockcount + 1; i++)
216        {
217                if(node == NULL) break;
218                next = node->next;
219                ret = movesatup(node);
220                node = next;
221        }
222
223        return ret;
224}
225
226int movesatup(struct sat* node)
227{
228        struct sat* prev = NULL, *next = NULL, *last = NULL;
229
230        if(node == NULL)
231        {
232                err("NULL detect");
233                return 1;
234        }
235       
236        //only one node
237        if(node->prev == NULL && node->next == NULL)
238                return 0;
239
240        //first node
241        if(node->prev == NULL)
242        {
243                last = getlastsat(sat);
244
245                if(node->next != NULL)
246                        node->next->prev = NULL;
247                sat = node->next;
248                node->next = NULL;
249                last->next = node;
250                node->prev = last;
251                return 99;
252        }
253
254        //haenge node aus
255        node->prev->next = node->next;
256        if(node->next != NULL)
257                node->next->prev = node->prev;
258
259        //save nodes next and prev
260        next = node->next;
261        prev = node->prev;
262
263        //haenge es eine pos voher ein
264        node->next = prev;
265        node->prev = prev->prev;
266       
267        if(prev->prev != NULL)
268                prev->prev->next = node;
269        else
270                sat = node;
271        prev->prev = node;
272
273        status.writesat = 1;
274        return 0;
275}
276
277struct sat* addsat(char *line, int count, struct sat* last)
278{
279        struct sat *newnode = NULL, *prev = NULL, *node = sat;
280        char *name = NULL;
281        int ret = 0;
282
283        newnode = (struct sat*)calloc(1, sizeof(struct sat));
284        if(newnode == NULL)
285        {
286                err("no memory");
287                return NULL;
288        }
289
290        name = malloc(MINMALLOC);
291        if(name == NULL)
292        {
293                err("no memory");
294                free(newnode);
295                return NULL;
296        }
297
298        status.writesat = 1;
299
300        ret = sscanf(line, "%[^#]#%d#%d#%d", name, &newnode->flag, &newnode->orbitalpos, &newnode->fetype);
301        if(ret != 4)
302        {
303                if(count > 0)
304                {
305                        err("satlist line %d not ok", count);
306                }
307                else
308                {
309                        err("add sat");
310                }
311                free(name);
312                free(newnode);
313                return NULL;
314        }
315
316        newnode->name = ostrshrink(name);
317
318        if(last == NULL)
319        {
320                while(node != NULL && strcoll(newnode->name, node->name) > 0)
321                {
322                        prev = node;
323                        node = node->next;
324                }
325        }
326        else
327        {
328                prev = last;
329                node = last->next;
330        }
331
332        if(prev == NULL)
333                sat = newnode;
334        else
335        {
336                prev->next = newnode;
337                newnode->prev = prev;
338        }
339        newnode->next = node;
340        if(node != NULL) node->prev = newnode;
341
342        return newnode;
343}
344
345int readsat(const char* filename)
346{
347        FILE *fd = NULL;
348        char *fileline = NULL, *tmpstr = NULL, *tmpstr0 = NULL, *tmpstr1 = NULL;
349        int linecount = 0, treffer0 = 1, treffer1 = 1, len = 0;
350        struct sat* last = NULL, *tmplast = NULL;
351       
352        tmpstr0 = getconfig("channellist", NULL);
353        tmpstr1 = getconfig("rchannellist", NULL);
354        if(ostrncmp("(SAT)-", tmpstr0, 6) == 0) treffer0 = 0;
355        if(ostrncmp("(SAT)-", tmpstr1, 6) == 0) treffer1 = 0;
356
357        fileline = malloc(MINMALLOC);
358        if(fileline == NULL)
359        {
360                err("no memory");
361                return 1;
362        }
363
364        fd = fopen(filename, "r");
365        if(fd == NULL)
366        {
367                perr("can't open %s", filename);
368                free(fileline);
369                return 1;
370        }
371
372        while(fgets(fileline, MINMALLOC, fd) != NULL)
373        {
374                if(fileline[0] == '#' || fileline[0] == '\n')
375                        continue;
376                len = strlen(fileline) - 1;
377                if(len >= 0 && fileline[len] == '\n')
378                        fileline[len] = '\0';
379                len--;
380                if(len >= 0 && fileline[len] == '\r')
381                        fileline[len] = '\0';
382
383                linecount++;
384
385                if(last == NULL) last = tmplast;
386                last = addsat(fileline, linecount, last);
387                if(last != NULL)
388                {
389                        tmplast = last;
390                        tmpstr = ostrcat("(SAT)-", last->name, 0, 0);
391
392                        if(treffer0 == 0 && ostrcmp(tmpstr, tmpstr0) == 0)
393                                treffer0 = 1;
394                        if(treffer1 == 0 && ostrcmp(tmpstr, tmpstr1) == 0)
395                                treffer1 = 1;
396                        free(tmpstr); tmpstr = NULL;
397                }
398        }
399       
400        if(treffer0 == 0) delconfig("channellist");
401        if(treffer1 == 0) delconfig("rchannellist");
402
403        status.writesat = 0;
404        free(fileline);
405        fclose(fd);
406        return 0;
407}
408
409void delsat(char *name)
410{
411        struct sat *node = sat, *prev = sat;
412
413        while(node != NULL)
414        {
415                if(ostrcmp(node->name, name) == 0)
416                {
417                        status.writesat = 1;
418                        if(node == sat)
419                        {
420                                sat = node->next;
421                                if(sat != NULL)
422                                        sat->prev = NULL;
423                        }
424                        else
425                        {
426                                prev->next = node->next;
427                                if(node->next != NULL)
428                                        node->next->prev = prev;       
429                        }
430
431                        deltransponderbyorbitalpos(node->orbitalpos);
432
433                        free(node->name);
434                        node->name = NULL;
435
436                        free(node);
437                        node = NULL;
438                        break;
439                }
440
441                prev = node;
442                node = node->next;
443        }
444}
445
446struct sat* getsat(char *name)
447{
448        struct sat *node = sat;
449
450        while(node != NULL)
451        {
452                if(ostrstr(node->name, name) != NULL)
453                        return node;
454
455                node = node->next;
456        }
457        debug(100, "sat not found (%s)", name);
458        return NULL;
459}
460
461void freesat()
462{
463        struct sat *node = sat, *prev = sat;
464
465        while(node != NULL)
466        {
467                prev = node;
468                node = node->next;
469                if(prev != NULL)
470                        delsat(prev->name);
471        }
472}
473
474//flag 0: add all provider
475//flag 1: add only provider with channel
476int sat2bouquet(int orbitalpos, int flag)
477{
478        int treffer = 0;
479        struct sat* snode = NULL;
480        struct mainbouquet* mnode = NULL;
481        struct channel* chnode = channel;
482        char* tmpstr = NULL;
483        char* path = NULL;
484       
485        snode = getsatbyorbitalpos(orbitalpos);
486        if(snode == NULL) return 1;
487
488        tmpstr = ostrcat(tmpstr, snode->name, 1, 0);
489        tmpstr = ostrcat(tmpstr, "#", 1, 0);
490        tmpstr = ostrcat(tmpstr, oitoa(status.servicetype), 1, 1);
491        tmpstr = ostrcat(tmpstr, "#", 1, 0);
492
493        path = realpath(getconfig("bouquetfile", NULL), NULL);
494        if(path != NULL)
495        {
496                path = dirname(path);
497                path = ostrcat(path, "/bouquets.", 1, 0);
498                                       
499                path = ostrcat(path, snode->name, 1, 0);
500                if(status.servicetype == 0) path = ostrcat(path, "_tv", 1, 0);
501                if(status.servicetype == 1) path = ostrcat(path, "_radio", 1, 0);
502
503                if(file_exist(path))
504                {
505                        free(tmpstr); tmpstr = NULL;
506                        free(path); path = NULL;
507                        return 1;
508                }
509
510                tmpstr = ostrcat(tmpstr, path, 1, 0);
511                mnode = addmainbouquet(tmpstr, 1, NULL);
512        }
513        free(tmpstr); tmpstr = NULL;
514
515        if(mnode != NULL)
516        {
517                while(chnode != NULL)
518                {
519                        if(chnode->transponder != NULL && chnode->transponder->orbitalpos == orbitalpos && chnode->servicetype == status.servicetype)
520                        {
521                                tmpstr = ostrcat(tmpstr, oitoa(chnode->serviceid), 1, 1);
522                                tmpstr = ostrcat(tmpstr, "#", 1, 0);
523                                tmpstr = ostrcat(tmpstr, ollutoa(chnode->transponderid), 1, 1);
524                                treffer = 1;
525                                addbouquet(&mnode->bouquet, tmpstr, status.servicetype, 1, NULL);
526                                free(tmpstr); tmpstr = NULL;
527                        }
528                        chnode = chnode->next;
529                }
530        }
531       
532        if(treffer == 0 && flag == 1)
533                delmainbouquet(path, 1);
534
535  free(path); path = NULL;
536        return 0;
537}
538
539int writesat(const char *filename)
540{
541        FILE *fd = NULL;
542        struct sat *node = sat;
543        int ret = 0;
544
545        fd = fopen(filename, "w");
546        if(fd == NULL)
547        {
548                perr("can't open %s", filename);
549                return 1;
550        }
551
552        while(node != NULL)
553        {
554                ret = fprintf(fd, "%s#%d#%d#%d\n", node->name, node->flag, node->orbitalpos, node->fetype);
555
556                if(ret < 0)
557                {
558                        perr("writting file %s", filename);
559                }
560                node = node->next;
561        }
562
563        fclose(fd);
564        return 0;
565}
566
567#endif
Note: See TracBrowser for help on using the repository browser.