Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 1 | //===-- NamedOptionValue.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Interpreter/NamedOptionValue.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Stream.h" |
| 17 | #include "lldb/Interpreter/Args.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 22 | |
| 23 | //------------------------------------------------------------------------- |
| 24 | // OptionValue |
| 25 | //------------------------------------------------------------------------- |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 26 | |
| 27 | // Get this value as a uint64_t value if it is encoded as a boolean, |
| 28 | // uint64_t or int64_t. Other types will cause "fail_value" to be |
| 29 | // returned |
| 30 | uint64_t |
| 31 | OptionValue::GetUInt64Value (uint64_t fail_value, bool *success_ptr) |
| 32 | { |
| 33 | if (success_ptr) |
| 34 | *success_ptr = true; |
| 35 | switch (GetType()) |
| 36 | { |
| 37 | case OptionValue::eTypeBoolean: return static_cast<OptionValueBoolean *>(this)->GetCurrentValue(); |
| 38 | case OptionValue::eTypeSInt64: return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue(); |
| 39 | case OptionValue::eTypeUInt64: return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue(); |
| 40 | default: |
| 41 | break; |
| 42 | } |
| 43 | if (success_ptr) |
| 44 | *success_ptr = false; |
| 45 | return fail_value; |
| 46 | } |
| 47 | |
| 48 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 49 | OptionValueBoolean * |
| 50 | OptionValue::GetAsBooleanValue () |
| 51 | { |
| 52 | if (GetType () == OptionValue::eTypeBoolean) |
| 53 | return static_cast<OptionValueBoolean *>(this); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | OptionValueSInt64 * |
| 58 | OptionValue::GetAsSInt64Value () |
| 59 | { |
| 60 | if (GetType () == OptionValue::eTypeSInt64) |
| 61 | return static_cast<OptionValueSInt64 *>(this); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | OptionValueUInt64 * |
| 66 | OptionValue::GetAsUInt64Value () |
| 67 | { |
| 68 | if (GetType () == OptionValue::eTypeUInt64) |
| 69 | return static_cast<OptionValueUInt64 *>(this); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | OptionValueString * |
| 74 | OptionValue::GetAsStringValue () |
| 75 | { |
| 76 | if (GetType () == OptionValue::eTypeString) |
| 77 | return static_cast<OptionValueString *>(this); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | OptionValueFileSpec * |
| 82 | OptionValue::GetAsFileSpecValue () |
| 83 | { |
| 84 | if (GetType () == OptionValue::eTypeFileSpec) |
| 85 | return static_cast<OptionValueFileSpec *>(this); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | OptionValueArray * |
| 90 | OptionValue::GetAsArrayValue () |
| 91 | { |
| 92 | if (GetType () == OptionValue::eTypeArray) |
| 93 | return static_cast<OptionValueArray *>(this); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | OptionValueDictionary * |
| 98 | OptionValue::GetAsDictionaryValue () |
| 99 | { |
| 100 | if (GetType () == OptionValue::eTypeDictionary) |
| 101 | return static_cast<OptionValueDictionary *>(this); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 106 | //------------------------------------------------------------------------- |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame] | 107 | // OptionValueCollection |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 108 | //------------------------------------------------------------------------- |
| 109 | |
| 110 | void |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame] | 111 | OptionValueCollection::GetQualifiedName (Stream &strm) |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 112 | { |
| 113 | if (m_parent) |
| 114 | { |
| 115 | m_parent->GetQualifiedName (strm); |
| 116 | strm.PutChar('.'); |
| 117 | } |
| 118 | strm << m_name; |
| 119 | } |
| 120 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 121 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 122 | //------------------------------------------------------------------------- |
| 123 | // OptionValueBoolean |
| 124 | //------------------------------------------------------------------------- |
| 125 | void |
| 126 | OptionValueBoolean::DumpValue (Stream &strm) |
| 127 | { |
| 128 | strm.PutCString (m_current_value ? "true" : "false"); |
| 129 | } |
| 130 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 131 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 132 | OptionValueBoolean::SetValueFromCString (const char *value_cstr) |
| 133 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 134 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 135 | bool success = false; |
| 136 | bool value = Args::StringToBoolean(value_cstr, false, &success); |
| 137 | if (success) |
| 138 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 139 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 140 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 141 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 142 | else |
| 143 | { |
| 144 | error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr); |
| 145 | } |
| 146 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | //------------------------------------------------------------------------- |
| 150 | // OptionValueSInt64 |
| 151 | //------------------------------------------------------------------------- |
| 152 | void |
| 153 | OptionValueSInt64::DumpValue (Stream &strm) |
| 154 | { |
| 155 | strm.Printf ("%lli", m_current_value); |
| 156 | } |
| 157 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 158 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 159 | OptionValueSInt64::SetValueFromCString (const char *value_cstr) |
| 160 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 161 | |
| 162 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 163 | bool success = false; |
| 164 | int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success); |
| 165 | if (success) |
| 166 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 167 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 168 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 169 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 170 | else |
| 171 | { |
| 172 | error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr); |
| 173 | } |
| 174 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | //------------------------------------------------------------------------- |
| 178 | // OptionValueUInt64 |
| 179 | //------------------------------------------------------------------------- |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 180 | |
| 181 | lldb::OptionValueSP |
| 182 | OptionValueUInt64::Create (const char *value_cstr, Error &error) |
| 183 | { |
| 184 | lldb::OptionValueSP value_sp (new OptionValueUInt64()); |
| 185 | error = value_sp->SetValueFromCString (value_cstr); |
| 186 | if (error.Fail()) |
| 187 | value_sp.reset(); |
| 188 | return value_sp; |
| 189 | } |
| 190 | |
| 191 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 192 | void |
| 193 | OptionValueUInt64::DumpValue (Stream &strm) |
| 194 | { |
| 195 | strm.Printf ("0x%llx", m_current_value); |
| 196 | } |
| 197 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 198 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 199 | OptionValueUInt64::SetValueFromCString (const char *value_cstr) |
| 200 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 201 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 202 | bool success = false; |
| 203 | uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success); |
| 204 | if (success) |
| 205 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 206 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 207 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 208 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 209 | else |
| 210 | { |
| 211 | error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr); |
| 212 | } |
| 213 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | //------------------------------------------------------------------------- |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 217 | // OptionValueDictionary |
| 218 | //------------------------------------------------------------------------- |
| 219 | void |
| 220 | OptionValueString::DumpValue (Stream &strm) |
| 221 | { |
| 222 | strm.Printf ("\"%s\"", m_current_value.c_str()); |
| 223 | } |
| 224 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 225 | Error |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 226 | OptionValueString::SetValueFromCString (const char *value_cstr) |
| 227 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 228 | m_value_was_set = true; |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 229 | SetCurrentValue (value_cstr); |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 230 | return Error (); |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | //------------------------------------------------------------------------- |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 236 | // OptionValueFileSpec |
| 237 | //------------------------------------------------------------------------- |
| 238 | void |
| 239 | OptionValueFileSpec::DumpValue (Stream &strm) |
| 240 | { |
| 241 | if (m_current_value) |
| 242 | { |
| 243 | if (m_current_value.GetDirectory()) |
| 244 | { |
| 245 | strm << '"' << m_current_value.GetDirectory(); |
| 246 | if (m_current_value.GetFilename()) |
| 247 | strm << '/' << m_current_value.GetFilename(); |
| 248 | strm << '"'; |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | strm << '"' << m_current_value.GetFilename() << '"'; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 257 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 258 | OptionValueFileSpec::SetValueFromCString (const char *value_cstr) |
| 259 | { |
| 260 | if (value_cstr && value_cstr[0]) |
| 261 | m_current_value.SetFile(value_cstr, false); |
| 262 | else |
| 263 | m_current_value.Clear(); |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 264 | m_value_was_set = true; |
| 265 | return Error(); |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | //------------------------------------------------------------------------- |
| 270 | // OptionValueArray |
| 271 | //------------------------------------------------------------------------- |
| 272 | void |
| 273 | OptionValueArray::DumpValue (Stream &strm) |
| 274 | { |
| 275 | const uint32_t size = m_values.size(); |
| 276 | for (uint32_t i = 0; i<size; ++i) |
| 277 | { |
| 278 | strm.Printf("[%u] ", i); |
| 279 | m_values[i]->DumpValue (strm); |
| 280 | } |
| 281 | } |
| 282 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 283 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 284 | OptionValueArray::SetValueFromCString (const char *value_cstr) |
| 285 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 286 | Error error; |
| 287 | error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr); |
| 288 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 291 | |
| 292 | uint64_t |
| 293 | OptionValueArray::GetUInt64ValueAtIndex (uint32_t idx, uint64_t fail_value, bool *success_ptr) const |
| 294 | { |
| 295 | if (idx < m_values.size()) |
| 296 | return m_values[idx]->GetUInt64Value (fail_value, success_ptr); |
| 297 | return fail_value; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 302 | //------------------------------------------------------------------------- |
| 303 | // OptionValueDictionary |
| 304 | //------------------------------------------------------------------------- |
| 305 | void |
| 306 | OptionValueDictionary::DumpValue (Stream &strm) |
| 307 | { |
| 308 | collection::iterator pos, end = m_values.end(); |
| 309 | |
| 310 | for (pos = m_values.begin(); pos != end; ++pos) |
| 311 | { |
| 312 | strm.Printf("%s=", pos->first.GetCString()); |
| 313 | pos->second->DumpValue (strm); |
| 314 | } |
| 315 | } |
| 316 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 317 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 318 | OptionValueDictionary::SetValueFromCString (const char *value_cstr) |
| 319 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame^] | 320 | Error error; |
| 321 | error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr); |
| 322 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 325 | lldb::OptionValueSP |
| 326 | OptionValueDictionary::GetValueForKey (const ConstString &key) const |
| 327 | { |
| 328 | lldb::OptionValueSP value_sp; |
| 329 | collection::const_iterator pos = m_values.find (key); |
| 330 | if (pos != m_values.end()) |
| 331 | value_sp = pos->second; |
| 332 | return value_sp; |
| 333 | } |
| 334 | |
| 335 | const char * |
| 336 | OptionValueDictionary::GetStringValueForKey (const ConstString &key) |
| 337 | { |
| 338 | collection::const_iterator pos = m_values.find (key); |
| 339 | if (pos != m_values.end()) |
| 340 | { |
| 341 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 342 | return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue(); |
| 343 | } |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | bool |
| 349 | OptionValueDictionary::SetStringValueForKey (const ConstString &key, |
| 350 | const char *value, |
| 351 | bool can_replace) |
| 352 | { |
| 353 | collection::const_iterator pos = m_values.find (key); |
| 354 | if (pos != m_values.end()) |
| 355 | { |
| 356 | if (!can_replace) |
| 357 | return false; |
| 358 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 359 | { |
| 360 | pos->second->SetValueFromCString(value); |
| 361 | return true; |
| 362 | } |
| 363 | } |
| 364 | m_values[key] = OptionValueSP (new OptionValueString (value)); |
| 365 | return true; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | bool |
| 370 | OptionValueDictionary::SetValueForKey (const ConstString &key, |
| 371 | const lldb::OptionValueSP &value_sp, |
| 372 | bool can_replace) |
| 373 | { |
| 374 | // Make sure the value_sp object is allowed to contain |
| 375 | // values of the type passed in... |
| 376 | if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) |
| 377 | { |
| 378 | if (!can_replace) |
| 379 | { |
| 380 | collection::const_iterator pos = m_values.find (key); |
| 381 | if (pos != m_values.end()) |
| 382 | return false; |
| 383 | } |
| 384 | m_values[key] = value_sp; |
| 385 | return true; |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | bool |
| 391 | OptionValueDictionary::DeleteValueForKey (const ConstString &key) |
| 392 | { |
| 393 | collection::iterator pos = m_values.find (key); |
| 394 | if (pos != m_values.end()) |
| 395 | { |
| 396 | m_values.erase(pos); |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 402 | |