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 std::pair<size_t, size_t>* range) { | 73 pair<size_t, size_t>* range) { |
74 std::map<string, std::pair<size_t, size_t> >::const_iterator iter = | 74 map<string, 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 std::vector<int>& path) { | 90 const string& file_path, const 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 std::pair<size_t, size_t> begin, end; | 95 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 std::map<string, string>& variables, | 109 void Printer::Print(const map<string, string>& variables, const char* text) { |
110 const char* text) { | |
111 int size = strlen(text); | 110 int size = strlen(text); |
112 int pos = 0; // The number of bytes we've written so far. | 111 int pos = 0; // The number of bytes we've written so far. |
113 substitutions_.clear(); | 112 substitutions_.clear(); |
114 | 113 |
115 for (int i = 0; i < size; i++) { | 114 for (int i = 0; i < size; i++) { |
116 if (text[i] == '\n') { | 115 if (text[i] == '\n') { |
117 // Saw newline. If there is more text, we may need to insert an indent | 116 // Saw newline. If there is more text, we may need to insert an indent |
118 // here. So, write what we have so far, including the '\n'. | 117 // here. So, write what we have so far, including the '\n'. |
119 WriteRaw(text + pos, i - pos + 1); | 118 WriteRaw(text + pos, i - pos + 1); |
120 pos = i + 1; | 119 pos = i + 1; |
(...skipping 16 matching lines...) Expand all Loading... |
137 end = text + pos; | 136 end = text + pos; |
138 } | 137 } |
139 int endpos = end - text; | 138 int endpos = end - text; |
140 | 139 |
141 string varname(text + pos, endpos - pos); | 140 string varname(text + pos, endpos - pos); |
142 if (varname.empty()) { | 141 if (varname.empty()) { |
143 // Two delimiters in a row reduce to a literal delimiter character. | 142 // Two delimiters in a row reduce to a literal delimiter character. |
144 WriteRaw(&variable_delimiter_, 1); | 143 WriteRaw(&variable_delimiter_, 1); |
145 } else { | 144 } else { |
146 // Replace with the variable's value. | 145 // Replace with the variable's value. |
147 std::map<string, string>::const_iterator iter = variables.find(varname); | 146 map<string, string>::const_iterator iter = variables.find(varname); |
148 if (iter == variables.end()) { | 147 if (iter == variables.end()) { |
149 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; | 148 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; |
150 } else { | 149 } else { |
151 size_t begin = offset_; | 150 size_t begin = offset_; |
152 WriteRaw(iter->second.data(), iter->second.size()); | 151 WriteRaw(iter->second.data(), iter->second.size()); |
153 std::pair<std::map<string, std::pair<size_t, size_t> >::iterator, | 152 pair<map<string, pair<size_t, size_t> >::iterator, bool> inserted = |
154 bool> | 153 substitutions_.insert( |
155 inserted = substitutions_.insert( | |
156 std::make_pair(varname, std::make_pair(begin, offset_))); | 154 std::make_pair(varname, std::make_pair(begin, offset_))); |
157 if (!inserted.second) { | 155 if (!inserted.second) { |
158 // This variable was used multiple times. Make its span have | 156 // This variable was used multiple times. Make its span have |
159 // negative length so we can detect it if it gets used in an | 157 // negative length so we can detect it if it gets used in an |
160 // annotation. | 158 // annotation. |
161 inserted.first->second = std::make_pair(1, 0); | 159 inserted.first->second = std::make_pair(1, 0); |
162 } | 160 } |
163 } | 161 } |
164 } | 162 } |
165 | 163 |
166 // Advance past this variable. | 164 // Advance past this variable. |
167 i = endpos; | 165 i = endpos; |
168 pos = endpos + 1; | 166 pos = endpos + 1; |
169 } | 167 } |
170 } | 168 } |
171 | 169 |
172 // Write the rest. | 170 // Write the rest. |
173 WriteRaw(text + pos, size - pos); | 171 WriteRaw(text + pos, size - pos); |
174 } | 172 } |
175 | 173 |
176 void Printer::Print(const char* text) { | 174 void Printer::Print(const char* text) { |
177 static std::map<string, string> empty; | 175 static map<string, string> empty; |
178 Print(empty, text); | 176 Print(empty, text); |
179 } | 177 } |
180 | 178 |
181 void Printer::Print(const char* text, | 179 void Printer::Print(const char* text, |
182 const char* variable, const string& value) { | 180 const char* variable, const string& value) { |
183 std::map<string, string> vars; | 181 map<string, string> vars; |
184 vars[variable] = value; | 182 vars[variable] = value; |
185 Print(vars, text); | 183 Print(vars, text); |
186 } | 184 } |
187 | 185 |
188 void Printer::Print(const char* text, | 186 void Printer::Print(const char* text, |
189 const char* variable1, const string& value1, | 187 const char* variable1, const string& value1, |
190 const char* variable2, const string& value2) { | 188 const char* variable2, const string& value2) { |
191 std::map<string, string> vars; | 189 map<string, string> vars; |
192 vars[variable1] = value1; | 190 vars[variable1] = value1; |
193 vars[variable2] = value2; | 191 vars[variable2] = value2; |
194 Print(vars, text); | 192 Print(vars, text); |
195 } | 193 } |
196 | 194 |
197 void Printer::Print(const char* text, | 195 void Printer::Print(const char* text, |
198 const char* variable1, const string& value1, | 196 const char* variable1, const string& value1, |
199 const char* variable2, const string& value2, | 197 const char* variable2, const string& value2, |
200 const char* variable3, const string& value3) { | 198 const char* variable3, const string& value3) { |
201 std::map<string, string> vars; | 199 map<string, string> vars; |
202 vars[variable1] = value1; | 200 vars[variable1] = value1; |
203 vars[variable2] = value2; | 201 vars[variable2] = value2; |
204 vars[variable3] = value3; | 202 vars[variable3] = value3; |
205 Print(vars, text); | 203 Print(vars, text); |
206 } | 204 } |
207 | 205 |
208 void Printer::Print(const char* text, | 206 void Printer::Print(const char* text, |
209 const char* variable1, const string& value1, | 207 const char* variable1, const string& value1, |
210 const char* variable2, const string& value2, | 208 const char* variable2, const string& value2, |
211 const char* variable3, const string& value3, | 209 const char* variable3, const string& value3, |
212 const char* variable4, const string& value4) { | 210 const char* variable4, const string& value4) { |
213 std::map<string, string> vars; | 211 map<string, string> vars; |
214 vars[variable1] = value1; | 212 vars[variable1] = value1; |
215 vars[variable2] = value2; | 213 vars[variable2] = value2; |
216 vars[variable3] = value3; | 214 vars[variable3] = value3; |
217 vars[variable4] = value4; | 215 vars[variable4] = value4; |
218 Print(vars, text); | 216 Print(vars, text); |
219 } | 217 } |
220 | 218 |
221 void Printer::Print(const char* text, | 219 void Printer::Print(const char* text, |
222 const char* variable1, const string& value1, | 220 const char* variable1, const string& value1, |
223 const char* variable2, const string& value2, | 221 const char* variable2, const string& value2, |
224 const char* variable3, const string& value3, | 222 const char* variable3, const string& value3, |
225 const char* variable4, const string& value4, | 223 const char* variable4, const string& value4, |
226 const char* variable5, const string& value5) { | 224 const char* variable5, const string& value5) { |
227 std::map<string, string> vars; | 225 map<string, string> vars; |
228 vars[variable1] = value1; | 226 vars[variable1] = value1; |
229 vars[variable2] = value2; | 227 vars[variable2] = value2; |
230 vars[variable3] = value3; | 228 vars[variable3] = value3; |
231 vars[variable4] = value4; | 229 vars[variable4] = value4; |
232 vars[variable5] = value5; | 230 vars[variable5] = value5; |
233 Print(vars, text); | 231 Print(vars, text); |
234 } | 232 } |
235 | 233 |
236 void Printer::Print(const char* text, | 234 void Printer::Print(const char* text, |
237 const char* variable1, const string& value1, | 235 const char* variable1, const string& value1, |
238 const char* variable2, const string& value2, | 236 const char* variable2, const string& value2, |
239 const char* variable3, const string& value3, | 237 const char* variable3, const string& value3, |
240 const char* variable4, const string& value4, | 238 const char* variable4, const string& value4, |
241 const char* variable5, const string& value5, | 239 const char* variable5, const string& value5, |
242 const char* variable6, const string& value6) { | 240 const char* variable6, const string& value6) { |
243 std::map<string, string> vars; | 241 map<string, string> vars; |
244 vars[variable1] = value1; | 242 vars[variable1] = value1; |
245 vars[variable2] = value2; | 243 vars[variable2] = value2; |
246 vars[variable3] = value3; | 244 vars[variable3] = value3; |
247 vars[variable4] = value4; | 245 vars[variable4] = value4; |
248 vars[variable5] = value5; | 246 vars[variable5] = value5; |
249 vars[variable6] = value6; | 247 vars[variable6] = value6; |
250 Print(vars, text); | 248 Print(vars, text); |
251 } | 249 } |
252 | 250 |
253 void Printer::Print(const char* text, | 251 void Printer::Print(const char* text, |
254 const char* variable1, const string& value1, | 252 const char* variable1, const string& value1, |
255 const char* variable2, const string& value2, | 253 const char* variable2, const string& value2, |
256 const char* variable3, const string& value3, | 254 const char* variable3, const string& value3, |
257 const char* variable4, const string& value4, | 255 const char* variable4, const string& value4, |
258 const char* variable5, const string& value5, | 256 const char* variable5, const string& value5, |
259 const char* variable6, const string& value6, | 257 const char* variable6, const string& value6, |
260 const char* variable7, const string& value7) { | 258 const char* variable7, const string& value7) { |
261 std::map<string, string> vars; | 259 map<string, string> vars; |
262 vars[variable1] = value1; | 260 vars[variable1] = value1; |
263 vars[variable2] = value2; | 261 vars[variable2] = value2; |
264 vars[variable3] = value3; | 262 vars[variable3] = value3; |
265 vars[variable4] = value4; | 263 vars[variable4] = value4; |
266 vars[variable5] = value5; | 264 vars[variable5] = value5; |
267 vars[variable6] = value6; | 265 vars[variable6] = value6; |
268 vars[variable7] = value7; | 266 vars[variable7] = value7; |
269 Print(vars, text); | 267 Print(vars, text); |
270 } | 268 } |
271 | 269 |
272 void Printer::Print(const char* text, | 270 void Printer::Print(const char* text, |
273 const char* variable1, const string& value1, | 271 const char* variable1, const string& value1, |
274 const char* variable2, const string& value2, | 272 const char* variable2, const string& value2, |
275 const char* variable3, const string& value3, | 273 const char* variable3, const string& value3, |
276 const char* variable4, const string& value4, | 274 const char* variable4, const string& value4, |
277 const char* variable5, const string& value5, | 275 const char* variable5, const string& value5, |
278 const char* variable6, const string& value6, | 276 const char* variable6, const string& value6, |
279 const char* variable7, const string& value7, | 277 const char* variable7, const string& value7, |
280 const char* variable8, const string& value8) { | 278 const char* variable8, const string& value8) { |
281 std::map<string, string> vars; | 279 map<string, string> vars; |
282 vars[variable1] = value1; | 280 vars[variable1] = value1; |
283 vars[variable2] = value2; | 281 vars[variable2] = value2; |
284 vars[variable3] = value3; | 282 vars[variable3] = value3; |
285 vars[variable4] = value4; | 283 vars[variable4] = value4; |
286 vars[variable5] = value5; | 284 vars[variable5] = value5; |
287 vars[variable6] = value6; | 285 vars[variable6] = value6; |
288 vars[variable7] = value7; | 286 vars[variable7] = value7; |
289 vars[variable8] = value8; | 287 vars[variable8] = value8; |
290 Print(vars, text); | 288 Print(vars, text); |
291 } | 289 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 // Buffer is big enough to receive the data; copy it. | 337 // Buffer is big enough to receive the data; copy it. |
340 memcpy(buffer_, data, size); | 338 memcpy(buffer_, data, size); |
341 buffer_ += size; | 339 buffer_ += size; |
342 buffer_size_ -= size; | 340 buffer_size_ -= size; |
343 offset_ += size; | 341 offset_ += size; |
344 } | 342 } |
345 | 343 |
346 } // namespace io | 344 } // namespace io |
347 } // namespace protobuf | 345 } // namespace protobuf |
348 } // namespace google | 346 } // namespace google |
OLD | NEW |