PGSLIC.lib  3.0
RomanNumeralsCmd.cpp
/*
* RomanNumeralsCmd.cpp
*
* A simple application that prints out a decimal argument as a Roman numeral.
* Provided here to demonstrate the use of the PGSLIC API. The RomanNumerals
* license includes an application specific setting MaxLimit which is
* configured by the license administrator in Salesforce.com.
*
* Checks for a valid license, checks the MaxLimit value has not been exceeded
* and prints out a number in Roman. The user is warned if the license will
* expire soon.
*
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <numeric>
#include <stdexcept>
#include <stdlib.h>
#include "../../include/example_utils.h"
#include "../../include/pgslic.h"
using namespace std;
using namespace nuvovis;
namespace
{
const char* PWD = "---Replace with your Developer Key---";
const char* LICS_DIR = "."; // Store license file in current working directory.
const string CONFIG_FILENAME = "RomanNumerals.ini"; // The current working directory is where the configuration file is stored.
const string APP_NAME = "RomanNumerals";
bool licenseOK()
{
switch (checkLicense(LICS_DIR, PWD, readTextFileUntilSpace(CONFIG_FILENAME).c_str(), APP_NAME.c_str()))
{
break;
cerr << "License error - " << getLastError() << endl;
return false;
cerr << "License expired on " << getExpiryDate() << endl;
return false;
{
int numSecondsStale = getStaleTime();
const int NUM_SECONDS_IN_48_HOURS = 60 * 60 * 48;
if (numSecondsStale < NUM_SECONDS_IN_48_HOURS)
{
cout << "Warning: License has been stale for " << numSecondsStale << " seconds" << endl;
}
else
{
cerr << "License has been stale for more than two days" << endl;
return false;
}
}
break; // We don't mind if it's only been stale for less than 48 hours
default:
throw runtime_error("Unexpected license status"); // Never happens
}
int daysUntilExpiry = getDaysUntilExpiry();
if (daysUntilExpiry <= 10 && daysUntilExpiry > 0)
cout << "Warning: Only " << daysUntilExpiry << " day(s) until license expires! Contact sales@myplace.com" << endl;
return true;
}
template<class T> T parseStr(const string& str)
{
stringstream ss(str);
T x;
ss >> x;
if (!ss)
throw runtime_error(string(string("Failed to convert ") + str).c_str());
return x;
}
typedef pair<int,string> NumeralPair;
NumeralPair RN(NumeralPair number, const NumeralPair& value)
{
while (number.first >= value.first)
{
number.second += value.second;
number.first -= value.first;
}
return number;
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2 && argc != 3)
{
cout <<
"Usage: RomanNumeralsCmd Number\n"
" Outputs the decimal number as Roman numerals\n"
"\n"
" RomanNumeralsCmd -info\n"
" View application version and license settings\n";
return 1;
}
else if (!licenseOK())
{
cout << "License activation required" << endl;
return 1;
}
else if (string(argv[1]) == string("-info"))
{
cout << getAppName() << endl;
cout << "Licensed to " << getClientName() << endl;
if (isPerpetual())
{
cout << "License never expires" << endl;
}
else
{
cout << "License expires on " << getExpiryDate() << endl;
}
cout << "Maximum number = " << getSetting("MaxLimit") << endl;
cout << "All settings = " << getSettings() << endl;
}
else
{
int number = parseStr<int>(argv[1]);
int maxLimit = parseStr<int>(getSetting("MaxLimit"));
if (number > maxLimit)
throw runtime_error(string(string("You are only licensed for numbers up to ") + getSetting("MaxLimit")).c_str());
// Output the decimal argument as Roman numerals
vector<NumeralPair> numeralPairs;
numeralPairs.push_back(NumeralPair(1000, "M"));
numeralPairs.push_back(NumeralPair(500, "D"));
numeralPairs.push_back(NumeralPair(100, "C"));
numeralPairs.push_back(NumeralPair(90, "XC"));
numeralPairs.push_back(NumeralPair(50, "L"));
numeralPairs.push_back(NumeralPair(40, "XL"));
numeralPairs.push_back(NumeralPair(10, "X"));
numeralPairs.push_back(NumeralPair(9, "IX"));
numeralPairs.push_back(NumeralPair(5, "V"));
numeralPairs.push_back(NumeralPair(4, "IV"));
numeralPairs.push_back(NumeralPair(1, "I"));
NumeralPair np = accumulate(numeralPairs.begin(), numeralPairs.end(), NumeralPair(number, ""), RN);
cout << np.second << endl;
}
}
catch (exception& e)
{
cout << "*** Error: " << e.what() << endl;
return 1;
}
return 0;
}
PGSLIC_API const char * getSetting(const char *settingName)
PGSLIC_API int isPerpetual()
PGSLIC_API const char * getExpiryDate()
PGSLIC_API const char * getAppName()
PGSLIC_API int getDaysUntilExpiry()
@ LICENSE_STALE
Definition: pgslic.h:96
@ LICENSE_EXPIRED
Definition: pgslic.h:96
@ LICENSE_OK
Definition: pgslic.h:96
@ LICENSE_ERROR
Definition: pgslic.h:96
PGSLIC_API const char * getClientName()
PGSLIC_API const char * getSettings()
PGSLIC_API int getStaleTime()
PGSLIC_API const char * getLastError()
PGSLIC_API LicenseStatus checkLicense(const char *licPath, const char *pwd, const char *key, const char *appName, const char *appInfo="", const char *cxnOpts="")