Remove some hard coding

This commit is contained in:
daleclack 2023-07-16 17:18:27 +08:00
parent 1663244fe5
commit ed095dfb24
3 changed files with 91 additions and 7 deletions

View File

@ -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);

70
cfgfile2/cfgfile.cc Normal file
View File

@ -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<str.size();i++){//Get Start Position
if(!isspace(str[i])){
break;
}
}
if(i==str.size()){//Only Space and Tabs in string
return;
}
start_pos=i;
for(i=str.size()-1;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!"<<std::endl;
return false;
}
//Read each line of config file and get value
char tmp[1000];
while(!cfgfile.eof()){
cfgfile.getline(tmp,1000);
std::string line(tmp);
std::size_t pos = line.find('=');//Get the position of '='
if(pos == std::string::npos){
continue;
}
std::string tmpkey=line.substr(0,pos);
if(tmpkey == key){
value=line.substr(pos+1);
Trim(value);
cfgfile.close();
return true;
}
}
cfgfile.close();
return false;
}

7
cfgfile2/cfgfile.hh Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <iostream>
#include <fstream>
#include <string>
bool readCfgFile(const char * cfgfilePath,const std::string &key,std::string &value);