From e24c5e05d6ab923f1d9ee22d0fdafb893d9732b1 Mon Sep 17 00:00:00 2001
From: lurumdare <8020186+lurumdare@users.noreply.github.com>
Date: 周六, 07 9月 2019 22:00:55 +0800
Subject: [PATCH] NULL to nullptr (#34)

---
 src/library/LicenseReader.cpp |   63 ++++++++++++++++---------------
 1 files changed, 33 insertions(+), 30 deletions(-)

diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp
index 2c6a179..9927426 100644
--- a/src/library/LicenseReader.cpp
+++ b/src/library/LicenseReader.cpp
@@ -18,6 +18,7 @@
 #include <fstream>
 #include <sstream>
 #include <stdlib.h>
+#include <math.h>
 #include "pc-identifiers.h"
 #include "LicenseReader.h"
 #include "base/StringUtils.h"
@@ -50,16 +51,16 @@
 EventRegistry FullLicenseInfo::validate(int sw_version) {
 	EventRegistry er;
 	os_initialize();
-	FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(),
+	const FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(),
 			license_signature.c_str());
-	bool sigVerified = sigVer == FUNC_RET_OK;
+	const bool sigVerified = sigVer == FUNC_RET_OK;
 	if (sigVerified) {
 		er.addEvent(LICENSE_VERIFIED, SVRT_INFO);
 	} else {
 		er.addEvent(LICENSE_CORRUPTED, SVRT_ERROR);
 	}
 	if (has_expiry) {
-		time_t now = time(NULL);
+		const time_t now = time(nullptr);
 		if (expires_on() < now) {
 			er.addEvent(PRODUCT_EXPIRED, SVRT_ERROR, "");
 		}
@@ -70,7 +71,7 @@
 	if (has_client_sig) {
 		PcSignature str_code;
 		strncpy(str_code, client_signature.c_str(), sizeof(str_code)-1);
-		EVENT_TYPE event = validate_pc_signature(str_code);
+		const EVENT_TYPE event = validate_pc_signature(str_code);
 		if (event != LICENSE_OK) {
 			er.addEvent(event, SVRT_ERROR);
 		}
@@ -79,7 +80,7 @@
 }
 
 void FullLicenseInfo::toLicenseInfo(LicenseInfo* license) const {
-	if (license != NULL) {
+	if (license != nullptr) {
 		strncpy(license->proprietary_data, extra_data.c_str(),
 		PROPRIETARY_DATA_SIZE);
 
@@ -89,11 +90,11 @@
 			license->expiry_date[0] = '\0';
 			license->days_left = 999999;
 		} else {
-			strncpy(license->expiry_date, to_date.c_str(), 11-1);
-			double secs = difftime(
+			strncpy(license->expiry_date, to_date.c_str(), 11);
+			const double secs = difftime(
 				seconds_from_epoch(to_date.c_str()),
-				time(NULL));
-			license->days_left = (int) secs / 60 * 60 * 24;
+				time(nullptr));
+			license->days_left = round(secs / (60 * 60 * 24));
 		}
 	}
 }
@@ -115,7 +116,7 @@
 	CSimpleIniA ini;
 	for (auto it = diskFiles.begin(); it != diskFiles.end(); it++) {
 		ini.Reset();
-		SI_Error rc = ini.LoadFile((*it).c_str());
+		const SI_Error rc = ini.LoadFile((*it).c_str());
 		if (rc < 0) {
 			result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SVRT_WARN, *it);
 			continue;
@@ -123,7 +124,7 @@
 			loadAtLeastOneFile = true;
 		}
 		const char* productNamePtr = product.c_str();
-		int sectionSize = ini.GetSectionSize(productNamePtr);
+		const int sectionSize = ini.GetSectionSize(productNamePtr);
 		if (sectionSize <= 0) {
 			result.addEvent(PRODUCT_NOT_LICENSED, SVRT_WARN, *it);
 			continue;
@@ -140,14 +141,14 @@
 		 *  application_data = xxxxxxxxx (optional string 16)
 		 */
 		const char * license_signature = ini.GetValue(productNamePtr,
-				"license_signature", NULL);
+				"license_signature", nullptr);
 		long license_version = ini.GetLongValue(productNamePtr,
 				"license_version", -1);
-		if (license_signature != NULL && license_version > 0) {
-			string from_date = trim_copy(
+		if (license_signature != nullptr && license_version > 0) {
+			const string from_date = trim_copy(
 					ini.GetValue(productNamePtr, "from_date",
 							FullLicenseInfo::UNUSED_TIME));
-			string to_date = trim_copy(
+			const string to_date = trim_copy(
 					ini.GetValue(productNamePtr, "to_date",
 							FullLicenseInfo::UNUSED_TIME));
 			string client_signature = trim_copy(
@@ -155,14 +156,16 @@
 			/*client_signature.erase(
 			 std::remove(client_signature.begin(), client_signature.end(), '-'),
 			 client_signature.end());*/
-			int from_sw_version = ini.GetLongValue(productNamePtr,
+			const int from_sw_version = ini.GetLongValue(productNamePtr,
 					"from_sw_version",
 					FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
-			int to_sw_version = ini.GetLongValue(productNamePtr,
+			const int to_sw_version = ini.GetLongValue(productNamePtr,
 					"to_sw_version", FullLicenseInfo::UNUSED_SOFTWARE_VERSION);
+			string extra_data = trim_copy(
+					ini.GetValue(productNamePtr, "extra_data", ""));
 			FullLicenseInfo licInfo(*it, product, license_signature,
 					(int) license_version, from_date, to_date, client_signature,
-					from_sw_version, to_sw_version);
+					from_sw_version, to_sw_version, extra_data);
 			licenseInfoOut.push_back(licInfo);
 			atLeastOneLicenseComplete = true;
 		} else {
@@ -185,11 +188,11 @@
 		EventRegistry& eventRegistry) {
 //bool hasFileLocation = false;
 	bool licenseFoundWithExplicitLocation = false;
-	if (licenseLocation.licenseFileLocation != NULL
+	if (licenseLocation.licenseFileLocation != nullptr
 			&& licenseLocation.licenseFileLocation[0] != '\0') {
 		//hasFileLocation = true;
-		string varName(licenseLocation.licenseFileLocation);
-		vector<string> declared_positions = splitLicensePositions(varName);
+		const string varName(licenseLocation.licenseFileLocation);
+		const vector<string> declared_positions = splitLicensePositions(varName);
 		vector<string> existing_pos = filterExistingFiles(declared_positions);
 		if (existing_pos.size() > 0) {
 			if (existing_pos.size() > 0) {
@@ -210,14 +213,14 @@
 bool LicenseReader::findFileWithEnvironmentVariable(vector<string>& diskFiles,
 		EventRegistry& eventRegistry) {
 	bool licenseFileFoundWithEnvVariable = false;
-	if (licenseLocation.environmentVariableName != NULL
+	if (licenseLocation.environmentVariableName != nullptr
 			&& licenseLocation.environmentVariableName[0] != '\0') {
-		string varName(licenseLocation.environmentVariableName);
+		const string varName(licenseLocation.environmentVariableName);
 		if (varName.length() > 0) {
 			//var name is passed in by the calling application.
 			char* env_var_value = getenv(varName.c_str());
-			if (env_var_value != NULL && env_var_value[0] != '\0') {
-				vector<string> declared_positions = splitLicensePositions(
+			if (env_var_value != nullptr && env_var_value[0] != '\0') {
+				const vector<string> declared_positions = splitLicensePositions(
 						string(env_var_value));
 				vector<string> existing_pos = filterExistingFiles(
 						declared_positions);
@@ -246,14 +249,14 @@
 
 EventRegistry LicenseReader::getLicenseDiskFiles(vector<string>& diskFiles) {
 	EventRegistry eventRegistry;
-	bool licenseFoundWithExplicitLocation = findLicenseWithExplicitLocation(
+	const bool licenseFoundWithExplicitLocation = findLicenseWithExplicitLocation(
 			diskFiles, eventRegistry);
 	bool foundNearModule = false;
 	if (licenseLocation.openFileNearModule) {
 		char fname[MAX_PATH] = { 0 };
-		FUNCTION_RETURN fret = getModuleName(fname);
+		const FUNCTION_RETURN fret = getModuleName(fname);
 		if (fret == FUNC_RET_OK) {
-			string temptativeLicense = string(fname) + ".lic";
+			const string temptativeLicense = string(fname) + ".lic";
 			ifstream f(temptativeLicense.c_str());
 			if (f.good()) {
 				foundNearModule = true;
@@ -269,7 +272,7 @@
 			LOG_WARN("Error determining module name.");
 		}
 	}
-	bool licenseFileFoundWithEnvVariable = findFileWithEnvironmentVariable(
+	const bool licenseFileFoundWithEnvVariable = findFileWithEnvironmentVariable(
 			diskFiles, eventRegistry);
 
 	if (!foundNearModule && !licenseFoundWithExplicitLocation
@@ -336,7 +339,7 @@
 void FullLicenseInfo::printAsIni(ostream & a_ostream) const {
 	CSimpleIniA ini;
 	string result;
-	string product = toupper_copy(trim_copy(this->product));
+	const string product = toupper_copy(trim_copy(this->product));
 	CSimpleIniA::StreamWriter sw(a_ostream);
 	ini.SetLongValue(product.c_str(), "license_version",
 	PROJECT_INT_VERSION);

--
Gitblit v1.9.1