[1] | 1 | //----------------------------------------------------------- |
---|
| 2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
| 3 | // arnault@lal.in2p3.fr |
---|
| 4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
| 5 | //----------------------------------------------------------- |
---|
| 6 | |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | #include <string.h> |
---|
| 10 | #include <ctype.h> |
---|
| 11 | |
---|
| 12 | #include "cmt_tag.h" |
---|
| 13 | #include "cmt_database.h" |
---|
| 14 | #include "cmt_log.h" |
---|
| 15 | |
---|
| 16 | /*----------------------------------------------------------*/ |
---|
| 17 | /* */ |
---|
| 18 | /* Operations on Tags */ |
---|
| 19 | /* */ |
---|
| 20 | /*----------------------------------------------------------*/ |
---|
| 21 | |
---|
| 22 | /*----------------------------------------------------------*/ |
---|
| 23 | void Tag::unmark () |
---|
| 24 | { |
---|
| 25 | if (!is_primary ()) return; |
---|
| 26 | if (!m_selected) return; |
---|
| 27 | |
---|
| 28 | Log; |
---|
| 29 | |
---|
| 30 | if ((m_priority == PriorityDefault) || |
---|
| 31 | (m_priority == PrioritySite) || |
---|
| 32 | (m_priority == PriorityUname)) return; |
---|
| 33 | |
---|
| 34 | log << "Unmarking tag[" << this << "] " << m_name << " p=" << m_priority << log_endl; |
---|
| 35 | |
---|
| 36 | m_selected = false; |
---|
| 37 | m_def_use = 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /*----------------------------------------------------------*/ |
---|
| 41 | void Tag::unmark_all () |
---|
| 42 | { |
---|
| 43 | static TagPtrVector& vector = tags (); |
---|
| 44 | |
---|
| 45 | int tag_number; |
---|
| 46 | |
---|
| 47 | for (tag_number = 0; tag_number < vector.size (); tag_number++) |
---|
| 48 | { |
---|
| 49 | Tag* tag = vector[tag_number]; |
---|
| 50 | |
---|
| 51 | tag->unmark (); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Restore the tag tree after cleaning up one tag and |
---|
| 57 | * its local tree. |
---|
| 58 | */ |
---|
| 59 | void Tag::restore_tree () |
---|
| 60 | { |
---|
| 61 | static TagPtrVector& vector = tags (); |
---|
| 62 | |
---|
| 63 | int tag_number; |
---|
| 64 | |
---|
| 65 | for (tag_number = 0; tag_number < vector.size (); tag_number++) |
---|
| 66 | { |
---|
| 67 | Tag* tag = vector[tag_number]; |
---|
| 68 | |
---|
| 69 | if (tag->is_primary () && tag->is_selected ()) |
---|
| 70 | { |
---|
| 71 | if (tag->m_tag_refs.size () > 0) |
---|
| 72 | { |
---|
| 73 | int number; |
---|
| 74 | |
---|
| 75 | for (number = 0; number < tag->m_tag_refs.size (); number++) |
---|
| 76 | { |
---|
| 77 | Tag* ref = tag->m_tag_refs[number]; |
---|
| 78 | |
---|
| 79 | ref->mark (); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /*----------------------------------------------------------*/ |
---|
| 87 | void Tag::mark () |
---|
| 88 | { |
---|
| 89 | if (!is_primary ()) return; |
---|
| 90 | if (m_selected) return; |
---|
| 91 | |
---|
| 92 | Log; |
---|
| 93 | |
---|
| 94 | if (m_tag_excludes.size () > 0) |
---|
| 95 | { |
---|
| 96 | int number; |
---|
| 97 | |
---|
| 98 | for (number = 0; number < m_tag_excludes.size (); number++) |
---|
| 99 | { |
---|
| 100 | Tag* ref = m_tag_excludes[number]; |
---|
| 101 | |
---|
| 102 | if (ref->is_selected ()) |
---|
| 103 | { |
---|
| 104 | if (m_priority > ref->m_priority) |
---|
| 105 | { |
---|
| 106 | // |
---|
| 107 | // Although this other contradictory tag is already selected, |
---|
| 108 | // its priority is lower. Therefore it will lose !! It has to be |
---|
| 109 | // unselected ... |
---|
| 110 | // |
---|
| 111 | |
---|
| 112 | ref->unmark (); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | /* |
---|
| 117 | if (!Cmt::quiet) |
---|
| 118 | { |
---|
| 119 | cerr << "Cannot mark excluded tag " << m_name << " p=" << m_priority |
---|
| 120 | << " because " << ref->m_name << "(" << ref->m_priority << ")" << endl; |
---|
| 121 | show (0); |
---|
| 122 | } |
---|
| 123 | */ |
---|
| 124 | |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | m_selected = true; |
---|
| 132 | |
---|
| 133 | log << "Marking tag[" << this << "] " << m_name << " p=" << m_priority << log_endl; |
---|
| 134 | |
---|
| 135 | if (m_tag_refs.size () > 0) |
---|
| 136 | { |
---|
| 137 | int number; |
---|
| 138 | |
---|
| 139 | for (number = 0; number < m_tag_refs.size (); number++) |
---|
| 140 | { |
---|
| 141 | Tag* ref = m_tag_refs[number]; |
---|
| 142 | |
---|
| 143 | ref->mark (); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /*----------------------------------------------------------*/ |
---|
| 149 | void Tag::action (const CmtSystem::cmt_string_vector& words, Use* use) |
---|
| 150 | { |
---|
| 151 | cmt_string name; |
---|
| 152 | Tag* tag; |
---|
| 153 | Tag* ref; |
---|
| 154 | |
---|
| 155 | if (words.size () < 1) return; |
---|
| 156 | name = words[1]; |
---|
| 157 | if (name == "") return; |
---|
| 158 | |
---|
| 159 | tag = add (name, PriorityUserTag, "use", use); |
---|
| 160 | |
---|
| 161 | int priority = PriorityUserTag; |
---|
| 162 | if (tag->m_priority > priority) |
---|
| 163 | { |
---|
| 164 | priority = tag->m_priority; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | //if (tag->m_tag_refs.size () == 0) |
---|
| 168 | //{ |
---|
| 169 | |
---|
| 170 | tag->m_set_use = use; |
---|
| 171 | |
---|
| 172 | //} |
---|
| 173 | |
---|
| 174 | for (int i = 2; i < words.size (); i++) |
---|
| 175 | { |
---|
| 176 | name = words[i]; |
---|
| 177 | if (name == "") break; |
---|
| 178 | ref = add (name, priority, "use", use); |
---|
| 179 | tag->add_tag_ref (ref); |
---|
| 180 | if (tag->is_selected ()) // it was previously selected |
---|
| 181 | { |
---|
| 182 | ref->mark (); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /*----------------------------------------------------------*/ |
---|
| 188 | void Tag::action_apply (const CmtSystem::cmt_string_vector& words, Use* use) |
---|
| 189 | { |
---|
| 190 | cmt_string name; |
---|
| 191 | Tag* tag; |
---|
| 192 | |
---|
| 193 | if (words.size () < 1) return; |
---|
| 194 | name = words[1]; |
---|
| 195 | if (name == "") return; |
---|
| 196 | |
---|
| 197 | Symbol::expand (name); |
---|
| 198 | |
---|
| 199 | if (name == "") |
---|
| 200 | { |
---|
| 201 | if (!Cmt::get_quiet ()) |
---|
| 202 | { |
---|
| 203 | cerr << "#CMT> Warning: apply_tag with empty name [" << words[1] << "]" << endl; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | tag = find (name); |
---|
| 209 | if (tag == 0) |
---|
| 210 | { |
---|
| 211 | tag = add (name, PriorityUserTag, "use", use); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | tag->mark (); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /*----------------------------------------------------------*/ |
---|
| 219 | void Tag::action_exclude (const CmtSystem::cmt_string_vector& words, Use* use) |
---|
| 220 | { |
---|
| 221 | cmt_string name; |
---|
| 222 | Tag* tag; |
---|
| 223 | Tag* ref; |
---|
| 224 | |
---|
| 225 | if (words.size () < 1) return; |
---|
| 226 | name = words[1]; |
---|
| 227 | if (name == "") return; |
---|
| 228 | |
---|
| 229 | tag = add (name, PriorityUserTag, "use", use); |
---|
| 230 | |
---|
| 231 | if (tag->m_tag_excludes.size () == 0) |
---|
| 232 | { |
---|
| 233 | tag->m_set_use = use; |
---|
| 234 | |
---|
| 235 | int i; |
---|
| 236 | |
---|
| 237 | for (i = 2; i < words.size (); i++) |
---|
| 238 | { |
---|
| 239 | cmt_string n; |
---|
| 240 | n = words[i]; |
---|
| 241 | if (n == "") break; |
---|
| 242 | ref = add (n, PriorityUserTag, "use", use); |
---|
| 243 | |
---|
| 244 | /* |
---|
| 245 | if (!Cmt::quiet) |
---|
| 246 | { |
---|
| 247 | cerr << "Excluding tag " << n << "(" << ref->m_priority << ") from tag " |
---|
| 248 | << name << "(" << tag->m_priority << ")" << endl; |
---|
| 249 | } |
---|
| 250 | */ |
---|
| 251 | |
---|
| 252 | tag->add_tag_exclude (ref); |
---|
| 253 | ref->add_tag_exclude (tag); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | // |
---|
| 257 | // We have to check that some of the excluded tags may be already selected. |
---|
| 258 | // Then we have to figure out which one has to win: |
---|
| 259 | // |
---|
| 260 | // the one with the highest priority |
---|
| 261 | // or the first that had been declared. |
---|
| 262 | // |
---|
| 263 | |
---|
| 264 | int count = 0; |
---|
| 265 | int winner_count = 0; |
---|
| 266 | |
---|
| 267 | Tag* winner = 0; |
---|
| 268 | |
---|
| 269 | if (tag->is_selected ()) |
---|
| 270 | { |
---|
| 271 | count++; |
---|
| 272 | winner = tag; |
---|
| 273 | winner_count = 1; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | for (i = 0; i < tag->m_tag_excludes.size (); i++) |
---|
| 277 | { |
---|
| 278 | Tag* ref = tag->m_tag_excludes[i]; |
---|
| 279 | |
---|
| 280 | if (ref == 0) continue; |
---|
| 281 | |
---|
| 282 | if (ref->is_selected ()) |
---|
| 283 | { |
---|
| 284 | count++; |
---|
| 285 | |
---|
| 286 | if ((winner == 0) || |
---|
| 287 | (ref->m_priority > winner->m_priority)) |
---|
| 288 | { |
---|
| 289 | winner = ref; |
---|
| 290 | winner_count = 1; |
---|
| 291 | } |
---|
| 292 | else if (ref->m_priority == winner->m_priority) |
---|
| 293 | { |
---|
| 294 | winner_count++; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if (count > 1) |
---|
| 300 | { |
---|
| 301 | if (winner_count > 1) |
---|
| 302 | { |
---|
| 303 | // |
---|
| 304 | // Several contradictory tags are selected and have the same priority!! |
---|
| 305 | // |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | // |
---|
| 309 | // We have at least one selected, and one winner. |
---|
| 310 | // All others will be unselected. |
---|
| 311 | // |
---|
| 312 | |
---|
| 313 | if (tag != winner) |
---|
| 314 | { |
---|
| 315 | tag->unmark (); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | for (i = 0; i < tag->m_tag_excludes.size (); i++) |
---|
| 319 | { |
---|
| 320 | Tag* ref = tag->m_tag_excludes[i]; |
---|
| 321 | |
---|
| 322 | if (ref == 0) continue; |
---|
| 323 | if (ref == winner) continue; |
---|
| 324 | |
---|
| 325 | ref->unmark (); |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /*----------------------------------------------------------*/ |
---|
| 332 | Tag* Tag::find (const cmt_string& name) |
---|
| 333 | { |
---|
| 334 | TagMap& map = tag_map (); |
---|
| 335 | |
---|
| 336 | Tag* result = map.find (name); |
---|
| 337 | |
---|
| 338 | return (result); |
---|
| 339 | |
---|
| 340 | /* |
---|
| 341 | TagPtrVector& vector = tags (); |
---|
| 342 | |
---|
| 343 | int tag_index; |
---|
| 344 | Tag* tag; |
---|
| 345 | |
---|
| 346 | if (vector.size () == 0) return (0); |
---|
| 347 | |
---|
| 348 | for (tag_index = 0; tag_index < vector.size (); tag_index++) |
---|
| 349 | { |
---|
| 350 | tag = vector[tag_index]; |
---|
| 351 | |
---|
| 352 | if ((tag != 0) && (tag->m_name == name)) |
---|
| 353 | { |
---|
| 354 | return (tag); |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | return (0); |
---|
| 359 | */ |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | /*----------------------------------------------------------*/ |
---|
| 363 | Tag* Tag::find (const cmt_string& name, TagMap& instances) |
---|
| 364 | { |
---|
| 365 | Tag* result = instances.find (name); |
---|
| 366 | |
---|
| 367 | return (result); |
---|
| 368 | |
---|
| 369 | /* |
---|
| 370 | int tag_index; |
---|
| 371 | |
---|
| 372 | if (instances.size () == 0) return (0); |
---|
| 373 | |
---|
| 374 | for (tag_index = 0; tag_index < instances.size (); tag_index++) |
---|
| 375 | { |
---|
| 376 | Tag& tag = instances[tag_index]; |
---|
| 377 | |
---|
| 378 | if (tag.m_name == name) |
---|
| 379 | { |
---|
| 380 | return (&tag); |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | return (0); |
---|
| 385 | */ |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | /*----------------------------------------------------------*/ |
---|
| 389 | Tag* Tag::add (const cmt_string& name, |
---|
| 390 | int priority, |
---|
| 391 | const cmt_string& context, |
---|
| 392 | Use* use) |
---|
| 393 | { |
---|
| 394 | Log; |
---|
| 395 | |
---|
| 396 | TagMap& map = tag_map (); |
---|
| 397 | TagPtrVector& vector = tags (); |
---|
| 398 | TagVector& instances = all_tags (); |
---|
| 399 | |
---|
| 400 | Tag* tag; |
---|
| 401 | |
---|
| 402 | if (name == "") return (0); |
---|
| 403 | |
---|
| 404 | tag = find (name); |
---|
| 405 | if (tag != 0) |
---|
| 406 | { |
---|
| 407 | if (priority > tag->m_priority) |
---|
| 408 | { |
---|
| 409 | tag->m_priority = priority; |
---|
| 410 | |
---|
| 411 | /* |
---|
| 412 | if (!Cmt::quiet) |
---|
| 413 | { |
---|
| 414 | cerr << "increasing priority of " << name << " p=" << priority << endl; |
---|
| 415 | } |
---|
| 416 | */ |
---|
| 417 | } |
---|
| 418 | else |
---|
| 419 | { |
---|
| 420 | /* |
---|
| 421 | if (!Cmt::quiet) |
---|
| 422 | { |
---|
| 423 | cerr << "keeping priority of " << name << " p=" << tag->m_priority << endl; |
---|
| 424 | } |
---|
| 425 | */ |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | log << "re-adding tag[" << tag << "] " << name << " p=" << priority << log_endl; |
---|
| 429 | |
---|
| 430 | return (tag); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | Tag& tag_object = instances.add (); |
---|
| 434 | tag = &tag_object; |
---|
| 435 | vector.push_back (tag); |
---|
| 436 | map.add (name, tag_object); |
---|
| 437 | |
---|
| 438 | log << "adding tag[" << tag << "] " << name << " p=" << priority << log_endl; |
---|
| 439 | |
---|
| 440 | tag->clear (); |
---|
| 441 | |
---|
| 442 | tag->m_name = name; |
---|
| 443 | tag->m_priority = priority; |
---|
| 444 | tag->m_def_use = use; |
---|
| 445 | tag->m_context = context; |
---|
| 446 | |
---|
| 447 | int pos = 0; |
---|
| 448 | int length = name.size (); |
---|
| 449 | while (pos < length) |
---|
| 450 | { |
---|
| 451 | int and_pos = name.find (pos, "&"); |
---|
| 452 | if ((and_pos == cmt_string::npos) && (pos == 0)) break; |
---|
| 453 | |
---|
| 454 | cmt_string op_name; |
---|
| 455 | |
---|
| 456 | if (and_pos == cmt_string::npos) |
---|
| 457 | { |
---|
| 458 | name.substr (pos, op_name); |
---|
| 459 | } |
---|
| 460 | else |
---|
| 461 | { |
---|
| 462 | name.substr (pos, and_pos - pos, op_name); |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if (op_name != "") |
---|
| 466 | { |
---|
| 467 | Tag* t = find (op_name); |
---|
| 468 | if (t == 0) |
---|
| 469 | { |
---|
| 470 | t = add (op_name, priority, context, use); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | if (t != 0) |
---|
| 474 | { |
---|
| 475 | tag->m_and_operands.push_back (t); |
---|
| 476 | if (t->get_priority () > priority) |
---|
| 477 | { |
---|
| 478 | tag->m_priority = t->get_priority (); |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | else |
---|
| 482 | { |
---|
| 483 | cerr << "#Tag::add> unknown tag " << op_name << " in tag expression" << endl; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | if (and_pos == cmt_string::npos) break; |
---|
| 488 | |
---|
| 489 | pos = and_pos + 1; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | return (tag); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /*----------------------------------------------------------*/ |
---|
| 496 | int Tag::tag_number () |
---|
| 497 | { |
---|
| 498 | static TagPtrVector& vector = tags (); |
---|
| 499 | |
---|
| 500 | return (vector.size ()); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | /*----------------------------------------------------------*/ |
---|
| 504 | Tag* Tag::tag (int index) |
---|
| 505 | { |
---|
| 506 | static TagPtrVector& vector = tags (); |
---|
| 507 | |
---|
| 508 | return (vector[index]); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | /*----------------------------------------------------------*/ |
---|
| 512 | void Tag::clear_all () |
---|
| 513 | { |
---|
| 514 | TagMap& map = tag_map (); |
---|
| 515 | TagPtrVector& vector = tags (); |
---|
| 516 | TagVector& instances = all_tags (); |
---|
| 517 | |
---|
| 518 | int tag_index; |
---|
| 519 | |
---|
| 520 | for (tag_index = 0; tag_index < instances.size (); tag_index++) |
---|
| 521 | { |
---|
| 522 | Tag& tag = instances[tag_index]; |
---|
| 523 | tag.clear (); |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | map.clear (); |
---|
| 527 | vector.clear (); |
---|
| 528 | instances.clear (); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | /*----------------------------------------------------------*/ |
---|
| 532 | Tag::TagMap& Tag::tag_map () |
---|
| 533 | { |
---|
| 534 | static Database& db = Database::instance (); |
---|
| 535 | TagMap& map = db.tag_map (); |
---|
| 536 | |
---|
| 537 | return (map); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | /*----------------------------------------------------------*/ |
---|
| 541 | Tag::TagVector& Tag::all_tags () |
---|
| 542 | { |
---|
| 543 | static Database& db = Database::instance (); |
---|
| 544 | TagVector& vector = db.all_tags (); |
---|
| 545 | |
---|
| 546 | return (vector); |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | /*----------------------------------------------------------*/ |
---|
| 550 | Tag::TagPtrVector& Tag::tags () |
---|
| 551 | { |
---|
| 552 | static Database& db = Database::instance (); |
---|
| 553 | TagPtrVector& vector = db.tags (); |
---|
| 554 | |
---|
| 555 | return (vector); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | /*----------------------------------------------------------*/ |
---|
| 559 | Tag* Tag::get_default () |
---|
| 560 | { |
---|
| 561 | static Tag default_tag; |
---|
| 562 | static bool initialized = false; |
---|
| 563 | if (!initialized) |
---|
| 564 | { |
---|
| 565 | initialized = true; |
---|
| 566 | |
---|
| 567 | default_tag.m_name = "Default"; |
---|
| 568 | default_tag.m_selected = true; |
---|
| 569 | default_tag.m_context = "Default"; |
---|
| 570 | default_tag.m_priority = PriorityDefault; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | return (&(default_tag)); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | /*----------------------------------------------------------*/ |
---|
| 577 | Tag::Tag () : |
---|
| 578 | m_name (""), |
---|
| 579 | m_selected (false), |
---|
| 580 | m_context (""), |
---|
| 581 | m_def_use (0), |
---|
| 582 | m_set_use (0), |
---|
| 583 | m_priority (0) |
---|
| 584 | { |
---|
| 585 | clear (); |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | /*----------------------------------------------------------*/ |
---|
| 589 | Tag::Tag (const Tag& other) |
---|
| 590 | { |
---|
| 591 | clear (); |
---|
| 592 | |
---|
| 593 | m_name = other.m_name; |
---|
| 594 | m_selected = other.m_selected; |
---|
| 595 | m_and_operands = other.m_and_operands; |
---|
| 596 | m_tag_refs = other.m_tag_refs; |
---|
| 597 | m_tag_excludes = other.m_tag_excludes; |
---|
| 598 | m_context = other.m_context; |
---|
| 599 | m_def_use = other.m_def_use; |
---|
| 600 | m_set_use = other.m_set_use; |
---|
| 601 | m_priority = other.m_priority; |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | /*----------------------------------------------------------*/ |
---|
| 605 | Tag& Tag::operator = (const Tag& other) |
---|
| 606 | { |
---|
| 607 | clear (); |
---|
| 608 | |
---|
| 609 | m_name = other.m_name; |
---|
| 610 | m_selected = other.m_selected; |
---|
| 611 | m_and_operands = other.m_and_operands; |
---|
| 612 | m_tag_refs = other.m_tag_refs; |
---|
| 613 | m_tag_excludes = other.m_tag_excludes; |
---|
| 614 | m_context = other.m_context; |
---|
| 615 | m_def_use = other.m_def_use; |
---|
| 616 | m_set_use = other.m_set_use; |
---|
| 617 | m_priority = other.m_priority; |
---|
| 618 | |
---|
| 619 | return (*this); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | /*----------------------------------------------------------*/ |
---|
| 623 | Tag::~Tag () |
---|
| 624 | { |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | /*----------------------------------------------------------*/ |
---|
| 628 | void Tag::clear () |
---|
| 629 | { |
---|
| 630 | m_name = ""; |
---|
| 631 | m_tag_refs.clear (); |
---|
| 632 | m_tag_excludes.clear (); |
---|
| 633 | m_priority = PriorityUserTag; |
---|
| 634 | m_def_use = 0; |
---|
| 635 | m_set_use = 0; |
---|
| 636 | m_context = ""; |
---|
| 637 | |
---|
| 638 | m_selected = false; |
---|
| 639 | m_and_operands.clear (); |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | /*----------------------------------------------------------*/ |
---|
| 643 | void Tag::add_tag_ref (Tag* ref) |
---|
| 644 | { |
---|
| 645 | if (ref == 0) return; |
---|
| 646 | |
---|
| 647 | if (m_tag_refs.size () > 0) |
---|
| 648 | { |
---|
| 649 | int number; |
---|
| 650 | |
---|
| 651 | for (number = 0; number < m_tag_refs.size (); number++) |
---|
| 652 | { |
---|
| 653 | Tag* t = m_tag_refs[number]; |
---|
| 654 | if (t == ref) return; |
---|
| 655 | } |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | m_tag_refs.push_back (ref); |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | /*----------------------------------------------------------*/ |
---|
| 662 | void Tag::add_tag_exclude (Tag* ref) |
---|
| 663 | { |
---|
| 664 | if (ref == 0) return; |
---|
| 665 | |
---|
| 666 | if (m_tag_excludes.size () > 0) |
---|
| 667 | { |
---|
| 668 | int number; |
---|
| 669 | |
---|
| 670 | for (number = 0; number < m_tag_excludes.size (); number++) |
---|
| 671 | { |
---|
| 672 | Tag* t = m_tag_excludes[number]; |
---|
| 673 | if (t == ref) return; |
---|
| 674 | } |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | m_tag_excludes.push_back (ref); |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | /*----------------------------------------------------------*/ |
---|
| 681 | void Tag::show_definition (bool quiet) const |
---|
| 682 | { |
---|
| 683 | static const cmt_string priority_text[] = { |
---|
| 684 | "Lowest", |
---|
| 685 | "Default", |
---|
| 686 | "Uname", |
---|
| 687 | "Config", |
---|
| 688 | "UserTag", |
---|
| 689 | "PrimaryUserTag", |
---|
| 690 | "Tag" |
---|
| 691 | }; |
---|
| 692 | |
---|
| 693 | if (m_name == "Default") return; |
---|
| 694 | |
---|
| 695 | cout << m_name; |
---|
| 696 | |
---|
| 697 | if (!quiet) |
---|
| 698 | { |
---|
| 699 | //cout << "context=" << m_context << " use=" << m_def_use << endl; |
---|
| 700 | |
---|
| 701 | if ((m_context == "use") || (m_def_use != 0)) |
---|
| 702 | { |
---|
| 703 | if (m_def_use != 0) |
---|
| 704 | { |
---|
| 705 | cout << " (from "; |
---|
| 706 | if (m_context != "use") cout << m_context; |
---|
| 707 | cout << "package " << m_def_use->get_package_name () << ")"; |
---|
| 708 | } |
---|
| 709 | } |
---|
| 710 | else |
---|
| 711 | { |
---|
| 712 | cout << " (from " << m_context << ")"; |
---|
| 713 | } |
---|
| 714 | //cout << " (" << priority_text[m_priority] << ")"; |
---|
| 715 | |
---|
| 716 | if (m_tag_refs.size () > 0) |
---|
| 717 | { |
---|
| 718 | int number; |
---|
| 719 | |
---|
| 720 | if (m_set_use != 0) |
---|
| 721 | { |
---|
| 722 | cout << " package " << m_set_use->get_package_name (); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | cout << " implies ["; |
---|
| 726 | |
---|
| 727 | for (number = 0; number < m_tag_refs.size (); number++) |
---|
| 728 | { |
---|
| 729 | Tag* ref = m_tag_refs[number]; |
---|
| 730 | if (number > 0) cout << " "; |
---|
| 731 | cout << ref->m_name; |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | cout << "]"; |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | if (m_tag_excludes.size () > 0) |
---|
| 738 | { |
---|
| 739 | int number; |
---|
| 740 | |
---|
| 741 | if (m_set_use != 0) |
---|
| 742 | { |
---|
| 743 | cout << " package " << m_set_use->get_package_name (); |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | cout << " excludes ["; |
---|
| 747 | |
---|
| 748 | for (number = 0; number < m_tag_excludes.size (); number++) |
---|
| 749 | { |
---|
| 750 | Tag* ref = m_tag_excludes[number]; |
---|
| 751 | if (number > 0) cout << " "; |
---|
| 752 | cout << ref->m_name; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | cout << "]"; |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | cout << endl; |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | /*----------------------------------------------------------*/ |
---|
| 762 | void Tag::show (bool quiet) const |
---|
| 763 | { |
---|
| 764 | if (is_primary () && is_selected ()) show_definition (quiet); |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | /*----------------------------------------------------------*/ |
---|
| 768 | bool Tag::is_selected () const |
---|
| 769 | { |
---|
| 770 | if (is_primary ()) |
---|
| 771 | { |
---|
| 772 | return (m_selected); |
---|
| 773 | } |
---|
| 774 | else |
---|
| 775 | { |
---|
| 776 | for (int i = 0; i < m_and_operands.size(); i++) |
---|
| 777 | { |
---|
| 778 | Tag* t = m_and_operands[i]; |
---|
| 779 | |
---|
| 780 | if (!t->is_selected ()) return (false); |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | return (true); |
---|
| 784 | } |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | /*----------------------------------------------------------*/ |
---|
| 788 | bool Tag::is_primary () const |
---|
| 789 | { |
---|
| 790 | return (m_and_operands.size() == 0); |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | /*----------------------------------------------------------*/ |
---|
| 794 | const cmt_string& Tag::get_name () const |
---|
| 795 | { |
---|
| 796 | return (m_name); |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | /*----------------------------------------------------------*/ |
---|
| 800 | int Tag::get_priority () const |
---|
| 801 | { |
---|
| 802 | return (m_priority); |
---|
| 803 | } |
---|
| 804 | |
---|
| 805 | /** |
---|
| 806 | * Recomputes all references to other tags using the new set of |
---|
| 807 | * instances. This concerns |
---|
| 808 | * |
---|
| 809 | * TagPtrVector m_and_operands; |
---|
| 810 | * TagPtrVector m_tag_refs; |
---|
| 811 | * TagPtrVector m_tag_excludes; |
---|
| 812 | * |
---|
| 813 | * The pointers stored in those reference vectors are pointing to Tag objects |
---|
| 814 | * in one collection |
---|
| 815 | * We want to convert these pointers to pointers to the same Tag objects but |
---|
| 816 | * stored in the other Tag collection provided in the argument. |
---|
| 817 | * |
---|
| 818 | */ |
---|
| 819 | void Tag::install (TagMap& instances) |
---|
| 820 | { |
---|
| 821 | int i; |
---|
| 822 | |
---|
| 823 | for (i = 0; i < m_and_operands.size (); i++) |
---|
| 824 | { |
---|
| 825 | Tag* t = m_and_operands[i]; |
---|
| 826 | if (t != 0) |
---|
| 827 | { |
---|
| 828 | t = find (t->m_name, instances); |
---|
| 829 | m_and_operands[i] = t; |
---|
| 830 | } |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | for (i = 0; i < m_tag_refs.size (); i++) |
---|
| 834 | { |
---|
| 835 | Tag* t = m_tag_refs[i]; |
---|
| 836 | if (t != 0) |
---|
| 837 | { |
---|
| 838 | t = find (t->m_name, instances); |
---|
| 839 | m_tag_refs[i] = t; |
---|
| 840 | } |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | for (i = 0; i < m_tag_excludes.size (); i++) |
---|
| 844 | { |
---|
| 845 | Tag* t = m_tag_excludes[i]; |
---|
| 846 | if (t != 0) |
---|
| 847 | { |
---|
| 848 | t = find (t->m_name, instances); |
---|
| 849 | m_tag_excludes[i] = t; |
---|
| 850 | } |
---|
| 851 | } |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | |
---|