~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/parsing.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2010, Google Inc.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *  1. Redistributions of source code must retain the above copyright notice,
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *  3. The name of the author may not be used to endorse or promote products
 
14
 *     derived from this software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
#ifndef TALK_P2P_BASE_PARSING_H_
 
29
#define TALK_P2P_BASE_PARSING_H_
 
30
 
 
31
#include <string>
 
32
#include <vector>
 
33
#include "talk/base/basictypes.h"
 
34
#include "talk/base/stringencode.h"
 
35
#include "talk/xmllite/xmlelement.h"  // Needed to delete ParseError.extra.
 
36
 
 
37
namespace cricket {
 
38
 
 
39
typedef std::vector<buzz::XmlElement*> XmlElements;
 
40
 
 
41
// We decided "bool Parse(in, out*, error*)" is generally the best
 
42
// parse signature.  "out Parse(in)" doesn't allow for errors.
 
43
// "error* Parse(in, out*)" doesn't allow flexible memory management.
 
44
 
 
45
// The error type for parsing.
 
46
struct ParseError {
 
47
 public:
 
48
  // explains the error
 
49
  std::string text;
 
50
  // provide details about what wasn't parsable
 
51
  const buzz::XmlElement* extra;
 
52
 
 
53
  ParseError() : extra(NULL) {}
 
54
 
 
55
  ~ParseError() {
 
56
    delete extra;
 
57
  }
 
58
 
 
59
  void SetText(const std::string& text) {
 
60
    this->text = text;
 
61
  }
 
62
};
 
63
 
 
64
// The error type for writing.
 
65
struct WriteError {
 
66
  std::string text;
 
67
 
 
68
  void SetText(const std::string& text) {
 
69
    this->text = text;
 
70
  }
 
71
};
 
72
 
 
73
// Convenience method for returning a message when parsing fails.
 
74
bool BadParse(const std::string& text, ParseError* err);
 
75
 
 
76
// Convenience method for returning a message when writing fails.
 
77
bool BadWrite(const std::string& text, WriteError* error);
 
78
 
 
79
// helper XML functions
 
80
std::string GetXmlAttr(const buzz::XmlElement* elem,
 
81
                       const buzz::QName& name,
 
82
                       const std::string& def);
 
83
std::string GetXmlAttr(const buzz::XmlElement* elem,
 
84
                       const buzz::QName& name,
 
85
                       const char* def);
 
86
// Return true if the value is "true" or "1".
 
87
bool GetXmlAttr(const buzz::XmlElement* elem,
 
88
                const buzz::QName& name, bool def);
 
89
int GetXmlAttr(const buzz::XmlElement* elem,
 
90
               const buzz::QName& name, int def);
 
91
 
 
92
template <class T>
 
93
bool AddXmlAttr(buzz::XmlElement* elem,
 
94
                const buzz::QName& name, const T& val) {
 
95
  std::string buf;
 
96
  if (!talk_base::ToString(val, &buf)) {
 
97
    return false;
 
98
  }
 
99
  elem->AddAttr(name, buf);
 
100
  return true;
 
101
}
 
102
 
 
103
template <class T>
 
104
bool SetXmlBody(buzz::XmlElement* elem, const T& val) {
 
105
  std::string buf;
 
106
  if (!talk_base::ToString(val, &buf)) {
 
107
    return false;
 
108
  }
 
109
  elem->SetBodyText(buf);
 
110
  return true;
 
111
}
 
112
 
 
113
const buzz::XmlElement* GetXmlChild(const buzz::XmlElement* parent,
 
114
                                    const std::string& name);
 
115
 
 
116
bool RequireXmlChild(const buzz::XmlElement* parent,
 
117
                     const std::string& name,
 
118
                     const buzz::XmlElement** child,
 
119
                     ParseError* error);
 
120
bool RequireXmlAttr(const buzz::XmlElement* elem,
 
121
                    const buzz::QName& name,
 
122
                    std::string* value,
 
123
                    ParseError* error);
 
124
void AddXmlAttrIfNonEmpty(buzz::XmlElement* elem,
 
125
                          const buzz::QName name,
 
126
                          const std::string& value);
 
127
void AddXmlChildren(buzz::XmlElement* parent,
 
128
                    const std::vector<buzz::XmlElement*>& children);
 
129
void CopyXmlChildren(const buzz::XmlElement* source, buzz::XmlElement* dest);
 
130
std::vector<buzz::XmlElement*> CopyOfXmlChildren(const buzz::XmlElement* elem);
 
131
 
 
132
}  // namespace cricket
 
133
 
 
134
#endif  // TALK_P2P_BASE_PARSING_H_