AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
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 
145  ret = mpImpl->Rescan(sections);
146  }
147 
148  return ret;
149 }
150 
151 AJAStatus AJASystemInfo::GetValue (const AJASystemInfoTag tag, string & outValue) const
152 {
153  outValue = "";
154  if (mpImpl && mpImpl->mValueMap.count(int(tag)) != 0)
155  {
156  outValue = mpImpl->mValueMap[int(tag)];
157  return AJA_STATUS_SUCCESS;
158  }
159  return AJA_STATUS_FAIL;
160 }
161 
162 AJAStatus AJASystemInfo::GetLabel (const AJASystemInfoTag tag, string & outLabel) const
163 {
164  outLabel = "";
165  if (mpImpl && mpImpl->mLabelMap.count(int(tag)) != 0)
166  {
167  outLabel = mpImpl->mLabelMap[int(tag)];
168  return AJA_STATUS_SUCCESS;
169  }
170  return AJA_STATUS_FAIL;
171 }
172 
174 {
175  if (clearTable)
176  outTable.clear();
177 
179  {
180  string label, value;
181  if (AJA_SUCCESS(GetLabel(tag, label)) && AJA_SUCCESS(GetValue(tag, value)))
182  if (!label.empty())
183  append(outTable, label, value);
184  }
185 
186  return outTable.empty() ? AJA_STATUS_FAIL : AJA_STATUS_SUCCESS;
187 }
188 
189 string AJASystemInfo::ToString (const size_t inValueWrapLen, const size_t inGutterWidth) const
190 {
191  AJALabelValuePairs infoTable;
192  append(infoTable, "HOST INFO");
193  GetLabelValuePairs(infoTable, false);
194  return ToString(infoTable, inValueWrapLen, inGutterWidth);
195 }
196 
197 void AJASystemInfo::ToString(string& allLabelsAndValues) const
198 {
199  allLabelsAndValues = ToString();
200 }
201 
202 ostream & operator << (ostream & outStream, const AJASystemInfo & inData)
203 {
204  outStream << inData.ToString();
205  return outStream;
206 }
207 
208 ostream & operator << (ostream & outStream, const AJALabelValuePair & inData)
209 {
210  string label(inData.first);
211  const string & value(inData.second);
212  if (label.empty())
213  return outStream;
214  aja::strip(label);
215  if (label.at(label.length()-1) == ':')
216  label.resize(label.length()-1);
217  aja::replace(label, " ", "_");
218  outStream << label << "=" << value;
219  return outStream;
220 }
221 
222 ostream & operator << (ostream & outStream, const AJALabelValuePairs & inData)
223 {
224  for (AJALabelValuePairsConstIter it(inData.begin()); it != inData.end(); )
225  {
226  outStream << *it;
227  if (++it != inData.end())
228  outStream << ", ";
229  }
230  return outStream;
231 }
info.h
Declares the AJASystemInfo class.
AJA_SystemInfoTag_System_Bios
@ AJA_SystemInfoTag_System_Bios
Definition: info.h:32
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_LAST
@ AJA_SystemInfoTag_LAST
Definition: info.h:52
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:151
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:68
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:173
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:55
AJALabelValuePairsConstIter
AJALabelValuePairs::const_iterator AJALabelValuePairsConstIter
Definition: info.h:70
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:202
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:69
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:162
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:78