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