伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

全套c++读取LINUX的配置文件源码

来源:本站原创 浏览:115次 时间:2022-09-11
全套c++读取LINUX的配置文件源码

祖仙教小凡仙 海鲨数据库架构师

/*

  • File: main.cpp
  • Author: zengfankun@小凡仙
  • Created on 2019年12月4日, 下午4:11
    */

#include <cstdlib>
#include <iostream>
#include <string>
#include <occi.h>
#include <vector>
#include <fstream>

using namespace std;
using namespace oracle::occi;

struct stc_dbconf //定义配置文件结构体
{
string dbname;
string classA;
string classB;
string classC;
string User;
string Password;
string Connect;
};

struct stc_dbFilter //定义过滤条件结构体{    string m_s_classA;    string m_s_classB;    string m_s_classC;    string m_s_dbname;          };

struct stc_sqlconf //定义SQL配置结构体
{
string sqlid;
string sqltype;
string sqltext;
};

void Load_DbConf(vector<stc_dbconf> &dblist);//装入DB配置文件
void Load_SqlConf(vector<stc_sqlconf> &sqlList);//装入SQL配置文件
//对配置列表过滤
void Filter_DbList(vector<stc_dbconf> &Filter_dblist,stc_dbFilter stc_Filter);
void Filter_SqlList(vector<stc_sqlconf> &sqlList,string psql_id);//对SQL列表过滤
//连接数据库并执行SQL
void ConnDb_ExecQuery(vector<stc_dbconf> &dblist,vector<stc_sqlconf> &sqlList);

int main(int argc, char** argv) //sqlid classA classB classC dbname
{ //参数列表 不输入用“NULLL" 占位
vector <stc_dbconf> dbconf_List; //DB配置文件列表
vector <stc_dbconf> Filted_dblist; //过滤后的DB配置文件列表
vector<stc_sqlconf> sqlList; //SQL配置文件列表
stc_dbFilter lstc_dbFilter; //过滤条件结构体变量

string m_s_sqlid;                  //SQLID变量string m_s_argv;cout<<"Your input Parameter:"<<endl;cout<<"SQL_ID"<<"   "<<"CLASSA"<<"   "<<"CLASSB"<<"   "<<"CLASSC"<<"   "<<"DBNAME"<<endl;cout<<"---------------------------------------------------------"<<endl;for (int i=1;i<argc; i++)    //对输入参数进行处理{          cout<<argv[i]<<"   ";} cout<<endl;cout<<endl;cout<<endl;if(argc<=0){ cout<<"You not any innput parameter"<<endl;}if (argc>=2)                       //参数必须大于等于2个{    m_s_sqlid =argv[1];           //参数赋值给变量    m_s_argv=argv[2];    if(m_s_argv=="NULL")    {        lstc_dbFilter.m_s_classA.clear(); //如果参数使用NULL占位,那么过滤变量置空    }    else    {            lstc_dbFilter.m_s_classA=argv[2];    }    m_s_argv=argv[3];    if(m_s_argv=="NULL")    {        lstc_dbFilter.m_s_classB.clear();    }        else    {      lstc_dbFilter.m_s_classB=argv[3];    }    m_s_argv=argv[4];    if(m_s_argv=="NULL")    {        lstc_dbFilter.m_s_classC.clear();    }    else    {           lstc_dbFilter.m_s_classC=argv[4];    }     m_s_argv=argv[5];    if(m_s_argv=="NULL")    {        lstc_dbFilter.m_s_dbname.clear();    }     else    {           lstc_dbFilter.m_s_dbname=argv[5];    }    m_s_argv="";}else { cout<<"You must innput two parameter"<<endl;}Load_DbConf(dbconf_List); //装入配置文件,把列表地址传进去添加数据if (dbconf_List.size()<=0){    cout<<"Not Found Any DB Info!"<<endl;    exit(1);}

// for (int i=0;i<dbconf_List.size();i++) //把配置文件打印出来观察,正式屏蔽
// { //尽在测试使用
// cout<<"Dbname: "<<dbconf_List[i].dbname<<endl;
// cout<<"ClassA: "<<dbconf_List[i].classA<<endl;
// cout<<"ClassB: "<<dbconf_List[i].classB<<endl;
// cout<<"ClassC: "<<dbconf_List[i].classC<<endl;
// cout<<"User: "<<dbconf_List[i].User<<endl;
// cout<<"Password: "<<dbconf_List[i].Password<<endl;
// cout<<"Connect: "<<dbconf_List[i].Connect<<endl;
// cout<<endl;
// cout<<endl;
// }

Filted_dblist=dbconf_List;            //保存配置文件列表,赋值给过滤后配置列表Filter_DbList(Filted_dblist,lstc_dbFilter); //把过滤后配置列表和过滤变量传给函数处理if(Filted_dblist.size()>0){         cout<<"Filted dbconf List:" <<endl;     cout<<"DBNAME"<<"   "<<"CLASSA"<<"   "<<"CLASSB"<<"   "     <<"CLASSC"<<"   "<<"USER"<<"   "<<"CONN"<<"   "<<endl;     cout<<"---------------------------------------------------------"<<endl;    for (int j=0; j<Filted_dblist.size();j++) //显示过滤后的DB列表    {       cout<<Filted_dblist[j].dbname\       <<"   "<<Filted_dblist[j].classA\       <<"   "<<Filted_dblist[j].classB\       <<"   "<<Filted_dblist[j].classC\       <<"   "<<Filted_dblist[j].User\       <<"   "<<Filted_dblist[j].Connect<<endl;       cout<<endl;       cout<<endl;       }  }else{    cout<<"Filed After  haven`t Found Any DB Info!"<<endl;    exit(1);} Load_SqlConf(sqlList);               //装入SQL配置文件到列表中 if(sqlList.size()<=0) {    cout<<"Not Font Any SQL Info!"<<endl;    exit(1);          }

// for (int K=0; K<sqlList.size();K++) //显示装入后的SQL列表,其实没有必要装入那么多SQL
// {
// cout<<"DISPLAY SQLconf List:" <<endl;
// cout<<"SQLID: "<<sqlList[K].sqlid<<endl;
// cout<<"SQLTYPE: "<<sqlList[K].sqltype<<endl;
// cout<<"SQLTEXT: "<<sqlList[K].sqltext<<endl;
// cout<<endl;
// cout<<endl;
// }

 Filter_SqlList(sqlList,m_s_sqlid); //这里把没有必要的SQL过滤掉 if (sqlList.size()<=0) {     cout<<"This Sql Filter to Zeo"<<endl;     exit(1); } for (int z=0;z<sqlList.size();z++) //显示过滤后的SQL {   cout<<"Filted SQLconf List:" <<endl;   cout<<"SQLID:   "<<sqlList[z].sqlid<<endl;   cout<<"SQLTYPE:   "<<sqlList[z].sqltype<<endl;   cout<<"SQLTEXT:   "<<sqlList[z].sqltext<<endl;   cout<<endl;   cout<<endl;             }  ConnDb_ExecQuery(Filted_dblist,sqlList);//正式去连数据库,并且执行指定的SQL

return 0;
}

void Load_DbConf(vector<stc_dbconf> &dblist)
{

char linebuf[1024];                //读入文件1行的字符数组int position=0;                    //确定等号的位置int linebuf_size=0;                //实际读入字符串大小stc_dbconf lv_dbconf;             //定义一个本地配置信息结构体变量string key;                       //定义KEY变量string value;                     //定义VALUE变量string tmp_line;                  //定义行的字符串变量,方便操作 ifstream db_config_file;         //文件操作的变量  db_config_file.open("db.conf",ios::in);//只读模式并在程序当前目录下打开文件  if(db_config_file==NULL)               //判断文件是否打开成功  {       cout<<"Open db.conf file failed"<<endl;       exit(1);  }  while(!db_config_file.eof())    //读取每一行,在文件结束前  {        db_config_file.getline(linebuf,1024); //读入一行,放入1024个字符数组中                                              //判断几个特殊字符,来区分DB信息块        if((linebuf[0]=='#')||(linebuf[0]=='\0')||(linebuf[0]=='\r')||(linebuf[0]=='\n'))        {              if (lv_dbconf.Connect.size()>0) //假如遇到换行,空行 注解               {                               //并且结构体变量有东西,则把它放进数组中                  dblist.push_back(lv_dbconf);                  lv_dbconf.Connect.clear();                  lv_dbconf.Password.clear();                  lv_dbconf.User.clear();                  lv_dbconf.classA.clear();                  lv_dbconf.classB.clear();                  lv_dbconf.classC.clear();                  lv_dbconf.dbname.clear();              }                           continue;                         //结束本次循环,继续读行数据        }        tmp_line=linebuf;                 //把字符数组赋值给行字符串变量        linebuf_size=tmp_line.size();      //获得真实行大小        position = 0;        for ( int i=0;i<linebuf_size;i++)   //获取等号的位置           {          if(tmp_line[i]=='=')              {            position = i;            break;          }           }       key.clear();       key.append(tmp_line,0,position); //获取等号前的KEY       value.clear();       if(key=="DBNAME")         {                        //获取KEY对应的VALUE值              value.append(tmp_line,position+1,linebuf_size-7);                lv_dbconf.dbname=value; //把VALUE值赋值给结构体变量              continue;           }                      if(key== "CLASSA")                               {              value.append(tmp_line,position+1,linebuf_size-6);                lv_dbconf.classA=value;              continue;                               }                                         if(key== "CLASSB")                               {              value.append(tmp_line,position+1,linebuf_size-6);               lv_dbconf.classB=value;              continue;                               }                                         if(key== "CLASSC")                               {              value.append(tmp_line,position+1,linebuf_size-6);               lv_dbconf.classC=value;              continue;                               }                                          if(key== "USER")                               {              value.append(tmp_line,position+1,linebuf_size-5);              lv_dbconf.User=value;              continue;                               }                                         if(key== "PASSWD")                               {              value.append(tmp_line,position+1,linebuf_size-7);              lv_dbconf.Password=value;              continue;                               }                                          if(key== "CONN")                               {              value.append(tmp_line,position+1,linebuf_size-5);               lv_dbconf.Connect=value;              continue;                               }                               }     if (lv_dbconf.Connect.size()>0) //读完文件后,结构体变量还有东西,放入数组中      {          dblist.push_back(lv_dbconf);          lv_dbconf.Connect.clear();          lv_dbconf.Password.clear();          lv_dbconf.User.clear();          lv_dbconf.classA.clear();          lv_dbconf.classB.clear();          lv_dbconf.classC.clear();          lv_dbconf.dbname.clear();      }  db_config_file.close(); //关闭文件//最后数组返回给了MAIN,因为通过地址传,非值传。

}

void Filter_DbList(vector <stc_dbconf> &Filter_dblist,stc_dbFilter stc_Filter)
{
vector<stc_dbconf> tmp_dblist; //临时DB配置列表
vector<stc_dbconf>::iterator iter; //DB配置列表的矢代器,说白了是指针。
tmp_dblist=Filter_dblist; //把DB配置文件赋给临时变量

if(!stc_Filter.m_s_classA.empty()) //非空继续,主函数对过滤条件如果NULL就置空{    for (iter=tmp_dblist.begin();iter!=tmp_dblist.end();) //循环遍历DB列表    {        if((*iter).classA !=stc_Filter.m_s_classA) //如果DB列表A类与过滤条件A不等        {                          tmp_dblist.erase(iter);               //则删除他        }        else         {            ++iter;                 //单独拿出来,是因为erase操作自动指向下一个元素        }    }}if(!stc_Filter.m_s_classB.empty()){  for (iter=tmp_dblist.begin();iter!=tmp_dblist.end();)    {        if((*iter).classB!=stc_Filter.m_s_classB)        {          tmp_dblist.erase(iter);        }         else         {            ++iter;        }    }}if(!stc_Filter.m_s_classC.empty()){    for (iter=tmp_dblist.begin();iter!=tmp_dblist.end();)    {        if((*iter).classC!=stc_Filter.m_s_classC)        {                        tmp_dblist.erase(iter);        }         else         {            ++iter;        }    }}   if(!stc_Filter.m_s_dbname.empty()){    for (iter=tmp_dblist.begin();iter!=tmp_dblist.end();)    {        if((*iter).dbname!=stc_Filter.m_s_dbname)        {           tmp_dblist.erase(iter);        }         else         {            ++iter;        }                }}  Filter_dblist=tmp_dblist; //最后把删除信息的列表,再赋值回。传给MAIN函数。

}

void Load_SqlConf(vector<stc_sqlconf> &sqlList) //装入SQL配置文件
{

char linebuf[1024];int position=0;int linebuf_size=0;stc_sqlconf lstc_sqlconf;string key;string value;string tmp_line;ifstream sqlconf_file;  sqlconf_file.open("sql.conf",ios::in);   //与装入DB配置文件差不多  if(sqlconf_file==NULL)  {       cout<<"Open sql.conf file failed"<<endl;       exit(1);  }   while(!sqlconf_file.eof())      {        sqlconf_file.getline(linebuf,1024);        if((linebuf[0]=='#')||(linebuf[0]=='\r')||(linebuf[0]=='\n'))//这里判断第一个字符,并且取消了\0空格        {              if (lstc_sqlconf.sqlid.size()>0)              {                  sqlList.push_back(lstc_sqlconf); //压入数组并清空结构体变量值                  lstc_sqlconf.sqlid.clear();                  lstc_sqlconf.sqltype.clear();                  lstc_sqlconf.sqltext.clear();              }                                    continue;            }        tmp_line=linebuf;        linebuf_size=tmp_line.size();        position = 0;        if(tmp_line.size()>0)        {                                for ( int i=0;i<linebuf_size;i++)             {              if(tmp_line[i]=='=')    //找到等号位置              {                position = i;                break;              }               }            if (position==0) //如果发现该行没有等号,意味着是SQL分成了多行            {               if(lstc_sqlconf.sqltext.size()>0)  //判断SQL文本变量是否有值,有意味着现在读取的是第二行               {                   lstc_sqlconf.sqltext.append("  "); //使用APPEN向后添加个空格,防止行与行之间没有空格                   lstc_sqlconf.sqltext.append(tmp_line);//把SQL剩余的行添加进去                   tmp_line.clear();               }               continue;            }           }       if(tmp_line.size()<=0)        {              if (lstc_sqlconf.sqlid.size()>0)              {                  sqlList.push_back(lstc_sqlconf); //压入数组并清空结构体变量值                  lstc_sqlconf.sqlid.clear();                  lstc_sqlconf.sqltype.clear();                  lstc_sqlconf.sqltext.clear();              }                          continue;       }//\0 配置文件有空行,则放弃读取处理       key.clear();       key.append(tmp_line,0,position);  //读取KEY                if(key=="SQLID")   //KEY分类处理         {               value.clear();                 //读取VALUE              value.append(tmp_line,position+1,linebuf_size-6);                lstc_sqlconf.sqlid=value;              continue;           }       if(key=="SQLTYPE")         {              value.clear();              value.append(tmp_line,position+1,linebuf_size-8);                lstc_sqlconf.sqltype=value;              continue;           }        if(key=="SQLTEXT") //SQL的第一行在这里读取         {              value.clear();              value.append(tmp_line,position+1,linebuf_size-8);                lstc_sqlconf.sqltext=value;                                       continue;           }               }  if (lstc_sqlconf.sqlid.size()>0) //把最后一个配置压入数组  {      sqlList.push_back(lstc_sqlconf);      lstc_sqlconf.sqlid.clear();      lstc_sqlconf.sqltype.clear();      lstc_sqlconf.sqltext.clear();  }   sqlconf_file.close();  //关闭SQL配置文件

}

void Filter_SqlList(vector<stc_sqlconf> &sqlList,string sql_id) //筛选SQL
{
vector<stc_sqlconf>::iterator iter;
if(!sql_id.empty())
{
for (iter=sqlList.begin();iter!=sqlList.end();)
{
if((*iter).sqlid !=sql_id)
{
sqlList.erase(iter);
}
else
{
++iter;
}
}
}
else
{
cout<<"Error:sqlid is not input"<<endl;
exit(1);
}
}

void ConnDb_ExecQuery(vector<stc_dbconf> &dblist,vector<stc_sqlconf> &sqlList) //连接数据库并执行SQL
{

Environment *env=NULL;Connection  *conn=NULL;Statement   *stmt=NULL;ResultSet   *rset=NULL;     //OCCI接口的环境对象,连接对象,语句对象,结果集对象if(dblist.size()<=0)    //对传入参数判断{    cout<<"Have not dbinfo"<<endl;    exit(1);}    if(sqlList.size()<=0){    cout<<"Have not Sql"<<endl;    exit(1);}    env = Environment::createEnvironment(Environment::DEFAULT); //环境对象初始化for (int i=0;i<dblist.size();i++)              //循环对多个DB{        try    {   conn =env->createConnection(dblist[i].User,dblist[i].Password,dblist[i].Connect);//创建连接对象        if(conn!=NULL)               cout<<dblist[i].dbname<<" Connect Success"<<endl;        else            cout<<dblist[i].dbname<<" Connect Failed!"<<endl;        stmt=conn->createStatement(sqlList[0].sqltext); //创建语句对象       if(sqlList[0].sqltype=="QUERY")       {            cout<<"Executing ..."<<endl;             rset=stmt->executeQuery();// this is fix select                   vector<MetaData>QueryColumnList=rset->getColumnListMetaData();            unsigned int col_size=QueryColumnList.size();            for (int x=0;x<col_size;x++)            {                cout<<QueryColumnList[x].getString(MetaData::ATTR_NAME).c_str()<<"   ";            }            cout<<endl;            cout<<"--------------------------------------------------------------"<<endl;

// vector<MetaData>::iterator iter;
// if(!QueryColumnList.empty())
// {
// for (iter=QueryColumnList.begin();iter!=QueryColumnList.end();iter++)
// {
// string ColName=iter->getString(MetaData::ATTR_NAME);
// cout<<ColName<<":"<<endl;
// }
//
// }
// }
//display line value
while(rset->next())
{
for (int col = 0; col < col_size; col++)
{
string reStr=rset->getString(col+1).empty() ? " ":rset->getString(col+1).c_str();
cout<<reStr<<" ";
}
cout<<endl;
}

       }       if(sqlList[0].sqltype=="DML")       {          cout<<"Executing ..."<<endl;           stmt->setAutoCommit(true);         int ResulRow=stmt->executeUpdate();         cout<<" row number:"<<ResulRow<<endl;       }      if(sqlList[0].sqltype=="DDL")      {         cout<<"Executing ..."<<endl;            stmt->executeUpdate();                      cout<<"Exec DDL Succes!"<<endl;      }      if(sqlList[0].sqltype=="ANONY")      {         cout<<"Executing ..."<<endl;           stmt->execute();         cout<<"Exec ANONYMOUS Succes!"<<endl;      }                             //正常情况下释放对象 必须TRY挂号里,        if (rset !=NULL)   //处理查询语句的结果集对象释放        {            stmt->closeResultSet(rset);        }        conn->terminateStatement(stmt);         env->terminateConnection(conn);    }    catch(SQLException e) //SQL异常处理 C++ catch后会执行后面的正常代码 而不是直接退出。其他DB还要执行不能EXIT(1)    {        cout<<e.what()<<endl;              //打印异常信息后关闭所有对象         if (rset !=NULL)  //执行非查询语句时,结果集对象是没有的。        {            stmt->closeResultSet(rset);        }            conn->terminateStatement(stmt);            env->terminateConnection(conn);    }     //C++ 没有FINALLY  }         Environment::terminateEnvironment(env); //环境对象放在循环后

}

下面是数据库连接信息配置文件:

#KEY必须大写,遵循实列的KEY,不能多也不能少一个字符.VALUE无所谓DBNAME=sharkdbCLASSA=TESTCLASSB=PLPMCLASSC=JNUSER=scottPASSWD=123456CONN=192.168.2.21:1521/SHARKDB#2232DBNAME=sharkdbCLASSA=DEVCLASSB=PLPMCLASSC=JNUSER=scottPASSWD=123456CONN=192.168.2.22:1521/SHARKDBDBNAME=sharkdbCLASSA=PRODUCCLASSB=PLPMCLASSC=JNUSER=scottPASSWD=123456CONN=192.168.2.23:1521/SHARKDB#HAI WAIDBNAME=sharkdbCLASSA=TESTCLASSB=PLPMCLASSC=HWUSER=scottPASSWD=123456CONN=192.168.2.24:1521/SHARKDBDBNAME=sharkdbCLASSA=DEVCLASSB=PLPMCLASSC=HWUSER=scottPASSWD=123456CONN=192.168.2.25:1521/SHARKDBDBNAME=sharkdbCLASSA=PRODUCCLASSB=PLPMCLASSC=HWUSER=scottPASSWD=123456CONN=192.168.2.26:1521/SHARKDB

下面是SQL语句配置文件:

# sqlid MUST ONLY# 这里KEY必须大写,其中TYPE的VALUE 必须大写 类型选择QUERY DML DDL # TEXT 不区分大小写,一个SQL可以多行,注意每行必须完整的语句,不能 from user_\   第二行接 objects# SQL 后面不可有分号 ;SQLID=9876523SQLTYPE=QUERYSQLTEXT=SELECT SYSDATE  FROM DUAL# select * from user_objectsSQLID=8766686SQLTYPE=QUERYSQLTEXT= SELECT OBJECT_NAME,OBJECT_TYPE,CREATED,LAST_DDL_TIME from user_objectsSQLID=123456SQLTYPE=QUERYSQLTEXT=SELECT * FROM EMPSQLID=123457SQLTYPE=QUERYSQLTEXT= SELECT                  EMPNO,                 ENAME,                 JOB,                 SAL         FROM EMPSQLID=123458SQLTYPE=DMLSQLTEXT= INSERT INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)          VALUES('7935','SHARK','DBA','7782',TO_DATE('1982-01-23 00:00:00','YYYY-MM-DD HH24:MI:SS'),15000,79.78,'10')SQLID=123459SQLTYPE=DMLSQLTEXT=UPDATE EMP SET SAL=SAL+1000 WHERE ENAME='TURNER'SQLID=123450SQLTYPE=DDLSQLTEXT=CREATE TABLE MY_OBJECT AS SELECT * FROM USER_OBJECTSSQLID=123451SQLTYPE=DDLSQLTEXT=ALTER TABLE MY_OBJECT ADD IS_DELETE NUMBER(1) DEFAULT 0SQLID=123452SQLTYPE=DDLSQLTEXT=CREATE INDEX IX_MYOBJECT_DDLTIME ON MY_OBJECT(LAST_DDL_TIME)SQLID=123453SQLTYPE=ANONYSQLTEXT=begin DBMS_STATS.GATHER_TABLE_STATS('SCOTT','MY_OBJECT',DEGREE=>1,CASCADE=>TRUE); end;

目前测试的是ORACLE 11.2.0.4版本 11.9.0不支持。另外没测试过AS SYSDBA角色登录。

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net