/*
群活动统计程序
用法:
ddu [-m <member> [-dn]] [-a <active>] [-n <increment>]
ddu /?
说明:
ddu 程序名,含意是daydayup
member 群成员名(简记符)
active 活动(简记符)
increment 活动数量,默认为+1
/? 帮助
关键字:
控制台程序编写之参数处理;
ini文件操作 api;
ver1.0
2010年8月8日
pz
疑问:
stru_param_tag 位域定义时,最先定义的位 应该为msb,但这里却是lsb.
*/
/*
Ver1.0 不支持member、active的简写,不需要daydayup.ini文件。
示例:
ddu -m pz -a Y -n 2
成员pz的Yellow值 +2。
若pz不存在,则建立之。
ddu -m pz
查询成员pz的活动统计情况
ddu
查询所有成员的活动统计情况
ddu -m pz -dn
删除成员pz
*/
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define DAT_FILE_PATH ".\\group.ini"
#define CFG_FILE_PATH ".\\daydayup.ini"
#define SEC_MEM_ID0 1 /* 第一个成员段的section index */
#define GetIniStr(section, key, _buf) ::GetPrivateProfileString(section, key, "0", _buf, 256, DAT_FILE_PATH)
#define SetIniStr(section, key, _buf) ::WritePrivateProfileString(section, key, _buf, DAT_FILE_PATH)
#define GetIniInt(section, key) ::GetPrivateProfileInt(section, key, 0, DAT_FILE_PATH)
void SetIniInt(const char *section, const char *key, int val)
{
char buf[10];
itoa(val, buf, 10);
::WritePrivateProfileString(section, key, buf, DAT_FILE_PATH);
}
typedef unsigned char uchar;
typedef unsigned short word;
typedef unsigned int dword;
#pragma pack(1)
struct stru_param_tag{
word sw_member:1;
word val_member:1;
word sw_delmember:1;
// word :0; // bug ?
word sw_active:1;
word val_active:1;
word sw_increment:1;
word val_increment:1;
}stru_param;
#pragma pack()
typedef enum en_command_tag{
eCMD_REPORT_ALL = 0,
eCMD_REPORT_MEM = 0x0003,
eCMD_STATISTIC_DEF = 0x001b,
eCMD_STATISTIC = 0x007b,
eCMD_DELETE_NAME = 0x0007,
}E_COMMAND;
// params
string sMember;
string sActive;
int iIncrement;
// ini file
int iSections;
int iKeys;
char cSectionNames[1024];
vector<string> vSectionNames;
char cSection[1024];
vector<string> vSection;
int iKeyVal;
int Distribute(char cSN[], int size, vector<string> &vSN);
int main(int argc, char *argv[])
{
int i, j;
E_COMMAND eCmd;
char buf[256];
string sTmp;
int iMembers;
int iAttributes;
string sSection;
string sKey;
string sKeyValue;
// 参数分析
for(i=1; i<argc; i++)
{
sTmp = argv[i];
// 独立的命令开关
if( "-dn" == sTmp)
{
stru_param.sw_delmember = 1;
}
else// 需要参数的命令开关
{
if(argc-1 == i)
{
cout<<sTmp<<" 缺少参数!"<<endl;
system("pause>nul");
return -1;
}
if("-m" == sTmp)
{
stru_param.sw_member = 1;
stru_param.val_member = 1;
sMember = argv[++i];
// cout<<sMember<<endl;
//cout<<"cmd:"<<*(word*)&stru_param<<endl;
}
else if("-a" == sTmp)
{
stru_param.sw_active = 1;
stru_param.val_active = 1;
sActive = argv[++i];
// cout<<sActive<<endl;
//cout<<"cmd:"<<*(word*)&stru_param<<endl;
}
else if("-n" == sTmp)
{
stru_param.sw_increment = 1;
stru_param.val_increment = 1;
iIncrement = atoi(argv[++i]);
// cout<<iIncrement<<endl;
}
}
}// end for
eCmd = *(E_COMMAND*)&stru_param; // cast
//cout<<"cmd:"<<*(word*)&stru_param<<endl;
// 取得所有的小节名
::GetPrivateProfileSectionNames(cSectionNames, 1024, DAT_FILE_PATH);
iSections = Distribute(cSectionNames, 1024, vSectionNames);
//cout<<"iSections:"<<iSections<<endl;
switch(eCmd)
{
case eCMD_REPORT_ALL:
cout<<"eCMD_REPORT_ALL"<<endl;
iMembers = GetIniInt("Global", "members");
iAttributes = GetIniInt("Global", "attributes");
if(iSections != iMembers+SEC_MEM_ID0)
{
cout<<"ini文件内容有误:成员数_总段数 = "<<iMembers<<"_"<<iSections <<endl;
}
for(i=0; i<iMembers; i++)
{
::GetPrivateProfileSection(vSectionNames[SEC_MEM_ID0+i].c_str(), cSection, 1024, DAT_FILE_PATH);
iKeys = Distribute(cSection, 1024, vSection);
cout<<vSectionNames[SEC_MEM_ID0+i]<<endl;
for(j=0; j<iKeys; j++)
{
cout<<vSection[j]<<endl;
}
cout<<"------------------"<<endl;
}
break;
case eCMD_REPORT_MEM:
cout<<"eCMD_REPORT_MEM"<<endl;
::GetPrivateProfileSection(sMember.c_str(), cSection, 1024, DAT_FILE_PATH);
iKeys = Distribute(cSection, 1024, vSection);
if(0 == iKeys)
{
cout<<"未找到 ["<<sMember<<"]"<<endl;
break;
}
cout<<sMember<<endl;
for(j=0; j<iKeys; j++)
{
cout<<vSection[j]<<endl;
}
cout<<"------------------"<<endl;
break;
case eCMD_STATISTIC_DEF:
//cout<<"eCMD_STATISTIC_DEF "<<endl;
iIncrement = 1;
/* fall through */
case eCMD_STATISTIC:
//cout<<"eCMD_STATISTIC "<<endl;
iKeyVal = GetIniInt(sMember.c_str(), sActive.c_str());
SetIniInt(sMember.c_str(), sActive.c_str(), iKeyVal+iIncrement);
break;
case eCMD_DELETE_NAME:
//cout<<"eCMD_DELETE_NAME "<<endl;
SetIniStr(sMember.c_str(), NULL, NULL);
break;
default:
cout<<ios::hex<<*(word*)&eCmd<<"参数不合法"<<endl;
system("pause>nul");
break;
}
return 0;
}
int Distribute(char cSN[], int size, vector<string> &vSN)
{
int i;
string item = "";
for(i=0; i<size-1; i++)
{
if(0 == cSN[i])
{
if(item != "")
vSN.push_back(item);
item = "";
if(0 == cSN[i+1]) // double '\0'
break;
}
else
item+=cSN[i];
}
return vSN.size();
}
|