C++ Samples
Demonstrates how to create a C++ application using the VideoXpert SDK
CppSamples::Common::Utility Class Reference

#include <Utility.h>

Public Member Functions

 Utility ()
 
 ~Utility ()
 

Static Public Member Functions

static bool Init ()
 Initialize the SDK. More...
 
static VxSdk::IVxSystemLogin (VxSdk::VxLoginInfo &loginInfo)
 Login to the VideoExpert system. More...
 
static std::string ConvertLocalTimetoUTC (struct tm t)
 Converts the local time to UTC format More...
 
static std::string ConvertUTCtoLocalTime (struct tm t)
 Converts the UTC time to local time format More...
 
static std::string Encode (const std::string &toEncode)
 Encode given std::string using Base64 More...
 
static struct tm GetDateAndTimeFromUser ()
 Get the date and time input from user and returns in struct format More...
 
static struct tm ParseDateTime (char *dateTime)
 Parse a date time string and return in struct format More...
 
static void ShowProgress (std::string statusMsg, unsigned int x, unsigned int n, int w=50)
 Show progress indicator in console More...
 
static std::string GetNewGuid ()
 Initializes a new instance of the System.Guid structure and returns equivalent std::string. More...
 
static int TzOffset ()
 Get the time zone offset in seconds More...
 
static void ClearScreen ()
 Clear the screen More...
 
static std::string ReadString ()
 Read a line from console input More...
 
static int ReadInt ()
 Read an integer from console input More...
 
static void Pause ()
 Pause for a user input More...
 
static std::string Replace (std::string original, std::string from, std::string to)
 Replace a give string More...
 
static std::string ConvertUTCTimeFormatToString (std::string utcFormat)
 Converts UTC time to string More...
 

Detailed Description

Definition at line 12 of file Utility.h.

Constructor & Destructor Documentation

CppSamples::Common::Utility::Utility ( )
inline

Definition at line 14 of file Utility.h.

14 {}
CppSamples::Common::Utility::~Utility ( )
inline

Definition at line 15 of file Utility.h.

15 {}

Member Function Documentation

void Utility::ClearScreen ( )
static

Clear the screen

Definition at line 249 of file Utility.cpp.

249  {
250 #ifndef WIN32
251  system("clear");
252 #else
253  system("cls");
254 #endif
255 }
string Utility::ConvertLocalTimetoUTC ( struct tm  t)
static

Converts the local time to UTC format

Parameters
tTime struct to convert
Parameters
ttime struct to convert

Definition at line 28 of file Utility.cpp.

28  {
29  // Convert local time to UTC
30  stringstream fmt;
31  time_t epochTime = mktime(&t);
32  struct tm timeinfo;
33 #ifndef WIN32
34  gmtime_r(&epochTime, &timeinfo);
35 #else
36  gmtime_s(&timeinfo, &epochTime);
37 #endif
38 
39  // Convert tm structure to string
40  fmt << timeinfo.tm_year + 1900 << "-"
41  << setfill('0') << setw(2) << (timeinfo.tm_mon + 1) << "-"
42  << setfill('0') << setw(2) << timeinfo.tm_mday << "T"
43  << setfill('0') << setw(2) << timeinfo.tm_hour << ":"
44  << setfill('0') << setw(2) << timeinfo.tm_min << ":"
45  << setfill('0') << setw(2) << timeinfo.tm_sec << "Z";
46  return fmt.str();
47 }
#define gmtime_s(time, result)
string Utility::ConvertUTCTimeFormatToString ( std::string  utcFormat)
static

Converts UTC time to string

Converts the UTC time format to string format (HH:MM::SS, Day Month)

Parameters
utcFormattime string in UTC format (YYYYmmddTHHMMSSZ)

Definition at line 312 of file Utility.cpp.

312  {
313  stringstream dateStream(utcFormat);
314  struct tm parseTime;
315  //Here parse date and time string value to time structure
316  dateStream >> get_time(&parseTime, "%Y-%m-%dT%H:%M:%S");
317  char buffer[18];
318  strftime(buffer, 18, "%X %x", &parseTime);
319 
320  return string(buffer);
321 }
string Utility::ConvertUTCtoLocalTime ( struct tm  t)
static

Converts the UTC time to local time format

Parameters
tTime struct to convert
Parameters
ttime struct to convert

Definition at line 53 of file Utility.cpp.

53  {
54  // Convert UTC to local time
55  stringstream fmt;
56  time_t epochTime = mktime(&t);
57  epochTime += TzOffset();
58  struct tm timeinfo;
59  localtime_s(&timeinfo, &epochTime);
60 
61  // Convert tm structure to string
62  fmt << timeinfo.tm_year + 1900 << "-"
63  << setfill('0') << setw(2) << (timeinfo.tm_mon + 1) << "-"
64  << setfill('0') << setw(2) << timeinfo.tm_mday << "T"
65  << setfill('0') << setw(2) << timeinfo.tm_hour << ":"
66  << setfill('0') << setw(2) << timeinfo.tm_min << ":"
67  << setfill('0') << setw(2) << timeinfo.tm_sec << "Z";
68  return fmt.str();
69 }
static int TzOffset()
Get the time zone offset in seconds
Definition: Utility.cpp:229
#define localtime_s(time, result)
string Utility::Encode ( const std::string &  toEncode)
static

Encode given std::string using Base64

Encode given string using Base64

Parameters
toEncodeString to encode
Parameters
toEncodestring to encode

Definition at line 75 of file Utility.cpp.

75  {
76  string kBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
77  const char* data = toEncode.c_str();
78  size_t size = toEncode.length();
79  string ret;
80  int i = 0;
81  unsigned char char_array_3[3];
82  unsigned char char_array_4[4];
83 
84  while (size--) {
85  char_array_3[i++] = *(data++);
86  if (i == 3) {
87  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
88  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
89  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
90  char_array_4[3] = char_array_3[2] & 0x3f;
91 
92  for (i = 0; i < 4; i++)
93  ret += kBase64Chars[char_array_4[i]];
94  i = 0;
95  }
96  }
97 
98  if (i) {
99  int j = 0;
100  for (j = i; j < 3; j++)
101  char_array_3[j] = '\0';
102 
103  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
104  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
105  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
106  char_array_4[3] = char_array_3[2] & 0x3f;
107 
108  for (j = 0; j < i + 1; j++)
109  ret += kBase64Chars[char_array_4[j]];
110 
111  while (i++ < 3)
112  ret += '=';
113  }
114  return ret;
115 }
tm Utility::GetDateAndTimeFromUser ( )
static

Get the date and time input from user and returns in struct format

Definition at line 120 of file Utility.cpp.

120  {
121  struct tm parseTime;
122  while (true) {
123  // Read a line from user input
124  string dateAndTimeVal;
125  getline(cin, dateAndTimeVal);
126 
127  if (!dateAndTimeVal.empty()) {
128  // Here parse date and time string value to time structure
129  stringstream dateStream(dateAndTimeVal);
130  dateStream >> get_time(&parseTime, "%Y-%m-%d %H:%M:%S");
131  if (dateStream.fail()) {
132  cout << "Invalid time.\n";
133  }
134  else {
135  break;
136  }
137  }
138  else {
139  cout << "Please enter a time.\n";
140  }
141  cout << "\n" << "Enter time (yyyy-mm-dd hh:mm:ss): ";
142  }
143 
144  // Create new instance of time structure to avoid unnecessary info.
145  struct tm t;
146  t.tm_mon = parseTime.tm_mon;
147  t.tm_year = parseTime.tm_year;
148  t.tm_mday = parseTime.tm_mday;
149  t.tm_hour = parseTime.tm_hour;
150  t.tm_min = parseTime.tm_min;
151  t.tm_sec = parseTime.tm_sec;
152  return t;
153 }
string Utility::GetNewGuid ( )
static

Initializes a new instance of the System.Guid structure and returns equivalent std::string.

Initializes a new instance of the System.Guid structure and returns equivalent string.

Returns
String value of new Guid.

Definition at line 204 of file Utility.cpp.

204  {
205  // Create new instance of GUID
206 #ifndef WIN32
207  uuid_t uuid;
208  uuid_generate_random ( uuid );
209  char buffer[37];
210  uuid_unparse ( uuid, buffer );
211  return string(buffer);
212 #else
213  GUID newGuid;
214  HRESULT hCreateGuid = CoCreateGuid(&newGuid);
215 
216  // Convert the GUID to string
217  char buffer[37];
218  sprintf_s(buffer, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
219  newGuid.Data1, newGuid.Data2, newGuid.Data3,
220  newGuid.Data4[0], newGuid.Data4[1], newGuid.Data4[2], newGuid.Data4[3],
221  newGuid.Data4[4], newGuid.Data4[5], newGuid.Data4[6], newGuid.Data4[7]);
222  return string(buffer);
223 #endif
224 }
static bool CppSamples::Common::Utility::Init ( )
static

Initialize the SDK.

IVxSystem * Utility::Login ( VxSdk::VxLoginInfo loginInfo)
static

Login to the VideoExpert system.

Definition at line 15 of file Utility.cpp.

15  {
16  // Login to the VideoExpert system and get an instance of it.
17  IVxSystem* system = nullptr;
18  VxSystemLogin(loginInfo, system);
19 
20  // Return the system
21  return system;
22 }
EXTERN_C VXAPI VxResult::Value VxSystemLogin(const VxLoginInfo &loginInfo, IVxSystem *&system)
tm Utility::ParseDateTime ( char *  dateTime)
static

Parse a date time string and return in struct format

Parses a date time string and returns in struct format

Definition at line 158 of file Utility.cpp.

158  {
159  struct tm parseTime;
160  string dateAndTimeVal = string(dateTime);
161 
162  // Here parse date and time string value to time structure
163  stringstream dateStream(dateAndTimeVal);
164  dateStream >> get_time(&parseTime, "%Y-%m-%dT%H:%M:%S");
165 
166  // Create new instance of time structure to avoid unnecessary info.
167  struct tm t;
168  t.tm_mon = parseTime.tm_mon;
169  t.tm_year = parseTime.tm_year;
170  t.tm_mday = parseTime.tm_mday;
171  t.tm_hour = parseTime.tm_hour;
172  t.tm_min = parseTime.tm_min;
173  t.tm_sec = parseTime.tm_sec;
174  return t;
175 }
void Utility::Pause ( )
static

Pause for a user input

Definition at line 280 of file Utility.cpp.

280  {
281 #ifndef WIN32
282  std::cout << "Press any key to continue...";
283  std::cin.clear();
284  fseek(stdin, 0, SEEK_END);
285  fflush(stdin);
286  while (std::cin.get() != '\n');
287 #else
288  system("pause");
289 #endif
290 }
int Utility::ReadInt ( )
static

Read an integer from console input

Definition at line 269 of file Utility.cpp.

269  {
270  string line = "";
271  cin >> line;
272  int value = atoi(line.c_str());
273  while (std::cin.get() != '\n');
274  return value;
275 }
string Utility::ReadString ( )
static

Read a line from console input

Read a string from console input

Definition at line 260 of file Utility.cpp.

260  {
261  string line = "";
262  std::getline(std::cin, line);
263  return line;
264 }
string Utility::Replace ( std::string  original,
std::string  from,
std::string  to 
)
static

Replace a give string

Definition at line 295 of file Utility.cpp.

295  {
296  int index = 0;
297  int len = from.length();
298  while (true) {
299  index = original.find(from);
300  if (index < 0) {
301  break;
302  }
303  original = original.replace(index, len, to);
304  }
305  return original;
306 }
void Utility::ShowProgress ( std::string  statusMsg,
unsigned int  x,
unsigned int  n,
int  w = 50 
)
static

Show progress indicator in console

Parameters
statusMsgMessage to display
xprogress value
nactual size
wwidth of bar

Definition at line 184 of file Utility.cpp.

184  {
185  if ((x != n) && (x % (n / 100 + 1) != 0))
186  return;
187 
188  float ratio = x / static_cast<float>(n);
189  int c = static_cast<int>(ratio * w);
190  int percentToDisplay = static_cast<int>(ratio * 100);
191  // Sometimes, it can go upto 101, 102; so limit it to 100.
192  if (percentToDisplay > 100)
193  percentToDisplay = 100;
194  cout << " " << statusMsg << " " << setfill(' ') << setw(3) << percentToDisplay << "% [";
195  for (int k = 0; k < c; k++) cout << "=";
196  for (int m = c; m < w; m++) cout << " ";
197  cout << "]\r" << flush;
198 }
int Utility::TzOffset ( )
static

Get the time zone offset in seconds

Definition at line 229 of file Utility.cpp.

229  {
230  int offset;
231  tm utcTm{ 0 }, localTm{ 0 };
232  time_t local = time(nullptr), utc;
233 #ifndef WIN32
234  gmtime_r(&local, &utcTm);
235 #else
236  gmtime_s(&utcTm, &local);
237 #endif
238  localtime_s(&localTm, &local);
239  localTm.tm_isdst = 0;
240  local = mktime(&localTm);
241  utc = mktime(&utcTm);
242  offset = static_cast<int>(difftime(local, utc));
243  return offset;
244 }
#define localtime_s(time, result)
#define gmtime_s(time, result)

The documentation for this class was generated from the following files: