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

📄 userdb.cpp

📁 swain-0.5.2.zip的源代码,比较好用,希望大家喜欢.
💻 CPP
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006  Daniel Lindstr鰉 and Daniel Nilsson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA  02110-1301, USA.
*/
#include "StdAfx.h"
#include "UserDB.h"

UserDB::UserDB(bool readFile){
	if(readFile){
		if(this->readFromFile()){
			//return;
		}
	}
	this->users = new DynList<User *>(NULL);
	this->groups = new DynList<Group *>(NULL);
}

UserDB::~UserDB(void){
	delete(this->users);
}


int UserDB::addUser(User *user){
	return(users->addItem(user));
}

void UserDB::removeUser(User *user){
	users->removeItem(user);
}

void UserDB::removeUser(int userid){
	users->removeIndex(userid);
}

bool UserDB::writeToFile(void){
	FILE *ufp, *gfp;
	User *user = NULL;
	Group *group = NULL;
	int u = -1;
	int count = 0;
	//skriver users till fil

	if((ufp = fopen(USERS_FILE, "w"))==NULL || (gfp = fopen(GROUPS_FILE, "w"))==NULL)
		return(false);
	for(int i=0; i<this->users->getSize(); i++){
		if(count == this->users->getCount())
			break;
		user = this->users->getItem(i);
		if(user == NULL)
			fprintf(ufp, "# #\n");
		else{
			fprintf(ufp, "%s %s\n", user->getName(), user->getPasswd());
			count++;
		}
	}


	//skriver grupperna till fil
	count = 0;
	for(int i=0; i<this->groups->getSize(); i++){
		if(count == this->groups->getCount())
			break;
		group = this->groups->getItem(i);
		if(group == NULL)
			fprintf(gfp, "# -1\n");
		else{
			fprintf(gfp, "%s %d ", group->getName(), group->getCount());
			count++;
			for(int j=0; j<group->getSize(); j++){
				u = group->getMember(j);
				if(u != -1)
					fprintf(gfp, "%d ", u);
			}
			fprintf(gfp, "\n");
		}
	}
	fclose(ufp);
	fclose(gfp);
	return(true);
}


bool UserDB::readFromFile(void){
	FILE *ufp, *gfp;
	int u;
	int i = 0;
	int count;
	char name[NAME_BUF_SIZE];
	char passwd[PASSWORD_BUF_SIZE];

	if((ufp = fopen(USERS_FILE, "r"))==NULL || (gfp = fopen(GROUPS_FILE, "r"))==NULL)
		return(false);

	//l鋝er anv鋘darna fr錸 fil
	delete(this->users);
	delete(this->groups);
	this->users = new DynList<User *>(NULL);
	this->groups = new DynList<Group *>(NULL);


	while(!feof(ufp)){
		if(fscanf(ufp, "%s %s\n", name, passwd) == 0)
			break;
		if(name[0] == '#')
			this->users->addItem(NULL);
		else
			this->addUser(name, passwd);
	}

	//l鋝er grupperna fr錸 fil
	while(!feof(gfp)){
		if(fscanf(gfp, "%s %d", name, &count) == 0)
			break;
		if(name[0] != '#'){
			this->createGroup(name);
			for(int j=0; j<count; j++){
				if(fscanf(gfp, " %d", &u) != 0){
					this->addUserToGroup(u, i);
				}
			}
			fscanf(gfp, "\n");
		}
		else{
			this->groups->addItem(NULL);
			fscanf(gfp, "\n");
		}
		i++;
	}

	fclose(ufp);
	fclose(gfp);
	return(true);
}


int UserDB::createGroup(char *name){
	return(this->groups->addItem(new Group(name)));
}


int UserDB::addUser(const char* name, const char *passwd){
	User *u = new User();
	u->setName(name);
	u->setPasswd(passwd);
	return(this->addUser(u));
}

void UserDB::addUserToGroup(int userid, int groupid){
	Group *group = (Group *)this->groups->getItem(groupid);
	if(group!=NULL)
		group->addMember(userid);
}

int UserDB::query(const int flags, const int *groups, int num_groups,  int *result, int result_size, int filteruser){
	int num = 0, i, size;
	User *user = NULL;
	Group *group = NULL;

	if(flags & QUERY_GET_GROUPS){
		size = this->groups->getSize();
		for(int i=0; i<size; i++){
			group = this->groups->getItem(i);
			if(group == NULL)
				continue;
			result[i] = i;
			num++;
			if(num == result_size)
				break;
		}
		return(num);
	}

	size = this->users->getSize();
	for(i=0; i<result_size; i++)
		result[i] = -1;
	
	for(i=0; i<size; i++){
		if(i == filteruser)
			continue;
		user = (User *)this->users->getItem(i);
		if(user == NULL)
			continue;
		if(flags & QUERY_FLAG_GROUPS){
			if(!userIsMemberOf(groups, num_groups, i))
				continue;
		}
		if((user->getStatus() == STATUS_ONLINE)&& !(flags & QUERY_FLAG_ONLINE))
			continue;
		if((user->getStatus() == STATUS_OFFLINE) && !(flags & QUERY_FLAG_OFFLINE))
			continue;
		if((user->getStatus() == STATUS_AWAY) && !(flags & QUERY_FLAG_AWAY))
			continue;
		result[num] = i;
		num++;
		if(num == result_size)
			break;
	}
	return(num);
}


bool UserDB::userIsMemberOf(const int *groups, int num_groups, int userid){
	Group *g = NULL;
	for(int i=0; i<num_groups; i++){
		g = (Group *)this->groups->getItem(groups[i]);
		if(g!=NULL && !g->isMember(userid))
			return(false);
		
	}
	return(true);
}


int UserDB::getUserId(const char *name){
	int num = this->users->getSize();
	User *user = NULL;
	for(int i=0; i<num; i++){
		user = this->users->getItem(i);
		if((user != NULL) && (0 == strcmp(user->getName(), name)))
			return(i);
	}
	return(-1);
}

bool UserDB::checkPasswd(int id, const char *passwd){
	User *user = this->users->getItem(id);
	if(user == NULL)
		return(false);
	return(0 == strcmp(user->getPasswd(), passwd));
}

STATUS UserDB::getUserStatus(int id){
	User *user = this->users->getItem(id);
	if(user!=NULL)
		return(user->getStatus());
	else
		return(STATUS_OFFLINE);
}

void UserDB::setUserStatus(int id, STATUS status){
	User *user = this->users->getItem(id);
	if(user != NULL)
		user->setStatus(status);
}

bool UserDB::userExists(int id){
	return(NULL != this->users->getItem(id));
}

bool UserDB::groupExists(int id){
	return(NULL != this->groups->getItem(id));
}

const char *UserDB::getName(int index){
	User *u = this->users->getItem(index);
	if(u == NULL)
		return(NULL);
	return(u->getName());
}

const char *UserDB::getGroup(int gid) {
	Group *g = groups->getItem(gid);
	if (g == NULL)
		return NULL;
	return g->getName();
}

int UserDB::getCount(void){
	return(this->users->getCount());
}

//returerar h鰃sta m鰆liga userid.
int UserDB::getDbSize(void){
	return(this->users->getSize()-1);
}

bool UserDB::setUserCId(int id, int cid){
	User *u;
	if((u = this->users->getItem(id)) == NULL)
		return(false);
	u->setCId(cid);
	return(true);
}

int UserDB::getUserCid(int id){
	User *u;
	if((u = this->users->getItem(id)) == NULL)
		return(-1);
	return(u->getCId());
}

int UserDB::getUserByCid(int cid) {
	int num = this->users->getSize();
	User *user = NULL;
	for (int i = 0; i < num; i++) {
		user = this->users->getItem(i);
		if (user != NULL && user->getCId() == cid)
			return i;
	}
	return -1;
}

bool UserDB::setUserAddr(int id, int addr){
	User *u;
	if((u = this->users->getItem(id)) == NULL)
		return false;
	u->setAddr(addr);
	return true;
}

int UserDB::getUserAddr(int id){
	User *u;
	if((u = this->users->getItem(id)) == NULL)
		return(-1);
	return(u->getAddr());

}

⌨️ 快捷键说明

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