Changeset 10289
- Timestamp:
- 04/05/11 12:08:24 (14 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/Converter.cc
r10287 r10289 37 37 Other conversion functions also exist. 38 38 39 A result of a conversion is valid if valid() returns true and either the 40 result of the conversion is empty and the format string was valid empty 41 or both contain contents. 39 To check if the compilation of the format string was successfull 40 the valid() member functio is provided. 42 41 43 42 The format parameter \b W(ord) is dedicated to this kind of conversion and … … 494 493 //! case of failure an empty vector is returned. 495 494 //! 495 //! @throws 496 //! std::runtime_error if the conversion was not successfull 497 //! 496 498 template <class T> 497 499 vector<T> Converter::Get(const std::string &str) const 498 500 { 499 501 if (!valid()) 500 return vector<T>();502 throw runtime_error("Compiled format invalid!"); 501 503 502 504 // If the format is empty we are already done … … 546 548 default: 547 549 // This should never happen! 548 wout << endl << kRed << "Format '" << i->first.first->name() << " not supported!" << endl; 549 break; 550 throw runtime_error("Format '"+string(i->first.first->name())+" not supported!"); 550 551 } 551 552 … … 564 565 { 565 566 line.clear(); // This is necesasary to get a proper response from tellg() 566 wout << kRed << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!" << endl; 567 wout << kRed << line.str() << endl; 568 wout << kRed << setw(int(line.tellg())) << " " << "^" << endl; 569 return vector<T>(); 567 568 ostringstream err; 569 err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n"; 570 err << line.str() << "\n"; 571 err << setw(int(line.tellg())) << " " << "^\n"; 572 throw runtime_error(err.str()); 570 573 } 571 574 … … 574 577 { 575 578 line.clear(); 576 wout << kRed << "Not enough arguments [fmt=" << fFormat << "]!" << endl; 577 wout << kRed << line.str() << endl; 578 wout << kRed << setw(int(line.tellg())+1) << " " << "^" << endl; 579 return vector<T>(); 579 580 ostringstream err; 581 err << "Not enough arguments [fmt=" << fFormat << "]!\n"; 582 err << line.str() << "\n"; 583 err << setw(int(line.tellg())+1) << " " << "^\n"; 584 throw runtime_error(err.str()); 580 585 } 581 586 … … 585 590 if (line.good() && !line.eof()) 586 591 { 587 wout << kRed << "More arguments available than expected [" << fFormat << "]!" << endl; 588 wout << kRed << line.str() << endl; 589 wout << kRed << setw(int(line.tellg())+1) << " " << "^" << endl; 590 return vector<T>(); 592 ostringstream err; 593 err << "More arguments available than expected [fmt=" << fFormat << "]!\n"; 594 err << line.str() << "\n"; 595 err << setw(int(line.tellg())+1) << " " << "^\n"; 596 throw runtime_error(err.str()); 591 597 } 592 598 … … 604 610 return Get<char>(str); 605 611 } 606 607 612 608 613 // -------------------------------------------------------------------------- … … 619 624 //! case of failure an empty vector is returned. 620 625 //! 626 //! @throws 627 //! std::runtime_error if the conversion was not successfull 628 //! 621 629 template<class T> 622 630 T Converter::Get(const void *dat, size_t size) const 623 631 { 624 632 if (!valid()) 625 return T();633 throw runtime_error("Compiled format invalid!"); 626 634 627 635 const char *ptr = reinterpret_cast<const char *>(dat); … … 632 640 if (ptr-size>=dat) 633 641 { 634 wout << kRed << "Format description '" << fFormat << "' exceeds available data size (" << size << ")" << endl; 635 return T(); 642 ostringstream err; 643 err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")"; 644 throw runtime_error(err.str()); 636 645 } 637 646 … … 659 668 case 'v': 660 669 // This should never happen! 661 wout << kRed << "Type 'void' not supported!" << endl; 662 return T(); 670 throw runtime_error("Type 'void' not supported!"); 663 671 default: 664 // This should never happen! 665 wout << kRed << "TypeId '" << i->first.first->name() << "' not known!" << endl; 666 return T(); 672 throw runtime_error("TypeId '"+string(i->first.first->name())+"' not known!"); 667 673 } 668 674 } … … 671 677 if (ptr-size!=dat) 672 678 { 673 wout << kRed << "Data block size (" << size << ") doesn't fit format description '" << fFormat << "'" << endl; 674 return T(); 679 ostringstream err; 680 err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]"; 681 throw runtime_error(err.str()); 675 682 } 676 683 -
trunk/FACT++/src/ServiceList.cc
r10267 r10289 554 554 555 555 const Converter conv(lout, fmt); 556 557 const vector<char> v = conv.GetVector(str.substr(p0)); 558 if (!conv.valid() || v.empty()!=conv.empty()) 559 { 560 lout << kRed << "Couldn't properly parse the input... ignored." << endl; 556 if (!conv) 557 { 558 lout << kRed << "Couldn't properly parse the format... ignored." << endl; 561 559 return false; 562 560 } 563 561 564 const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size()); 565 if (rc) 566 lout << kGreen << "Command " << cmd << " emitted successfully to DimClient." << endl; 567 else 568 lout << kRed << "ERROR - Sending command " << cmd << " failed." << endl; 562 try 563 { 564 const vector<char> v = conv.GetVector(str.substr(p0)); 565 566 const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size()); 567 if (rc) 568 lout << kGreen << "Command " << cmd << " emitted successfully to DimClient." << endl; 569 else 570 lout << kRed << "ERROR - Sending command " << cmd << " failed." << endl; 571 } 572 catch (const std::runtime_error &e) 573 { 574 lout << kRed << e.what() << endl; 575 return false; 576 } 577 569 578 return true; 570 579 } -
trunk/FACT++/src/StateMachineImp.cc
r10273 r10289 234 234 235 235 const Converter conv(lout, fmt, false); 236 237 const vector<char> v = conv.GetVector(str.substr(p0)); 238 if (!conv.valid() || v.empty()!=conv.empty()) 239 { 240 lout << kRed << "Couldn't properly parse the input... ignored." << endl; 236 if (!conv) 237 { 238 lout << kRed << "Couldn't properly parse the format... ignored." << endl; 241 239 return false; 242 240 } 243 241 244 return PostEvent(*evt, v.data(), v.size()); 245 /* 246 const Converter c(lout, fmt, str.substr(p0)); 247 248 // Parsing was not successfull 249 if (!c.GetRc()) 250 { 251 lout << kRed << "Couldn't properly parse the input... ignored." << endl; 252 return false; 253 } 254 255 return PostEvent(*evt, c.Ptr(), c.Size()); 256 */ 242 try 243 { 244 const vector<char> v = conv.GetVector(str.substr(p0)); 245 return PostEvent(*evt, v.data(), v.size()); 246 } 247 catch (const std::runtime_error &e) 248 { 249 lout << kRed << e.what() << endl; 250 } 251 252 return false; 257 253 } 258 254 -
trunk/FACT++/src/dataLogger.cc
r10272 r10289 637 637 638 638 const Converter conv(Out(), I->getFormat()); 639 640 std::string text = conv.GetString(I->getData(), I->getSize()); 641 if (!conv.valid() || text.empty()!=conv.empty()) 639 if (!conv) 642 640 { 641 Error("Couldn't properly parse the format... ignored."); 642 return; 643 } 644 645 std::string text; 646 try 647 { 648 text = conv.GetString(I->getData(), I->getSize()); 649 } 650 catch (const std::runtime_error &e) 651 { 652 Out() << kRed << e.what() << endl; 643 653 Error("Couldn't properly parse the data... ignored."); 644 654 return; … … 706 716 707 717 // std::string text = ToString(evt.GetFormat().c_str(), evt.GetData(), evt.GetSize()); 718 708 719 const Converter conv(Out(), evt.GetFormat()); 709 710 std::string text = conv.GetString(evt.GetData(), evt.GetSize()); 711 if (!conv.valid() || text.empty()!=conv.empty()) 720 if (!conv) 712 721 { 722 Error("Couldn't properly parse the format... ignored."); 723 return GetCurrentState(); 724 } 725 726 std::string text; 727 try 728 { 729 text = conv.GetString(evt.GetData(), evt.GetSize()); 730 } 731 catch (const std::runtime_error &e) 732 { 733 Out() << kRed << e.what() << endl; 713 734 Error("Couldn't properly parse the data... ignored."); 714 735 return GetCurrentState(); 715 736 } 737 716 738 717 739 if (text.empty())
Note:
See TracChangeset
for help on using the changeset viewer.