⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a_readconf.cpp

📁 一个不错
💻 CPP
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2006  Read parameter values from file printconfig.cfg  Build: make a_readconf*/#include "../src/wyregfile.h"#include "../src/wy_uty.h"// Class for printing parameters.// Use member load_parameter(const char*) to load setting from a regular file//class PrintConfig {    static const char CharRmk='#';        // remark in config file    static const WyCSeg ID_FontPointSize; // identifer, config file    static const WyCSeg ID_FontWeight;    // identifer, config file    static const WyCSeg ID_FontSpacing;   // identifer, config file    static const WyCSeg ID_FontBorder;    // identifer, config file    static const WyCSeg ID_PageLine;      // identifer, config file    static const WyCSeg ChSetBlank;       // character set, blanks    static const WyCSeg ChSetBlankRmk;    // character set, blands+remark    size_t _pointSize;    size_t _fontWeight;    size_t _fontSpacing;    long   _fontBorder;    long   _pageLine;  public:    PrintConfig();    size_t point_size() const { return _pointSize; };    size_t font_weight() const { return _fontWeight; };    size_t font_spacing() const { return _fontSpacing; };    long   font_border() const { return _fontBorder; };    long   page_line() const { return _pageLine; };    WyRet load_parameter(const char*);};const WyCSeg PrintConfig::ID_FontPointSize="FontPointSize";const WyCSeg PrintConfig::ID_FontWeight="FontWeight";const WyCSeg PrintConfig::ID_FontSpacing="FontSpacing";const WyCSeg PrintConfig::ID_FontBorder="FontBorder";const WyCSeg PrintConfig::ID_PageLine="PageLine";const WyCSeg PrintConfig::ChSetBlank(" \n\r\t");const WyCSeg PrintConfig::ChSetBlankRmk(" \n\r\t#");PrintConfig::PrintConfig(){ _pointSize=1;   // The default setting is that _fontWeight=2;  // if these numbers are shown, we know something wrong _fontSpacing=3; _fontBorder=4; _pageLine=5;};// [Syn] Read data (printing parameters) from pathname cfg_pathname//// [Ret] Ok//       ...//WyRet PrintConfig::load_parameter(const char* cfg_pathname)try { WyRegFile cfile(cfg_pathname, O_RDONLY); WyRet r; // loop each line read and try reading the parameters for(;;) {   char rbuf[256]; // read buffer (or using WyStr)   size_t n_rd;    // number of bytes read   if((r=cfile._read_till(rbuf,sizeof(rbuf),n_rd,'\n'))!=Ok) {     WY_RETURN(r);   }   if(n_rd==0) {     break;        // eof   }   WyCSeg cs(rbuf,n_rd);   //-------------------- Find 1st word   size_t idx0_bgn(0);   if(cs.findc_not(&idx0_bgn,idx0_bgn,ChSetBlank)==false) {     continue;   }   if(cs[idx0_bgn]==CharRmk) {     continue;   }   size_t idx0_end;   if(cs.findc(&idx0_end,idx0_bgn+1,ChSetBlankRmk)==false) {     continue;   }   if(cs[idx0_end]==CharRmk) {     continue;   }   //-------------------- Find 2nd word   size_t idx1_bgn;   if(cs.findc_not(&idx1_bgn,idx0_end,ChSetBlank)==false) {     continue;   }   if(cs[idx1_bgn]==CharRmk) {     continue;   }   size_t idx1_end;   if(cs.findc(&idx1_end,idx1_bgn+1,ChSetBlankRmk)==false) {     continue;   }   // So, we identified two tokens by now   const WyCSeg tok0(rbuf+idx0_bgn,idx0_end-idx0_bgn);   const WyCSeg tok1(rbuf+idx1_bgn,idx1_end-idx1_bgn);   //-------------------- Try recognize identifier#1   if(ID_FontPointSize==tok0) {     long value;          if((r=Wy::_strnum(value,NULL,tok1))!=Ok) {       continue;     }     if((value<0)||(value>99)) {       continue;     }     _pointSize=value;     continue;   }   //-------------------- Try recognize identifier#2   if(ID_FontWeight==tok0) {     long value;          if((r=Wy::_strnum(value,NULL,tok1))!=Ok) {       continue;     }     if((value<0)||(value>99)) {       continue;     }     _fontWeight=value;     continue;   }   //-------------------- Try recognize identifier#3   if(ID_FontSpacing==tok0) {     long value;          if((r=Wy::_strnum(value,NULL,tok1))!=Ok) {       continue;     }     if((value<0)||(value>99)) {       continue;     }     _fontSpacing=value;     continue;   }   //-------------------- Try recognize identifier#4   if(ID_FontBorder==tok0) {     long value;          if((r=Wy::_strnum(value,NULL,tok1))!=Ok) {       continue;     }     if((value<-99)||(value>99)) {       continue;     }     _fontBorder=value;     continue;   }   //-------------------- Try recognize identifier#5   if(ID_PageLine==tok0) {     long value;          if((r=Wy::_strnum(value,NULL,tok1))!=Ok) {       continue;     }     if((value<-99)||(value>99)) {       continue;     }     _pageLine=value;     continue;   } } return(Ok);}catch(const WyRet& e) {  Wy::cerr << Wy::wrd(e) << '\n';  throw;};int main(void)try { WyRet r; PrintConfig pcfg; r=pcfg.load_parameter("printconfig.cfg"); if(r!=Ok) {   Wy::cerr << Wy::wrd(r) << '\n';   return(-1); } // Print parameter values loaded in pcfg Wy::cout << "FontPointSize= " << pcfg.point_size() << '\n'; Wy::cout << "FontWeight   = " << pcfg.font_weight() << '\n'; Wy::cout << "FontSpacing  = " << pcfg.font_spacing() << '\n'; Wy::cout << "FontBorder   = " << pcfg.font_border() << '\n'; Wy::cout << "PageLine     = " << pcfg.page_line() << '\n'; return(0);}catch(const WyRet& e) {  Wy::cerr << "main() caught: " << Wy::wrd(e) << '\n';  return(e->c_repcode());}catch(...) {  Wy::cerr << "main() caught: Unknown\n";  throw;};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -