CMS 3D CMS Logo

types.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 // definition of type encoding/decoding functions
3 // ----------------------------------------------------------------------
4 
5 // ----------------------------------------------------------------------
6 // prerequisite source files and headers
7 // ----------------------------------------------------------------------
8 
10 
11 #include "boost/lexical_cast.hpp"
14 #include <cctype>
15 #include <cstdlib>
16 #include <limits>
17 #include <sstream>
18 #include <stdexcept>
19 #include <cassert>
20 
21 using namespace edm;
22 
23 // ----------------------------------------------------------------------
24 // utility functions
25 // ----------------------------------------------------------------------
26 
27 static char to_hex(unsigned int i) { return i + (i < 10u ? '0' : ('A' - 10)); }
28 
29 // ----------------------------------------------------------------------
30 
31 static unsigned int from_hex(char c) {
32  switch (c) {
33  case '0':
34  case '1':
35  case '2':
36  case '3':
37  case '4':
38  case '5':
39  case '6':
40  case '7':
41  case '8':
42  case '9':
43  return c - '0';
44  case 'a':
45  case 'b':
46  case 'c':
47  case 'd':
48  case 'e':
49  case 'f':
50  return 10 + c - 'a';
51  case 'A':
52  case 'B':
53  case 'C':
54  case 'D':
55  case 'E':
56  case 'F':
57  return 10 + c - 'A';
58  default:
59  return 0;
60  }
61 } // from_hex()
62 
63 static void append_hex_rep(std::string& s, unsigned int c) {
64  s += to_hex(c / 16u);
65  s += to_hex(c % 16u);
66 } // append_hex_rep()
67 
68 // ----------------------------------------------------------------------
69 // Bool
70 // ----------------------------------------------------------------------
71 
72 bool edm::decode(bool& to, std::string const& from) {
73  if (from == "true") {
74  to = true;
75  return true;
76  } else if (from == "false") {
77  to = false;
78  return true;
79  } else {
80  return false;
81  }
82 } // decode to bool
83 
84 // ----------------------------------------------------------------------
85 
86 bool edm::encode(std::string& to, bool from) {
87  to = from ? "true" : "false";
88  return true;
89 } // encode from bool
90 
91 // ----------------------------------------------------------------------
92 // vBool
93 // ----------------------------------------------------------------------
94 
95 bool edm::decode(std::vector<bool>& to, std::string const& from) {
96  std::vector<std::string> temp;
97  if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
98  return false;
99  }
100 
101  to.clear();
102  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
103  bool val = false;
104  if (!decode(val, *b)) {
105  return false;
106  }
107  to.push_back(val);
108  }
109  return true;
110 } // decode to vector<bool>
111 
112 // ----------------------------------------------------------------------
113 
114 bool edm::encode(std::string& to, std::vector<bool> const& from) {
115  to = "{";
116 
117  std::string converted;
118  for (std::vector<bool>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
119  if (!encode(converted, *b)) {
120  return false;
121  }
122  if (b != from.begin()) {
123  to += ",";
124  }
125  to += converted;
126  }
127  to += '}';
128  return true;
129 } // encode from vector<bool>
130 
131 // ----------------------------------------------------------------------
132 // Int32
133 // ----------------------------------------------------------------------
134 
135 bool edm::decode(int& to, std::string const& from) {
136  std::string::const_iterator b = from.begin(), e = from.end();
137 
138  if (*b != '+' && *b != '-') {
139  return false;
140  }
141  int sign = (*b == '+') ? +1 : -1;
142 
143  to = 0;
144  while (++b != e) {
145  if (!std::isdigit(*b)) {
146  return false;
147  }
148  to = 10 * to + (*b - '0');
149  }
150  to *= sign;
151 
152  return true;
153 } // decode to int
154 
155 // ----------------------------------------------------------------------
156 
157 bool edm::encode(std::string& to, int from) {
158  bool is_negative = (from < 0);
159  if (is_negative) {
160  from = -from; // TODO: work around this for most negative integer
161  }
162  to.clear();
163  do {
164  to = static_cast<char>(from % 10 + '0') + to;
165  from /= 10;
166  } while (from > 0);
167  to = (is_negative ? '-' : '+') + to;
168 
169  return true;
170 } // encode from int
171 
172 // ----------------------------------------------------------------------
173 // Int64
174 // ----------------------------------------------------------------------
175 
176 bool edm::decode(long long& to, std::string const& from) {
177  std::string::const_iterator b = from.begin(), e = from.end();
178 
179  if (*b != '+' && *b != '-') {
180  return false;
181  }
182  int sign = (*b == '+') ? +1 : -1;
183 
184  to = 0;
185  while (++b != e) {
186  if (!std::isdigit(*b)) {
187  return false;
188  }
189  to = 10 * to + (*b - '0');
190  }
191  to *= sign;
192 
193  return true;
194 } // decode to int
195 
196 // ----------------------------------------------------------------------
197 
198 bool edm::encode(std::string& to, long long from) {
199  bool is_negative = (from < 0);
200  if (is_negative) {
201  from = -from; // TODO: work around this for most negative integer
202  }
203 
204  to.clear();
205  do {
206  to = static_cast<char>(from % 10 + '0') + to;
207  from /= 10;
208  } while (from > 0);
209  to = (is_negative ? '-' : '+') + to;
210 
211  return true;
212 } // encode from int
213 
214 // ----------------------------------------------------------------------
215 // vInt32
216 // ----------------------------------------------------------------------
217 
218 bool edm::decode(std::vector<int>& to, std::string const& from) {
219  std::vector<std::string> temp;
220  if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
221  return false;
222  }
223 
224  to.clear();
225  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
226  int val = 0;
227  if (!decode(val, *b)) {
228  return false;
229  }
230  to.push_back(val);
231  }
232 
233  return true;
234 } // decode to vector<int>
235 
236 // ----------------------------------------------------------------------
237 
238 bool edm::encode(std::string& to, std::vector<int> const& from) {
239  to = "{";
240 
241  std::string converted;
242  for (std::vector<int>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
243  if (!encode(converted, *b)) {
244  return false;
245  }
246 
247  if (b != from.begin()) {
248  to += ",";
249  }
250  to += converted;
251  }
252 
253  to += '}';
254  return true;
255 } // encode from vector<int>
256 
257 // ----------------------------------------------------------------------
258 // vInt64
259 // ----------------------------------------------------------------------
260 
261 bool edm::decode(std::vector<long long>& to, std::string const& from) {
262  std::vector<std::string> temp;
263  if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
264  return false;
265  }
266 
267  to.clear();
268  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
269  long long val = 0LL;
270  if (!decode(val, *b)) {
271  return false;
272  }
273  to.push_back(val);
274  }
275 
276  return true;
277 } // decode to vector<int>
278 
279 // ----------------------------------------------------------------------
280 
281 bool edm::encode(std::string& to, std::vector<long long> const& from) {
282  to = "{";
283 
284  std::string converted;
285  for (std::vector<long long>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
286  if (!encode(converted, *b)) {
287  return false;
288  }
289  if (b != from.begin()) {
290  to += ",";
291  }
292  to += converted;
293  }
294  to += '}';
295  return true;
296 } // encode from vector<int>
297 
298 // ----------------------------------------------------------------------
299 // Uint32
300 // ----------------------------------------------------------------------
301 
302 bool edm::decode(unsigned int& to, std::string const& from) {
303  std::string::const_iterator b = from.begin(), e = from.end();
304 
305  to = 0u;
306  for (; b != e; ++b) {
307  if (*b == 'u' || *b == 'U') {
308  return true;
309  }
310  if (!std::isdigit(*b)) {
311  return false;
312  }
313  to = 10u * to + (*b - '0');
314  }
315  return true;
316 } // decode to unsigned
317 
318 // ----------------------------------------------------------------------
319 
320 bool edm::encode(std::string& to, unsigned int from) {
321  to.clear();
322  do {
323  to = static_cast<char>(from % 10 + '0') + to;
324  from /= 10u;
325  } while (from > 0u);
326 
327  return true;
328 } // encode from unsigned
329 
330 // ----------------------------------------------------------------------
331 // Uint64
332 // ----------------------------------------------------------------------
333 
334 bool edm::decode(unsigned long long& to, std::string const& from) {
335  std::string::const_iterator b = from.begin(), e = from.end();
336  to = 0u;
337  for (; b != e; ++b) {
338  if (*b == 'u' || *b == 'U') {
339  return true;
340  }
341  if (!std::isdigit(*b)) {
342  return false;
343  }
344  to = 10u * to + (*b - '0');
345  }
346  return true;
347 } // decode to unsigned
348 
349 // ----------------------------------------------------------------------
350 
351 bool edm::encode(std::string& to, unsigned long long from) {
352  to.clear();
353  do {
354  to = static_cast<char>(from % 10 + '0') + to;
355  from /= 10u;
356  } while (from > 0u);
357 
358  return true;
359 } // encode from unsigned
360 
361 // ----------------------------------------------------------------------
362 // vUint32
363 // ----------------------------------------------------------------------
364 
365 bool edm::decode(std::vector<unsigned int>& to, std::string const& from) {
366  std::vector<std::string> temp;
367  if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
368  return false;
369  }
370  to.clear();
371  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
372  unsigned int val = 0;
373  if (!decode(val, *b)) {
374  return false;
375  }
376  to.push_back(val);
377  }
378 
379  return true;
380 } // decode to vector<unsigned int>
381 
382 // ----------------------------------------------------------------------
383 
384 bool edm::encode(std::string& to, std::vector<unsigned int> const& from) {
385  to = "{";
386 
387  std::string converted;
388  for (std::vector<unsigned int>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
389  if (!encode(converted, *b)) {
390  return false;
391  }
392  if (b != from.begin()) {
393  to += ",";
394  }
395  to += converted;
396  }
397 
398  to += '}';
399  return true;
400 } // encode from vector<unsigned int>
401 
402 // ----------------------------------------------------------------------
403 // vUint64
404 // ----------------------------------------------------------------------
405 
406 bool edm::decode(std::vector<unsigned long long>& to, std::string const& from) {
407  std::vector<std::string> temp;
408  if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
409  return false;
410  }
411  to.clear();
412  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
413  unsigned long long val = 0ULL;
414  if (!decode(val, *b)) {
415  return false;
416  }
417  to.push_back(val);
418  }
419 
420  return true;
421 } // decode to vector<unsigned int>
422 
423 // ----------------------------------------------------------------------
424 
425 bool edm::encode(std::string& to, std::vector<unsigned long long> const& from) {
426  to = "{";
427 
428  std::string converted;
429  for (std::vector<unsigned long long>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
430  if (!encode(converted, *b)) {
431  return false;
432  }
433 
434  if (b != from.begin()) {
435  to += ",";
436  }
437  to += converted;
438  }
439 
440  to += '}';
441  return true;
442 } // encode from vector<unsigned int>
443 
444 // ----------------------------------------------------------------------
445 // Double
446 // ----------------------------------------------------------------------
447 
448 bool edm::decode(double& to, std::string const& from) {
449  if (from == "NaN") {
450  to = std::numeric_limits<double>::quiet_NaN();
451  } else if (from == "+inf" || from == "inf") {
452  to = std::numeric_limits<double>::has_infinity ? std::numeric_limits<double>::infinity()
454  } else if (from == "-inf") {
455  to = std::numeric_limits<double>::has_infinity ? -std::numeric_limits<double>::infinity()
457  }
458 
459  else {
460  try {
461  // std::cerr << "from:" << from << std::endl;
462  to = boost::lexical_cast<double>(from);
463  // std::cerr << "to:" << to << std::endl;
464  } catch (boost::bad_lexical_cast&) {
465  return false;
466  }
467  }
468  return true;
469 }
470 
471 // ----------------------------------------------------------------------
472 
473 bool edm::encode(std::string& to, double from) {
474  std::ostringstream ost;
475  ost.precision(std::numeric_limits<double>::digits10 + 1);
476  ost << from;
477  if (!ost)
478  return false;
479  to = ost.str();
480  return true;
481 }
482 
483 // ----------------------------------------------------------------------
484 // vDouble
485 // ----------------------------------------------------------------------
486 
487 bool edm::decode(std::vector<double>& to, std::string const& from) {
488  std::vector<std::string> temp;
489  if (!split(std::back_inserter(temp), from, '{', ',', '}'))
490  return false;
491 
492  to.clear();
493  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
494  double val;
495  if (!decode(val, *b))
496  return false;
497  to.push_back(val);
498  }
499 
500  return true;
501 } // decode to vector<double>
502 
503 // ----------------------------------------------------------------------
504 
505 bool edm::encode(std::string& to, std::vector<double> const& from) {
506  to = "{";
507 
508  std::string converted;
509  for (std::vector<double>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
510  if (!encode(converted, *b))
511  return false;
512 
513  if (b != from.begin())
514  to += ",";
515  to += converted;
516  }
517 
518  to += '}';
519  return true;
520 } // encode from vector<double>
521 
522 // ----------------------------------------------------------------------
523 // String
524 // ----------------------------------------------------------------------
525 
526 bool edm::decode(std::string& to, std::string const& from) {
527  /*std::cerr << "Decoding: " << from << '\n'; //DEBUG*/
528  std::string::const_iterator b = from.begin(), e = from.end();
529 
530  to = "";
531  to.reserve((e - b) / 2);
532  char c = '\0';
533  for (bool even_pos = true; b != e; ++b, even_pos = !even_pos) {
534  if (even_pos) {
535  /*std::cerr << "Even: |"
536  << *b
537  << "| giving "
538  << from_hex(*b)
539  << "\n"; //DEBUG*/
540  c = static_cast<char>(from_hex(*b));
541  } else {
542  /*std::cerr << "Odd: |"
543  << *b
544  << "| giving "
545  << from_hex(*b)
546  << "\n"; //DEBUG*/
547  c = static_cast<char>(c * 16 + from_hex(*b));
548  // if(std::isalnum(c)) {
549  /*std::cerr << "Ans: |" << c << "|\n"; //DEBUG*/
550  to += c;
551  //}
552  //else { // keep all special chars encoded
553  //to += "\\x";
554  //to += to_hex_rep(c);
555  //}
556  }
557  }
558  /*std::cerr << "Decoded: " << to << '\n'; //DEBUG*/
559  return true;
560 } // decode to String
561 
562 // ----------------------------------------------------------------------
563 // FileInPath
564 // ----------------------------------------------------------------------
565 
566 bool edm::decode(FileInPath& to, std::string const& from) {
567  std::istringstream is(from);
569  temp.readFromParameterSetBlob(is);
570  if (!is)
571  return false;
572  to = temp;
573  return true;
574 } // decode to FileInPath
575 
576 bool edm::encode(std::string& to, FileInPath const& from) {
577  std::ostringstream ost;
578  ost << from;
579  if (!ost)
580  return false;
581  to = ost.str();
582  return true;
583 }
584 
585 // ----------------------------------------------------------------------
586 // InputTag
587 // ----------------------------------------------------------------------
588 
589 bool edm::decode(InputTag& to, std::string const& from) {
590  to = InputTag(from);
591  return true;
592 } // decode to InputTag
593 
594 bool edm::encode(std::string& to, InputTag const& from) {
595  to = from.encode();
596  return true;
597 }
598 
599 // ----------------------------------------------------------------------
600 // VInputTag
601 // ----------------------------------------------------------------------
602 
603 bool edm::decode(std::vector<InputTag>& to, std::string const& from) {
604  std::vector<std::string> strings;
605  decode(strings, from);
606 
607  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
608  stringItr != stringItrEnd;
609  ++stringItr) {
610  to.push_back(InputTag(*stringItr));
611  }
612  return true;
613 } // decode to VInputTag
614 
615 bool edm::encode(std::string& to, std::vector<InputTag> const& from) {
616  std::vector<std::string> strings;
617  for (std::vector<InputTag>::const_iterator tagItr = from.begin(), tagItrEnd = from.end(); tagItr != tagItrEnd;
618  ++tagItr) {
619  strings.push_back(tagItr->encode());
620  }
621  encode(to, strings);
622  return true;
623 }
624 
625 // ----------------------------------------------------------------------
626 // ESInputTag
627 // ----------------------------------------------------------------------
628 
629 bool edm::decode(ESInputTag& to, std::string const& from) {
630  to = ESInputTag(from);
631  return true;
632 } // decode to InputTag
633 
634 bool edm::encode(std::string& to, ESInputTag const& from) {
635  to = from.encode();
636  return true;
637 }
638 
639 // ----------------------------------------------------------------------
640 // VESInputTag
641 // ----------------------------------------------------------------------
642 
643 bool edm::decode(std::vector<ESInputTag>& to, std::string const& from) {
644  std::vector<std::string> strings;
645  decode(strings, from);
646 
647  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
648  stringItr != stringItrEnd;
649  ++stringItr) {
650  to.push_back(ESInputTag(*stringItr));
651  }
652  return true;
653 } // decode to VInputTag
654 
655 bool edm::encode(std::string& to, std::vector<ESInputTag> const& from) {
656  std::vector<std::string> strings;
657  for (std::vector<ESInputTag>::const_iterator tagItr = from.begin(), tagItrEnd = from.end(); tagItr != tagItrEnd;
658  ++tagItr) {
659  strings.push_back(tagItr->encode());
660  }
661  encode(to, strings);
662  return true;
663 }
664 
665 // ----------------------------------------------------------------------
666 // EventID
667 // ----------------------------------------------------------------------
668 
669 bool edm::decode(edm::EventID& to, std::string const& from) {
670  std::vector<std::string> tokens = edm::tokenize(from, ":");
671  assert(tokens.size() == 2 || tokens.size() == 3);
672  unsigned int run = strtoul(tokens[0].c_str(), nullptr, 0);
673  unsigned int lumi = (tokens.size() == 2 ? 0 : strtoul(tokens[1].c_str(), nullptr, 0));
674  unsigned long long event = strtoull(tokens[tokens.size() - 1].c_str(), nullptr, 0);
675  to = edm::EventID(run, lumi, event);
676 
677  return true;
678 } // decode to EventID
679 
680 bool edm::encode(std::string& to, edm::EventID const& from) {
681  std::ostringstream os;
682  if (from.luminosityBlock() == 0U) {
683  os << from.run() << ":" << from.event();
684  } else {
685  os << from.run() << ":" << from.luminosityBlock() << ":" << from.event();
686  }
687  to = os.str();
688  return true;
689 }
690 
691 // ----------------------------------------------------------------------
692 // VEventID
693 // ----------------------------------------------------------------------
694 
695 bool edm::decode(std::vector<edm::EventID>& to, std::string const& from) {
696  std::vector<std::string> strings;
697  decode(strings, from);
698 
699  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
700  stringItr != stringItrEnd;
701  ++stringItr) {
702  edm::EventID eventID;
703  decode(eventID, *stringItr);
704  to.push_back(eventID);
705  }
706  return true;
707 } // decode to VInputTag
708 
709 bool edm::encode(std::string& to, std::vector<edm::EventID> const& from) {
710  std::vector<std::string> strings;
711  for (std::vector<edm::EventID>::const_iterator idItr = from.begin(), idItrEnd = from.end(); idItr != idItrEnd;
712  ++idItr) {
713  std::string encodedEventID;
714  encode(encodedEventID, *idItr);
715  strings.push_back(encodedEventID);
716  }
717  encode(to, strings);
718  return true;
719 }
720 
721 // ----------------------------------------------------------------------
722 // LuminosityBlockID
723 // ----------------------------------------------------------------------
724 
726  std::vector<std::string> tokens = edm::tokenize(from, ":");
727  assert(tokens.size() == 2);
728  unsigned int run = strtoul(tokens[0].c_str(), nullptr, 0);
729  unsigned int lumi = strtoul(tokens[1].c_str(), nullptr, 0);
730  to = edm::LuminosityBlockID(run, lumi);
731  return true;
732 } // decode to LuminosityBlockID
733 
735  std::ostringstream os;
736  os << from.run() << ":" << from.luminosityBlock();
737  to = os.str();
738  return true;
739 }
740 
741 // ----------------------------------------------------------------------
742 // VLuminosityBlockID
743 // ----------------------------------------------------------------------
744 
745 bool edm::decode(std::vector<edm::LuminosityBlockID>& to, std::string const& from) {
746  std::vector<std::string> strings;
747  decode(strings, from);
748 
749  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
750  stringItr != stringItrEnd;
751  ++stringItr) {
752  edm::LuminosityBlockID lumiID;
753  decode(lumiID, *stringItr);
754  to.push_back(lumiID);
755  }
756  return true;
757 } // decode to VInputTag
758 
759 bool edm::encode(std::string& to, std::vector<edm::LuminosityBlockID> const& from) {
760  std::vector<std::string> strings;
761  for (std::vector<edm::LuminosityBlockID>::const_iterator idItr = from.begin(), idItrEnd = from.end();
762  idItr != idItrEnd;
763  ++idItr) {
764  std::string encodedLuminosityBlockID;
765  encode(encodedLuminosityBlockID, *idItr);
766  strings.push_back(encodedLuminosityBlockID);
767  }
768  encode(to, strings);
769  return true;
770 }
771 
772 // ----------------------------------------------------------------------
773 // LuminosityBlockRange
774 // ----------------------------------------------------------------------
775 
777  std::vector<std::string> tokens = edm::tokenize(from, "-");
778  assert(tokens.size() == 2);
781  edm::decode(begin, tokens[0]);
782  edm::decode(end, tokens[1]);
783  to = edm::LuminosityBlockRange(begin.run(), begin.luminosityBlock(), end.run(), end.luminosityBlock());
784  return true;
785 } // decode to LuminosityBlockRange
786 
788  std::ostringstream os;
789  os << from.startRun() << ":" << from.startLumi() << "-" << from.endRun() << ":" << from.endLumi();
790  to = os.str();
791  return true;
792 }
793 
794 // ----------------------------------------------------------------------
795 // VLuminosityBlockRange
796 // ----------------------------------------------------------------------
797 
798 bool edm::decode(std::vector<edm::LuminosityBlockRange>& to, std::string const& from) {
799  std::vector<std::string> strings;
800  decode(strings, from);
801 
802  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
803  stringItr != stringItrEnd;
804  ++stringItr) {
805  edm::LuminosityBlockRange lumiRange;
806  decode(lumiRange, *stringItr);
807  to.push_back(lumiRange);
808  }
809  return true;
810 } // decode to VInputTag
811 
812 bool edm::encode(std::string& to, std::vector<edm::LuminosityBlockRange> const& from) {
813  std::vector<std::string> strings;
814  for (std::vector<edm::LuminosityBlockRange>::const_iterator idItr = from.begin(), idItrEnd = from.end();
815  idItr != idItrEnd;
816  ++idItr) {
817  std::string encodedLuminosityBlockRange;
818  encode(encodedLuminosityBlockRange, *idItr);
819  strings.push_back(encodedLuminosityBlockRange);
820  }
821  encode(to, strings);
822  return true;
823 }
824 
825 // ----------------------------------------------------------------------
826 // EventRange
827 // ----------------------------------------------------------------------
828 
830  std::vector<std::string> tokens = edm::tokenize(from, "-");
831  assert(tokens.size() == 2);
834  edm::decode(begin, tokens[0]);
835  edm::decode(end, tokens[1]);
836  assert((begin.luminosityBlock() == 0) == (end.luminosityBlock() == 0));
837  to = edm::EventRange(
838  begin.run(), begin.luminosityBlock(), begin.event(), end.run(), end.luminosityBlock(), end.event());
839  return true;
840 } // decode to EventRange
841 
843  std::ostringstream os;
844  if (from.startLumi() == 0) {
845  assert(from.endLumi() == 0);
846  os << from.startRun() << ":" << from.startEvent() << "-" << from.endRun() << ":" << from.endEvent();
847  } else {
848  assert(from.endLumi() != 0);
849  os << from.startRun() << ":" << from.startLumi() << ":" << from.startEvent() << "-" << from.endRun() << ":"
850  << from.endLumi() << ":" << from.endEvent();
851  }
852  to = os.str();
853  return true;
854 }
855 
856 // ----------------------------------------------------------------------
857 // VEventRange
858 // ----------------------------------------------------------------------
859 
860 bool edm::decode(std::vector<edm::EventRange>& to, std::string const& from) {
861  std::vector<std::string> strings;
862  decode(strings, from);
863 
864  for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
865  stringItr != stringItrEnd;
866  ++stringItr) {
867  edm::EventRange eventRange;
868  decode(eventRange, *stringItr);
869  to.push_back(eventRange);
870  }
871  return true;
872 }
873 
874 bool edm::encode(std::string& to, std::vector<edm::EventRange> const& from) {
875  std::vector<std::string> strings;
876  for (std::vector<edm::EventRange>::const_iterator idItr = from.begin(), idItrEnd = from.end(); idItr != idItrEnd;
877  ++idItr) {
878  std::string encodedEventRange;
879  encode(encodedEventRange, *idItr);
880  strings.push_back(encodedEventRange);
881  }
882  encode(to, strings);
883  return true;
884 }
885 
886 // ----------------------------------------------------------------------
887 
888 bool edm::encode(std::string& to, std::string const& from) {
889  std::string::const_iterator b = from.begin(), e = from.end();
890 
891  enum escape_state { NONE, BACKSLASH, HEX, HEX1, OCT1, OCT2 };
892 
893  escape_state state = NONE;
894  int code = 0;
895  to = "";
896  for (; b != e; ++b) {
897  /*std::cerr << "State: " << state << "; char = " << *b << '\n'; //DEBUG*/
898  switch (state) {
899  case NONE: {
900  if (*b == '\\')
901  state = BACKSLASH;
902  else
903  append_hex_rep(to, *b);
904  /*std::cerr << "To: |" << to << "|\n"; //DEBUG*/
905  break;
906  }
907  case BACKSLASH: {
908  code = 0;
909  switch (*b) {
910  case 'x':
911  case 'X': {
912  state = HEX;
913  break;
914  }
915  case '0':
916  case '1':
917  case '2':
918  case '3':
919  case '4':
920  case '5':
921  case '6':
922  case '7': {
923  code = 8 * code + from_hex(*b);
924  state = OCT1;
925  break;
926  }
927  case 'n': {
928  append_hex_rep(to, 10);
929  state = NONE;
930  break;
931  }
932  case 't': {
933  append_hex_rep(to, 9);
934  state = NONE;
935  break;
936  }
937  default: {
938  append_hex_rep(to, *b);
939  state = NONE;
940  break;
941  }
942  }
943  break;
944  }
945  case HEX: {
946  to += *b;
947  state = HEX1;
948  break;
949  }
950  case HEX1: {
951  to += *b;
952  state = NONE;
953  break;
954  }
955  case OCT1: {
956  switch (*b) {
957  case '0':
958  case '1':
959  case '2':
960  case '3':
961  case '4':
962  case '5':
963  case '6':
964  case '7': {
965  code = 8 * code + from_hex(*b);
966  state = OCT2;
967  break;
968  }
969  default: {
970  append_hex_rep(to, code);
971  state = NONE;
972  break;
973  }
974  }
975  break;
976  }
977  case OCT2: {
978  switch (*b) {
979  case '0':
980  case '1':
981  case '2':
982  case '3':
983  case '4':
984  case '5':
985  case '6':
986  case '7': {
987  code = 8 * code + from_hex(*b);
988  break;
989  }
990  default: {
991  append_hex_rep(to, code);
992  break;
993  }
994  }
995  state = NONE;
996  break;
997  }
998  default: {
999  throw std::logic_error("can't happen");
1000  break;
1001  }
1002  }
1003  } // for
1004 
1005  return true;
1006 } // encode from String
1007 
1008 // ----------------------------------------------------------------------
1009 // vString
1010 // ----------------------------------------------------------------------
1011 
1012 bool edm::decode(std::vector<std::string>& to, std::string const& from) {
1013  std::vector<std::string> temp;
1014  if (!split(std::back_inserter(temp), from, '{', ',', '}'))
1015  return false;
1016 
1017  to.clear();
1018  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
1019  std::string val;
1020  // treat blank string specially
1021  if (*b == "XXX") {
1022  val = "";
1023  } else if (!decode(val, *b)) {
1024  return false;
1025  }
1026  to.push_back(val);
1027  }
1028 
1029  return true;
1030 } // decode to vector<string>
1031 
1032 // ----------------------------------------------------------------------
1033 
1034 bool edm::encode(std::string& to, std::vector<std::string> const& from) {
1035  to = "{";
1036 
1037  std::string converted;
1038  for (std::vector<std::string>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
1039  // treat blank string specially
1040  if (b->empty()) {
1041  converted = "XXX";
1042  } else if (!encode(converted, *b)) {
1043  return false;
1044  }
1045 
1046  if (b != from.begin())
1047  to += ",";
1048  to += converted;
1049  }
1050 
1051  to += '}';
1052  return true;
1053 } // encode from vector<string>
1054 
1055 // ----------------------------------------------------------------------
1056 // ParameterSet
1057 // ----------------------------------------------------------------------
1058 
1060  to = ParameterSet(from);
1061  return true;
1062 } // decode to ParameterSet
1063 
1064 // ----------------------------------------------------------------------
1065 
1067  to = from.toString();
1068  return true;
1069 } // encode from ParameterSet
1070 
1071 // ----------------------------------------------------------------------
1072 // vPSet
1073 // ----------------------------------------------------------------------
1074 
1075 bool edm::decode(std::vector<ParameterSet>& to, std::string const& from) {
1076  std::vector<std::string> temp;
1077  if (!split(std::back_inserter(temp), from, '{', ',', '}'))
1078  return false;
1079 
1080  to.clear();
1081  for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
1082  ParameterSet val;
1083  if (!decode(val, *b)) {
1084  return false;
1085  }
1086  to.push_back(val);
1087  }
1088 
1089  return true;
1090 } // decode to vector<ParameterSet>
1091 
1092 // ----------------------------------------------------------------------
1093 
1094 bool edm::encode(std::string& to, std::vector<ParameterSet> const& from) {
1095  to = "{";
1096 
1097  std::string converted;
1098  for (std::vector<ParameterSet>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
1099  if (!encode(converted, *b)) {
1100  return false;
1101  }
1102  if (b != from.begin()) {
1103  to += ",";
1104  }
1105  to += converted;
1106  }
1107  to += '}';
1108  return true;
1109 } // encode from vector<ParameterSet>
1110 
1111 // ----------------------------------------------------------------------
RunNumber_t run() const
Definition: EventID.h:39
EventNumber_t event() const
Definition: EventID.h:41
std::string toString() const
static char to_hex(unsigned int i)
Definition: types.cc:27
bool encode(std::string &, bool)
Definition: types.cc:86
RunNumber_t startRun() const
Definition: EventRange.h:46
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
std::string encode() const
Definition: InputTag.cc:159
LuminosityBlockNumber_t startLumi() const
static unsigned int from_hex(char c)
Definition: types.cc:31
RunNumber_t endRun() const
Definition: EventRange.h:47
EventNumber_t endEvent() const
Definition: EventRange.h:51
const double infinity
RunNumber_t run() const
bool decode(bool &, std::string const &)
Definition: types.cc:72
bool split(OutIter result, std::string const &string_to_split, char first, char sep, char last)
Definition: split.h:68
#define end
Definition: vmac.h:39
RunNumber_t startRun() const
static void append_hex_rep(std::string &s, unsigned int c)
Definition: types.cc:63
LuminosityBlockNumber_t endLumi() const
Definition: EventRange.h:49
std::string encode() const
Definition: ESInputTag.cc:50
EventNumber_t startEvent() const
Definition: EventRange.h:50
double b
Definition: hdecay.h:120
LuminosityBlockNumber_t luminosityBlock() const
LuminosityBlockNumber_t startLumi() const
Definition: EventRange.h:48
#define begin
Definition: vmac.h:32
HLT enums.
void readFromParameterSetBlob(std::istream &is)
Definition: FileInPath.cc:275
RunNumber_t endRun() const
LuminosityBlockNumber_t endLumi() const
Definition: event.py:1
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator