Audacity  2.2.2
XMLFileReader.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  XMLFileReader.cpp
6 
7  Dominic Mazzoni
8 
9 *******************************************************************//*******************************************************************/
15 
16 #include <wx/defs.h>
17 #include <wx/ffile.h>
18 #include <wx/intl.h>
19 
20 #include <string.h>
21 
22 #include "XMLFileReader.h"
23 #include "../Internat.h"
24 
26 {
27  mParser = XML_ParserCreate(NULL);
28  XML_SetUserData(mParser, (void *)this);
29  XML_SetElementHandler(mParser, startElement, endElement);
30  XML_SetCharacterDataHandler(mParser, charHandler);
31  mBaseHandler = NULL;
32  mErrorStr = wxT("");
33  mHandler.reserve(128);
34 }
35 
37 {
38  XML_ParserFree(mParser);
39 }
40 
42  const wxString &fname)
43 {
44  wxFFile theXMLFile(fname, wxT("rb"));
45  if (!theXMLFile.IsOpened()) {
46  mErrorStr.Printf(_("Could not open file: \"%s\""), fname);
47  return false;
48  }
49 
50  mBaseHandler = baseHandler;
51 
52  const size_t bufferSize = 16384;
53  char buffer[16384];
54  int done = 0;
55  do {
56  size_t len = fread(buffer, 1, bufferSize, theXMLFile.fp());
57  done = (len < bufferSize);
58  if (!XML_Parse(mParser, buffer, len, done)) {
59  mErrorStr.Printf(_("Error: %hs at line %lu"),
60  XML_ErrorString(XML_GetErrorCode(mParser)),
61  (long unsigned int)XML_GetCurrentLineNumber(mParser));
62  theXMLFile.Close();
63  return false;
64  }
65  } while (!done);
66 
67  theXMLFile.Close();
68 
69  // Even though there were no parse errors, we only succeed if
70  // the first-level handler actually got called, and didn't
71  // return false.
72  if (mBaseHandler)
73  return true;
74  else {
75  mErrorStr.Printf(_("Could not load file: \"%s\""), fname);
76  return false;
77  }
78 }
79 
81 {
82  return mErrorStr;
83 }
84 
85 // static
86 void XMLFileReader::startElement(void *userData, const char *name,
87  const char **atts)
88 {
89  XMLFileReader *This = (XMLFileReader *)userData;
90  Handlers &handlers = This->mHandler;
91 
92  if (handlers.empty()) {
93  handlers.push_back(This->mBaseHandler);
94  }
95  else {
96  if (XMLTagHandler *const handler = handlers.back())
97  handlers.push_back(handler->ReadXMLChild(name));
98  else
99  handlers.push_back(NULL);
100  }
101 
102  if (XMLTagHandler *& handler = handlers.back()) {
103  if (!handler->ReadXMLTag(name, atts)) {
104  handler = nullptr;
105  if (handlers.size() == 1)
106  This->mBaseHandler = nullptr;
107  }
108  }
109 }
110 
111 // static
112 void XMLFileReader::endElement(void *userData, const char *name)
113 {
114  XMLFileReader *This = (XMLFileReader *)userData;
115  Handlers &handlers = This->mHandler;
116 
117  if (XMLTagHandler *const handler = handlers.back())
118  handler->ReadXMLEndTag(name);
119 
120  handlers.pop_back();
121 }
122 
123 // static
124 void XMLFileReader::charHandler(void *userData, const char *s, int len)
125 {
126  XMLFileReader *This = (XMLFileReader *)userData;
127  Handlers &handlers = This->mHandler;
128 
129  if (XMLTagHandler *const handler = handlers.back())
130  handler->ReadXMLContent(s, len);
131 }
XMLTagHandler * mBaseHandler
Definition: XMLFileReader.h:39
bool Parse(XMLTagHandler *baseHandler, const wxString &fname)
Reads a file and passes the results through an XMLTagHandler.
Definition: XMLFileReader.h:18
Handlers mHandler
Definition: XMLFileReader.h:41
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:70
std::vector< XMLTagHandler * > Handlers
Definition: XMLFileReader.h:40
XML_Parser mParser
Definition: XMLFileReader.h:38
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
wxString mErrorStr
Definition: XMLFileReader.h:42
const wxChar * name
Definition: Distortion.cpp:94
static void startElement(void *userData, const char *name, const char **atts)
wxString GetErrorStr()
static void endElement(void *userData, const char *name)
static void charHandler(void *userData, const char *s, int len)