comparison OrthancFramework/Sources/SerializationToolbox.cpp @ 4775:add0337b928a

refactoring parsing of numbers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Aug 2021 10:24:36 +0200
parents f4dbdb2dcba6
children 7053502fbf97
comparison
equal deleted inserted replaced
4771:9f207131c7f4 4775:add0337b928a
22 22
23 #include "PrecompiledHeaders.h" 23 #include "PrecompiledHeaders.h"
24 #include "SerializationToolbox.h" 24 #include "SerializationToolbox.h"
25 25
26 #include "OrthancException.h" 26 #include "OrthancException.h"
27 #include "Toolbox.h"
27 28
28 #if ORTHANC_ENABLE_DCMTK == 1 29 #if ORTHANC_ENABLE_DCMTK == 1
29 # include "DicomParsing/FromDcmtkBridge.h" 30 # include "DicomParsing/FromDcmtkBridge.h"
30 #endif 31 #endif
32
33 #include <boost/lexical_cast.hpp>
34
31 35
32 namespace Orthanc 36 namespace Orthanc
33 { 37 {
34 static bool ParseTagInternal(DicomTag& tag, 38 static bool ParseTagInternal(DicomTag& tag,
35 const char* name) 39 const char* name)
443 it = values.begin(); it != values.end(); ++it) 447 it = values.begin(); it != values.end(); ++it)
444 { 448 {
445 value[it->first.Format()] = it->second; 449 value[it->first.Format()] = it->second;
446 } 450 }
447 } 451 }
452
453
454 template <typename T,
455 bool allowSigned>
456 static bool ParseValue(T& target,
457 const std::string& source)
458 {
459 try
460 {
461 std::string value = Toolbox::StripSpaces(source);
462 if (value.empty())
463 {
464 return false;
465 }
466 else if (!allowSigned &&
467 value[0] == '-')
468 {
469 return false;
470 }
471 else
472 {
473 target = boost::lexical_cast<T>(value);
474 return true;
475 }
476 }
477 catch (boost::bad_lexical_cast&)
478 {
479 return false;
480 }
481 }
482
483
484 bool SerializationToolbox::ParseInteger32(int32_t& target,
485 const std::string& source)
486 {
487 int64_t tmp;
488 if (ParseValue<int64_t, true>(tmp, source))
489 {
490 target = static_cast<int32_t>(tmp);
491 return (tmp == static_cast<int64_t>(target)); // Check no overflow occurs
492 }
493 else
494 {
495 return false;
496 }
497 }
498
499
500 bool SerializationToolbox::ParseInteger64(int64_t& target,
501 const std::string& source)
502 {
503 return ParseValue<int64_t, true>(target, source);
504 }
505
506
507 bool SerializationToolbox::ParseUnsignedInteger32(uint32_t& target,
508 const std::string& source)
509 {
510 uint64_t tmp;
511 if (ParseValue<uint64_t, false>(tmp, source))
512 {
513 target = static_cast<uint32_t>(tmp);
514 return (tmp == static_cast<uint64_t>(target)); // Check no overflow occurs
515 }
516 else
517 {
518 return false;
519 }
520 }
521
522
523 bool SerializationToolbox::ParseUnsignedInteger64(uint64_t& target,
524 const std::string& source)
525 {
526 return ParseValue<uint64_t, false>(target, source);
527 }
528
529
530 bool SerializationToolbox::ParseFloat(float& target,
531 const std::string& source)
532 {
533 return ParseValue<float, true>(target, source);
534 }
535
536
537 bool SerializationToolbox::ParseDouble(double& target,
538 const std::string& source)
539 {
540 return ParseValue<double, true>(target, source);
541 }
542
543
544 static bool GetFirstItem(std::string& target,
545 const std::string& source)
546 {
547 std::vector<std::string> tokens;
548 Toolbox::TokenizeString(tokens, source, '\\');
549
550 if (tokens.empty())
551 {
552 return false;
553 }
554 else
555 {
556 target = tokens[0];
557 return true;
558 }
559 }
560
561
562 bool SerializationToolbox::ParseFirstInteger32(int32_t& target,
563 const std::string& source)
564 {
565 std::string first;
566 if (GetFirstItem(first, source))
567 {
568 return ParseInteger32(target, first);
569 }
570 else
571 {
572 return false;
573 }
574 }
575
576
577 bool SerializationToolbox::ParseFirstInteger64(int64_t& target,
578 const std::string& source)
579 {
580 std::string first;
581 if (GetFirstItem(first, source))
582 {
583 return ParseInteger64(target, first);
584 }
585 else
586 {
587 return false;
588 }
589 }
590
591
592 bool SerializationToolbox::ParseFirstUnsignedInteger32(uint32_t& target,
593 const std::string& source)
594 {
595 std::string first;
596 if (GetFirstItem(first, source))
597 {
598 return ParseUnsignedInteger32(target, first);
599 }
600 else
601 {
602 return false;
603 }
604 }
605
606
607 bool SerializationToolbox::ParseFirstUnsignedInteger64(uint64_t& target,
608 const std::string& source)
609 {
610 std::string first;
611 if (GetFirstItem(first, source))
612 {
613 return ParseUnsignedInteger64(target, first);
614 }
615 else
616 {
617 return false;
618 }
619 }
620
621
622 bool SerializationToolbox::ParseFirstFloat(float& target,
623 const std::string& source)
624 {
625 std::string first;
626 if (GetFirstItem(first, source))
627 {
628 return ParseFloat(target, first);
629 }
630 else
631 {
632 return false;
633 }
634 }
635
636
637 bool SerializationToolbox::ParseFirstDouble(double& target,
638 const std::string& source)
639 {
640 std::string first;
641 if (GetFirstItem(first, source))
642 {
643 return ParseDouble(target, first);
644 }
645 else
646 {
647 return false;
648 }
649 }
448 } 650 }