parser.cpp 9.01 KB
Newer Older
1 2 3
/*****************************************************************************
 * parser.cpp : SAP configuration file parser
 ****************************************************************************
4
 * Copyright (C) 1998-2005 VideoLAN
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * $Id$
6
 *
7
 * Authors: Arnaud Schauly <gitan@via.ecp.fr>
8
 *          Clément Stenac <zorglub@via.ecp.fr>
9 10
 *          Damien Lucas <nitrox@videolan.org>
 *          Philippe Van Hecke <philippe.vanhecke@belnet.be>
11
 *          Derk-Jan Hartman <hartman at videolan dot org>
12
 *          Rémi Denis-Courmont <rem at videolan dot org>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

33 34 35 36 37 38 39 40 41 42 43 44
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <vector>

45 46 47 48 49 50 51 52 53 54 55
#ifdef HAVE_ICONV
# include <langinfo.h>
# include <iconv.h>
#else
//typedef unsigned iconv_t;
# define iconv_open( a, b ) ((iconv_t)(-1))
# define iconv_close( a ) ((void)0)
# define iconv( a, b, c, d, e ) ((size_t)(-1))
#endif


56 57 58 59 60 61 62
using namespace std;

#include "sapserver.h"
#include "program.h"
#include "message.h"
#include "parser.h"

63
Config::Config(string f) : daemon(false), dot(false)
64
{
65 66
    file       =    f;
    ttl        =    DEFAULT_TTL;
67
    interface  =    NULL;
68 69 70
    delay      =    DEFAULT_DELAY;
    type       =    TYPE_SAP;
    reverse    =    false;
71 72 73 74
}

Config::~Config()
{
75 76 77 78
    if (interface != NULL)
        free (interface);
    for (int i = Programs.size() - 1; i >= 0; i--)
        delete Programs[i];
79 80 81 82 83 84 85 86 87 88 89 90
}

unsigned int Config::GetTTL()
{
    return ttl;
}

void Config::SetTTL(unsigned int t)
{
    ttl=t;
}

91 92 93 94 95 96 97 98 99 100
unsigned int Config::GetDelay()
{
    return delay;
}

void Config::SetDelay(unsigned int d)
{
    delay=d;
}

101 102 103 104 105
bool Config::GetDaemonMode(void)
{
    return daemon;
}

106 107 108 109 110
bool Config::GetReverse(void)
{
    return reverse;
}

111 112
void Config::SetDaemonMode(bool d)
{
113
    daemon=d;
114 115
}

116 117 118 119 120 121 122
bool Config::GetDotMode(void)
{
    return dot;
}

void Config::SetDotMode(bool d)
{
123
    dot=d;
124 125 126
}


127 128 129 130
void Config::SetFile(char* s)
{
    file=s;
}
131

132 133
void Config::SetReverse(bool d)
{
134
    reverse=d;
135 136
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
137
void Config::SetType(const char *s)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
{
    if(!strcasecmp(s,"slp"))
    {
	type=TYPE_SLP;  
    }
    else
    {
        type=TYPE_SAP;
    }
}

int Config::GetType()
{
    return type;
}

154 155 156 157 158
const char *Config::GetInterface()
{
    return interface;
}

159 160 161
/**********************************************************
 * Gets what is before delim in source. Puts it into dest *
 **********************************************************/
162
void Config::strgetb(const char *source,char *dest,char delim)
163
{
164
    char *ptr = dest;
165

166 167 168 169
    // Cut the line just before the delim char
    while(*source != '\0' && *source != delim)
        *(ptr++) = *(source++);
    *(ptr--) = '\0';
170

171 172 173
    // Remove the trailing blanks
    while((ptr >= dest) && isspace(*ptr))
        *(ptr--) = '\0';
174 175 176
}

/********************************************************** 
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
177
 * Gets what is after delim in source. Puts it into dest * 
178
 **********************************************************/ 
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
179
void Config::strgeta(const char *source,char *dest,char delim)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
    unsigned long i=0,j=0;
    dest[0]=0;
    while(source[i] != delim && i < strlen(source))
    {
        i++;
    }
    i++;
    if(i>=strlen(source))
    {
        return;
    }
    while(i < strlen(source))
    {
        if(source[i]!='\n') dest[j]=source[i];
        else dest[j]=0;
        j++;i++;
    }
    dest[j]=0;
}


/********************************************************************
  Reads the configuration file and fills the program list
*********************************************************************/
int Config::Parse()
{
    FILE *fd;
    char line[1024];   //TODO line length should not be limited
    char tline[1024];
    unsigned int something=0;
    Program *pp= new Program();
212 213 214 215
    iconv_t to_unicode, check_unicode;

    check_unicode = iconv_open("UTF-8", "UTF-8");
    to_unicode = iconv_open("UTF-8", nl_langinfo (CODESET));
216 217 218 219

    fd=fopen(file.c_str(),"r");
    if(!fd)
    {
220
        fprintf(stderr, "- Unable to open %s\n",file.c_str());
221 222 223
        return(-1);
    }

224
    while(fgets(line,1024,fd))
225
    {
226
        if(!strlen(line))
227 228 229 230 231 232
        {
            break;
        }

        strgetb(line,line,'#'); /* Handle the comments */

233
        /* Set SAP TTL  (ttl is a member of config) */
234
        if(strstr(line,"sap_ttl="))
235 236 237 238
        {
            strgeta(line,tline,'=');
            ttl=atoi(tline);
        }
239

240 241 242 243 244 245 246
        /* Set SAP Delay  (delay is a member of config) */
        if(strstr(line,"sap_delay="))
        {
            strgeta(line,tline,'=');
            delay=atoi(tline);
        }

247 248 249 250 251 252 253
        /* Set SAP Interface  (interface is a member of config) */
        if(strstr(line,"interface="))
        {
            strgeta(line,tline,'=');
            interface=strdup(tline);
        }

254
        /*  Beginning of programs parsing */
255 256 257 258 259
        if(strstr(line,"[program]"))
        {
            if(something)
            {
                /* We were in a program with at least one field filled */
260 261 262 263
                if (!pp->GetPort().length())
                {
                    pp->SetPort(DEFAULT_PORT);
                }
264 265 266 267 268 269 270 271
                Programs.push_back(pp);
                pp=new Program();
                something=0;
            }
        }

        if(strstr(line,"name="))
        {
272 273
            ICONV_CONST char *tptr;
            char *uptr, uline[6139];
274 275
            size_t tlen, ulen;

276 277
            strgeta(line,tline,'=');
            something=1;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

            tptr=tline;
            uptr=uline;
            tlen=strlen(tline) + 1;
            ulen=sizeof(uline);
            if(iconv(check_unicode, &tptr, &tlen, &uptr, &ulen) == (size_t)(-1))
            {
                tptr=tline;
                uptr=uline;
                tlen=strlen(tline) + 1;
                ulen=sizeof(uline);
                if(iconv(to_unicode, &tptr, &tlen, &uptr, &ulen) == (size_t)(-1))
                    uptr = tline;
                else
                    uptr = uline;
            }
            else
                uptr = uline;

            pp->SetName(uptr);
298
        }
299

300 301 302 303 304 305
        if(strstr(line,"program_ttl="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetTTL(tline);
        }
306

307 308
        if(strstr(line,"playlist_group="))
        {
309 310
            ICONV_CONST char *tptr;
            char *uptr, uline[6139];
311 312
            size_t tlen, ulen;

313 314
            strgeta(line,tline,'=');
            something=1;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

            tptr=tline;
            uptr=uline;
            tlen=strlen(tline) + 1;
            ulen=sizeof(uline);
            if(iconv(check_unicode, &tptr, &tlen, &uptr, &ulen) == (size_t)(-1))
            {
                tptr=tline;
                uptr=uline;
                tlen=strlen(tline) + 1;
                ulen=sizeof(uline);
                if(iconv(to_unicode, &tptr, &tlen, &uptr, &ulen) == (size_t)(-1))
                    uptr = tline;
                else
                    uptr = uline;
            }
            else
                uptr = uline;

            pp->SetPlGroup(uptr);
335 336 337
            pp->SetHasPlGroup(true);
        }

338 339 340 341 342
        if(strstr(line,"type=") && strstr(line,"rtp") )
        {
            pp->SetRTP(true);
        }

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        if(strstr(line,"user="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetUser(tline);
        }

        if(strstr(line,"machine="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetMachine(tline);
        }

        if(strstr(line,"site="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetSite(tline);
        }

        if(strstr(line,"address="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetAddress(tline);
        }
370 371 372 373 374 375
        if(strstr(line,"port="))
        {
            strgeta(line,tline,'=');
            something=1;
            pp->SetPort(tline); 
        }
376 377
    }

378
    if(something)
379
    {
380 381 382 383 384
        if (!pp->GetPort().length())
        {
            pp->SetPort(DEFAULT_PORT);
        }
        Programs.push_back(pp);
385
    }
386

387
    fclose (fd);
388 389 390 391 392
    if(check_unicode != (iconv_t)(-1))
        iconv_close(check_unicode);
    if(to_unicode != (iconv_t)(-1))
        iconv_close(to_unicode);

393 394
    return(0);
}