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 | |
Caroline Tice | dfb2e20 | 2011-04-22 05:08:45 +0000 | [diff] [blame] | 105 | const char * |
| 106 | OptionValue::GetStringValue () |
| 107 | { |
| 108 | if (GetType () == OptionValue::eTypeString) |
| 109 | return static_cast<OptionValueString *>(this)->GetCurrentValue(); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | uint64_t |
| 114 | OptionValue::GetUInt64Value () |
| 115 | { |
| 116 | if (GetType () == OptionValue::eTypeUInt64) |
| 117 | return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue(); |
| 118 | return 0; |
| 119 | } |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 120 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 121 | //------------------------------------------------------------------------- |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame] | 122 | // OptionValueCollection |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 123 | //------------------------------------------------------------------------- |
| 124 | |
| 125 | void |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame] | 126 | OptionValueCollection::GetQualifiedName (Stream &strm) |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 127 | { |
| 128 | if (m_parent) |
| 129 | { |
| 130 | m_parent->GetQualifiedName (strm); |
| 131 | strm.PutChar('.'); |
| 132 | } |
| 133 | strm << m_name; |
| 134 | } |
| 135 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 136 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 137 | //------------------------------------------------------------------------- |
| 138 | // OptionValueBoolean |
| 139 | //------------------------------------------------------------------------- |
| 140 | void |
| 141 | OptionValueBoolean::DumpValue (Stream &strm) |
| 142 | { |
| 143 | strm.PutCString (m_current_value ? "true" : "false"); |
| 144 | } |
| 145 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 146 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 147 | OptionValueBoolean::SetValueFromCString (const char *value_cstr) |
| 148 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 149 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 150 | bool success = false; |
| 151 | bool value = Args::StringToBoolean(value_cstr, false, &success); |
| 152 | if (success) |
| 153 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 154 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 155 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 156 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 157 | else |
| 158 | { |
| 159 | error.SetErrorStringWithFormat ("invalid boolean string value: '%s'\n", value_cstr); |
| 160 | } |
| 161 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | //------------------------------------------------------------------------- |
| 165 | // OptionValueSInt64 |
| 166 | //------------------------------------------------------------------------- |
| 167 | void |
| 168 | OptionValueSInt64::DumpValue (Stream &strm) |
| 169 | { |
| 170 | strm.Printf ("%lli", m_current_value); |
| 171 | } |
| 172 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 173 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 174 | OptionValueSInt64::SetValueFromCString (const char *value_cstr) |
| 175 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 176 | |
| 177 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 178 | bool success = false; |
| 179 | int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success); |
| 180 | if (success) |
| 181 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 182 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 183 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 184 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 185 | else |
| 186 | { |
| 187 | error.SetErrorStringWithFormat ("invalid int64_t string value: '%s'\n", value_cstr); |
| 188 | } |
| 189 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | //------------------------------------------------------------------------- |
| 193 | // OptionValueUInt64 |
| 194 | //------------------------------------------------------------------------- |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 195 | |
| 196 | lldb::OptionValueSP |
| 197 | OptionValueUInt64::Create (const char *value_cstr, Error &error) |
| 198 | { |
| 199 | lldb::OptionValueSP value_sp (new OptionValueUInt64()); |
| 200 | error = value_sp->SetValueFromCString (value_cstr); |
| 201 | if (error.Fail()) |
| 202 | value_sp.reset(); |
| 203 | return value_sp; |
| 204 | } |
| 205 | |
| 206 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 207 | void |
| 208 | OptionValueUInt64::DumpValue (Stream &strm) |
| 209 | { |
| 210 | strm.Printf ("0x%llx", m_current_value); |
| 211 | } |
| 212 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 213 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 214 | OptionValueUInt64::SetValueFromCString (const char *value_cstr) |
| 215 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 216 | Error error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 217 | bool success = false; |
| 218 | uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success); |
| 219 | if (success) |
| 220 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 221 | m_value_was_set = true; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 222 | m_current_value = value; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 223 | } |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 224 | else |
| 225 | { |
| 226 | error.SetErrorStringWithFormat ("invalid uint64_t string value: '%s'\n", value_cstr); |
| 227 | } |
| 228 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | //------------------------------------------------------------------------- |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 232 | // OptionValueDictionary |
| 233 | //------------------------------------------------------------------------- |
| 234 | void |
| 235 | OptionValueString::DumpValue (Stream &strm) |
| 236 | { |
| 237 | strm.Printf ("\"%s\"", m_current_value.c_str()); |
| 238 | } |
| 239 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 240 | Error |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 241 | OptionValueString::SetValueFromCString (const char *value_cstr) |
| 242 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 243 | m_value_was_set = true; |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 244 | SetCurrentValue (value_cstr); |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 245 | return Error (); |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | //------------------------------------------------------------------------- |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 251 | // OptionValueFileSpec |
| 252 | //------------------------------------------------------------------------- |
| 253 | void |
| 254 | OptionValueFileSpec::DumpValue (Stream &strm) |
| 255 | { |
| 256 | if (m_current_value) |
| 257 | { |
| 258 | if (m_current_value.GetDirectory()) |
| 259 | { |
| 260 | strm << '"' << m_current_value.GetDirectory(); |
| 261 | if (m_current_value.GetFilename()) |
| 262 | strm << '/' << m_current_value.GetFilename(); |
| 263 | strm << '"'; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | strm << '"' << m_current_value.GetFilename() << '"'; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 272 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 273 | OptionValueFileSpec::SetValueFromCString (const char *value_cstr) |
| 274 | { |
| 275 | if (value_cstr && value_cstr[0]) |
| 276 | m_current_value.SetFile(value_cstr, false); |
| 277 | else |
| 278 | m_current_value.Clear(); |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 279 | m_value_was_set = true; |
| 280 | return Error(); |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | //------------------------------------------------------------------------- |
| 285 | // OptionValueArray |
| 286 | //------------------------------------------------------------------------- |
| 287 | void |
| 288 | OptionValueArray::DumpValue (Stream &strm) |
| 289 | { |
| 290 | const uint32_t size = m_values.size(); |
| 291 | for (uint32_t i = 0; i<size; ++i) |
| 292 | { |
| 293 | strm.Printf("[%u] ", i); |
| 294 | m_values[i]->DumpValue (strm); |
| 295 | } |
| 296 | } |
| 297 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 298 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 299 | OptionValueArray::SetValueFromCString (const char *value_cstr) |
| 300 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 301 | Error error; |
| 302 | error.SetErrorStringWithFormat ("array option values don't yet support being set by string: '%s'\n", value_cstr); |
| 303 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 306 | |
| 307 | uint64_t |
| 308 | OptionValueArray::GetUInt64ValueAtIndex (uint32_t idx, uint64_t fail_value, bool *success_ptr) const |
| 309 | { |
| 310 | if (idx < m_values.size()) |
| 311 | return m_values[idx]->GetUInt64Value (fail_value, success_ptr); |
| 312 | return fail_value; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 317 | //------------------------------------------------------------------------- |
| 318 | // OptionValueDictionary |
| 319 | //------------------------------------------------------------------------- |
| 320 | void |
| 321 | OptionValueDictionary::DumpValue (Stream &strm) |
| 322 | { |
| 323 | collection::iterator pos, end = m_values.end(); |
| 324 | |
| 325 | for (pos = m_values.begin(); pos != end; ++pos) |
| 326 | { |
| 327 | strm.Printf("%s=", pos->first.GetCString()); |
| 328 | pos->second->DumpValue (strm); |
| 329 | } |
| 330 | } |
| 331 | |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 332 | Error |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 333 | OptionValueDictionary::SetValueFromCString (const char *value_cstr) |
| 334 | { |
Greg Clayton | 17cd995 | 2011-04-22 03:55:06 +0000 | [diff] [blame] | 335 | Error error; |
| 336 | error.SetErrorStringWithFormat ("dictionary option values don't yet support being set by string: '%s'\n", value_cstr); |
| 337 | return error; |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 340 | lldb::OptionValueSP |
| 341 | OptionValueDictionary::GetValueForKey (const ConstString &key) const |
| 342 | { |
| 343 | lldb::OptionValueSP value_sp; |
| 344 | collection::const_iterator pos = m_values.find (key); |
| 345 | if (pos != m_values.end()) |
| 346 | value_sp = pos->second; |
| 347 | return value_sp; |
| 348 | } |
| 349 | |
| 350 | const char * |
| 351 | OptionValueDictionary::GetStringValueForKey (const ConstString &key) |
| 352 | { |
| 353 | collection::const_iterator pos = m_values.find (key); |
| 354 | if (pos != m_values.end()) |
| 355 | { |
| 356 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 357 | return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue(); |
| 358 | } |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | bool |
| 364 | OptionValueDictionary::SetStringValueForKey (const ConstString &key, |
| 365 | const char *value, |
| 366 | bool can_replace) |
| 367 | { |
| 368 | collection::const_iterator pos = m_values.find (key); |
| 369 | if (pos != m_values.end()) |
| 370 | { |
| 371 | if (!can_replace) |
| 372 | return false; |
| 373 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 374 | { |
| 375 | pos->second->SetValueFromCString(value); |
| 376 | return true; |
| 377 | } |
| 378 | } |
| 379 | m_values[key] = OptionValueSP (new OptionValueString (value)); |
| 380 | return true; |
| 381 | |
| 382 | } |
| 383 | |
| 384 | bool |
| 385 | OptionValueDictionary::SetValueForKey (const ConstString &key, |
| 386 | const lldb::OptionValueSP &value_sp, |
| 387 | bool can_replace) |
| 388 | { |
| 389 | // Make sure the value_sp object is allowed to contain |
| 390 | // values of the type passed in... |
| 391 | if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) |
| 392 | { |
| 393 | if (!can_replace) |
| 394 | { |
| 395 | collection::const_iterator pos = m_values.find (key); |
| 396 | if (pos != m_values.end()) |
| 397 | return false; |
| 398 | } |
| 399 | m_values[key] = value_sp; |
| 400 | return true; |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | bool |
| 406 | OptionValueDictionary::DeleteValueForKey (const ConstString &key) |
| 407 | { |
| 408 | collection::iterator pos = m_values.find (key); |
| 409 | if (pos != m_values.end()) |
| 410 | { |
| 411 | m_values.erase(pos); |
| 412 | return true; |
| 413 | } |
| 414 | return false; |
| 415 | } |
| 416 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 417 | |