/[svn]/liblscp/trunk/examples/parser.c
ViewVC logotype

Contents of /liblscp/trunk/examples/parser.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92 - (show annotations) (download)
Thu May 27 10:32:44 2004 UTC (19 years, 10 months ago) by capela
File MIME type: text/plain
File size: 3601 byte(s)
Initial alpha release.

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

  ViewVC Help
Powered by ViewVC