File: xql.cpp

package info (click to toggle)
musicbrainz 1.0.1.final-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,168 kB
  • ctags: 2,610
  • sloc: ansic: 16,052; sh: 8,658; cpp: 6,283; perl: 327; makefile: 112
file content (158 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2000 Robert Kaye
   
   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

     $Id: xql.cpp,v 1.8 2001/03/05 22:19:03 robert Exp $

----------------------------------------------------------------------------*/

// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <stdio.h>
#include <map>
#include <assert.h>
#include "xql.h"

XQL::XQL(bool useUTF8) :
      MBParse(useUTF8)
{
}

XQL::~XQL(void)
{
}

Error XQL::BeginElement(const string &oElement, AttrMap &oAttrMap)
{
    map<string, string>::iterator i;
    int                           j;
    string                        oKey;
    char                          szIndex[100];

    m_oPath += string("/") + oElement;  

    if (m_oNodeHist.find(m_oPath) == m_oNodeHist.end())
        m_oNodeHist[m_oPath] = 0;
    else
    {
        j = m_oNodeHist[m_oPath] + 1;
        m_oNodeHist[m_oPath] = j;
        sprintf(szIndex, "[%d]", j);
        m_oPath += string(szIndex);
    }

    for(i = oAttrMap.begin(); i != oAttrMap.end(); i++)
    {
        oKey = m_oPath + string("/@") + (*i).first; 
        m_oList[oKey] = (*i).second;
        //printf("parse: %s ==> '%s'\n", oKey.c_str(), (*i).second.c_str());
    }

    return kError_NoErr;
}

Error XQL::EndElement(const string &oElement)
{
    const char *pPtr;
    int         iOffset;  

    pPtr = strrchr(m_oPath.c_str(), '/');
    if (pPtr == NULL)
       return kError_NoErr;

    iOffset = pPtr - m_oPath.c_str();
    m_oPath.erase(iOffset, m_oPath.length() - iOffset); 

    return kError_NoErr;
}

Error XQL::PCData(const string &oData)
{
    //printf("parse: %s ==> '%s'\n", m_oPath.c_str(), oData.c_str());
    m_oList[m_oPath] += oData;

    return kError_NoErr;
}

const string &XQL::Query(const string &oKeyArg)
{
    string::size_type pos;
    string            oKey = oKeyArg;

    for(;;)
    {
        pos = oKey.find("[0]", 0);
        if (pos == string::npos)
           break;

        oKey.erase(pos, 3);
    }

    if (m_oList.find(oKey) == m_oList.end())
    {
        return m_oEmpty;
    }

    //printf("rq: '%s' => '%s'\n", oKeyArg.c_str(), m_oList[oKey].c_str()); 

    return m_oList[oKey];
}

#if 0
static char *query1 = "/TrackInfo/GeneralInfo/Title";
static char *query2 = "/TrackInfo/GeneralInfo/Artist/@ID";
static char *query3 = "/TrackInfo/ExtendedInfo/Lyrics/SyncLyrics/LyricEvent[]/Text";
static char *query4 = "/TrackInfo/ExtendedInfo/Lyrics/SyncLyrics/LyricEvent[]";

int main(void)
{
    XQL oList;
    string      oValue;
    Error       eRet;

    eRet = oList.ParseFile(string("tsample.xml"));
    if (eRet != kError_NoErr)
    {
        string oErr;

        oList.GetErrorString(oErr);
        printf("Parse error: %s\n", oErr.c_str()); 
        return 0;
    }
 
    printf("Query: %s ==> '%s'\n", query1, 
           oList.Query(string(query1), 0).c_str());

    printf("Query: %s ==> '%s'\n", query2, 
           oList.Query(string(query2), 0).c_str());

    printf("Query: %s ==> '%s'\n", query3, 
           oList.Query(string(query3), 2).c_str());

    printf("Query: %s ==> '%s'\n", query4, 
           oList.Query(string(query4), 3).c_str());

    return 0;
} 
#endif