📄 global.cpp
字号:
#include "StdAfx.h"
#include "global.h"
//获取图片目录
BOOL getImageDir(char *path,int &length){
//获取当前模块的地址
::GetModuleFileName(AfxGetInstanceHandle(),path,length);
//去掉文件名,得到当前目录
for (int i=length-1;i;i--)
{
if (path[i-1]=='\\')
{
path[i] = '\0';
length = i;
break;
}
}
strcat(path,"image\\");
length = length+6;
return true;
}
//初始化ItemList
int ItemList::nextID = 0;
ItemList::ItemList(char *url,char *title,char *author,char *description,char *pubTime,int isRead /* = false */,int id){
int length = strlen(url);
this->url = new char[length+1];
strcpy(this->url,url);
length = strlen(title);
this->title = new char[length+1];
strcpy(this->title,title);
length = strlen(author);
this->author = new char[length+1];
strcpy(this->author,author);
length = strlen(description);
this->description = new char[length+1];
strcpy(this->description,description);
this->time = new char[strlen(pubTime)+1];
strcpy(this->time,pubTime);
this->isRead = isRead;
this->id = id;
}
//析构函数
ItemList::~ItemList(){
if (this->author)
{
delete[] this->author;
this->author = NULL;
}
if (this->description)
{
delete[] this->description;
this->description = NULL;
}
if (this->time)
{
delete[] this->time;
this->time = NULL;
}
if (this->title)
{
delete[] this->title;
this->title = NULL;
}
if (this->url)
{
delete[] this->url;
this->url = NULL;
}
}
// 转换各种时间格式
// style=0,rtf时间,style=1,iso时间
// DateTime::DateTime(char *timestr,int style){
// date = 0;
// time = 0;
// int itemp = 0;
// char *temp = new char[5];
// if (strlen(timestr)<25)
// {
// date = 0;
// time = 0;
// return;
// }
// else{
// if (style)
// {
// //读取年
// strncpy(temp,timestr,4);
// temp[4] = '\0';
// itemp = atoi(temp);
// date = date + itemp*10000;
// //读取月
// strncpy(temp,timestr+5,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// date = date + itemp*100;
// //读取日
// strncpy(temp,timestr+8,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// date = date + itemp;
//
// //读取时
// strncpy(temp,timestr+12,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time + itemp*10000;
// //读取分
// strncpy(temp,timestr+15,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time + itemp*100;
// //读取秒
// strncpy(temp,timestr+18,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time + itemp;
// }
// else{
// strncpy(temp,timestr+5,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// date = date+itemp;
// char *month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
// for (int i=0;i<12;i++)
// {
// if (!strncmp(timestr+8,month[i],3))
// {
// date = date+100*(i+1);
// break;
// }
// }
// strncpy(temp,timestr+12,4);
// temp[4] = '\0';
// itemp = atoi(temp);
// date = date+10000*itemp;
//
// strncpy(temp,timestr+17,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time+itemp*10000;
//
// strncpy(temp,timestr+20,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time+itemp*100;
//
// strncpy(temp,timestr+23,2);
// temp[2] = '\0';
// itemp = atoi(temp);
// time = time+itemp;
//
//
// }
// delete temp;
// }
// }
DateTime::DateTime(char *time){
this->date = 0;
this->time = 0;
char temp[5];
int lastSpli = -1;
int length = strlen(time);
if (isdigit(time[0]))
{
strncpy(temp,time,4);
temp[4]='\0';
date = atoi(temp)*10000;
lastSpli = 4;
if (isdigit(time[lastSpli+2]))
{
strncpy(temp,time+lastSpli+1,2);
temp[2]='\0';
date = date+atoi(temp)*100;
lastSpli = lastSpli+3;
}
else{
strncpy(temp,time+lastSpli+1,1);
temp[1]='\0';
date = date+atoi(temp)*100;
lastSpli = lastSpli+2;
}
if (isdigit(time[lastSpli+2]))
{
strncpy(temp,time+lastSpli+1,2);
temp[2]='\0';
date = date+atoi(temp);
lastSpli = lastSpli+3;
}
else{
strncpy(temp,time+lastSpli+1,1);
temp[1]='\0';
date = date+atoi(temp);
lastSpli = lastSpli+2;
}
if (isdigit(time[lastSpli+2]))
{
strncpy(temp,time+lastSpli+1,2);
temp[2]='\0';
this->time = this->time+atoi(temp)*10000;
lastSpli = lastSpli+3;
}
else{
strncpy(temp,time+lastSpli+1,1);
temp[1]='\0';
this->time = this->time+atoi(temp)*10000;
lastSpli = lastSpli+2;
}
if (isdigit(time[lastSpli+2]))
{
strncpy(temp,time+lastSpli+1,2);
temp[2]='\0';
this->time = this->time+atoi(temp)*100;
lastSpli = lastSpli+3;
}
else{
strncpy(temp,time+lastSpli+1,1);
temp[1]='\0';
this->time = this->time+atoi(temp)*100;
lastSpli = lastSpli+2;
}
if (isdigit(time[lastSpli+2]))
{
strncpy(temp,time+lastSpli+1,2);
temp[2]='\0';
this->time = this->time+atoi(temp);
lastSpli = lastSpli+3;
}
else{
strncpy(temp,time+lastSpli+1,1);
temp[1]='\0';
this->time = this->time+atoi(temp);
lastSpli = lastSpli+2;
}
}
else{
int yearstart = 11;
if (isdigit(time[6]))
{
yearstart = 12;
strncpy(temp,time+5,2);
temp[2] = '\0';
date = date+atoi(temp);
}
else{
strncpy(temp,time+5,1);
temp[1] = '\0';
date = date+atoi(temp);
}
char *month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
for (int i=0;i<12;i++)
{
if (!strncmp(time+yearstart-4,month[i],3))
{
date = date+100*(i+1);
break;
}
}
strncpy(temp,time+yearstart,4);
temp[4] = '\0';
date = date+10000*atoi(temp);
strncpy(temp,time+yearstart+5,2);
temp[2] = '\0';
this->time = this->time+atoi(temp)*10000;
strncpy(temp,time+20,2);
temp[2] = '\0';
this->time = this->time+atoi(temp)*100;
strncpy(temp,time+23,2);
temp[2] = '\0';
this->time = this->time+atoi(temp);
}
}
bool DateTime::operator <(const DateTime& other){
if (this->date<other.date)
{
return true;
}
else if (this->date>other.date)
{
return false;
}
if (this->time<other.time)
{
return true;
}
return false;
}
char* DateTime::toString(){
if (date==0&&time==0)
{
char *result = new char[20];
// strcpy(result,"0000/00/00 00:00:00");
// return result;
CTime time = CTime::GetCurrentTime();
CString cs = time.Format("%Y/%m/%d %H:%M:%S");
strcpy(result,(LPTSTR)(LPCTSTR)cs);
return result;
}
char *result = new char[20];
char *pcdate = new char[12];
char *pctime = new char[9];
itoa(date,pcdate,10);
itoa(time,pctime,10);
if (strlen(pctime)<6)
{
sprintf(pctime,"%06d",time);
}
strncpy(result,pcdate,4);
result[4]='/';
strncpy(result+5,pcdate+4,2);
result[7]='/';
strncpy(result+8,pcdate+6,2);
result[10] = ' ';
strncpy(result+11,pctime,2);
result[13]=':';
strncpy(result+14,pctime+2,2);
result[16]=':';
strncpy(result+17,pctime+4,2);
result[19]='\0';
delete[] pctime;
delete[] pcdate;
if (strlen(result)!=19)
{
CTime time = CTime::GetCurrentTime();
CString cs = time.Format("%Y/%m/%d %H:%M:%S");
strcpy(result,(LPTSTR)(LPCTSTR)cs);
}
return result;
}
//根据名字获取图片句柄
CBitmap* getImage(char *name){
TCHAR path[255];
int length = 255;
//获取图片目录路径
getImageDir(path,length);
//获取图片的路径
strcpy(path,name);
//载入图片
HBITMAP h = (HBITMAP)::LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
CBitmap *bmp = new CBitmap;
bmp->Attach(h);
return bmp;
}
//通过图片文件的名称创建一个imagelist
void buildImageList(CImageList *imageList,char **names,int size){
TCHAR path[255];
int length = 255;
//获取图片目录的路径
getImageDir(path,length);
//对每一个图片文件,载入,并添加到列表中
for (int i=0;i<size;i++)
{
strcat(path,names[i]);
HBITMAP h = (HBITMAP)::LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
CBitmap bmp;
bmp.Attach(h);
imageList->Add(&bmp,RGB(0,0,0));
path[length]='\0';
}
}
//设置菜单的图片
void setMenuItemBitmapsByFileName(CMenu *menu,int position,char *name){
TCHAR path[255];
int length = 255;
getImageDir(path,length);
strcat(path,name);
HBITMAP h = (HBITMAP)::LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
::SetMenuItemBitmaps(menu->m_hMenu,0,MF_BYPOSITION,h,h);
}
int MyTreeNode::nextID = 0;
MyTreeNode::MyTreeNode(char* name,int id){
int length = strlen(name);
this->name = new char[length+1];
strcpy(this->name,name);
this->nextSubling = NULL;
if (id>=0)
{
this->id = id;
}
else{
this->id = MyTreeNode::nextID;
MyTreeNode::nextID++;
}
}
MyTreeNode::~MyTreeNode(){
delete[] this->name;
}
DirNode::DirNode(char *name,int id):MyTreeNode(name,id){
this->firstChild = NULL;
this->type = 0;
}
DirNode::~DirNode(){
}
ChannelNode::ChannelNode(char* name,char* url,char *time,int id):MyTreeNode(name,id){
if (url)
{
int length = strlen(url);
this->url = new char[length+1];
strcpy(this->url,url);
}
else{
this->url = new char[1];
this->url[0]='\0';
}
this->type = 1;
if (time)
{
this->pubTime = new char[strlen(time)+1];
strcpy(this->pubTime,time);
}
else{
this->pubTime = new char[20];
strcpy(this->pubTime,"0000/00/00 00:00:00");
}
}
ChannelNode::~ChannelNode(){
if (url)
{
delete[] this->url;
this->url = NULL;
}
if (this->pubTime)
{
delete[] this->pubTime;
this->pubTime = NULL;
}
}
CollectionNode::CollectionNode(char* name,char* url /* = NULL */,int id):MyTreeNode(name,id){
if (url)
{
int length = strlen(url);
this->url = new char[length+1];
strcpy(this->url,url);
}
else{
this->url = new char[1];
this->url[0]='\0';
}
this->type = 2;
}
CollectionNode::~CollectionNode(){
if (url)
{
delete[] this->url;
this->url = NULL;
}
}
ItemList* operator+(ItemList* left,ItemList &right){
if (left==NULL)
{
return &right;
}
else{
ItemList *temp = left;
while (temp->nextItem!=NULL)
{
temp = temp->nextItem;
}
temp->nextItem = &right;
return left;
}
}
Channel::Channel(char *source,char *time,char *title,ItemList *items /* = NULL */){
int sourceLength = strlen(source);
this->source = new char[sourceLength+1];
strcpy(this->source,source);
int timelength = strlen(time);
this->pubTime = new char[20];
//strcpy(this->pubTime,time);
strcpy(this->pubTime,"0000/00/00 00:00:00");
int titleLength = strlen(title);
this->title = new char[titleLength+1];
strcpy(this->title,title);
this->items = items;
}
Channel::~Channel(){
delete[] source;
delete[] pubTime;
delete[] title;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -