Panda3D
documentSpec.cxx
1 // Filename: documentSpec.cxx
2 // Created by: drose (28Jan03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "documentSpec.h"
16 #include "indent.h"
17 
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: DocumentSpec::compare_to
21 // Access: Published
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 int DocumentSpec::
25 compare_to(const DocumentSpec &other) const {
26  if (_flags != other._flags) {
27  return (_flags - other._flags);
28  }
29  int c = _url.compare_to(other._url);
30  if (c != 0) {
31  return c;
32  }
33  if (has_tag()) {
34  c = _tag.compare_to(other._tag);
35  if (c != 0) {
36  return c;
37  }
38  }
39  if (has_date()) {
40  c = _date.compare_to(other._date);
41  if (c != 0) {
42  return c;
43  }
44  }
45 
46  // We don't consider _request_mode or _cache_control significant in
47  // the comparison.
48 
49  return 0;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: DocumentSpec::input
54 // Access: Published
55 // Description: Can be used to read in the DocumentSpec from a stream
56 // generated either by output() or write(). Returns
57 // true on success, false on failure.
58 ////////////////////////////////////////////////////////////////////
59 bool DocumentSpec::
60 input(istream &in) {
61  // First, clear the spec.
62  (*this) = DocumentSpec();
63 
64  char ch;
65  in >> ch;
66  if (ch != '[') {
67  return false;
68  }
69  in >> _url;
70  in >> ch;
71  if (ch == '(') {
72  // Scan the tag, up to but not including the closing paren.
73  string tag;
74  in >> ch;
75  while (!in.fail() && !in.eof() && ch != ')') {
76  tag += ch;
77  // We want to include embedded whitespace, so we use get().
78  ch = in.get();
79  }
80  set_tag(HTTPEntityTag(tag));
81 
82  // Now ch is the close paren following the tag; skip to the next
83  // character.
84  in >> ch;
85  }
86 
87  // Scan the date, up to but not including the closing bracket.
88  if (ch != ']') {
89  string date;
90  while (!in.fail() && !in.eof() && ch != ']') {
91  date += ch;
92  ch = in.get();
93  }
94 
95  set_date(HTTPDate(date));
96  if (!get_date().is_valid()) {
97  return false;
98  }
99  }
100 
101  return true;
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: DocumentSpec::output
106 // Access: Published
107 // Description:
108 ////////////////////////////////////////////////////////////////////
109 void DocumentSpec::
110 output(ostream &out) const {
111  out << "[ " << get_url();
112  if (has_tag()) {
113  out << " (" << get_tag() << ")";
114  }
115  if (has_date()) {
116  out << " " << get_date();
117  }
118  out << " ]";
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: DocumentSpec::write
123 // Access: Published
124 // Description:
125 ////////////////////////////////////////////////////////////////////
126 void DocumentSpec::
127 write(ostream &out, int indent_level) const {
128  indent(out, indent_level)
129  << "[ " << get_url();
130  if (has_tag()) {
131  out << "\n";
132  indent(out, indent_level + 2)
133  << "(" << get_tag() << ")";
134  }
135  if (has_date()) {
136  out << "\n";
137  indent(out, indent_level + 2)
138  << get_date();
139  }
140  out << " ]\n";
141 }
const URLSpec & get_url() const
Retrieves the URL of the DocumentSpec.
Definition: documentSpec.I:138
A container for an "entity tag" from an HTTP server.
Definition: httpEntityTag.h:27
bool has_date() const
Returns true if a last-modified date is associated with the DocumentSpec.
Definition: documentSpec.I:211
void set_date(const HTTPDate &date)
Changes the last-modified date associated with the DocumentSpec.
Definition: documentSpec.I:199
bool input(istream &in)
Can be used to read in the DocumentSpec from a stream generated either by output() or write()...
void set_tag(const HTTPEntityTag &tag)
Changes the identity tag associated with the DocumentSpec.
Definition: documentSpec.I:149
int compare_to(const HTTPEntityTag &other) const
Returns a number less than zero if this HTTPEntityTag sorts before the other one, greater than zero i...
int compare_to(const HTTPDate &other) const
Returns a number less than zero if this HTTPDate sorts before the other one, greater than zero if it ...
Definition: httpDate.I:134
const HTTPDate & get_date() const
Returns the last-modified date associated with the DocumentSpec, if there is one. ...
Definition: documentSpec.I:223
const HTTPEntityTag & get_tag() const
Returns the identity tag associated with the DocumentSpec, if there is one.
Definition: documentSpec.I:176
A container for an HTTP-legal time/date indication.
Definition: httpDate.h:30
int compare_to(const URLSpec &other) const
Returns a number less than zero if this URLSpec sorts before the other one, greater than zero if it s...
Definition: urlSpec.I:84
A descriptor that refers to a particular version of a document.
Definition: documentSpec.h:33
bool has_tag() const
Returns true if an identity tag is associated with the DocumentSpec.
Definition: documentSpec.I:161