1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// parser.c
//
/****************************************************************************
liblscp - LinuxSampler Control Protocol API
Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved.
This 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 2.1 of the License, or (at your option) any later version.
This 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.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
#include "parser.h"
// Case unsensitive comparison substitutes.
#if defined(WIN32)
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
//-------------------------------------------------------------------------
// Simple token parser.
const char *lscp_parser_strtok ( char *pchBuffer, const char *pszDelim, char **ppch )
{
const char *pszToken;
if (pchBuffer == NULL)
pchBuffer = *ppch;
pchBuffer += strspn(pchBuffer, pszDelim);
if (*pchBuffer == '\0')
return NULL;
pszToken = pchBuffer;
pchBuffer = strpbrk(pszToken, pszDelim);
if (pchBuffer == NULL) {
*ppch = strchr(pszToken, '\0');
} else {
*pchBuffer = '\0';
*ppch = pchBuffer + 1;
while (strchr(pszDelim, **ppch))
(*ppch)++;
}
return pszToken;
}
void lscp_parser_init ( lscp_parser_t *pParser, const char *pchBuffer, int cchBuffer )
{
memset(pParser, 0, sizeof(lscp_parser_t));
pParser->pchBuffer = (char *) malloc(cchBuffer + 1);
if (pParser->pchBuffer) {
memcpy(pParser->pchBuffer, pchBuffer, cchBuffer);
pParser->pchBuffer[cchBuffer] = (char) 0;
pParser->pszToken = lscp_parser_strtok(pParser->pchBuffer, " \t\r\n", &(pParser->pch));
}
}
const char *lscp_parser_next ( lscp_parser_t *pParser )
{
const char *pszToken = pParser->pszToken;
if (pParser->pszToken)
pParser->pszToken = lscp_parser_strtok(NULL, " \t\r\n", &(pParser->pch));
return pszToken;
}
int lscp_parser_nextint ( lscp_parser_t *pParser )
{
int ret = 0;
if (pParser->pszToken) {
ret = atoi(pParser->pszToken);
lscp_parser_next(pParser);
}
return ret;
}
float lscp_parser_nextnum ( lscp_parser_t *pParser )
{
float ret = 0;
if (pParser->pszToken) {
ret = (float) atof(pParser->pszToken);
lscp_parser_next(pParser);
}
return ret;
}
int lscp_parser_test ( lscp_parser_t *pParser, const char *pszToken )
{
int ret = (pParser->pszToken != NULL);
if (ret)
ret = (strcasecmp(pParser->pszToken, pszToken) == 0);
if (ret)
lscp_parser_next(pParser);
return ret;
}
int lscp_parser_test2 ( lscp_parser_t *pParser, const char *pszToken, const char *pszToken2 )
{
int ret = lscp_parser_test(pParser, pszToken);
if (ret)
ret = lscp_parser_test(pParser, pszToken2);
return ret;
}
void lscp_parser_free ( lscp_parser_t *pParser )
{
if (pParser->pchBuffer)
free(pParser->pchBuffer);
pParser->pchBuffer = NULL;
}
// end of parser.c
|