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

Go to the SVN repository for this file.

1 /* $Id: version.cpp 100668 2023-08-24 17:03:58Z gouriano $
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  const char* version_pattern = "version";
313 
314  pos = lo_vstr.find(version_pattern);
315  if (pos == string::npos) {
316  version_pattern = "v.";
317  pos = lo_vstr.find(version_pattern);
318 
319  if (pos == string::npos) {
320  version_pattern = "ver";
321  pos = lo_vstr.find(version_pattern);
322 
323  if (pos == string::npos) {
324  version_pattern = "";
325  // find the first space-digit and assume it's version
326  const char* ch = vstr_str;
327  for (; *ch; ++ch) {
328  if (isdigit((unsigned char)(*ch))) {
329  if (ch == vstr_str) {
330  // check if it's version
331  const char* ch2 = ch + 1;
332  for (;*ch2; ++ch2) {
333  if (!isdigit((unsigned char)(*ch2))) {
334  break;
335  }
336  } // for
337  if (*ch2 == '.') {
338  pos = ch - vstr_str;
339  break;
340  } else {
341  continue;
342  }
343  } else {
344  if (isspace((unsigned char) ch[-1])) {
345  pos = ch - vstr_str;
346  break;
347  }
348  }
349  } // if digit
350 
351  } // for
352  }
353  }
354  }
355 
356 
357  if (pos != string::npos) {
358  int pname_end = (int)(pos - 1);
359  for (; pname_end >= 0; --pname_end) {
360  char ch = vstr[pname_end];
361  if (!isspace((unsigned char) ch))
362  break;
363  } // for
364  if (pname_end <= 0) {
365  } else {
366  program_name->append(vstr.c_str(), pname_end + 1);
367  }
368 
369  pos += strlen(version_pattern);
370  for(; pos < vstr.length(); ++pos) {
371  char ch = vstr[pos];
372  if (ch == '.')
373  continue;
374  if (!isspace((unsigned char) ch))
375  break;
376  } // for
377 
378  const char* ver_str = vstr_str + pos;
379  s_ConvertVersionInfo(ver, ver_str);
380  return;
381  } else {
382  *ver = CVersionInfo::kAny;
383  *program_name = vstr;
384  NStr::TruncateSpacesInPlace(*program_name);
385  if (program_name->empty()) {
386  NCBI_THROW2(CStringException, eFormat, "Version string is empty", 0);
387  }
388  }
389 
390 
391 }
392 
393 /////////////////////////////////////////////////////////////////////////////
394 // CComponentVersionInfoAPI
395 
397  int ver_major,
398  int ver_minor,
399  int patch_level,
400  const string& name, const SBuildInfo& build_info)
401  : CVersionInfo(ver_major, ver_minor, patch_level, name),
402  m_ComponentName( component_name ), m_BuildInfo(build_info)
403 {
404 }
405 
407  const string& version,
408  const string& name, const SBuildInfo& build_info)
409  : CVersionInfo( version, name),
410  m_ComponentName( component_name ), m_BuildInfo(build_info)
411 
412 {
413 }
414 
416 {
417  CNcbiOstrstream os;
418  os << GetComponentName() << ": " << CVersionInfo::Print() << endl << m_BuildInfo.Print(2);
419  return CNcbiOstrstreamToString(os);
420 }
421 
423 {
424  CNcbiOstrstream os;
425  os << "<component name=\"" << NStr::XmlEncode(GetComponentName()) << "\">\n" <<
426  CVersionInfo::PrintXml() << endl <<
427  m_BuildInfo.PrintXml() <<
428  "</component>" << endl;
429  return CNcbiOstrstreamToString(os);
430 }
431 
433 {
434  CNcbiOstrstream os;
435  os << "{ \"name\": " << NStr::JsonEncode(GetComponentName(),NStr::eJsonEnc_Quoted) <<
436  ", \"version_info\": " << CVersionInfo::PrintJson() << ",\n" <<
437  " \"build_info\": " << m_BuildInfo.PrintJson() << "}";
438  return CNcbiOstrstreamToString(os);
439 }
440 
441 /////////////////////////////////////////////////////////////////////////////
442 // SBuildInfo
443 
445  : date(__DATE__ " " __TIME__) {
446 }
447 
449 {
450  if (!value.empty()) {
451  m_extra.push_back( make_pair(key, value));
452  }
453  return *this;
454 }
455 
457 {
458  if (value != 0) {
459  m_extra.push_back( make_pair(key, NStr::NumericToString(value)));
460  }
461  return *this;
462 }
463 
464 string SBuildInfo::GetExtraValue( EExtra key, const string& default_value) const
465 {
466  if (key == eBuildDate) {
467  return date;
468  } else if (key == eBuildTag) {
469  return tag;
470  } else {
471  for( const auto& e : m_extra) {
472  if (e.first == key) {
473  return e.second;
474  }
475  }
476  }
477  return default_value;
478 }
479 
481 {
482  switch (key)
483  {
484  case eBuildDate: return "Build-Date";
485  case eBuildTag: return "Build-Tag";
486  case eTeamCityProjectName: return "TeamCity-Project-Name";
487  case eTeamCityBuildConf: return "TeamCity-BuildConf-Name";
488  case eTeamCityBuildNumber: return "TeamCity-Build-Number";
489  case eBuildID: return "Build-ID";
490  case eSubversionRevision: return "Subversion-Revision";
491  case eStableComponentsVersion: return "Stable-Components-Version";
492  case eDevelopmentVersion: return "Development-Version";
493  case eProductionVersion: return "Production-Version";
494  case eBuiltAs: return "Built-As";
495  case eRevision: return "Revision";
496  case eGitBranch: return "GitBranch";
497  }
498  return "Unknown";
499 }
500 
502 {
503  switch (key)
504  {
505  case eBuildDate: return "ncbi_app_build_date";
506  case eBuildTag: return "ncbi_app_build_tag";
507  case eTeamCityProjectName: return "ncbi_app_tc_project";
508  case eTeamCityBuildConf: return "ncbi_app_tc_conf";
509  case eTeamCityBuildNumber: return "ncbi_app_tc_build";
510  case eBuildID: return "ncbi_app_build_id";
511  case eSubversionRevision: return "ncbi_app_vcs_revision";
512  case eStableComponentsVersion: return "ncbi_app_sc_version";
513  case eDevelopmentVersion: return "ncbi_app_dev_version";
514  case eProductionVersion: return "ncbi_app_prod_version";
515  case eBuiltAs: return "ncbi_app_built_as";
516  case eRevision: return "ncbi_app_revision";
517  case eGitBranch: return "ncbi_app_gitbranch";
518  }
519  return "ncbi_app_unk";
520 }
521 
523 {
524  if (key == eBuildDate) {
525  return "date";
526  } else if (key == eBuildTag) {
527  return "tag";
528  }
529  string s(ExtraName(key));
530  return NStr::ReplaceInPlace(NStr::ToLower(s),"-", "_");
531 }
532 
534 {
535  return ExtraNameXml(key);
536 }
537 
538 string SBuildInfo::Print(size_t offset) const
539 {
540  string prefix(offset+1, ' ');
541  CNcbiOstrstream os;
542  if (!date.empty()) {
543  os << prefix << ExtraName(eBuildDate) << ": " << date << endl;
544  }
545 
546  if (!tag.empty()) {
547  os << prefix << ExtraName(eBuildDate) << ": " << tag << endl;
548  }
549  for( const auto& e : m_extra) {
550  os << prefix << ExtraName(e.first) << ": " << e.second << endl;
551  }
552  return CNcbiOstrstreamToString(os);
553 }
554 
555 string SBuildInfo::PrintXml(void) const
556 {
557  CNcbiOstrstream os;
558  os << "<build_info";
559  if ( !date.empty() ) {
560  os << ' ' << ExtraNameXml(eBuildDate) << "=\"" << NStr::XmlEncode(date) << '\"';
561  }
562  if ( !tag.empty() ) {
563  os << ' ' << ExtraNameXml(eBuildTag) << "=\"" << NStr::XmlEncode(tag) << '\"';
564  }
565  os << ">" << endl;
566  for( const auto& e : m_extra) {
567  os << '<' << ExtraNameXml(e.first) << '>' << NStr::XmlEncode(e.second) << "</" << ExtraNameXml(e.first) << '>' << endl;
568  }
569  os << "</build_info>" << endl;
570  return CNcbiOstrstreamToString(os);
571 }
572 
573 string SBuildInfo::PrintJson(void) const
574 {
575  CNcbiOstrstream os;
576  bool need_separator = false;
577  os << '{';
578  if ( !date.empty() ) {
580  need_separator = true;
581  }
582  if ( !tag.empty() ) {
583  if ( need_separator ) os << ", ";
584  os << '\"' << ExtraNameJson(eBuildTag) << "\": " << NStr::JsonEncode(tag, NStr::eJsonEnc_Quoted);
585  need_separator = true;
586  }
587  for( const auto& e : m_extra) {
588  if ( need_separator ) os << ", ";
589  os << '\"' << ExtraNameJson(e.first) << "\": " << NStr::JsonEncode(e.second, NStr::eJsonEnc_Quoted);
590  need_separator = true;
591  }
592  os << '}';
593  return CNcbiOstrstreamToString(os);
594 }
595 
597 {
598  // According to the documentation, if the compiler cannot get the time of day
599  // it will give you question marks for __DATE__ and __TIME__.
600  try {
601  return CTime(date, "b D Y h:m:s");
602  }
603  catch (CTimeException&) {}
604  return CTime();
605 }
606 
607 
608 /////////////////////////////////////////////////////////////////////////////
609 // CVersion
610 
612  : m_VersionInfo(new CVersionInfo(0,0)),
613  m_BuildInfo(build_info)
614 {
615  m_VersionInfo->SetVersion(m_VersionInfo->GetMajor(), m_VersionInfo->GetMinor(),
617 }
618 
620  : m_VersionInfo(new CVersionInfo(version)),
621  m_BuildInfo(build_info)
622 {
623 }
624 
626 {
627  to.m_VersionInfo.reset(new CVersionInfo(*from.m_VersionInfo));
628  to.m_BuildInfo = from.m_BuildInfo;
629 
630  for (const auto& c : from.m_Components) {
631  to.m_Components.emplace_back(new CComponentVersionInfoAPI(*c));
632  }
633 }
634 
636  : CObject(version)
637 {
638  x_Copy(*this, version);
639 }
640 
642 {
644  x_Copy(*this, version);
645  return *this;
646 }
647 
648 void CVersionAPI::SetVersionInfo( int ver_major, int ver_minor,
649  int patch_level, const string& ver_name)
650 {
651  m_VersionInfo.reset( new CVersionInfo(
652  ver_major, ver_minor, patch_level, ver_name) );
653 }
654 
655 void CVersionAPI::SetVersionInfo( int ver_major, int ver_minor,
656  int patch_level, const string& ver_name,
657  const SBuildInfo& build_info)
658 {
659  m_VersionInfo.reset( new CVersionInfo(
660  ver_major, ver_minor, patch_level, ver_name) );
661  m_BuildInfo = build_info;
662 }
663 
665 {
666  m_VersionInfo.reset( version );
667 }
668 
670  const SBuildInfo& build_info)
671 {
672  m_VersionInfo.reset( version );
673  m_BuildInfo = build_info;
674 }
675 
677 {
678  return *m_VersionInfo;
679 }
680 
682  const string& component_name, int ver_major, int ver_minor,
683  int patch_level, const string& ver_name, const SBuildInfo& build_info)
684 {
685  m_Components.emplace_back(
686  new CComponentVersionInfoAPI(component_name, ver_major, ver_minor,
687  patch_level, ver_name, build_info));
688 }
689 
691 {
692  m_Components.emplace_back(component);
693 }
694 
696 {
697  return m_BuildInfo;
698 }
699 
701 {
702  return NCBI_PACKAGE_NAME;
703 }
704 
706 {
710 }
711 
713 {
714  return NCBI_PACKAGE_CONFIG;
715 }
716 
717 
718 string CVersionAPI::Print(const string& appname, TPrintFlags flags) const
719 {
720  CNcbiOstrstream os;
721 
722  if (flags & fVersionInfo) {
723  os << appname << ": " << m_VersionInfo->Print() << endl;
724  }
725 
726 #if NCBI_PACKAGE
727  if (flags & ( fPackageShort | fPackageFull )) {
728  os << " Package: " << GetPackageName() << ' '
729  << GetPackageVersion().Print() << ", build "
730  << NCBI_SBUILDINFO_DEFAULT().date
731  << endl;
732  }
733  if (flags & fPackageFull) {
734  os << " Package-Config: " << ' ' << GetPackageConfig() << endl;
735  }
736 #endif
737 
738 #ifdef NCBI_SIGNATURE
739  if (flags & fBuildSignature) {
740  os << " Build-Signature: " << ' ' << NCBI_SIGNATURE << endl;
741  }
742 #endif
743  if (flags & fGI64bit) {
744 #ifdef NCBI_INT8_GI
745  os << " GI-64bit: TRUE" << endl;
746 #else
747  os << " GI-64bit: FALSE" << endl;
748 #endif
749  }
750 
751  if (flags & fBuildInfo) {
752  os << m_BuildInfo.Print();
753  }
754 
755  if (flags & fComponents) {
756  for (const auto& c : m_Components) {
757  os << endl << ' ' << c->Print() << endl;
758  }
759  }
760  return CNcbiOstrstreamToString(os);
761 }
762 
763 
764 string CVersionAPI::PrintXml(const string& appname, TPrintFlags flags) const
765 {
766  CNcbiOstrstream os;
767 
768  os << "<?xml version=\"1.0\"?>\n"
769  "<ncbi_version xmlns=\"ncbi:version\"\n"
770  " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
771  " xs:schemaLocation=\"ncbi:version ncbi_version.xsd\">\n";
772 
773  if (flags & fVersionInfo) {
774  if ( !appname.empty() ) {
775  os << "<appname>" << NStr::XmlEncode(appname) << "</appname>\n";
776  }
777  os << m_VersionInfo->PrintXml();
778  }
779 
780  if (flags & fComponents) {
781  for (const auto& c : m_Components) {
782  os << c->PrintXml();
783  }
784  }
785 
786 #if NCBI_PACKAGE
787  if (flags & ( fPackageShort | fPackageFull )) {
788  os << "<package name=\"" << NStr::XmlEncode(GetPackageName()) << "\">\n" <<
790  NCBI_SBUILDINFO_DEFAULT().PrintXml();
791  if (flags & fPackageFull) {
792  os << "<config>" << NStr::XmlEncode(GetPackageConfig()) << "</config>\n";
793  }
794  os << "</package>\n";
795  }
796 #endif
797 
798 #ifdef NCBI_SIGNATURE
799  if (flags & fBuildSignature) {
800  os << "<build_signature>" << NStr::XmlEncode(NCBI_SIGNATURE) << "</build_signature>\n";
801  }
802 #endif
803 
804  if (flags & fBuildInfo) {
805  os << m_BuildInfo.PrintXml();
806  }
807 
808  os << "</ncbi_version>\n";
809 
810  return CNcbiOstrstreamToString(os);
811 }
812 
813 
814 string CVersionAPI::PrintJson(const string& appname, TPrintFlags flags) const
815 {
816  CNcbiOstrstream os;
817  bool need_separator = false;
818 
819  os << "{\n \"ncbi_version\": {\n";
820 
821  if (flags & fVersionInfo) {
822  if ( !appname.empty() ) {
823  os << " \"appname\": " << NStr::JsonEncode(appname, NStr::eJsonEnc_Quoted) << ",\n";
824  }
825  os << " \"version_info\": " << m_VersionInfo->PrintJson();
826  need_separator = true;
827  }
828 
829  if (flags & fComponents) {
830  if ( need_separator ) os << ",\n";
831  os << " \"component\": [";
832  need_separator = false;
833  for (const auto& c : m_Components) {
834  if ( need_separator ) os << ",";
835  os << "\n " << c->PrintJson();
836  need_separator = true;
837  }
838  os << "]";
839  need_separator = true;
840  }
841 
842 #if NCBI_PACKAGE
843  if (flags & ( fPackageShort | fPackageFull )) {
844  if ( need_separator ) os << ",\n";
845  os << " \"package\": {\n" <<
846  " \"name\": " << NStr::JsonEncode(GetPackageName(), NStr::eJsonEnc_Quoted) << ",\n" <<
847  " \"version_info\": " << GetPackageVersion().PrintJson() << ",\n" <<
848  " \"build_info\": " << NCBI_SBUILDINFO_DEFAULT().PrintJson();
849  if (flags & fPackageFull) {
850  os << ",\n \"config\": " << NStr::JsonEncode(GetPackageConfig(), NStr::eJsonEnc_Quoted);
851  }
852  os << "}";
853  need_separator = true;
854  }
855 #endif
856 
857 #ifdef NCBI_SIGNATURE
858  if (flags & fBuildSignature) {
859  if ( need_separator ) os << ",\n";
860  os << " \"build_signature\": " << NStr::JsonEncode(NCBI_SIGNATURE, NStr::eJsonEnc_Quoted);
861  need_separator = true;
862  }
863 #endif
864 
865  if (flags & fBuildInfo) {
866  if ( need_separator ) os << ",\n";
867  os << " \"build_info\": " << m_BuildInfo.PrintJson();
868  need_separator = true;
869  }
870 
871  os << "\n }\n}\n";
872  return CNcbiOstrstreamToString(os);
873 }
874 
875 
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
CObject –.
Definition: ncbiobj.hpp:180
CStringException –.
Definition: ncbistr.hpp:4505
CTimeException –.
Definition: ncbitime.hpp:2076
CTime –.
Definition: ncbitime.hpp:296
CVersionInfo –.
char value[7]
Definition: config.c:431
static void s_ConvertVersionInfo(CVersionInfo *vi, const char *str)
Definition: version.cpp:73
Define CVersionInfo, a version info storage class.
static uch flags
#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
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3197
static string XmlEncode(const CTempString str, TXmlEncode flags=eXmlEnc_Contents)
Encode a string for XML.
Definition: ncbistr.cpp:4032
static string JsonEncode(const CTempString str, EJsonEncode encoding=eJsonEnc_UTF8)
Encode a string for JSON.
Definition: ncbistr.cpp:4630
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:3401
static string & ToLower(string &str)
Convert string to lower case – string& version.
Definition: ncbistr.cpp:405
@ eJsonEnc_Quoted
Quote resulting string.
Definition: ncbistr.hpp:3094
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:464
int m_Minor
Minor number.
int TPrintFlags
Binary OR of EPrintFlags.
static string ExtraNameJson(EExtra key)
Definition: version.cpp:533
static void x_Copy(CVersionAPI &to, const CVersionAPI &from)
Definition: version.cpp:625
void SetVersionInfo(int ver_major, int ver_minor, int patch_level=0, const string &ver_name=kEmptyStr)
Set version information.
Definition: version.cpp:648
int GetMajor(void) const
Major version.
string PrintJson(void) const
Definition: version.cpp:573
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:611
const SBuildInfo & GetBuildInfo() const
Get build info (date and tag, if set)
Definition: version.cpp:695
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:676
int m_PatchLevel
Patch level.
virtual string Print(void) const
Print version information.
Definition: version.cpp:415
string Print(size_t offset=0) const
Definition: version.cpp:538
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:705
SBuildInfo(void)
Definition: version.cpp:444
static string GetPackageConfig(void)
Definition: version.cpp:712
SBuildInfo & Extra(EExtra key, const string &value)
Definition: version.cpp:448
static string ExtraName(EExtra key)
Definition: version.cpp:480
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:641
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:396
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:432
string Print(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, plain text.
Definition: version.cpp:718
EMatch
Version comparison result.
static string GetPackageName(void)
Definition: version.cpp:700
int GetPatchLevel(void) const
Patch level.
string PrintXml(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, XML.
Definition: version.cpp:764
string PrintXml(void) const
Definition: version.cpp:555
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:681
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:522
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:422
CTime GetBuildTime(void) const
Converts 'date' parameter to CTime.
Definition: version.cpp:596
void SetVersion(int ver_major, int ver_minor, int patch_level=0)
Definition: version.cpp:204
static string ExtraNameAppLog(EExtra key)
Definition: version.cpp:501
string PrintJson(const string &appname, TPrintFlags flags=fPrintAll) const
Print version data, JSON.
Definition: version.cpp:814
@ 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
static int version
Definition: mdb_load.c:29
const struct ncbi::grid::netcache::search::fields::KEY key
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
static const char * prefix[]
Definition: pcregrep.c:405
int offset
Definition: replacements.h:160
static const char * str(char *buf, int n)
Definition: stats.c:84
This class allows to add build info (date and tag) to application version.
Definition: version_api.hpp:62
#define _ASSERT
Modified on Thu Nov 30 04:56:33 2023 by modify_doxy.py rev. 669887