2012-07-01 08:49:19 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
|
|
|
|
|
|
|
This file is part of LibMaxsi.
|
|
|
|
|
|
|
|
LibMaxsi is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
LibMaxsi 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 Lesser General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
readparamstring.cpp
|
|
|
|
A kinda dirty hack to ease transition to the /dev/video API.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <libmaxsi/platform.h>
|
|
|
|
#include <libmaxsi/string.h>
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2012-09-22 16:53:11 -04:00
|
|
|
#include <string.h>
|
2012-07-01 08:49:19 -04:00
|
|
|
#include <readparamstring.h>
|
|
|
|
|
|
|
|
using namespace Maxsi;
|
|
|
|
|
|
|
|
extern "C" bool ReadParamString(const char* str, ...)
|
|
|
|
{
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( String::Seek(str, '\n') ) { errno = EINVAL; }
|
2012-07-01 08:49:19 -04:00
|
|
|
const char* keyname;
|
|
|
|
va_list args;
|
|
|
|
while ( *str )
|
|
|
|
{
|
2012-09-22 17:00:54 -04:00
|
|
|
size_t varlen = strcspn(str, ",");
|
2012-07-01 08:49:19 -04:00
|
|
|
if ( !varlen ) { str++; continue; }
|
2012-09-22 17:00:54 -04:00
|
|
|
size_t namelen = strcspn(str, "=");
|
2012-09-22 10:44:50 -04:00
|
|
|
if ( !namelen ) { errno = EINVAL; goto cleanup; }
|
|
|
|
if ( !str[namelen] ) { errno = EINVAL; goto cleanup; }
|
|
|
|
if ( varlen < namelen ) { errno = EINVAL; goto cleanup; }
|
2012-07-01 08:49:19 -04:00
|
|
|
size_t valuelen = varlen - 1 /*=*/ - namelen;
|
|
|
|
char* name = String::Substring(str, 0, namelen);
|
|
|
|
if ( !name ) { goto cleanup; }
|
|
|
|
char* value = String::Substring(str, namelen+1, valuelen);
|
|
|
|
if ( !value ) { delete[] name; goto cleanup; }
|
|
|
|
va_start(args, str);
|
|
|
|
while ( (keyname = va_arg(args, const char*)) )
|
|
|
|
{
|
|
|
|
char** nameptr = va_arg(args, char**);
|
2012-09-22 16:53:11 -04:00
|
|
|
if ( strcmp(keyname, name) ) { continue; }
|
2012-07-01 08:49:19 -04:00
|
|
|
*nameptr = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
if ( !keyname ) { delete[] value; }
|
|
|
|
delete[] name;
|
|
|
|
str += varlen;
|
2012-09-22 16:56:36 -04:00
|
|
|
str += strspn(str, ",");
|
2012-07-01 08:49:19 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
va_start(args, str);
|
|
|
|
while ( (keyname = va_arg(args, const char*)) )
|
|
|
|
{
|
|
|
|
char** nameptr = va_arg(args, char**);
|
|
|
|
delete[] *nameptr; *nameptr = NULL;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
return false;
|
|
|
|
}
|