File: parse.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 Emusic.com
   
   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: parse.cpp,v 1.6 2000/10/26 14:40:08 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 "parse.h"

void BeginElement(void *data, const XML_Char *el, const XML_Char **attr)
{
    map<string, string> oMap;
    string key, value;

    for(; *attr;)
    {
        if (((MBParse *)data)->UseUTF8())
        {
           key = string((char *)*(attr++));
           value = string((char *)*(attr++));
        }
        else
        {
           key = ConvertToISO((char *)*(attr++));
           value = ConvertToISO((char *)*(attr++));
        }
        oMap[key] = value;
    }

    ((MBParse *)data)->BeginElement(string(el), oMap);
}

void EndElement(void *data, const XML_Char *el)
{
    if (((MBParse *)data)->UseUTF8())
       ((MBParse *)data)->EndElement(string(el));
    else
       ((MBParse *)data)->EndElement(ConvertToISO((char *)el));
}

void PCData(void *data, const XML_Char *charData, int len)
{
    char *temp;

    temp = new char[len + 1];
    strncpy(temp, (char *)charData, len);
    temp[len] = 0;
    if (((MBParse *)data)->UseUTF8())
       ((MBParse *)data)->PCData(string(temp));
    else
       ((MBParse *)data)->PCData(ConvertToISO((char *)temp));
    delete temp;
}

MBParse::MBParse(bool useUTF8)
{
    m_useUTF8 = useUTF8;

    m_pParser = (XML_Parser *)XML_ParserCreate(NULL);
    XML_SetUserData(m_pParser, this);
    XML_SetElementHandler(m_pParser, ::BeginElement, ::EndElement);
    XML_SetCharacterDataHandler(m_pParser, ::PCData);
}

MBParse::~MBParse(void)
{
    XML_ParserFree(m_pParser);
}

Error MBParse::ParseFile(const string &oFile)
{
    assert(0); 
    return kError_NoErr;
}

Error MBParse::ParseString(const string &oXML)
{
    return XML_Parse(m_pParser, oXML.c_str(), oXML.length(), 1) ? 
           kError_NoErr : kError_ParseError;
}

void MBParse::GetErrorString(string &oError)
{
    oError = string(XML_ErrorString(XML_GetErrorCode(m_pParser)));
}

int MBParse::GetErrorLine(void)
{
    return XML_GetCurrentLineNumber(m_pParser);
}

const string ConvertToISO(const char *utf8)
{
   unsigned char *in, *buf;
   unsigned char *out, *end;
   string               ret;

   in = (unsigned char *)utf8;
   buf = out = new unsigned char[strlen(utf8) + 1];
   end = in + strlen(utf8);
   for(;*in != 0x00 && in <= end; in++, out++)
   {
       if (*in < 0x80)
       {  /* lower 7-bits unchanged */
          *out = *in;
       }
       else
       if (*in > 0xC3)
       { /* discard anything above 0xFF */
          *out = '?';
       }
       else
       if (*in & 0xC0)
       { /* parse upper 7-bits */
          if (in >= end)
            *out = 0;
          else
          {
            *out = (((*in) & 0x1F) << 6) | (0x3F & (*(++in))); 
          }
       }
       else
       {
          *out = '?';  /* this should never happen */
       }
   }
   *out = 0x00; /* append null */
   ret = string((char *)buf);
   delete buf;

   return ret;
}