2012-07-01 08:49:19 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
This file is part of the Sortix C Library.
|
2012-07-01 08:49:19 -04:00
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
The Sortix C Library 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.
|
2012-07-01 08:49:19 -04:00
|
|
|
|
2012-09-28 18:36:46 -04:00
|
|
|
The Sortix C Library 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.
|
2012-07-01 08:49:19 -04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2012-09-28 18:36:46 -04:00
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-01 08:49:19 -04:00
|
|
|
|
|
|
|
readparamstring.cpp
|
|
|
|
A kinda dirty hack to ease transition to the /dev/video API.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2012-09-23 07:09:12 -04:00
|
|
|
#include <stdarg.h>
|
2012-09-22 16:53:11 -04:00
|
|
|
#include <string.h>
|
2012-07-01 08:49:19 -04:00
|
|
|
#include <readparamstring.h>
|
|
|
|
|
2012-09-23 07:09:12 -04:00
|
|
|
static char* Substring(const char* src, size_t offset, size_t length)
|
|
|
|
{
|
|
|
|
char* dest = new char[length + 1];
|
|
|
|
if ( !dest ) { return NULL; }
|
|
|
|
memcpy(dest, src + offset, length * sizeof(char));
|
|
|
|
dest[length] = 0;
|
|
|
|
return dest;
|
|
|
|
}
|
2012-07-01 08:49:19 -04:00
|
|
|
|
|
|
|
extern "C" bool ReadParamString(const char* str, ...)
|
|
|
|
{
|
2012-09-23 05:59:55 -04:00
|
|
|
if ( strchr(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;
|
2012-09-23 07:09:12 -04:00
|
|
|
char* name = Substring(str, 0, namelen);
|
2012-07-01 08:49:19 -04:00
|
|
|
if ( !name ) { goto cleanup; }
|
2012-09-23 07:09:12 -04:00
|
|
|
char* value = Substring(str, namelen+1, valuelen);
|
2012-07-01 08:49:19 -04:00
|
|
|
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;
|
|
|
|
}
|