Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:30

0001 // ----------------------------------------------------------------------
0002 // definition of type encoding/decoding functions
0003 // ----------------------------------------------------------------------
0004 
0005 // ----------------------------------------------------------------------
0006 // prerequisite source files and headers
0007 // ----------------------------------------------------------------------
0008 
0009 #include "FWCore/ParameterSet/interface/types.h"
0010 
0011 #include "FWCore/ParameterSet/src/split.h"
0012 #include "FWCore/Utilities/interface/Parse.h"
0013 #include <cctype>
0014 #include <cstdlib>
0015 #include <limits>
0016 #include <sstream>
0017 #include <stdexcept>
0018 #include <cassert>
0019 
0020 using namespace edm;
0021 
0022 // ----------------------------------------------------------------------
0023 // utility functions
0024 // ----------------------------------------------------------------------
0025 
0026 static char to_hex(unsigned int i) { return i + (i < 10u ? '0' : ('A' - 10)); }
0027 
0028 // ----------------------------------------------------------------------
0029 
0030 static unsigned int from_hex(char c) {
0031   switch (c) {
0032     case '0':
0033     case '1':
0034     case '2':
0035     case '3':
0036     case '4':
0037     case '5':
0038     case '6':
0039     case '7':
0040     case '8':
0041     case '9':
0042       return c - '0';
0043     case 'a':
0044     case 'b':
0045     case 'c':
0046     case 'd':
0047     case 'e':
0048     case 'f':
0049       return 10 + c - 'a';
0050     case 'A':
0051     case 'B':
0052     case 'C':
0053     case 'D':
0054     case 'E':
0055     case 'F':
0056       return 10 + c - 'A';
0057     default:
0058       return 0;
0059   }
0060 }  // from_hex()
0061 
0062 static void append_hex_rep(std::string& s, unsigned int c) {
0063   s += to_hex(c / 16u);
0064   s += to_hex(c % 16u);
0065 }  // append_hex_rep()
0066 
0067 // ----------------------------------------------------------------------
0068 // Bool
0069 // ----------------------------------------------------------------------
0070 
0071 bool edm::decode(bool& to, std::string const& from) {
0072   if (from == "true") {
0073     to = true;
0074     return true;
0075   } else if (from == "false") {
0076     to = false;
0077     return true;
0078   } else {
0079     return false;
0080   }
0081 }  // decode to bool
0082 
0083 // ----------------------------------------------------------------------
0084 
0085 bool edm::encode(std::string& to, bool from) {
0086   to = from ? "true" : "false";
0087   return true;
0088 }  // encode from bool
0089 
0090 // ----------------------------------------------------------------------
0091 // vBool
0092 // ----------------------------------------------------------------------
0093 
0094 bool edm::decode(std::vector<bool>& to, std::string const& from) {
0095   std::vector<std::string> temp;
0096   if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
0097     return false;
0098   }
0099 
0100   to.clear();
0101   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0102     bool val = false;
0103     if (!decode(val, *b)) {
0104       return false;
0105     }
0106     to.push_back(val);
0107   }
0108   return true;
0109 }  // decode to vector<bool>
0110 
0111 // ----------------------------------------------------------------------
0112 
0113 bool edm::encode(std::string& to, std::vector<bool> const& from) {
0114   to = "{";
0115 
0116   std::string converted;
0117   for (std::vector<bool>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0118     if (!encode(converted, *b)) {
0119       return false;
0120     }
0121     if (b != from.begin()) {
0122       to += ",";
0123     }
0124     to += converted;
0125   }
0126   to += '}';
0127   return true;
0128 }  // encode from vector<bool>
0129 
0130 // ----------------------------------------------------------------------
0131 // Int32
0132 // ----------------------------------------------------------------------
0133 
0134 bool edm::decode(int& to, std::string const& from) {
0135   std::string::const_iterator b = from.begin(), e = from.end();
0136 
0137   if (*b != '+' && *b != '-') {
0138     return false;
0139   }
0140   int sign = (*b == '+') ? +1 : -1;
0141 
0142   to = 0;
0143   while (++b != e) {
0144     if (!std::isdigit(*b)) {
0145       return false;
0146     }
0147     to = 10 * to + (*b - '0');
0148   }
0149   to *= sign;
0150 
0151   return true;
0152 }  // decode to int
0153 
0154 // ----------------------------------------------------------------------
0155 
0156 bool edm::encode(std::string& to, int from) {
0157   bool is_negative = (from < 0);
0158   if (is_negative) {
0159     from = -from;  // TODO: work around this for most negative integer
0160   }
0161   to.clear();
0162   do {
0163     to = static_cast<char>(from % 10 + '0') + to;
0164     from /= 10;
0165   } while (from > 0);
0166   to = (is_negative ? '-' : '+') + to;
0167 
0168   return true;
0169 }  // encode from int
0170 
0171 // ----------------------------------------------------------------------
0172 // Int64
0173 // ----------------------------------------------------------------------
0174 
0175 bool edm::decode(long long& to, std::string const& from) {
0176   std::string::const_iterator b = from.begin(), e = from.end();
0177 
0178   if (*b != '+' && *b != '-') {
0179     return false;
0180   }
0181   int sign = (*b == '+') ? +1 : -1;
0182 
0183   to = 0;
0184   while (++b != e) {
0185     if (!std::isdigit(*b)) {
0186       return false;
0187     }
0188     to = 10 * to + (*b - '0');
0189   }
0190   to *= sign;
0191 
0192   return true;
0193 }  // decode to int
0194 
0195 // ----------------------------------------------------------------------
0196 
0197 bool edm::encode(std::string& to, long long from) {
0198   bool is_negative = (from < 0);
0199   if (is_negative) {
0200     from = -from;  // TODO: work around this for most negative integer
0201   }
0202 
0203   to.clear();
0204   do {
0205     to = static_cast<char>(from % 10 + '0') + to;
0206     from /= 10;
0207   } while (from > 0);
0208   to = (is_negative ? '-' : '+') + to;
0209 
0210   return true;
0211 }  // encode from int
0212 
0213 // ----------------------------------------------------------------------
0214 // vInt32
0215 // ----------------------------------------------------------------------
0216 
0217 bool edm::decode(std::vector<int>& to, std::string const& from) {
0218   std::vector<std::string> temp;
0219   if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
0220     return false;
0221   }
0222 
0223   to.clear();
0224   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0225     int val = 0;
0226     if (!decode(val, *b)) {
0227       return false;
0228     }
0229     to.push_back(val);
0230   }
0231 
0232   return true;
0233 }  // decode to vector<int>
0234 
0235 // ----------------------------------------------------------------------
0236 
0237 bool edm::encode(std::string& to, std::vector<int> const& from) {
0238   to = "{";
0239 
0240   std::string converted;
0241   for (std::vector<int>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0242     if (!encode(converted, *b)) {
0243       return false;
0244     }
0245 
0246     if (b != from.begin()) {
0247       to += ",";
0248     }
0249     to += converted;
0250   }
0251 
0252   to += '}';
0253   return true;
0254 }  // encode from vector<int>
0255 
0256 // ----------------------------------------------------------------------
0257 // vInt64
0258 // ----------------------------------------------------------------------
0259 
0260 bool edm::decode(std::vector<long long>& to, std::string const& from) {
0261   std::vector<std::string> temp;
0262   if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
0263     return false;
0264   }
0265 
0266   to.clear();
0267   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0268     long long val = 0LL;
0269     if (!decode(val, *b)) {
0270       return false;
0271     }
0272     to.push_back(val);
0273   }
0274 
0275   return true;
0276 }  // decode to vector<int>
0277 
0278 // ----------------------------------------------------------------------
0279 
0280 bool edm::encode(std::string& to, std::vector<long long> const& from) {
0281   to = "{";
0282 
0283   std::string converted;
0284   for (std::vector<long long>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0285     if (!encode(converted, *b)) {
0286       return false;
0287     }
0288     if (b != from.begin()) {
0289       to += ",";
0290     }
0291     to += converted;
0292   }
0293   to += '}';
0294   return true;
0295 }  // encode from vector<int>
0296 
0297 // ----------------------------------------------------------------------
0298 // Uint32
0299 // ----------------------------------------------------------------------
0300 
0301 bool edm::decode(unsigned int& to, std::string const& from) {
0302   std::string::const_iterator b = from.begin(), e = from.end();
0303 
0304   to = 0u;
0305   for (; b != e; ++b) {
0306     if (*b == 'u' || *b == 'U') {
0307       return true;
0308     }
0309     if (!std::isdigit(*b)) {
0310       return false;
0311     }
0312     to = 10u * to + (*b - '0');
0313   }
0314   return true;
0315 }  // decode to unsigned
0316 
0317 // ----------------------------------------------------------------------
0318 
0319 bool edm::encode(std::string& to, unsigned int from) {
0320   to.clear();
0321   do {
0322     to = static_cast<char>(from % 10 + '0') + to;
0323     from /= 10u;
0324   } while (from > 0u);
0325 
0326   return true;
0327 }  // encode from unsigned
0328 
0329 // ----------------------------------------------------------------------
0330 // Uint64
0331 // ----------------------------------------------------------------------
0332 
0333 bool edm::decode(unsigned long long& to, std::string const& from) {
0334   std::string::const_iterator b = from.begin(), e = from.end();
0335   to = 0u;
0336   for (; b != e; ++b) {
0337     if (*b == 'u' || *b == 'U') {
0338       return true;
0339     }
0340     if (!std::isdigit(*b)) {
0341       return false;
0342     }
0343     to = 10u * to + (*b - '0');
0344   }
0345   return true;
0346 }  // decode to unsigned
0347 
0348 // ----------------------------------------------------------------------
0349 
0350 bool edm::encode(std::string& to, unsigned long long from) {
0351   to.clear();
0352   do {
0353     to = static_cast<char>(from % 10 + '0') + to;
0354     from /= 10u;
0355   } while (from > 0u);
0356 
0357   return true;
0358 }  // encode from unsigned
0359 
0360 // ----------------------------------------------------------------------
0361 // vUint32
0362 // ----------------------------------------------------------------------
0363 
0364 bool edm::decode(std::vector<unsigned int>& to, std::string const& from) {
0365   std::vector<std::string> temp;
0366   if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
0367     return false;
0368   }
0369   to.clear();
0370   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0371     unsigned int val = 0;
0372     if (!decode(val, *b)) {
0373       return false;
0374     }
0375     to.push_back(val);
0376   }
0377 
0378   return true;
0379 }  // decode to vector<unsigned int>
0380 
0381 // ----------------------------------------------------------------------
0382 
0383 bool edm::encode(std::string& to, std::vector<unsigned int> const& from) {
0384   to = "{";
0385 
0386   std::string converted;
0387   for (std::vector<unsigned int>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0388     if (!encode(converted, *b)) {
0389       return false;
0390     }
0391     if (b != from.begin()) {
0392       to += ",";
0393     }
0394     to += converted;
0395   }
0396 
0397   to += '}';
0398   return true;
0399 }  // encode from vector<unsigned int>
0400 
0401 // ----------------------------------------------------------------------
0402 // vUint64
0403 // ----------------------------------------------------------------------
0404 
0405 bool edm::decode(std::vector<unsigned long long>& to, std::string const& from) {
0406   std::vector<std::string> temp;
0407   if (!split(std::back_inserter(temp), from, '{', ',', '}')) {
0408     return false;
0409   }
0410   to.clear();
0411   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0412     unsigned long long val = 0ULL;
0413     if (!decode(val, *b)) {
0414       return false;
0415     }
0416     to.push_back(val);
0417   }
0418 
0419   return true;
0420 }  // decode to vector<unsigned int>
0421 
0422 // ----------------------------------------------------------------------
0423 
0424 bool edm::encode(std::string& to, std::vector<unsigned long long> const& from) {
0425   to = "{";
0426 
0427   std::string converted;
0428   for (std::vector<unsigned long long>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0429     if (!encode(converted, *b)) {
0430       return false;
0431     }
0432 
0433     if (b != from.begin()) {
0434       to += ",";
0435     }
0436     to += converted;
0437   }
0438 
0439   to += '}';
0440   return true;
0441 }  // encode from vector<unsigned int>
0442 
0443 // ----------------------------------------------------------------------
0444 // Double
0445 // ----------------------------------------------------------------------
0446 
0447 bool edm::decode(double& to, std::string const& from) {
0448   if (from == "NaN") {
0449     to = std::numeric_limits<double>::quiet_NaN();
0450   } else if (from == "+inf" || from == "inf") {
0451     to = std::numeric_limits<double>::has_infinity ? std::numeric_limits<double>::infinity()
0452                                                    : std::numeric_limits<double>::max();
0453   } else if (from == "-inf") {
0454     to = std::numeric_limits<double>::has_infinity ? -std::numeric_limits<double>::infinity()
0455                                                    : -std::numeric_limits<double>::max();
0456   }
0457 
0458   else {
0459     try {
0460       // std::cerr << "from:" << from << std::endl;
0461       to = std::stod(from);
0462       // std::cerr << "to:" << to << std::endl;
0463     } catch (const std::exception&) {
0464       return false;
0465     }
0466   }
0467   return true;
0468 }
0469 
0470 // ----------------------------------------------------------------------
0471 
0472 bool edm::encode(std::string& to, double from) {
0473   std::ostringstream ost;
0474   ost.precision(std::numeric_limits<double>::max_digits10);
0475   ost << from;
0476   if (!ost)
0477     return false;
0478   to = ost.str();
0479   return true;
0480 }
0481 
0482 // ----------------------------------------------------------------------
0483 // vDouble
0484 // ----------------------------------------------------------------------
0485 
0486 bool edm::decode(std::vector<double>& to, std::string const& from) {
0487   std::vector<std::string> temp;
0488   if (!split(std::back_inserter(temp), from, '{', ',', '}'))
0489     return false;
0490 
0491   to.clear();
0492   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
0493     double val;
0494     if (!decode(val, *b))
0495       return false;
0496     to.push_back(val);
0497   }
0498 
0499   return true;
0500 }  // decode to vector<double>
0501 
0502 // ----------------------------------------------------------------------
0503 
0504 bool edm::encode(std::string& to, std::vector<double> const& from) {
0505   to = "{";
0506 
0507   std::string converted;
0508   for (std::vector<double>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
0509     if (!encode(converted, *b))
0510       return false;
0511 
0512     if (b != from.begin())
0513       to += ",";
0514     to += converted;
0515   }
0516 
0517   to += '}';
0518   return true;
0519 }  // encode from vector<double>
0520 
0521 // ----------------------------------------------------------------------
0522 // String
0523 // ----------------------------------------------------------------------
0524 
0525 bool edm::decode(std::string& to, std::string const& from) {
0526   /*std::cerr << "Decoding: " << from << '\n'; //DEBUG*/
0527   std::string::const_iterator b = from.begin(), e = from.end();
0528 
0529   to = "";
0530   to.reserve((e - b) / 2);
0531   char c = '\0';
0532   for (bool even_pos = true; b != e; ++b, even_pos = !even_pos) {
0533     if (even_pos) {
0534       /*std::cerr << "Even: |"
0535                   << *b
0536                   << "|   giving "
0537                   << from_hex(*b)
0538                   << "\n"; //DEBUG*/
0539       c = static_cast<char>(from_hex(*b));
0540     } else {
0541       /*std::cerr << "Odd:  |"
0542                   << *b
0543                   << "|   giving "
0544                   << from_hex(*b)
0545                   << "\n"; //DEBUG*/
0546       c = static_cast<char>(c * 16 + from_hex(*b));
0547       //      if(std::isalnum(c))  {
0548       /*std::cerr << "Ans:  |" << c << "|\n"; //DEBUG*/
0549       to += c;
0550       //}
0551       //else  { // keep all special chars encoded
0552       //to += "\\x";
0553       //to += to_hex_rep(c);
0554       //}
0555     }
0556   }
0557   /*std::cerr << "Decoded: " << to << '\n'; //DEBUG*/
0558   return true;
0559 }  // decode to String
0560 
0561 // ----------------------------------------------------------------------
0562 // FileInPath
0563 // ----------------------------------------------------------------------
0564 
0565 bool edm::decode(FileInPath& to, std::string const& from) {
0566   std::istringstream is(from);
0567   FileInPath temp;
0568   temp.readFromParameterSetBlob(is);
0569   if (!is)
0570     return false;
0571   to = temp;
0572   return true;
0573 }  // decode to FileInPath
0574 
0575 bool edm::encode(std::string& to, FileInPath const& from) {
0576   std::ostringstream ost;
0577   ost << from;
0578   if (!ost)
0579     return false;
0580   to = ost.str();
0581   return true;
0582 }
0583 
0584 // ----------------------------------------------------------------------
0585 // InputTag
0586 // ----------------------------------------------------------------------
0587 
0588 bool edm::decode(InputTag& to, std::string const& from) {
0589   to = InputTag(from);
0590   return true;
0591 }  // decode to InputTag
0592 
0593 bool edm::encode(std::string& to, InputTag const& from) {
0594   to = from.encode();
0595   return true;
0596 }
0597 
0598 // ----------------------------------------------------------------------
0599 // VInputTag
0600 // ----------------------------------------------------------------------
0601 
0602 bool edm::decode(std::vector<InputTag>& to, std::string const& from) {
0603   std::vector<std::string> strings;
0604   decode(strings, from);
0605 
0606   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0607        stringItr != stringItrEnd;
0608        ++stringItr) {
0609     to.push_back(InputTag(*stringItr));
0610   }
0611   return true;
0612 }  // decode to VInputTag
0613 
0614 bool edm::encode(std::string& to, std::vector<InputTag> const& from) {
0615   std::vector<std::string> strings;
0616   for (std::vector<InputTag>::const_iterator tagItr = from.begin(), tagItrEnd = from.end(); tagItr != tagItrEnd;
0617        ++tagItr) {
0618     strings.push_back(tagItr->encode());
0619   }
0620   encode(to, strings);
0621   return true;
0622 }
0623 
0624 // ----------------------------------------------------------------------
0625 // ESInputTag
0626 // ----------------------------------------------------------------------
0627 
0628 bool edm::decode(ESInputTag& to, std::string const& from) {
0629   if (not from.empty() and std::string::npos == from.find(':')) {
0630     to = ESInputTag(from, "");
0631   } else {
0632     to = ESInputTag(from);
0633   }
0634   return true;
0635 }  // decode to InputTag
0636 
0637 bool edm::encode(std::string& to, ESInputTag const& from) {
0638   to = from.encode();
0639   if (not to.empty() and to.back() == ':') {
0640     to.pop_back();
0641   }
0642   return true;
0643 }
0644 
0645 // ----------------------------------------------------------------------
0646 // VESInputTag
0647 // ----------------------------------------------------------------------
0648 
0649 bool edm::decode(std::vector<ESInputTag>& to, std::string const& from) {
0650   std::vector<std::string> strings;
0651   decode(strings, from);
0652 
0653   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0654        stringItr != stringItrEnd;
0655        ++stringItr) {
0656     to.push_back(ESInputTag(*stringItr));
0657   }
0658   return true;
0659 }  // decode to VInputTag
0660 
0661 bool edm::encode(std::string& to, std::vector<ESInputTag> const& from) {
0662   std::vector<std::string> strings;
0663   for (std::vector<ESInputTag>::const_iterator tagItr = from.begin(), tagItrEnd = from.end(); tagItr != tagItrEnd;
0664        ++tagItr) {
0665     strings.push_back(tagItr->encode());
0666   }
0667   encode(to, strings);
0668   return true;
0669 }
0670 
0671 // ----------------------------------------------------------------------
0672 // EventID
0673 // ----------------------------------------------------------------------
0674 
0675 bool edm::decode(edm::EventID& to, std::string const& from) {
0676   std::vector<std::string> tokens = edm::tokenize(from, ":");
0677   assert(tokens.size() == 2 || tokens.size() == 3);
0678   unsigned int run = strtoul(tokens[0].c_str(), nullptr, 0);
0679   unsigned int lumi = (tokens.size() == 2 ? 0 : strtoul(tokens[1].c_str(), nullptr, 0));
0680   unsigned long long event = strtoull(tokens[tokens.size() - 1].c_str(), nullptr, 0);
0681   to = edm::EventID(run, lumi, event);
0682 
0683   return true;
0684 }  // decode to EventID
0685 
0686 bool edm::encode(std::string& to, edm::EventID const& from) {
0687   std::ostringstream os;
0688   if (from.luminosityBlock() == 0U) {
0689     os << from.run() << ":" << from.event();
0690   } else {
0691     os << from.run() << ":" << from.luminosityBlock() << ":" << from.event();
0692   }
0693   to = os.str();
0694   return true;
0695 }
0696 
0697 // ----------------------------------------------------------------------
0698 // VEventID
0699 // ----------------------------------------------------------------------
0700 
0701 bool edm::decode(std::vector<edm::EventID>& to, std::string const& from) {
0702   std::vector<std::string> strings;
0703   decode(strings, from);
0704 
0705   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0706        stringItr != stringItrEnd;
0707        ++stringItr) {
0708     edm::EventID eventID;
0709     decode(eventID, *stringItr);
0710     to.push_back(eventID);
0711   }
0712   return true;
0713 }  // decode to VInputTag
0714 
0715 bool edm::encode(std::string& to, std::vector<edm::EventID> const& from) {
0716   std::vector<std::string> strings;
0717   for (std::vector<edm::EventID>::const_iterator idItr = from.begin(), idItrEnd = from.end(); idItr != idItrEnd;
0718        ++idItr) {
0719     std::string encodedEventID;
0720     encode(encodedEventID, *idItr);
0721     strings.push_back(encodedEventID);
0722   }
0723   encode(to, strings);
0724   return true;
0725 }
0726 
0727 // ----------------------------------------------------------------------
0728 // LuminosityBlockID
0729 // ----------------------------------------------------------------------
0730 
0731 bool edm::decode(edm::LuminosityBlockID& to, std::string const& from) {
0732   std::vector<std::string> tokens = edm::tokenize(from, ":");
0733   assert(tokens.size() == 2);
0734   unsigned int run = strtoul(tokens[0].c_str(), nullptr, 0);
0735   unsigned int lumi = strtoul(tokens[1].c_str(), nullptr, 0);
0736   to = edm::LuminosityBlockID(run, lumi);
0737   return true;
0738 }  // decode to LuminosityBlockID
0739 
0740 bool edm::encode(std::string& to, edm::LuminosityBlockID const& from) {
0741   std::ostringstream os;
0742   os << from.run() << ":" << from.luminosityBlock();
0743   to = os.str();
0744   return true;
0745 }
0746 
0747 // ----------------------------------------------------------------------
0748 // VLuminosityBlockID
0749 // ----------------------------------------------------------------------
0750 
0751 bool edm::decode(std::vector<edm::LuminosityBlockID>& to, std::string const& from) {
0752   std::vector<std::string> strings;
0753   decode(strings, from);
0754 
0755   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0756        stringItr != stringItrEnd;
0757        ++stringItr) {
0758     edm::LuminosityBlockID lumiID;
0759     decode(lumiID, *stringItr);
0760     to.push_back(lumiID);
0761   }
0762   return true;
0763 }  // decode to VInputTag
0764 
0765 bool edm::encode(std::string& to, std::vector<edm::LuminosityBlockID> const& from) {
0766   std::vector<std::string> strings;
0767   for (std::vector<edm::LuminosityBlockID>::const_iterator idItr = from.begin(), idItrEnd = from.end();
0768        idItr != idItrEnd;
0769        ++idItr) {
0770     std::string encodedLuminosityBlockID;
0771     encode(encodedLuminosityBlockID, *idItr);
0772     strings.push_back(encodedLuminosityBlockID);
0773   }
0774   encode(to, strings);
0775   return true;
0776 }
0777 
0778 // ----------------------------------------------------------------------
0779 // LuminosityBlockRange
0780 // ----------------------------------------------------------------------
0781 
0782 bool edm::decode(edm::LuminosityBlockRange& to, std::string const& from) {
0783   std::vector<std::string> tokens = edm::tokenize(from, "-");
0784   assert(tokens.size() == 2);
0785   edm::LuminosityBlockID begin;
0786   edm::LuminosityBlockID end;
0787   edm::decode(begin, tokens[0]);
0788   edm::decode(end, tokens[1]);
0789   to = edm::LuminosityBlockRange(begin.run(), begin.luminosityBlock(), end.run(), end.luminosityBlock());
0790   return true;
0791 }  // decode to LuminosityBlockRange
0792 
0793 bool edm::encode(std::string& to, edm::LuminosityBlockRange const& from) {
0794   std::ostringstream os;
0795   os << from.startRun() << ":" << from.startLumi() << "-" << from.endRun() << ":" << from.endLumi();
0796   to = os.str();
0797   return true;
0798 }
0799 
0800 // ----------------------------------------------------------------------
0801 // VLuminosityBlockRange
0802 // ----------------------------------------------------------------------
0803 
0804 bool edm::decode(std::vector<edm::LuminosityBlockRange>& to, std::string const& from) {
0805   std::vector<std::string> strings;
0806   decode(strings, from);
0807 
0808   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0809        stringItr != stringItrEnd;
0810        ++stringItr) {
0811     edm::LuminosityBlockRange lumiRange;
0812     decode(lumiRange, *stringItr);
0813     to.push_back(lumiRange);
0814   }
0815   return true;
0816 }  // decode to VInputTag
0817 
0818 bool edm::encode(std::string& to, std::vector<edm::LuminosityBlockRange> const& from) {
0819   std::vector<std::string> strings;
0820   for (std::vector<edm::LuminosityBlockRange>::const_iterator idItr = from.begin(), idItrEnd = from.end();
0821        idItr != idItrEnd;
0822        ++idItr) {
0823     std::string encodedLuminosityBlockRange;
0824     encode(encodedLuminosityBlockRange, *idItr);
0825     strings.push_back(encodedLuminosityBlockRange);
0826   }
0827   encode(to, strings);
0828   return true;
0829 }
0830 
0831 // ----------------------------------------------------------------------
0832 // EventRange
0833 // ----------------------------------------------------------------------
0834 
0835 bool edm::decode(edm::EventRange& to, std::string const& from) {
0836   std::vector<std::string> tokens = edm::tokenize(from, "-");
0837   assert(tokens.size() == 2);
0838   edm::EventID begin;
0839   edm::EventID end;
0840   edm::decode(begin, tokens[0]);
0841   edm::decode(end, tokens[1]);
0842   assert((begin.luminosityBlock() == 0) == (end.luminosityBlock() == 0));
0843   to = edm::EventRange(
0844       begin.run(), begin.luminosityBlock(), begin.event(), end.run(), end.luminosityBlock(), end.event());
0845   return true;
0846 }  // decode to EventRange
0847 
0848 bool edm::encode(std::string& to, edm::EventRange const& from) {
0849   std::ostringstream os;
0850   if (from.startLumi() == 0) {
0851     assert(from.endLumi() == 0);
0852     os << from.startRun() << ":" << from.startEvent() << "-" << from.endRun() << ":" << from.endEvent();
0853   } else {
0854     assert(from.endLumi() != 0);
0855     os << from.startRun() << ":" << from.startLumi() << ":" << from.startEvent() << "-" << from.endRun() << ":"
0856        << from.endLumi() << ":" << from.endEvent();
0857   }
0858   to = os.str();
0859   return true;
0860 }
0861 
0862 // ----------------------------------------------------------------------
0863 // VEventRange
0864 // ----------------------------------------------------------------------
0865 
0866 bool edm::decode(std::vector<edm::EventRange>& to, std::string const& from) {
0867   std::vector<std::string> strings;
0868   decode(strings, from);
0869 
0870   for (std::vector<std::string>::const_iterator stringItr = strings.begin(), stringItrEnd = strings.end();
0871        stringItr != stringItrEnd;
0872        ++stringItr) {
0873     edm::EventRange eventRange;
0874     decode(eventRange, *stringItr);
0875     to.push_back(eventRange);
0876   }
0877   return true;
0878 }
0879 
0880 bool edm::encode(std::string& to, std::vector<edm::EventRange> const& from) {
0881   std::vector<std::string> strings;
0882   for (std::vector<edm::EventRange>::const_iterator idItr = from.begin(), idItrEnd = from.end(); idItr != idItrEnd;
0883        ++idItr) {
0884     std::string encodedEventRange;
0885     encode(encodedEventRange, *idItr);
0886     strings.push_back(encodedEventRange);
0887   }
0888   encode(to, strings);
0889   return true;
0890 }
0891 
0892 // ----------------------------------------------------------------------
0893 
0894 bool edm::encode(std::string& to, std::string const& from) {
0895   std::string::const_iterator b = from.begin(), e = from.end();
0896 
0897   enum escape_state { NONE, BACKSLASH, HEX, HEX1, OCT1, OCT2 };
0898 
0899   escape_state state = NONE;
0900   int code = 0;
0901   to = "";
0902   for (; b != e; ++b) {
0903     /*std::cerr << "State: " << state << "; char = " << *b << '\n'; //DEBUG*/
0904     switch (state) {
0905       case NONE: {
0906         if (*b == '\\')
0907           state = BACKSLASH;
0908         else
0909           append_hex_rep(to, *b);
0910         /*std::cerr << "To: |" << to << "|\n"; //DEBUG*/
0911         break;
0912       }
0913       case BACKSLASH: {
0914         code = 0;
0915         switch (*b) {
0916           case 'x':
0917           case 'X': {
0918             state = HEX;
0919             break;
0920           }
0921           case '0':
0922           case '1':
0923           case '2':
0924           case '3':
0925           case '4':
0926           case '5':
0927           case '6':
0928           case '7': {
0929             code = 8 * code + from_hex(*b);
0930             state = OCT1;
0931             break;
0932           }
0933           case 'n': {
0934             append_hex_rep(to, 10);
0935             state = NONE;
0936             break;
0937           }
0938           case 't': {
0939             append_hex_rep(to, 9);
0940             state = NONE;
0941             break;
0942           }
0943           default: {
0944             append_hex_rep(to, *b);
0945             state = NONE;
0946             break;
0947           }
0948         }
0949         break;
0950       }
0951       case HEX: {
0952         to += *b;
0953         state = HEX1;
0954         break;
0955       }
0956       case HEX1: {
0957         to += *b;
0958         state = NONE;
0959         break;
0960       }
0961       case OCT1: {
0962         switch (*b) {
0963           case '0':
0964           case '1':
0965           case '2':
0966           case '3':
0967           case '4':
0968           case '5':
0969           case '6':
0970           case '7': {
0971             code = 8 * code + from_hex(*b);
0972             state = OCT2;
0973             break;
0974           }
0975           default: {
0976             append_hex_rep(to, code);
0977             state = NONE;
0978             break;
0979           }
0980         }
0981         break;
0982       }
0983       case OCT2: {
0984         switch (*b) {
0985           case '0':
0986           case '1':
0987           case '2':
0988           case '3':
0989           case '4':
0990           case '5':
0991           case '6':
0992           case '7': {
0993             code = 8 * code + from_hex(*b);
0994             break;
0995           }
0996           default: {
0997             append_hex_rep(to, code);
0998             break;
0999           }
1000         }
1001         state = NONE;
1002         break;
1003       }
1004       default: {
1005         throw std::logic_error("can't happen");
1006         break;
1007       }
1008     }
1009   }  // for
1010 
1011   return true;
1012 }  // encode from String
1013 
1014 // ----------------------------------------------------------------------
1015 // vString
1016 // ----------------------------------------------------------------------
1017 
1018 bool edm::decode(std::vector<std::string>& to, std::string const& from) {
1019   std::vector<std::string> temp;
1020   if (!split(std::back_inserter(temp), from, '{', ',', '}'))
1021     return false;
1022 
1023   to.clear();
1024   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
1025     std::string val;
1026     // treat blank string specially
1027     if (*b == "XXX") {
1028       val = "";
1029     } else if (!decode(val, *b)) {
1030       return false;
1031     }
1032     to.push_back(val);
1033   }
1034 
1035   return true;
1036 }  // decode to vector<string>
1037 
1038 // ----------------------------------------------------------------------
1039 
1040 bool edm::encode(std::string& to, std::vector<std::string> const& from) {
1041   to = "{";
1042 
1043   std::string converted;
1044   for (std::vector<std::string>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
1045     // treat blank string specially
1046     if (b->empty()) {
1047       converted = "XXX";
1048     } else if (!encode(converted, *b)) {
1049       return false;
1050     }
1051 
1052     if (b != from.begin())
1053       to += ",";
1054     to += converted;
1055   }
1056 
1057   to += '}';
1058   return true;
1059 }  // encode from vector<string>
1060 
1061 // ----------------------------------------------------------------------
1062 // ParameterSet
1063 // ----------------------------------------------------------------------
1064 
1065 bool edm::decode(ParameterSet& to, std::string const& from) {
1066   to = ParameterSet(from);
1067   return true;
1068 }  // decode to ParameterSet
1069 
1070 // ----------------------------------------------------------------------
1071 
1072 bool edm::encode(std::string& to, ParameterSet const& from) {
1073   to = from.toString();
1074   return true;
1075 }  // encode from ParameterSet
1076 
1077 // ----------------------------------------------------------------------
1078 // vPSet
1079 // ----------------------------------------------------------------------
1080 
1081 bool edm::decode(std::vector<ParameterSet>& to, std::string const& from) {
1082   std::vector<std::string> temp;
1083   if (!split(std::back_inserter(temp), from, '{', ',', '}'))
1084     return false;
1085 
1086   to.clear();
1087   for (std::vector<std::string>::const_iterator b = temp.begin(), e = temp.end(); b != e; ++b) {
1088     ParameterSet val;
1089     if (!decode(val, *b)) {
1090       return false;
1091     }
1092     to.push_back(val);
1093   }
1094 
1095   return true;
1096 }  // decode to vector<ParameterSet>
1097 
1098 // ----------------------------------------------------------------------
1099 
1100 bool edm::encode(std::string& to, std::vector<ParameterSet> const& from) {
1101   to = "{";
1102 
1103   std::string converted;
1104   for (std::vector<ParameterSet>::const_iterator b = from.begin(), e = from.end(); b != e; ++b) {
1105     if (!encode(converted, *b)) {
1106       return false;
1107     }
1108     if (b != from.begin()) {
1109       to += ",";
1110     }
1111     to += converted;
1112   }
1113   to += '}';
1114   return true;
1115 }  // encode from vector<ParameterSet>
1116 
1117 // ----------------------------------------------------------------------