AJA NTV2 SDK  18.0.0.2717
NTV2 SDK 18.0.0.2717
info.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 // include the system dependent implementation class
9 #if defined(AJA_WINDOWS)
11 #elif defined(AJA_LINUX)
13 #elif defined(AJA_MAC)
15 #elif defined(AJA_BAREMETAL)
17 #endif
18 #include "ajabase/system/info.h"
19 #include <cstring>
20 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23 
24 using namespace std;
25 
26 
27 // STATIC
28 string AJASystemInfo::ToString (const AJALabelValuePairs & inLabelValuePairs, const size_t inMaxValWidth, const size_t inGutterWidth)
29 {
30  typedef std::vector<string> ValueLines;
31  typedef ValueLines::const_iterator ValueLinesConstIter;
32  const string gutterStr (inGutterWidth, ' ');
33 
34  // Measure longest label length...
35  // BUGBUGBUG Multi-byte UTF8 characters should only be counted as one character
36  size_t longestLabelLen(0);
37  for (AJALabelValuePairsConstIter it(inLabelValuePairs.begin()); it != inLabelValuePairs.end(); ++it)
38  if (it->first.length() > longestLabelLen)
39  longestLabelLen = it->first.length();
40  longestLabelLen++; // Plus the ':'
41 
42  // Iterate over everything again, this time "printing" the map's contents...
43  ostringstream oss;
44  for (AJALabelValuePairsConstIter it(inLabelValuePairs.begin()); it != inLabelValuePairs.end(); ++it)
45  {
46  static const string lineBreakChars("\r\n");
47  string label(it->first), value(it->second);
48  const bool hasLineBreaks (value.find_first_of(lineBreakChars) != string::npos);
49  if (value.empty() && it != inLabelValuePairs.begin()) // Empty value string is a special case...
50  oss << endl; // ...don't append ':' and prepend an extra blank line
51  else
52  label += ":";
53 
54  if (!hasLineBreaks && !inMaxValWidth)
55  { // No wrapping or line-breaks -- just output the line...
56  oss << setw(int(longestLabelLen)) << left << label << gutterStr << value << endl;
57  continue; // ...and move on to the next
58  }
59 
60  // Wrapping/line-breaking:
61  ValueLines valueLines, finalLines;
62  if (hasLineBreaks)
63  {
64  static const string lineBreakDelims[] = {"\r\n", "\r", "\n"};
65  aja::replace(value, lineBreakDelims[0], lineBreakDelims[2]); // CRLF => LF
66  aja::replace(value, lineBreakDelims[1], lineBreakDelims[2]); // CR => LF
67  valueLines = aja::split(value, lineBreakDelims[2][0]); // Split on LF
68  }
69  else
70  valueLines.push_back(value);
71  if (inMaxValWidth)
72  for (ValueLinesConstIter iter(valueLines.begin()); iter != valueLines.end(); ++iter)
73  {
74  const string & lineStr(*iter);
75  size_t pos(0);
76  do
77  {
78  finalLines.push_back(lineStr.substr(pos, inMaxValWidth));
79  pos += inMaxValWidth;
80  } while (pos < lineStr.length());
81  } // for each valueLine
82  else
83  finalLines = valueLines;
84 
85  const string wrapIndentStr (longestLabelLen + inGutterWidth, ' ');
86  for (size_t ndx(0); ndx < finalLines.size(); ndx++)
87  {
88  const string & valStr(finalLines.at(ndx));
89  if (ndx)
90  oss << wrapIndentStr << valStr << endl;
91  else
92  oss << setw(int(longestLabelLen)) << left << label << gutterStr << valStr << endl;
93  }
94  } // for each label/value pair
95 
96  return oss.str();
97 }
98 
99 
101 {
102  // create the implementation class
103  mpImpl = new AJASystemInfoImpl(units);
104 
105  Rescan(sections);
106 }
107 
109 {
110  if(mpImpl)
111  {
112  delete mpImpl;
113  mpImpl = NULL;
114  }
115 }
116 
118 {
120  if(mpImpl)
121  {
122  // labels
123  mpImpl->mLabelMap[int(AJA_SystemInfoTag_System_Model)] = "System Model";
124  mpImpl->mLabelMap[int(AJA_SystemInfoTag_System_Bios)] = "System BIOS";
125  mpImpl->mLabelMap[int(AJA_SystemInfoTag_System_Name)] = "System Name";
126  mpImpl->mLabelMap[int(AJA_SystemInfoTag_System_BootTime)] = "System Boot Time";
127  mpImpl->mLabelMap[int(AJA_SystemInfoTag_OS_ProductName)] = "OS Product Name";
128  mpImpl->mLabelMap[int(AJA_SystemInfoTag_OS_Version)] = "OS Version";
129  mpImpl->mLabelMap[int(AJA_SystemInfoTag_OS_VersionBuild)] = "OS Build";
130  mpImpl->mLabelMap[int(AJA_SystemInfoTag_OS_KernelVersion)] = "OS Kernel Version";
131  mpImpl->mLabelMap[int(AJA_SystemInfoTag_CPU_Type)] = "CPU Type";
132  mpImpl->mLabelMap[int(AJA_SystemInfoTag_CPU_NumCores)] = "CPU Num Cores";
133  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Mem_Total)] = "Memory Total";
134  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Mem_Used)] = "Memory Used";
135  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Mem_Free)] = "Memory Free";
136  mpImpl->mLabelMap[int(AJA_SystemInfoTag_GPU_Type)] = "GPU Type";
137 
138  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_UserHome)] = "User Home Path";
139  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_PersistenceStoreUser)] = "User Persistence Store Path";
140  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_PersistenceStoreSystem)] = "System Persistence Store Path";
141  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_Applications)] = "AJA Applications Path";
142  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_Utilities)] = "AJA Utilities Path";
143  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_Firmware)] = "AJA Firmware Path";
144  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_NTV2Plugins)] = "AJA Plugins Path";
145  mpImpl->mLabelMap[int(AJA_SystemInfoTag_Path_NTV2VirtualDevices)] = "AJA Virtual Devices Path";
146 
147  ret = mpImpl->Rescan(sections);
148  }
149 
150  return ret;
151 }
152 
153 AJAStatus AJASystemInfo::GetValue (const AJASystemInfoTag tag, string & outValue) const
154 {
155  outValue = "";
156  if (mpImpl && mpImpl->mValueMap.count(int(tag)) != 0)
157  {
158  outValue = mpImpl->mValueMap[int(tag)];
159  return AJA_STATUS_SUCCESS;
160  }
161  return AJA_STATUS_FAIL;
162 }
163 
164 AJAStatus AJASystemInfo::GetLabel (const AJASystemInfoTag tag, string & outLabel) const
165 {
166  outLabel = "";
167  if (mpImpl && mpImpl->mLabelMap.count(int(tag)) != 0)
168  {
169  outLabel = mpImpl->mLabelMap[int(tag)];
170  return AJA_STATUS_SUCCESS;
171  }
172  return AJA_STATUS_FAIL;
173 }
174 
176 {
177  if (clearTable)
178  outTable.clear();
179 
181  {
182  string label, value;
183  if (AJA_SUCCESS(GetLabel(tag, label)) && AJA_SUCCESS(GetValue(tag, value)))
184  if (!label.empty())
185  append(outTable, label, value);
186  }
187 
188  return outTable.empty() ? AJA_STATUS_FAIL : AJA_STATUS_SUCCESS;
189 }
190 
191 string AJASystemInfo::ToString (const size_t inValueWrapLen, const size_t inGutterWidth) const
192 {
193  AJALabelValuePairs infoTable;
194  append(infoTable, "HOST INFO");
195  GetLabelValuePairs(infoTable, false);
196  return ToString(infoTable, inValueWrapLen, inGutterWidth);
197 }
198 
199 void AJASystemInfo::ToString(string& allLabelsAndValues) const
200 {
201  allLabelsAndValues = ToString();
202 }
203 
204 ostream & operator << (ostream & outStream, const AJASystemInfo & inData)
205 {
206  outStream << inData.ToString();
207  return outStream;
208 }
209 
210 ostream & operator << (ostream & outStream, const AJALabelValuePair & inData)
211 {
212  string label(inData.first);
213  const string & value(inData.second);
214  if (label.empty())
215  return outStream;
216  aja::strip(label);
217  if (label.at(label.length()-1) == ':')
218  label.resize(label.length()-1);
219  aja::replace(label, " ", "_");
220  outStream << label << "=" << value;
221  return outStream;
222 }
223 
224 ostream & operator << (ostream & outStream, const AJALabelValuePairs & inData)
225 {
226  for (AJALabelValuePairsConstIter it(inData.begin()); it != inData.end(); )
227  {
228  outStream << *it;
229  if (++it != inData.end())
230  outStream << ", ";
231  }
232  return outStream;
233 }
info.h
Declares the AJASystemInfo class.
AJA_SystemInfoTag_System_Bios
@ AJA_SystemInfoTag_System_Bios
Definition: info.h:32
AJA_SystemInfoTag_Path_NTV2VirtualDevices
@ AJA_SystemInfoTag_Path_NTV2VirtualDevices
Definition: info.h:52
aja::strip
std::string & strip(std::string &str, const std::string &ws)
Definition: common.cpp:461
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
aja::split
void split(const std::string &str, const char delim, std::vector< std::string > &elems)
Definition: common.cpp:350
AJA_SystemInfoTag_Path_Firmware
@ AJA_SystemInfoTag_Path_Firmware
Definition: info.h:50
AJASystemInfoMemoryUnit
AJASystemInfoMemoryUnit
Definition: info.h:19
AJASystemInfoTag
AJASystemInfoTag
Definition: info.h:29
AJA_SystemInfoTag_OS_KernelVersion
@ AJA_SystemInfoTag_OS_KernelVersion
Definition: info.h:38
AJA_SystemInfoTag_Path_Applications
@ AJA_SystemInfoTag_Path_Applications
Definition: info.h:48
AJA_SystemInfoTag_Path_NTV2Plugins
@ AJA_SystemInfoTag_Path_NTV2Plugins
Definition: info.h:51
AJA_SystemInfoTag_LAST
@ AJA_SystemInfoTag_LAST
Definition: info.h:54
AJA_SystemInfoTag_CPU_Type
@ AJA_SystemInfoTag_CPU_Type
Definition: info.h:39
AJAStatus
AJAStatus
Definition: types.h:378
AJA_SystemInfoTag_CPU_NumCores
@ AJA_SystemInfoTag_CPU_NumCores
Definition: info.h:40
aja::replace
std::string & replace(std::string &str, const std::string &from, const std::string &to)
Definition: common.cpp:110
AJA_STATUS_FAIL
@ AJA_STATUS_FAIL
Definition: types.h:382
AJASystemInfo::GetValue
virtual AJAStatus GetValue(const AJASystemInfoTag inTag, std::string &outValue) const
Answers with the host system info value string for the given AJASystemInfoTag.
Definition: info.cpp:153
AJA_SystemInfoTag_System_Name
@ AJA_SystemInfoTag_System_Name
Definition: info.h:33
AJASystemInfo::AJASystemInfo
AJASystemInfo(const AJASystemInfoMemoryUnit inUnits=AJA_SystemInfoMemoryUnit_Megabytes, AJASystemInfoSections sections=AJA_SystemInfoSection_All)
Default constructor that instantiates the platform-specific implementation, then calls Rescan.
Definition: info.cpp:100
AJA_SystemInfoTag_System_BootTime
@ AJA_SystemInfoTag_System_BootTime
Definition: info.h:34
AJA_SystemInfoTag_OS_Version
@ AJA_SystemInfoTag_OS_Version
Definition: info.h:36
AJALabelValuePair
std::pair< std::string, std::string > AJALabelValuePair
A pair of strings comprising a label and a value.
Definition: info.h:70
AJA_SystemInfoTag_Mem_Used
@ AJA_SystemInfoTag_Mem_Used
Definition: info.h:42
AJASystemInfo::ToString
virtual void ToString(std::string &outAllLabelsAndValues) const
Answers with a multi-line string that contains the complete host system info table.
AJASystemInfo::GetLabelValuePairs
virtual AJAStatus GetLabelValuePairs(AJALabelValuePairs &outTable, bool clearTable=false) const
Generates a "table" of label/value pairs that contains the complete host system info table.
Definition: info.cpp:175
infoimpl.h
AJASystemInfo::Rescan
virtual AJAStatus Rescan(AJASystemInfoSections sections=AJA_SystemInfoSection_All)
Definition: info.cpp:117
AJA_SUCCESS
#define AJA_SUCCESS(_status_)
Definition: types.h:370
AJA_SystemInfoTag_Path_PersistenceStoreUser
@ AJA_SystemInfoTag_Path_PersistenceStoreUser
Definition: info.h:46
AJASystemInfoSections
AJASystemInfoSections
Definition: info.h:57
AJALabelValuePairsConstIter
AJALabelValuePairs::const_iterator AJALabelValuePairsConstIter
Definition: info.h:72
AJASystemInfoImpl
Definition: infoimpl.h:14
AJA_SystemInfoTag_Path_PersistenceStoreSystem
@ AJA_SystemInfoTag_Path_PersistenceStoreSystem
Definition: info.h:47
AJA_SystemInfoTag_OS_ProductName
@ AJA_SystemInfoTag_OS_ProductName
Definition: info.h:35
infoimpl.h
Declares the AJASystemInfoImpl class.
AJASystemInfo::~AJASystemInfo
virtual ~AJASystemInfo()
Definition: info.cpp:108
operator<<
ostream & operator<<(ostream &outStream, const AJASystemInfo &inData)
Definition: info.cpp:204
std
Definition: json.hpp:5362
AJA_SystemInfoTag_Path_Utilities
@ AJA_SystemInfoTag_Path_Utilities
Definition: info.h:49
AJA_SystemInfoTag_Path_UserHome
@ AJA_SystemInfoTag_Path_UserHome
Definition: info.h:45
AJA_SystemInfoTag_Mem_Free
@ AJA_SystemInfoTag_Mem_Free
Definition: info.h:43
AJA_SystemInfoTag_GPU_Type
@ AJA_SystemInfoTag_GPU_Type
Definition: info.h:44
infoimpl.h
Declares the AJASystemInfoImpl class.
AJA_SystemInfoTag_OS_VersionBuild
@ AJA_SystemInfoTag_OS_VersionBuild
Definition: info.h:37
AJALabelValuePairs
std::vector< AJALabelValuePair > AJALabelValuePairs
An ordered sequence of label/value pairs.
Definition: info.h:71
AJASystemInfo::GetLabel
virtual AJAStatus GetLabel(const AJASystemInfoTag inTag, std::string &outLabel) const
Answers with the host system info label string for the given AJASystemInfoTag.
Definition: info.cpp:164
AJA_SystemInfoTag_Mem_Total
@ AJA_SystemInfoTag_Mem_Total
Definition: info.h:41
infoimpl.h
Declares the AJASystemInfoImpl class.
AJA_SystemInfoTag_System_Model
@ AJA_SystemInfoTag_System_Model
Definition: info.h:31
AJASystemInfo
Definition: info.h:80