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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 921 - (hide annotations) (download)
Sun Sep 24 12:55:48 2006 UTC (17 years, 7 months ago) by capela
File MIME type: text/plain
File size: 3605 byte(s)
GPL address update.

1 capela 92 // parser.c
2     //
3     /****************************************************************************
4     liblscp - LinuxSampler Control Protocol API
5 capela 869 Copyright (C) 2004-2006, rncbc aka Rui Nuno Capela. All rights reserved.
6 capela 92
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 capela 921 You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 capela 92
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