From ed095dfb247a42c85ae2470e9fdab6d02d07bc25 Mon Sep 17 00:00:00 2001 From: daleclack Date: Sun, 16 Jul 2023 17:18:27 +0800 Subject: [PATCH] Remove some hard coding --- XeRelease_Gtkmm4/src/xerelease.cc | 21 ++++++---- cfgfile2/cfgfile.cc | 70 +++++++++++++++++++++++++++++++ cfgfile2/cfgfile.hh | 7 ++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 cfgfile2/cfgfile.cc create mode 100644 cfgfile2/cfgfile.hh diff --git a/XeRelease_Gtkmm4/src/xerelease.cc b/XeRelease_Gtkmm4/src/xerelease.cc index 4baf6f7..57bd2ea 100644 --- a/XeRelease_Gtkmm4/src/xerelease.cc +++ b/XeRelease_Gtkmm4/src/xerelease.cc @@ -8,6 +8,10 @@ static json data1; // typedef void(*LP)(struct tm *local);//define a pointer function +#define MAX_PATH 260 +static const int longterm_year = 2019, longterm_month = 1, longterm_day = 11; +static const int stable_year = 2017, stable_month = 5, stable_day = 19; +static const int develop_year = 2017, develop_month = 5, develop_day = 19; int total_day(int year, int month, int day) { @@ -120,14 +124,15 @@ void dale(struct tm *local) void longterm(struct tm *local, const char *lts, char *str) { // Print Version of longterm release in the file - char filename[57]; + char filename[MAX_PATH]; path_translate(filename, lts); // sprintf(filename, "xe-%c.x", lts[0]); int lts_ver = 0; // default release version - int year1 = 2019, month1 = 1, day1 = 11, year2 = local->tm_year + 1900, + int year2 = local->tm_year + 1900, month2 = local->tm_mon + 1, day2 = local->tm_mday; // get release version - lts_ver = total_year_day(year1, year2) - total_day(year1, month1, day1) + + lts_ver = total_year_day(longterm_year, year2) - + total_day(longterm_year, longterm_month, longterm_day) + total_day(year2, month2, day2); // For show in dialog or console snprintf(str, 57, "Xeinit LTS version:%s.%d\n", lts, lts_ver); @@ -147,10 +152,11 @@ void stable(struct tm *local, const char *rel, char *str) path_translate(filename, rel); // sprintf(filename, "xe-%c.x", rel[0]); int devel1; // stable release version - int year1 = 2017, month1 = 5, day1 = 19, year2 = local->tm_year + 1900, + int year2 = local->tm_year + 1900, month2 = local->tm_mon + 1, day2 = local->tm_mday; // get release version - devel1 = total_year_day(year1, year2) - total_day(year1, month1, day1) + + devel1 = total_year_day(stable_year, year2) - + total_day(stable_year, stable_month, stable_day) + total_day(year2, month2, day2); snprintf(str, 57, "Xeinit stable Version:%s.%d\n", rel, devel1); freopen(filename, "a", stdout); @@ -169,10 +175,11 @@ void develop(struct tm *local, const char *devel, char *str) path_translate(filename, devel); // sprintf(filename, "xe-%c.x", devel[0]); int devel1; // development version - int year1 = 2017, month1 = 5, day1 = 19, year2 = local->tm_year + 1900, + int year2 = local->tm_year + 1900, month2 = local->tm_mon + 1, day2 = local->tm_mday; // get release version - devel1 = total_year_day(year1, year2) - total_day(year1, month1, day1) + + devel1 = total_year_day(develop_year, year2) - + total_day(develop_year, develop_month, develop_day) + total_day(year2, month2, day2); snprintf(str, 57, "Xeinit devel Version:%s.%d\n", devel, devel1); freopen(filename, "a", stdout); diff --git a/cfgfile2/cfgfile.cc b/cfgfile2/cfgfile.cc new file mode 100644 index 0000000..49fcb68 --- /dev/null +++ b/cfgfile2/cfgfile.cc @@ -0,0 +1,70 @@ +// A Test to read Config files +// This is a modified version +// The Config File Should be a text file +// and the content as follows +// key=value + +#include "cfgfile.hh" + +static void Trim(std::string &str){ + if(str.empty()){//String is empty + return; + } + + size_t i,start_pos,end_pos; + + for(i=0;i=0;i--){//Get End Position + if(!isspace(str[i])){ + break; + } + } + + end_pos=i; + + str=str.substr(start_pos,end_pos+1); +} + +bool readCfgFile(const char * cfgfilePath,const std::string &key,std::string &value){ + //Open The Config File + std::fstream cfgfile; + cfgfile.open(cfgfilePath); + if(!cfgfile.is_open()){ + std::cout<<"Failed to open the file!"<