File indexing completed on 2024-04-06 12:20:28
0001 #include "L1Trigger/L1TCommon/interface/Parameter.h"
0002 #include <cstdlib>
0003 #include <memory>
0004
0005 using namespace std;
0006
0007 namespace l1t {
0008
0009 Parameter::Parameter(
0010 const char *id, const char *procOrRole, const char *type, const char *value, const char *delimeter) {
0011 this->id = id;
0012 this->procOrRole = procOrRole;
0013 this->type = type;
0014 this->scalarOrVector = value;
0015 this->delim = delimeter;
0016 }
0017
0018 Parameter::Parameter(const char *id,
0019 const char *procOrRole,
0020 const char *types,
0021 const char *columns,
0022 const vector<string> &rows,
0023 const char *delimeter) {
0024 this->id = id;
0025 this->procOrRole = procOrRole;
0026 this->type = types;
0027
0028 map<int, string> colIndexToName;
0029 unique_ptr<char, void (*)(void *)> copy(strdup(columns), free);
0030 unsigned long nItems = 0;
0031 char *saveptr;
0032 for (const char *item = strtok_r(copy.get(), delimeter, &saveptr); item != nullptr;
0033 item = strtok_r(nullptr, delimeter, &saveptr), nItems++) {
0034
0035 size_t pos = 0, len = strlen(item);
0036 while (pos < len && isspace(item[pos]))
0037 pos++;
0038 while (len > 0 && isspace(item[--len]))
0039 ;
0040 string str = (pos < len + 1 ? string(item + pos, len + 1 - pos) : item);
0041
0042 colIndexToName.insert(make_pair(nItems, str));
0043 columnNameToIndex.insert(make_pair(str, nItems));
0044 if (table.insert(make_pair(str, vector<string>(rows.size()))).second == false)
0045 throw runtime_error("Duplicate column name: '" + str + "'");
0046 }
0047
0048 for (unsigned int r = 0; r < rows.size(); r++) {
0049 unique_ptr<char, void (*)(void *)> copy(strdup(rows[r].c_str()), free);
0050 for (unsigned int pos = 0; pos < nItems; pos++) {
0051 char *item = strtok_r((pos == 0 ? copy.get() : nullptr), delimeter, &saveptr);
0052 if (item == nullptr)
0053 throw runtime_error("Too few elements in '" + rows[r] + "'");
0054
0055
0056 size_t p = 0, len = strlen(item);
0057 while (p < len && isspace(item[p]))
0058 p++;
0059 while (len > 0 && isspace(item[--len]))
0060 ;
0061
0062 table[colIndexToName[pos]][r] = (pos < len + 1 ? string(item + p, len + 1 - p) : item);
0063 }
0064 if (strtok_r(nullptr, delimeter, &saveptr) != nullptr)
0065 throw runtime_error("Too many elements in '" + rows[r] + "', expected " + to_string(nItems));
0066 }
0067
0068 this->delim = delimeter;
0069 }
0070
0071
0072 template <>
0073 long long castTo<long long>(const char *arg) {
0074 char *endptr = nullptr;
0075 long long retval = strtoll(arg, &endptr, 0);
0076 if (*endptr == '\0')
0077 return retval;
0078 else
0079 throw runtime_error("Cannot convert '" + string(arg) + "' to integral type");
0080 }
0081
0082
0083 template <>
0084 bool castTo<bool>(const char *arg) {
0085 if (strlen(arg) > 3) {
0086
0087 if (strstr(arg, "true") != nullptr && strstr(arg, "false") == nullptr)
0088 return true;
0089
0090 if (strstr(arg, "true") == nullptr && strstr(arg, "false") != nullptr)
0091 return false;
0092 }
0093
0094 char *endptr = nullptr;
0095 long retval = strtol(arg, &endptr, 0);
0096 if (*endptr == '\0')
0097 return retval;
0098
0099 throw runtime_error("Cannot convert '" + string(arg) + "' to boolean");
0100 }
0101
0102 template <>
0103 char castTo<char>(const char *arg) {
0104 return castTo<long long>(arg);
0105 }
0106 template <>
0107 short castTo<short>(const char *arg) {
0108 return castTo<long long>(arg);
0109 }
0110 template <>
0111 int castTo<int>(const char *arg) {
0112 return castTo<long long>(arg);
0113 }
0114 template <>
0115 long castTo<long>(const char *arg) {
0116 return castTo<long long>(arg);
0117 }
0118
0119 template <>
0120 long double castTo<long double>(const char *arg) {
0121 char *endptr = nullptr;
0122 long double retval = strtold(arg, &endptr);
0123 if (*endptr == '\0')
0124 return retval;
0125 else
0126 throw runtime_error("Cannot convert '" + string(arg) + "' to floating point type");
0127 }
0128 template <>
0129 float castTo<float>(const char *arg) {
0130 return castTo<long double>(arg);
0131 }
0132 template <>
0133 double castTo<double>(const char *arg) {
0134 return castTo<long double>(arg);
0135 }
0136
0137 template <>
0138 unsigned long long castTo<unsigned long long>(const char *arg) {
0139 char *endptr = nullptr;
0140 unsigned long long retval = strtoull(arg, &endptr, 0);
0141 if (*endptr == '\0')
0142 return retval;
0143 else
0144 throw runtime_error("Cannot convert '" + string(arg) + "' to unsigned integral type");
0145 }
0146 template <>
0147 unsigned char castTo<unsigned char>(const char *arg) {
0148 return castTo<unsigned long long>(arg);
0149 }
0150 template <>
0151 unsigned short castTo<unsigned short>(const char *arg) {
0152 return castTo<unsigned long long>(arg);
0153 }
0154 template <>
0155 unsigned int castTo<unsigned int>(const char *arg) {
0156 return castTo<unsigned long long>(arg);
0157 }
0158 template <>
0159 unsigned long castTo<unsigned long>(const char *arg) {
0160 return castTo<unsigned long long>(arg);
0161 }
0162
0163 }