NCBI C++ ToolKit
version.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: version.cpp 102709 2024-06-28 14:11:08Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Denis Vakatov, Vladimir Ivanov
27  *
28  * File Description:
29  * CVersionInfo -- a version info storage class
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/version.hpp>
36 #include <common/ncbi_source_ver.h>
37 
38 #include <utility>
39 #include <algorithm>
40 
42 
43 
45  int ver_minor,
46  int patch_level,
47  const string& name)
48  : m_Major(ver_major),
49  m_Minor(ver_minor),
50  m_PatchLevel(patch_level),
51  m_Name(name)
52 
53 {
54 }
55 
56 
58  const string& name)
59 {
61  if (!name.empty()) {
62  m_Name = name;
63  }
64 }
65 
67 {
68  _ASSERT( flags == kAny || flags == kLatest);
69  m_Major = m_Minor = m_PatchLevel = (flags == kAny) ? 0 : -1;
70 }
71 
72 static
73 void s_ConvertVersionInfo(CVersionInfo* vi, const char* str)
74 {
75  int major, minor, patch = 0;
76  if (!isdigit((unsigned char)(*str))) {
77  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
78  }
79  major = atoi(str);
80  if (major < 0) {
81  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
82  }
83  for (; *str && isdigit((unsigned char)(*str)); ++str) {}
84  if (*str != '.') {
85  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
86  }
87  ++str;
88  if (!isdigit((unsigned char)(*str))) {
89  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
90  }
91 
92  minor = atoi(str);
93  if (minor < 0) {
94  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
95  }
96  for (; *str && isdigit((unsigned char)(*str)); ++str) {}
97 
98  if (*str != 0) {
99  if (*str != '.') {
100  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
101  }
102  ++str;
103  patch = atoi(str);
104  if (patch < 0) {
105  NCBI_THROW2(CStringException, eFormat, "Invalid version format", 0);
106  }
107  }
108 
109  vi->SetVersion(major, minor, patch);
110 
111 }
112 
113 
114 void CVersionInfo::FromStr(const string& version)
115 {
116  s_ConvertVersionInfo(this, version.c_str());
117 }
118 
119 
120 string CVersionInfo::Print(void) const
121 {
122  if (m_Major < 0) {
123  return kEmptyStr;
124  }
125  CNcbiOstrstream os;
126  os << m_Major << "." << (m_Minor >= 0 ? m_Minor : 0);
127  if (m_PatchLevel >= 0) {
128  os << "." << m_PatchLevel;
129  }
130  if ( !m_Name.empty() ) {
131  os << " (" << m_Name << ")";
132  }
133  return CNcbiOstrstreamToString(os);
134 }
135 
136 
137 string CVersionInfo::PrintXml(void) const
138 {
139  CNcbiOstrstream os;
140  os << "<version_info";
141  if (m_Major >= 0) {
142  os << " major=\"" << m_Major <<
143  "\" minor=\"" << (m_Minor >= 0 ? m_Minor : 0) << "\"";
144  if (m_PatchLevel >= 0) {
145  os << " patch_level=\"" << m_PatchLevel << "\"";
146  }
147  }
148  if ( !m_Name.empty() ) {
149  os << " name=\"" << NStr::XmlEncode(m_Name) << "\"";
150  }
151  os << "/>\n";
152  return CNcbiOstrstreamToString(os);
153 }
154 
155 
156 string CVersionInfo::PrintJson(void) const
157 {
158  CNcbiOstrstream os;
159  os << "{";
160  bool need_separator = false;
161  if (m_Major >= 0) {
162  os << "\"major\": " << m_Major <<
163  ", \"minor\": " << (m_Minor >= 0 ? m_Minor : 0);
164  if (m_PatchLevel >= 0) {
165  os << ", \"patch_level\": " << m_PatchLevel;
166  }
167  need_separator = true;
168  }
169  if ( !m_Name.empty() ) {
170  if ( need_separator ) os << ", ";
171  os << "\"name\": " << NStr::JsonEncode(m_Name, NStr::eJsonEnc_Quoted);
172  }
173  os << "}";
174  return CNcbiOstrstreamToString(os);
175 }
176 
177 
179 CVersionInfo::Match(const CVersionInfo& version_info) const
180 {
181  if (GetMajor() != version_info.GetMajor())
182  return eNonCompatible;
183 
184  if (GetMinor() < version_info.GetMinor())
185  return eNonCompatible;
186 
187  if (GetMinor() > version_info.GetMinor())
188  return eBackwardCompatible;
189 
190  // Minor versions are equal.
191 
192  if (GetPatchLevel() == version_info.GetPatchLevel()) {
193  return eFullyCompatible;
194  }
195 
196  if (GetPatchLevel() > version_info.GetPatchLevel()) {
197  return eBackwardCompatible;
198  }
199 
201 
202 }
203 
204 void CVersionInfo::SetVersion(int ver_major,
205  int ver_minor,
206  int patch_level)
207 {
208  m_Major = ver_major;
209  m_Minor = ver_minor;
210  m_PatchLevel = patch_level;
211 }
212 
213 
214 
216  const CVersionInfo& cinfo,
217  int& best_major,
218  int& best_minor,
219  int& best_patch_level)
220 {
221  int major = cinfo.GetMajor();
222  int minor = cinfo.GetMinor();
223  int patch_level = cinfo.GetPatchLevel();
224 
225  if (info.GetMajor() == -1) { // best major search
226  if (major > best_major) {
227  best_major = major;
228  best_minor = minor;
229  best_patch_level = patch_level;
230  return true;
231  }
232  } else { // searching for the specific major version
233  // Do not chose between major versions.
234  // If they are not equal then they are not compatible.
235  if (info.GetMajor() != major) {
236  return false;
237  }
238  }
239 
240  if (info.GetMinor() == -1) { // best minor search
241  if (minor > best_minor) {
242  best_major = major;
243  best_minor = minor;
244  best_patch_level = patch_level;
245  return true;
246  }
247  } else {
248  if (info.GetMinor() > minor) {
249  return false;
250  }
251  if (info.GetMinor() < minor) {
252  best_major = major;
253  best_minor = minor;
254  best_patch_level = patch_level;
255  return true;
256  }
257  }
258 
259  // Major and minor versions are equal.
260  // always looking for the best patch
261  if (patch_level > best_patch_level) {
262  best_major = major;
263  best_minor = minor;
264  best_patch_level = patch_level;
265  return true;
266  }
267  return false;
268 }
269 
270 
271 void ParseVersionString(const string& vstr,
272  string* program_name,
273  CVersionInfo* ver)
274 {
275  _ASSERT(program_name);
276  _ASSERT(ver);
277 
278  if (vstr.empty()) {
279  NCBI_THROW2(CStringException, eFormat, "Version string is empty", 0);
280  }
281 
282  program_name->erase();
283 
284 
285  string lo_vstr(vstr); NStr::ToLower(lo_vstr);
286  string::size_type pos;
287 
288  const char* vstr_str = vstr.c_str();
289 
290  // 2.3.4 (program)
291 
292  pos = lo_vstr.find("(");
293  if (pos != string::npos) {
294  string::size_type pos2 = lo_vstr.find(")", pos);
295  if (pos2 == string::npos) { // not found
297  eFormat, "Version string format error", 0);
298  }
299  for (++pos; pos < pos2; ++pos) {
300  program_name->push_back(vstr.at(pos));
301  }
303 
304  s_ConvertVersionInfo(ver, vstr.c_str());
305  return;
306 
307  }
308 
309 
310  // all other normal formats
311 
312  static const char* version_patterns[] = {
313  "version",
314  "v.",
315  "ver",
316  "v",
317  nullptr
318  };
319  const char* version_pattern = kEmptyCStr;
320  for (const char** ppattern = version_patterns; *ppattern != nullptr;
321  ++ppattern) {
322  pos = lo_vstr.rfind(*ppattern);
323  if (pos != NPOS) {
324  // sanity check
325  auto pos2
326  = lo_vstr.find_first_not_of(" .", pos + strlen(*ppattern));
327  if ( !isdigit(lo_vstr[pos2]) ) {
328  pos = NPOS;
329  continue;
330  }
331  version_pattern = *ppattern;
332  break;
333  }
334  }
335 
336  if (pos == string::npos) {
337  // find the first space-digit and assume it's version
338  const char* ch = vstr_str;
339  for (; *ch; ++ch) {
340  if (isdigit((unsigned char)(*ch))) {
341  if (ch == vstr_str) {
342  // check if it's version
343  const char* ch2 = ch + 1;
344  for (;*ch2; ++ch2) {
345  if (!isdigit((unsigned char)(*ch2))) {
346  break;
347  }
348  } // for
349  if (*ch2 == '.') {
350  pos = ch - vstr_str;
351  break;
352  } else {
353  continue;
354  }
355  } else {
356  if (isspace((unsigned char) ch[-1])) {
357  pos = ch - vstr_str;
358  break;
359  }
360  }
361  } // if digit
362 
363  } // for
364  }
365 
366 
367  if (pos != string::npos) {
368  int pname_end = (int)(pos - 1);
369  for (; pname_end >= 0; --pname_end) {
370  char ch = vstr[pname_end];
371  if (!isspace((unsigned char) ch))
372  break;
373  } // for
374  if (pname_end <= 0) {
375  } else {
376  program_name->append(vstr.c_str(), pname_end + 1);
377  }
378 
379  pos += strlen(version_pattern);
380  for(; pos < vstr.length(); ++pos) {
381  char ch = vstr[pos];
382  if (ch == '.')
383  continue;
384  if (!isspace((unsigned char) ch))
385  break;
386  } // for
387 
388  const char* ver_str = vstr_str + pos;
389  s_ConvertVersionInfo(ver, ver_str);
390  return;
391  } else {
392  *ver = CVersionInfo::kAny;
393  *program_name = vstr;
394  NStr::TruncateSpacesInPlace(*program_name);
395  if (program_name->empty()) {
396  NCBI_THROW2(CStringException, eFormat, "Version string is empty", 0);
397  }
398  }
399 
400 
401 }
402 
403 /////////////////////////////////////////////////////////////////////////////
404 // CComponentVersionInfoAPI
405 
407  int ver_major,
408  int ver_minor,
409  int patch_level,
410  const string& name, const SBuildInfo& build_info)
411  : CVersionInfo(ver_major, ver_minor, patch_level, name),
412  m_ComponentName( component_name ), m_BuildInfo(build_info)
413 {
414 }
415 
417  const string& version,
418  const string& name, const SBuildInfo& build_info)
419  : CVersionInfo( version, name),
420  m_ComponentName( component_name ), m_BuildInfo(build_info)
421 
422 {
423 }
424 
426 {
427  CNcbiOstrstream os;
428  os << GetComponentName() << ": " << CVersionInfo::Print() << endl << m_BuildInfo.Print(2);
429  return CNcbiOstrstreamToString(os);
430 }
431 
433 {
434  CNcbiOstrstream os;
435  os << "<component name=\"" << NStr::XmlEncode(GetComponentName()) << "\">\n" <<
436  CVersionInfo::PrintXml() << endl <<
437  m_BuildInfo.PrintXml() <<
438  "</component>" << endl;
439  return CNcbiOstrstreamToString(os);
440 }
441 
443 {
444  CNcbiOstrstream os;
445  os << "{ \"name\": " << NStr::JsonEncode(GetComponentName(),NStr::eJsonEnc_Quoted) <<
446  ", \"version_info\": " << CVersionInfo::PrintJson() << ",\n" <<
447  " \"build_info\": " << m_BuildInfo.PrintJson() << "}";
448  return CNcbiOstrstreamToString(os);
449 }
450 
451 /////////////////////////////////////////////////////////////////////////////
452 // SBuildInfo
453 
455  : date(__DATE__ " " __TIME__) {
456 }
457 
459 {
460  if (!value.empty()) {
461  m_extra.push_back( make_pair(key, value));
462  }
463  return *this;
464 }
465 
467 {
468  if (value != 0) {
469  m_extra.push_back( make_pair(key, NStr::NumericToString(value)));
470  }
471  return *this;
472 }
473 
474 string SBuildInfo::GetExtraValue( EExtra key, const string& default_value) const
475 {
476  if (key == eBuildDate) {
477  return date;
478  } else if (key == eBuildTag) {
479  return tag;
480  } else {
481  for( const auto& e : m_extra) {
482  if (e.first == key) {
483  return e.second;
484  }
485  }
486  }
487  return default_value;
488 }
489 
491 {
492  switch (key)
493  {
494  case eBuildDate: return "Build-Date";
495  case eBuildTag: return "Build-Tag";
496  case eTeamCityProjectName: return "TeamCity-Project-Name";
497  case eTeamCityBuildConf: return "TeamCity-BuildConf-Name";
498  case eTeamCityBuildNumber: return "TeamCity-Build-Number";
499  case eBuildID: return "Build-ID";
500  case eSubversionRevision: return "Subversion-Revision";
501  case eStableComponentsVersion: return "Stable-Components-Version";
502  case eDevelopmentVersion: return "Development-Version";
503  case eProductionVersion: return "Production-Version";
504  case eBuiltAs: return "Built-As";
505  case eRevision: return "Revision";
506  case eGitBranch: return "GitBranch";
507  }
508  return "Unknown";
509 }
510 
512 {
513  switch (key)
514  {
515  case eBuildDate: return "ncbi_app_build_date";
516  case eBuildTag: return "ncbi_app_build_tag";
517  case eTeamCityProjectName: return "ncbi_app_tc_project";
518  case eTeamCityBuildConf: return "ncbi_app_tc_conf";
519  case eTeamCityBuildNumber: return "ncbi_app_tc_build";
520  case eBuildID: return "ncbi_app_build_id";
521  case eSubversionRevision: return "ncbi_app_vcs_revision";
522  case eStableComponentsVersion: return "ncbi_app_sc_version";
523  case eDevelopmentVersion: return "ncbi_app_dev_version";
524  case eProductionVersion: return "ncbi_app_prod_version";
525  case eBuiltAs: return "ncbi_app_built_as";
526  case eRevision: return "ncbi_app_revision";
527  case eGitBranch: return "ncbi_app_gitbranch";
528  }
529  return "ncbi_app_unk";
530 }
531 
533 {
534  if (key == eBuildDate) {
535  return "date";
536  } else if (key == eBuildTag) {
537  return "tag";
538  }
539  string s(ExtraName(key));
540  return NStr::ReplaceInPlace(NStr::ToLower(s),"-", "_");
541 }
542 
544 {
545  return ExtraNameXml(key);
546 }
547 
548 string SBuildInfo::Print(size_t offset) const
549 {
550  string prefix(offset+1, ' ');
551  CNcbiOstrstream os;
552  if (!date.empty()) {
553  os << prefix << ExtraName(eBuildDate) << ": " << date << endl;
554  }
555 
556  if (!tag.empty()) {
557  os << prefix << ExtraName(eBuildDate) << ": " << tag << endl;
558  }
559  for( const auto& e : m_extra) {
560  os << prefix << ExtraName(e.first) << ": " << e.second << endl;
561  }
562  return CNcbiOstrstreamToString(os);
563 }
564 
565 string SBuildInfo::PrintXml(void) const
566 {
567  CNcbiOstrstream os;
568  os << "<build_info";
569  if ( !date.empty() ) {
570  os << ' ' << ExtraNameXml(eBuildDate) << "=\"" << NStr::XmlEncode(date) << '\"';
571  }
572  if ( !tag.empty() ) {
573  os << ' ' << ExtraNameXml(eBuildTag) << "=\"" << NStr::XmlEncode(tag) << '\"';
574  }
575  os << ">" << endl;
576  for( const auto& e : m_extra) {
577  os << '<' << ExtraNameXml(e.first) << '>' << NStr::XmlEncode(e.second) << "</" << ExtraNameXml(e.first) << '>' << endl;
578  }
579  os << "</build_info>" << endl;
580  return CNcbiOstrstreamToString(os);
581 }
582 
583 string SBuildInfo::PrintJson(void) const
584 {
585  CNcbiOstrstream os;
586  bool need_separator = false;
587  os << '{';
588  if ( !date.empty() ) {
590  need_separator = true;
591  }
592  if ( !tag.empty() ) {
593  if ( need_separator ) os << ", ";
594  os << '\"' << ExtraNameJson(eBuildTag) << "\": " << NStr::JsonEncode(tag, NStr::eJsonEnc_Quoted);
595  need_separator = true;
596  }
597  for( const auto& e : m_extra) {
598  if ( need_separator ) os << ", ";
599  os << '\"' << ExtraNameJson(e.first) << "\": " << NStr::JsonEncode(e.second, NStr::eJsonEnc_Quoted);
600  need_separator = true;
601  }
602  os << '}';
603  return CNcbiOstrstreamToString(os);
604 }
605 
607 {
608  // According to the documentation, if the compiler cannot get the time of day
609  // it will give you question marks for __DATE__ and __TIME__.
610  try {
611  return CTime(date, "b D Y h:m:s");
612  }
613  catch (CTimeException&) {}
614  return CTime();
615 }
616 
617 
618 /////////////////////////////////////////////////////////////////////////////
619 // CVersion
620 
622  : m_VersionInfo(new CVersionInfo(0,0)),
623  m_BuildInfo(build_info)
624 {
625  m_VersionInfo->SetVersion(m_VersionInfo->GetMajor(), m_VersionInfo->GetMinor(),
627 }
628 
630  : m_VersionInfo(new CVersionInfo(version)),
631  m_BuildInfo(build_info)
632 {
633 }
634 
636 {
637  to.m_VersionInfo.reset(new CVersionInfo(*from.m_VersionInfo));
638  to.m_BuildInfo = from.m_BuildInfo;
639 
640  for (const auto& c : from.m_Components) {
641  to.m_Components.emplace_back(new CComponentVersionInfoAPI(*c));
642  }
643 }
644 
646  : CObject(version)
647 {
648  x_Copy(*this, version);
649 }
650 
652 {
654  x_Copy(*this, version);
655  return *this;
656 }
657 
658 void CVersionAPI::SetVersionInfo( int ver_major, int ver_minor,
659  int patch_level, const string& ver_name)
660 {
661  m_VersionInfo.reset( new CVersionInfo(
662  ver_major, ver_minor, patch_level, ver_name) );
663 }
664 
665 void CVersionAPI::SetVersionInfo( int ver_major, int ver_minor,
666  int patch_level, const string& ver_name,
667  const SBuildInfo& build_info)
668 {
669  m_VersionInfo.reset( new CVersionInfo(
670  ver_major, ver_minor, patch_level, ver_name) );
671  m_BuildInfo = build_info;
672 }
673 
675 {
676  m_VersionInfo.reset( version );
677 }
678 
680  const SBuildInfo& build_info)
681 {
682  m_VersionInfo.reset( version );
683  m_BuildInfo = build_info;
684 }
685 
687 {
688  return *m_VersionInfo;
689 }
690 
692  const string& component_name, int ver_major, int ver_minor,
693  int patch_level, const string& ver_name, const SBuildInfo& build_info)
694 {
695  m_Components.emplace_back(
696  new CComponentVersionInfoAPI(component_name, ver_major, ver_minor,
697  patch_level, ver_name, build_info));
698 }
699 
701 {
702  m_Components.emplace_back(component);
703 }
704 
706 {
707  return m_BuildInfo;
708 }
709 
711 {
712  return NCBI_PACKAGE_NAME;
713 }
714 
716 {
720 }
721 
723 {
724  return NCBI_PACKAGE_CONFIG;
725 }
726 
727 
728 string CVersionAPI::Print(const string& appname, TPrintFlags flags) const
729 {
730  CNcbiOstrstream os;
731 
732  if (flags & fVersionInfo) {
733  os << appname << ": " << m_VersionInfo->Print() << endl;
734  }
735 
736 #if NCBI_PACKAGE
737  if (flags & ( fPackageShort | fPackageFull )) {
738  os << " Package: " << GetPackageName() << ' '
739  << GetPackageVersion().Print() << ", build "
740  << NCBI_SBUILDINFO_DEFAULT().date
741  << endl;
742  }
743  if (flags & fPackageFull) {
744  os << " Package-Config: " << ' ' << GetPackageConfig() << endl;
745  }
746 #endif
747 
748 #ifdef NCBI_SIGNATURE
749  if (flags & fBuildSignature) {
750  os << " Build-Signature: " << ' ' << NCBI_SIGNATURE << endl;
751  }
752 #endif
753  if (flags & fGI64bit) {
754 #ifdef NCBI_INT8_GI
755  os << " GI-64bit: TRUE" << endl;
756 #else
757  os << " GI-64bit: FALSE" << endl;
758 #endif
759  }
760 
761  if (flags & fBuildInfo) {
762  os << m_BuildInfo.Print();
763  }
764 
765  if (flags & fComponents) {
766  for (const auto& c : m_Components) {
767  os << endl << ' ' << c->Print() << endl;
768  }
769  }
770  return CNcbiOstrstreamToString(os);
771 }
772 
773 
774 string CVersionAPI::PrintXml(const string& appname, TPrintFlags flags) const
775 {
776  CNcbiOstrstream os;
777 
778  os << "<?xml version=\"1.0\"?>\n"
779  "<ncbi_version xmlns=\"ncbi:version\"\n"
780  " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
781  " xs:schemaLocation=\"ncbi:version ncbi_version.xsd\">\n";
782 
783  if (flags & fVersionInfo) {
784  if ( !appname.empty() ) {
785  os << "<appname>" << NStr::XmlEncode(appname) << "</appname>\n";
786  }
787  os << m_VersionInfo->PrintXml();
788  }
789 
790  if (flags & fComponents) {
791  for (const auto& c : m_Components) {
792  os << c->PrintXml();
793  }
794  }
795 
796 #if NCBI_PACKAGE
797  if (flags & ( fPackageShort | fPackageFull )) {
798  os << "<package name=\"" << NStr::XmlEncode(GetPackageName()) << "\">\n" <<
800  NCBI_SBUILDINFO_DEFAULT().PrintXml();
801  if (flags & fPackageFull) {
802  os << "<config>" << NStr::XmlEncode(GetPackageConfig()) << "</config>\n";
803  }
804  os << "</package>\n";
805  }
806 #endif
807 
808 #ifdef NCBI_SIGNATURE
809  if (flags & fBuildSignature) {
810  os << "<build_signature>" << NStr::XmlEncode(NCBI_SIGNATURE) << "</build_signature>\n";
811  }
812 #endif
813 
814  if (flags & fBuildInfo) {
815  os << m_BuildInfo.PrintXml();
816  }
817 
818  os << "</ncbi_version>\n";
819 
820  return CNcbiOstrstreamToString(os);
821 }
822 
823 
824 string CVersionAPI::PrintJson(const string& appname, TPrintFlags flags) const
825 {
826  CNcbiOstrstream os;
827  bool need_separator = false;
828 
829  os << "{\n \"ncbi_version\": {\n";
830 
831  if (flags & fVersionInfo) {
832  if ( !appname.empty() ) {
833  os << " \"appname\": " << NStr::JsonEncode(appname, NStr::eJsonEnc_Quoted) << ",\n";
834  }
835  os << " \"version_info\": " << m_VersionInfo->PrintJson();
836  need_separator = true;
837  }
838 
839  if (flags & fComponents) {
840  if ( need_separator ) os << ",\n";
841  os << " \"component\": [";
842  need_separator = false;
843  for (const auto& c : m_Components) {
844  if ( need_separator ) os << ",";
845  os << "\n " << c->PrintJson();
846  need_separator = true;
847  }
848  os << "]";
849  need_separator = true;
850  }
851 
852 #if NCBI_PACKAGE
853  if (flags & ( fPackageShort | fPackageFull )) {
854  if ( need_separator ) os << ",\n";
855  os << " \"package\": {\n" <<
856  " \"name\": " << NStr::JsonEncode(GetPackageName(), NStr::eJsonEnc_Quoted) << ",\n" <<
857  " \"version_info\": " << GetPackageVersion().PrintJson() << ",\n" <<
858  " \"build_info\": " << NCBI_SBUILDINFO_DEFAULT().PrintJson();
859  if (flags & fPackageFull) {
860  os << ",\n \"config\": " << NStr::JsonEncode(GetPackageConfig(), NStr::eJsonEnc_Quoted);
861  }
862  os << "}";
863  need_separator = true;
864  }
865 #endif
866 
867 #ifdef NCBI_SIGNATURE
868  if (flags & fBuildSignature) {
869  if ( need_separator ) os << ",\n";
870  os << " \"build_signature\": " << NStr::JsonEncode(NCBI_SIGNATURE, NStr::eJsonEnc_Quoted);
871  need_separator = true;
872  }
873 #endif
874 
875  if (flags & fBuildInfo) {
876  if ( need_separator ) os << ",\n";
877  os << " \"build_info\": " << m_BuildInfo.PrintJson();
878  need_separator = true;
879  }
880 
881  os << "\n }\n}\n";
882  return CNcbiOstrstreamToString(os);
883 }
884 
885 
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
CObject –.
Definition: ncbiobj.hpp:180
CStringException –.
Definition: ncbistr.hpp:4500
CTimeException –.
Definition: ncbitime.hpp:2075
CTime –.
Definition: ncbitime.hpp:296
CVersionInfo –.
static void s_ConvertVersionInfo(CVersionInfo *vi, const char *str)
Definition: version.cpp:73
Define CVersionInfo, a version info storage class.
static uch flags
static const char * str(char *buf, int n)
Definition: stats.c:84
int offset
Definition: replacements.h:160
#define NCBI_THROW2(exception_class, err_code, message, extra)
Throw exception with extra parameter.
Definition: ncbiexpt.hpp:1754
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
#define NPOS
Definition: ncbistr.hpp:133
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate whitespace in a string (in-place)
Definition: ncbistr.cpp:3192
static string XmlEncode(const CTempString str, TXmlEncode flags=eXmlEnc_Contents)
Encode a string for XML.
Definition: ncbistr.cpp:4027
static string JsonEncode(const CTempString str, EJsonEncode encoding=eJsonEnc_UTF8)
Encode a string for JSON.
Definition: ncbistr.cpp:4625
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3396
const char *const kEmptyCStr
Empty "C" string (points to a '\0').
Definition: ncbistr.cpp:68
static string & ToLower(string &str)
Convert string to lower case – string& version.
Definition: ncbistr.cpp:405
@ eJsonEnc_Quoted
Quote resulting string.
Definition: ncbistr.hpp:3096
void ParseVersionString(const string &vstr, string *program_name, CVersionInfo *ver)
Parse string, extract version info and program name (case insensitive)
Definition: version.cpp:271
string GetExtraValue(EExtra key, const string &default_value=kEmptyStr) const
Definition: version.cpp:474
int m_Minor
Minor number.
int TPrintFlags
Binary OR of EPrintFlags.
static string ExtraNameJson(EExtra key)
Definition: version.cpp:543
static void x_Copy(CVersionAPI &to, const CVersionAPI &from)
Definition: version.cpp:635
void SetVersionInfo(int ver_major, int ver_minor, int patch_level=0, const string &ver_name=kEmptyStr)
Set version information.
Definition: version.cpp:658
int GetMajor(void) const
Major version.
string PrintJson(void) const
Definition: version.cpp:583
EMatch Match(const CVersionInfo &version_info) const
Check if version matches another version.
Definition: version.cpp:179
CVersionAPI(const SBuildInfo &build_info=SBuildInfo())
Definition: version.cpp:621
const SBuildInfo & GetBuildInfo() const
Get build info (date and tag, if set)
Definition: version.cpp:705
bool IsBetterVersion(const CVersionInfo &info, const CVersionInfo &cinfo, int &best_major, int &best_minor, int &best_patch_level)
Return true if one version info is matches another better than the best variant.
Definition: version.cpp:215
const CVersionInfo & GetVersionInfo() const
Get version information.
Definition: version.cpp:686
int m_PatchLevel
Patch level.
virtual string Print(void) const
Print version information.
Definition: version.cpp:425
string Print(size_t offset=0) const
Definition: version.cpp:548
const string & GetComponentName(void) const
Get component name.
virtual string Print(void) const
Print version information.
Definition: version.cpp:120
static CVersionInfo GetPackageVersion(void)
Definition: version.cpp:715
SBuildInfo(void)
Definition: version.cpp:454
static string GetPackageConfig(void)
Definition: version.cpp:722
SBuildInfo & Extra(EExtra key, const string &value)
Definition: version.cpp:458
static string ExtraName(EExtra key)
Definition: version.cpp:490
virtual string PrintJson(void) const
Print version information as JSON.
Definition: version.cpp:156
int GetMinor(void) const
Minor version.
CVersionAPI & operator=(const CVersionAPI &version)
Definition: version.cpp:651
string date
Definition: version_api.hpp:79
vector< pair< EExtra, string > > m_extra
Definition: version_api.hpp:81
CComponentVersionInfoAPI(const string &component_name, int ver_major, int ver_minor, int patch_level, const string &ver_name, const SBuildInfo &build_info)
Constructor.
Definition: version.cpp:406
unique_ptr< CVersionInfo > m_VersionInfo
CVersionInfo(int ver_major, int ver_minor, int patch_level=0, const string &name=kEmptyStr)
Constructor.
Definition: version.cpp:44
virtual string PrintJson(void) const
Print version information as JSON.
Definition: version.cpp:442
string Print(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, plain text.
Definition: version.cpp:728
EMatch
Version comparison result.
static string GetPackageName(void)
Definition: version.cpp:710
int GetPatchLevel(void) const
Patch level.
string PrintXml(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, XML.
Definition: version.cpp:774
string PrintXml(void) const
Definition: version.cpp:565
void AddComponentVersion(const string &component_name, int ver_major, int ver_minor, int patch_level, const string &ver_name, const SBuildInfo &build_info)
Add component version information.
Definition: version.cpp:691
string tag
Definition: version_api.hpp:80
vector< unique_ptr< CComponentVersionInfoAPI > > m_Components
virtual string PrintXml(void) const
Print version information as XML (see ncbi_version.xsd)
Definition: version.cpp:137
string m_Name
Name.
int m_Major
Major number.
SBuildInfo m_BuildInfo
static string ExtraNameXml(EExtra key)
Definition: version.cpp:532
void FromStr(const string &version)
Take version info from string.
Definition: version.cpp:114
#define NCBI_SBUILDINFO_DEFAULT()
Definition: version.hpp:119
virtual string PrintXml(void) const
Print version information ax XML.
Definition: version.cpp:432
CTime GetBuildTime(void) const
Converts 'date' parameter to CTime.
Definition: version.cpp:606
void SetVersion(int ver_major, int ver_minor, int patch_level=0)
Definition: version.cpp:204
static string ExtraNameAppLog(EExtra key)
Definition: version.cpp:511
string PrintJson(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, JSON.
Definition: version.cpp:824
@ fPackageShort
Print package info, if available.
@ fBuildSignature
Print build signature, if available.
@ fComponents
Print components version info.
@ fGI64bit
Print info about GI size.
@ fVersionInfo
Print version info.
@ fPackageFull
Print package info, if available.
@ fBuildInfo
Print build info (date and tag)
@ eConditionallyCompatible
patch level incompatibility
@ eNonCompatible
major, minor does not match
@ eFullyCompatible
exactly the same version
@ eBackwardCompatible
patch level is newer
@ eTeamCityBuildNumber
Definition: version_api.hpp:69
@ eRevision
Not necessarily numeric.
Definition: version_api.hpp:76
@ eTeamCityProjectName
Definition: version_api.hpp:67
@ eSubversionRevision
Numeric if present.
Definition: version_api.hpp:71
@ eStableComponentsVersion
Definition: version_api.hpp:72
@ eTeamCityBuildConf
Definition: version_api.hpp:68
@ eDevelopmentVersion
Definition: version_api.hpp:73
@ eProductionVersion
Definition: version_api.hpp:74
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
static MDB_envinfo info
Definition: mdb_load.c:37
const string version
version string
Definition: variables.hpp:66
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
static const BitmapCharRec ch2
Definition: ncbi_10x20.c:1819
#define NCBI_PACKAGE_CONFIG
#define NCBI_PACKAGE_NAME
#define NCBI_PACKAGE_VERSION_PATCH
#define NCBI_PACKAGE_VERSION_MINOR
#define NCBI_PACKAGE_VERSION_MAJOR
int isspace(Uchar c)
Definition: ncbictype.hpp:69
int isdigit(Uchar c)
Definition: ncbictype.hpp:64
This class allows to add build info (date and tag) to application version.
Definition: version_api.hpp:62
#define _ASSERT
Modified on Fri Sep 20 14:57:09 2024 by modify_doxy.py rev. 669887