]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/DmxTools.cpp
mono mixer: use filter_t directly instead of dummy aout_filter_t
[vlc] / modules / video_filter / atmo / DmxTools.cpp
1 /*
2  * DmxTools.cpp: functions to convert , or ; separated numbers into an integer array
3  *
4  * See the README.txt file for copyright information and how to reach the author(s).
5  *
6  * $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 # include "config.h"
11 #endif
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include "DmxTools.h"
17
18 int IsValidDmxStartString(char *startChannels)
19 {
20   if(!startChannels) return -1;
21
22   char channel[16];
23   int tmp;
24   int ch = 0;
25   int i = 0;
26
27   while(*startChannels) {
28       if(*startChannels == ',' || *startChannels == ';') {
29          if(i > 0) {
30             channel[i] = 0;
31             // atoi ?
32             tmp = atoi(channel);
33             if((tmp >= 0) && (tmp<=253))
34                 ch++;
35             else
36                 return -2; // invalid channel number!
37             i = 0;
38          }
39       }
40       else
41       {
42         if((*startChannels >= '0') && (*startChannels <= '9')) {
43             if(i < 3)
44                 channel[i++] = *startChannels;
45             else
46                 return -3; // invalid index length!
47         } else {
48             if(*startChannels != ' ') {
49                 return -4; // invalid character found!
50             }
51         }
52       }
53       startChannels++;
54   }
55
56   // process the rest (or last channel)
57   if(!*startChannels && (i>0)) {
58       channel[i] = 0;
59       tmp = atoi(channel);
60       if((tmp >= 0) && (tmp<=253)) {
61          ch++;
62       } else
63          return -2;
64   }
65
66   return ch;
67 }
68
69 int *ConvertDmxStartChannelsToInt(int numChannels, char *startChannels)
70 {
71   if(!numChannels || !startChannels) return NULL;
72   int *channels = new int[numChannels + 1];
73   // tmp buffer to store channel number!
74   char channel[16];
75   int next_dmx_ch = 0;
76   int tmp;
77   int ch = 0;
78   int i = 0;
79
80   while(*startChannels) {
81       if(*startChannels == ',' || *startChannels == ';') {
82          if(i > 0) {
83             channel[i] = 0;
84             // atoi ?
85             tmp = atoi(channel);
86             if((tmp >= 0) && (tmp<=253)) {
87                 next_dmx_ch = tmp + 3;
88                 channels[ch++] = tmp;
89                 if(ch == numChannels)
90                    break;
91             } else
92                 break;
93             i = 0;
94          }
95       }
96       if((*startChannels >= '0') && (*startChannels <= '9')) {
97          if(i < 3)
98             channel[i++] = *startChannels;
99          else
100             break;
101       }
102       startChannels++;
103   }
104
105   if(!*startChannels && (i>0)) {
106       channel[i] = 0;
107       tmp = atoi(channel);
108       if((tmp >= 0) && (tmp<=253)) {
109          next_dmx_ch = tmp + 3;
110          channels[ch++] = tmp;
111       }
112   }
113   //
114   // fillup the array with the logical next dmx channels - for simple devices that should work!
115   //
116   while(ch < numChannels) {
117         if(next_dmx_ch>253) next_dmx_ch=0; // wrap arround :) better than indexing memory out of range
118         channels[ch++] = next_dmx_ch;
119         next_dmx_ch += 3;
120   }
121   channels[ch++] = -1; // last Entry :)
122
123   return channels;
124 }
125
126 char *ConvertDmxStartChannelsToString(int numChannels, int *startChannels)
127 {
128   // maxBufSize worst case having numChannels 256 each 3 digits Adress and one colon 256*4 bytes + zero byte
129   char tmp[1025];
130   // fuck up! (should not happen)
131   if(numChannels > 256) return NULL;
132
133
134   char *psz_tmp = tmp;
135   for(int i = 0; i < numChannels; i++) {
136       if(startChannels[i] == -1) break;
137       if(i > 0) {
138          *psz_tmp = ',';
139          psz_tmp++;
140          *psz_tmp = 0;
141       }
142       int n = sprintf(psz_tmp, "%d", startChannels[i] );
143       if(n > 0)
144          psz_tmp += n;
145   }
146
147   return strdup(tmp);
148 }
149