OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 annotation_collector_(annotation_collector) {} | 63 annotation_collector_(annotation_collector) {} |
64 | 64 |
65 Printer::~Printer() { | 65 Printer::~Printer() { |
66 // Only BackUp() if we have called Next() at least once and never failed. | 66 // Only BackUp() if we have called Next() at least once and never failed. |
67 if (buffer_size_ > 0 && !failed_) { | 67 if (buffer_size_ > 0 && !failed_) { |
68 output_->BackUp(buffer_size_); | 68 output_->BackUp(buffer_size_); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 bool Printer::GetSubstitutionRange(const char* varname, | 72 bool Printer::GetSubstitutionRange(const char* varname, |
73 pair<size_t, size_t>* range) { | 73 std::pair<size_t, size_t>* range) { |
74 map<string, pair<size_t, size_t> >::const_iterator iter = | 74 std::map<string, std::pair<size_t, size_t> >::const_iterator iter = |
75 substitutions_.find(varname); | 75 substitutions_.find(varname); |
76 if (iter == substitutions_.end()) { | 76 if (iter == substitutions_.end()) { |
77 GOOGLE_LOG(DFATAL) << " Undefined variable in annotation: " << varname; | 77 GOOGLE_LOG(DFATAL) << " Undefined variable in annotation: " << varname; |
78 return false; | 78 return false; |
79 } | 79 } |
80 if (iter->second.first > iter->second.second) { | 80 if (iter->second.first > iter->second.second) { |
81 GOOGLE_LOG(DFATAL) << " Variable used for annotation used multiple times: " | 81 GOOGLE_LOG(DFATAL) << " Variable used for annotation used multiple times: " |
82 << varname; | 82 << varname; |
83 return false; | 83 return false; |
84 } | 84 } |
85 *range = iter->second; | 85 *range = iter->second; |
86 return true; | 86 return true; |
87 } | 87 } |
88 | 88 |
89 void Printer::Annotate(const char* begin_varname, const char* end_varname, | 89 void Printer::Annotate(const char* begin_varname, const char* end_varname, |
90 const string& file_path, const vector<int>& path) { | 90 const string& file_path, const std::vector<int>& path) { |
91 if (annotation_collector_ == NULL) { | 91 if (annotation_collector_ == NULL) { |
92 // Can't generate signatures with this Printer. | 92 // Can't generate signatures with this Printer. |
93 return; | 93 return; |
94 } | 94 } |
95 pair<size_t, size_t> begin, end; | 95 std::pair<size_t, size_t> begin, end; |
96 if (!GetSubstitutionRange(begin_varname, &begin) || | 96 if (!GetSubstitutionRange(begin_varname, &begin) || |
97 !GetSubstitutionRange(end_varname, &end)) { | 97 !GetSubstitutionRange(end_varname, &end)) { |
98 return; | 98 return; |
99 } | 99 } |
100 if (begin.first > end.second) { | 100 if (begin.first > end.second) { |
101 GOOGLE_LOG(DFATAL) << " Annotation has negative length from " << begin_varn
ame | 101 GOOGLE_LOG(DFATAL) << " Annotation has negative length from " << begin_varn
ame |
102 << " to " << end_varname; | 102 << " to " << end_varname; |
103 } else { | 103 } else { |
104 annotation_collector_->AddAnnotation(begin.first, end.second, file_path, | 104 annotation_collector_->AddAnnotation(begin.first, end.second, file_path, |
105 path); | 105 path); |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 void Printer::Print(const map<string, string>& variables, const char* text) { | 109 void Printer::Print(const std::map<string, string>& variables, |
| 110 const char* text) { |
110 int size = strlen(text); | 111 int size = strlen(text); |
111 int pos = 0; // The number of bytes we've written so far. | 112 int pos = 0; // The number of bytes we've written so far. |
112 substitutions_.clear(); | 113 substitutions_.clear(); |
113 | 114 |
114 for (int i = 0; i < size; i++) { | 115 for (int i = 0; i < size; i++) { |
115 if (text[i] == '\n') { | 116 if (text[i] == '\n') { |
116 // Saw newline. If there is more text, we may need to insert an indent | 117 // Saw newline. If there is more text, we may need to insert an indent |
117 // here. So, write what we have so far, including the '\n'. | 118 // here. So, write what we have so far, including the '\n'. |
118 WriteRaw(text + pos, i - pos + 1); | 119 WriteRaw(text + pos, i - pos + 1); |
119 pos = i + 1; | 120 pos = i + 1; |
(...skipping 16 matching lines...) Expand all Loading... |
136 end = text + pos; | 137 end = text + pos; |
137 } | 138 } |
138 int endpos = end - text; | 139 int endpos = end - text; |
139 | 140 |
140 string varname(text + pos, endpos - pos); | 141 string varname(text + pos, endpos - pos); |
141 if (varname.empty()) { | 142 if (varname.empty()) { |
142 // Two delimiters in a row reduce to a literal delimiter character. | 143 // Two delimiters in a row reduce to a literal delimiter character. |
143 WriteRaw(&variable_delimiter_, 1); | 144 WriteRaw(&variable_delimiter_, 1); |
144 } else { | 145 } else { |
145 // Replace with the variable's value. | 146 // Replace with the variable's value. |
146 map<string, string>::const_iterator iter = variables.find(varname); | 147 std::map<string, string>::const_iterator iter = variables.find(varname); |
147 if (iter == variables.end()) { | 148 if (iter == variables.end()) { |
148 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; | 149 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; |
149 } else { | 150 } else { |
150 size_t begin = offset_; | 151 size_t begin = offset_; |
151 WriteRaw(iter->second.data(), iter->second.size()); | 152 WriteRaw(iter->second.data(), iter->second.size()); |
152 pair<map<string, pair<size_t, size_t> >::iterator, bool> inserted = | 153 std::pair<std::map<string, std::pair<size_t, size_t> >::iterator, |
153 substitutions_.insert( | 154 bool> |
| 155 inserted = substitutions_.insert( |
154 std::make_pair(varname, std::make_pair(begin, offset_))); | 156 std::make_pair(varname, std::make_pair(begin, offset_))); |
155 if (!inserted.second) { | 157 if (!inserted.second) { |
156 // This variable was used multiple times. Make its span have | 158 // This variable was used multiple times. Make its span have |
157 // negative length so we can detect it if it gets used in an | 159 // negative length so we can detect it if it gets used in an |
158 // annotation. | 160 // annotation. |
159 inserted.first->second = std::make_pair(1, 0); | 161 inserted.first->second = std::make_pair(1, 0); |
160 } | 162 } |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 // Advance past this variable. | 166 // Advance past this variable. |
165 i = endpos; | 167 i = endpos; |
166 pos = endpos + 1; | 168 pos = endpos + 1; |
167 } | 169 } |
168 } | 170 } |
169 | 171 |
170 // Write the rest. | 172 // Write the rest. |
171 WriteRaw(text + pos, size - pos); | 173 WriteRaw(text + pos, size - pos); |
172 } | 174 } |
173 | 175 |
174 void Printer::Print(const char* text) { | 176 void Printer::Print(const char* text) { |
175 static map<string, string> empty; | 177 static std::map<string, string> empty; |
176 Print(empty, text); | 178 Print(empty, text); |
177 } | 179 } |
178 | 180 |
179 void Printer::Print(const char* text, | 181 void Printer::Print(const char* text, |
180 const char* variable, const string& value) { | 182 const char* variable, const string& value) { |
181 map<string, string> vars; | 183 std::map<string, string> vars; |
182 vars[variable] = value; | 184 vars[variable] = value; |
183 Print(vars, text); | 185 Print(vars, text); |
184 } | 186 } |
185 | 187 |
186 void Printer::Print(const char* text, | 188 void Printer::Print(const char* text, |
187 const char* variable1, const string& value1, | 189 const char* variable1, const string& value1, |
188 const char* variable2, const string& value2) { | 190 const char* variable2, const string& value2) { |
189 map<string, string> vars; | 191 std::map<string, string> vars; |
190 vars[variable1] = value1; | 192 vars[variable1] = value1; |
191 vars[variable2] = value2; | 193 vars[variable2] = value2; |
192 Print(vars, text); | 194 Print(vars, text); |
193 } | 195 } |
194 | 196 |
195 void Printer::Print(const char* text, | 197 void Printer::Print(const char* text, |
196 const char* variable1, const string& value1, | 198 const char* variable1, const string& value1, |
197 const char* variable2, const string& value2, | 199 const char* variable2, const string& value2, |
198 const char* variable3, const string& value3) { | 200 const char* variable3, const string& value3) { |
199 map<string, string> vars; | 201 std::map<string, string> vars; |
200 vars[variable1] = value1; | 202 vars[variable1] = value1; |
201 vars[variable2] = value2; | 203 vars[variable2] = value2; |
202 vars[variable3] = value3; | 204 vars[variable3] = value3; |
203 Print(vars, text); | 205 Print(vars, text); |
204 } | 206 } |
205 | 207 |
206 void Printer::Print(const char* text, | 208 void Printer::Print(const char* text, |
207 const char* variable1, const string& value1, | 209 const char* variable1, const string& value1, |
208 const char* variable2, const string& value2, | 210 const char* variable2, const string& value2, |
209 const char* variable3, const string& value3, | 211 const char* variable3, const string& value3, |
210 const char* variable4, const string& value4) { | 212 const char* variable4, const string& value4) { |
211 map<string, string> vars; | 213 std::map<string, string> vars; |
212 vars[variable1] = value1; | 214 vars[variable1] = value1; |
213 vars[variable2] = value2; | 215 vars[variable2] = value2; |
214 vars[variable3] = value3; | 216 vars[variable3] = value3; |
215 vars[variable4] = value4; | 217 vars[variable4] = value4; |
216 Print(vars, text); | 218 Print(vars, text); |
217 } | 219 } |
218 | 220 |
219 void Printer::Print(const char* text, | 221 void Printer::Print(const char* text, |
220 const char* variable1, const string& value1, | 222 const char* variable1, const string& value1, |
221 const char* variable2, const string& value2, | 223 const char* variable2, const string& value2, |
222 const char* variable3, const string& value3, | 224 const char* variable3, const string& value3, |
223 const char* variable4, const string& value4, | 225 const char* variable4, const string& value4, |
224 const char* variable5, const string& value5) { | 226 const char* variable5, const string& value5) { |
225 map<string, string> vars; | 227 std::map<string, string> vars; |
226 vars[variable1] = value1; | 228 vars[variable1] = value1; |
227 vars[variable2] = value2; | 229 vars[variable2] = value2; |
228 vars[variable3] = value3; | 230 vars[variable3] = value3; |
229 vars[variable4] = value4; | 231 vars[variable4] = value4; |
230 vars[variable5] = value5; | 232 vars[variable5] = value5; |
231 Print(vars, text); | 233 Print(vars, text); |
232 } | 234 } |
233 | 235 |
234 void Printer::Print(const char* text, | 236 void Printer::Print(const char* text, |
235 const char* variable1, const string& value1, | 237 const char* variable1, const string& value1, |
236 const char* variable2, const string& value2, | 238 const char* variable2, const string& value2, |
237 const char* variable3, const string& value3, | 239 const char* variable3, const string& value3, |
238 const char* variable4, const string& value4, | 240 const char* variable4, const string& value4, |
239 const char* variable5, const string& value5, | 241 const char* variable5, const string& value5, |
240 const char* variable6, const string& value6) { | 242 const char* variable6, const string& value6) { |
241 map<string, string> vars; | 243 std::map<string, string> vars; |
242 vars[variable1] = value1; | 244 vars[variable1] = value1; |
243 vars[variable2] = value2; | 245 vars[variable2] = value2; |
244 vars[variable3] = value3; | 246 vars[variable3] = value3; |
245 vars[variable4] = value4; | 247 vars[variable4] = value4; |
246 vars[variable5] = value5; | 248 vars[variable5] = value5; |
247 vars[variable6] = value6; | 249 vars[variable6] = value6; |
248 Print(vars, text); | 250 Print(vars, text); |
249 } | 251 } |
250 | 252 |
251 void Printer::Print(const char* text, | 253 void Printer::Print(const char* text, |
252 const char* variable1, const string& value1, | 254 const char* variable1, const string& value1, |
253 const char* variable2, const string& value2, | 255 const char* variable2, const string& value2, |
254 const char* variable3, const string& value3, | 256 const char* variable3, const string& value3, |
255 const char* variable4, const string& value4, | 257 const char* variable4, const string& value4, |
256 const char* variable5, const string& value5, | 258 const char* variable5, const string& value5, |
257 const char* variable6, const string& value6, | 259 const char* variable6, const string& value6, |
258 const char* variable7, const string& value7) { | 260 const char* variable7, const string& value7) { |
259 map<string, string> vars; | 261 std::map<string, string> vars; |
260 vars[variable1] = value1; | 262 vars[variable1] = value1; |
261 vars[variable2] = value2; | 263 vars[variable2] = value2; |
262 vars[variable3] = value3; | 264 vars[variable3] = value3; |
263 vars[variable4] = value4; | 265 vars[variable4] = value4; |
264 vars[variable5] = value5; | 266 vars[variable5] = value5; |
265 vars[variable6] = value6; | 267 vars[variable6] = value6; |
266 vars[variable7] = value7; | 268 vars[variable7] = value7; |
267 Print(vars, text); | 269 Print(vars, text); |
268 } | 270 } |
269 | 271 |
270 void Printer::Print(const char* text, | 272 void Printer::Print(const char* text, |
271 const char* variable1, const string& value1, | 273 const char* variable1, const string& value1, |
272 const char* variable2, const string& value2, | 274 const char* variable2, const string& value2, |
273 const char* variable3, const string& value3, | 275 const char* variable3, const string& value3, |
274 const char* variable4, const string& value4, | 276 const char* variable4, const string& value4, |
275 const char* variable5, const string& value5, | 277 const char* variable5, const string& value5, |
276 const char* variable6, const string& value6, | 278 const char* variable6, const string& value6, |
277 const char* variable7, const string& value7, | 279 const char* variable7, const string& value7, |
278 const char* variable8, const string& value8) { | 280 const char* variable8, const string& value8) { |
279 map<string, string> vars; | 281 std::map<string, string> vars; |
280 vars[variable1] = value1; | 282 vars[variable1] = value1; |
281 vars[variable2] = value2; | 283 vars[variable2] = value2; |
282 vars[variable3] = value3; | 284 vars[variable3] = value3; |
283 vars[variable4] = value4; | 285 vars[variable4] = value4; |
284 vars[variable5] = value5; | 286 vars[variable5] = value5; |
285 vars[variable6] = value6; | 287 vars[variable6] = value6; |
286 vars[variable7] = value7; | 288 vars[variable7] = value7; |
287 vars[variable8] = value8; | 289 vars[variable8] = value8; |
288 Print(vars, text); | 290 Print(vars, text); |
289 } | 291 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 // Buffer is big enough to receive the data; copy it. | 339 // Buffer is big enough to receive the data; copy it. |
338 memcpy(buffer_, data, size); | 340 memcpy(buffer_, data, size); |
339 buffer_ += size; | 341 buffer_ += size; |
340 buffer_size_ -= size; | 342 buffer_size_ -= size; |
341 offset_ += size; | 343 offset_ += size; |
342 } | 344 } |
343 | 345 |
344 } // namespace io | 346 } // namespace io |
345 } // namespace protobuf | 347 } // namespace protobuf |
346 } // namespace google | 348 } // namespace google |
OLD | NEW |