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

📄 storage_file.c

📁 打魔兽战网的都知道他是什么
💻 C
📖 第 1 页 / 共 2 页
字号:
	eventlog(eventlog_level_error, __FUNCTION__, "file storage not initilized");	return NULL;    }    /* ONLY if requesting for a username and if savebyname() is true     * PS: yes its kind of a hack, we will make a proper index file     */    if (accname && prefs_get_savebyname()) {	pathname = xmalloc(strlen(accountsdir) + 1 + strlen(accname) + 1);	/* dir + / + file + NUL */	sprintf(pathname, "%s/%s", accountsdir, accname);	if (access(pathname, F_OK))	/* if it doesn't exist */	{	    xfree((void *) pathname);	    return NULL;	}	return pathname;    }    return NULL;}static int file_cmp_info(t_storage_info * info1, t_storage_info * info2){    return strcmp((const char *) info1, (const char *) info2);}static const char *file_escape_key(const char *key){    return key;}static int file_load_clans(t_load_clans_func cb){    char const *dentry;    t_pdir *clandir;    char *pathname;    int clantag;    t_clan *clan;    FILE *fp;    char *clanname, *motd, *p;    char line[1024];    int cid, creation_time;    int member_uid, member_join_time;    char member_status;    t_clanmember *member;    if (cb == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");	return -1;    }    if (!(clandir = p_opendir(clansdir)))    {	eventlog(eventlog_level_error, __FUNCTION__, "unable to open clan directory \"%s\" for reading (p_opendir: %s)", clansdir, pstrerror(errno));	return -1;    }    eventlog(eventlog_level_trace, __FUNCTION__, "start reading clans");    pathname = xmalloc(strlen(clansdir) + 1 + 4 + 1);    while ((dentry = p_readdir(clandir)) != NULL)    {	if (dentry[0] == '.')	    continue;	if (strlen(dentry) > 4)	{	    eventlog(eventlog_level_error, __FUNCTION__, "found too long clan filename in clandir \"%s\"", dentry);	    continue;	}	sprintf(pathname, "%s/%s", clansdir, dentry);	clantag = str_to_clantag(dentry);	if ((fp = fopen(pathname, "r")) == NULL)	{	    eventlog(eventlog_level_error, __FUNCTION__, "can't open clanfile \"%s\"", pathname);	    continue;	}	clan = xmalloc(sizeof(t_clan));	clan->clantag = clantag;	if (!fgets(line, 1024, fp))	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: no first line");	    xfree((void*)clan);	    continue;	}	clanname = line;	if (*clanname != '"')	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	clanname++;	p = strchr(clanname, '"');	if (!p)	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	*p = '\0';	if (strlen(clanname) >= CLAN_NAME_MAX)	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	p++;	if (*p != ',')	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	p++;	if (*p != '"')	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	motd = p + 1;	p = strchr(motd, '"');	if (!p)	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid clan file: invalid first line");	    xfree((void*)clan);	    continue;	}	*p = '\0';	if (sscanf(p + 1, ",%d,%d\n", &cid, &creation_time) != 2)	{	    eventlog(eventlog_level_error, __FUNCTION__, "invalid first line in clanfile");	    xfree((void*)clan);	    continue;	}	clan->clanname = xstrdup(clanname);	clan->clan_motd = xstrdup(motd);	clan->clanid = cid;	clan->creation_time = (time_t) creation_time;	clan->created = 1;	clan->modified = 0;	clan->channel_type = prefs_get_clan_channel_default_private();	eventlog(eventlog_level_trace, __FUNCTION__, "name: %s motd: %s clanid: %i time: %i", clanname, motd, cid, creation_time);	clan->members = list_create();	while (fscanf(fp, "%i,%c,%i\n", &member_uid, &member_status, &member_join_time) == 3)	{	    member = xmalloc(sizeof(t_clanmember));	    if (!(member->memberacc = accountlist_find_account_by_uid(member_uid)))	    {		eventlog(eventlog_level_error, __FUNCTION__, "cannot find uid %u", member_uid);		xfree((void *) member);		continue;	    }	    member->status = member_status - '0';	    member->join_time = member_join_time;	    member->clan = clan;	    if ((member->status == CLAN_NEW) && (time(NULL) - member->join_time > prefs_get_clan_newer_time() * 3600))	    {		member->status = CLAN_PEON;		clan->modified = 1;	    }	    list_append_data(clan->members, member);	    account_set_clanmember(member->memberacc, member);	    eventlog(eventlog_level_trace, __FUNCTION__, "added member: uid: %i status: %c join_time: %i", member_uid, member_status + '0', member_join_time);	}	fclose(fp);	cb(clan);    }    xfree((void *) pathname);    if (p_closedir(clandir) < 0)    {	eventlog(eventlog_level_error, __FUNCTION__, "unable to close clan directory \"%s\" (p_closedir: %s)", clansdir, pstrerror(errno));    }    eventlog(eventlog_level_trace, __FUNCTION__, "finished reading clans");    return 0;}static int file_write_clan(void *data){    FILE *fp;    t_elem *curr;    t_clanmember *member;    char *clanfile;    t_clan *clan = (t_clan *) data;    clanfile = xmalloc(strlen(clansdir) + 1 + 4 + 1);    sprintf(clanfile, "%s/%c%c%c%c", clansdir, clan->clantag >> 24, (clan->clantag >> 16) & 0xff, (clan->clantag >> 8) & 0xff, clan->clantag & 0xff);    if ((fp = fopen(clanfile, "w")) == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "can't open clanfile \"%s\"", clanfile);	xfree((void *) clanfile);	return -1;    }    fprintf(fp, "\"%s\",\"%s\",%i,%i\n", clan->clanname, clan->clan_motd, clan->clanid, (int) clan->creation_time);    LIST_TRAVERSE(clan->members, curr)    {	if (!(member = elem_get_data(curr)))	{	    eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem in list");	    continue;	}	if ((member->status == CLAN_NEW) && (time(NULL) - member->join_time > prefs_get_clan_newer_time() * 3600))	    member->status = CLAN_PEON;	fprintf(fp, "%i,%c,%u\n", account_get_uid(member->memberacc), member->status + '0', (unsigned) member->join_time);    }    fclose(fp);    xfree((void *) clanfile);    return 0;}static int file_remove_clan(int clantag){    char *tempname;    tempname = xmalloc(strlen(clansdir) + 1 + 4 + 1);    sprintf(tempname, "%s/%c%c%c%c", clansdir, clantag >> 24, (clantag >> 16) & 0xff, (clantag >> 8) & 0xff, clantag & 0xff);    if (remove((const char *) tempname) < 0)    {	eventlog(eventlog_level_error, __FUNCTION__, "could not delete clan file \"%s\" (remove: %s)", (char *) tempname, pstrerror(errno));	xfree(tempname);	return -1;    }    xfree(tempname);    return 0;}static int file_remove_clanmember(int uid){    return 0;}static int file_load_teams(t_load_teams_func cb){    char const *dentry;    t_pdir *teamdir;    char *pathname;    unsigned int teamid;    t_team *team;    FILE *fp;    char * line;    unsigned int fteamid,lastgame;    unsigned char size;    char clienttag[5];    int i;    if (cb == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "get NULL callback");	return -1;    }    if (!(teamdir = p_opendir(teamsdir)))    {	eventlog(eventlog_level_error, __FUNCTION__, "unable to open team directory \"%s\" for reading (p_opendir: %s)", teamsdir, pstrerror(errno));	return -1;    }    eventlog(eventlog_level_trace, __FUNCTION__, "start reading teams");    pathname = xmalloc(strlen(teamsdir) + 1 + 8 + 1);    while ((dentry = p_readdir(teamdir)) != NULL)    {	if (dentry[0] == '.')	    continue;	if (strlen(dentry) != 8)	{	    eventlog(eventlog_level_error, __FUNCTION__, "found invalid team filename in teamdir \"%s\"", dentry);	    continue;	}	sprintf(pathname, "%s/%s", teamsdir, dentry);	teamid = (unsigned int)strtoul(dentry,NULL,16); // we use hexadecimal teamid as filename	if ((fp = fopen(pathname, "r")) == NULL)	{	    eventlog(eventlog_level_error, __FUNCTION__, "can't open teamfile \"%s\"", pathname);	    continue;	}	team = xmalloc(sizeof(t_team));	team->teamid = teamid;	if (!(line = file_get_line(fp)))	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: file is empty");	    goto load_team_failure;	}		if (sscanf(line,"%u,%c,%4s,%u",&fteamid,&size,clienttag,&lastgame)!=4)	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: invalid number of arguments on first line");	    goto load_team_failure;	}		if (fteamid != teamid)	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: filename and stored teamid don't match");	    goto load_team_failure;	}		size -='0';	if ((size<2) || (size>4))	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: invalid team size");	    goto load_team_failure;	}	team->size = size;		if (!(tag_check_client(team->clienttag = tag_str_to_uint(clienttag))))	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: invalid clienttag");	    goto load_team_failure;	}	team->lastgame = (time_t)lastgame;	if (!(line= file_get_line(fp)))	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: missing 2nd line");	    goto load_team_failure;	}	if (sscanf(line,"%u,%u,%u,%u",&team->teammembers[0],&team->teammembers[1],&team->teammembers[2],&team->teammembers[3])!=4)	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: invalid number of arguments on 2nd line");	    goto load_team_failure;	}	for (i=0; i<MAX_TEAMSIZE;i++)	{	   if (i<size)	   {		if ((team->teammembers[i]==0))		{	    		eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: too few members");	    		goto load_team_failure;		}	   }	   else	   {	   	if ((team->teammembers[i]!=0))		{	    		eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: too many members");	    		goto load_team_failure;		}	   }	   team->members[i] = NULL;	}	if (!(line= file_get_line(fp)))	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: missing 3rd line");	    goto load_team_failure;	}	if (sscanf(line,"%d,%d,%d,%d,%d",&team->wins,&team->losses,&team->xp,&team->level,&team->rank)!=5)	{	    eventlog(eventlog_level_error,__FUNCTION__,"invalid team file: invalid number of arguments on 3rd line");	    goto load_team_failure;	}	eventlog(eventlog_level_trace,__FUNCTION__,"succesfully loaded team %s",dentry);	cb(team);		goto load_team_success;	load_team_failure:	  xfree((void*)team);	  eventlog(eventlog_level_error,__FUNCTION__,"error while reading file \"%s\"",dentry);	load_team_success:	file_get_line(NULL); // clear file_get_line buffer	fclose(fp);    }    xfree((void *) pathname);    if (p_closedir(teamdir) < 0)    {	eventlog(eventlog_level_error, __FUNCTION__, "unable to close team directory \"%s\" (p_closedir: %s)", teamsdir, pstrerror(errno));    }    eventlog(eventlog_level_trace, __FUNCTION__, "finished reading teams");    return 0;}static int file_write_team(void *data){    FILE *fp;    char *teamfile;    t_team *team = (t_team *) data;    teamfile = xmalloc(strlen(teamsdir) + 1 + 8 + 1);    sprintf(teamfile, "%s/%08x", teamsdir, team->teamid);    if ((fp = fopen(teamfile, "w")) == NULL)    {	eventlog(eventlog_level_error, __FUNCTION__, "can't open teamfile \"%s\"", teamfile);	xfree((void *) teamfile);	return -1;    }    fprintf(fp,"%u,%c,%s,%u\n",team->teamid,team->size+'0',clienttag_uint_to_str(team->clienttag),(unsigned int)team->lastgame);    fprintf(fp,"%u,%u,%u,%u\n",team->teammembers[0],team->teammembers[1],team->teammembers[2],team->teammembers[3]);    fprintf(fp,"%d,%d,%d,%d,%d\n",team->wins,team->losses,team->xp,team->level,team->rank);    fclose(fp);    xfree((void *) teamfile);        return 0;}static int file_remove_team(unsigned int teamid){    char *tempname;    tempname = xmalloc(strlen(clansdir) + 1 + 8 + 1);    sprintf(tempname, "%s/%08x", clansdir, teamid);    if (remove((const char *) tempname) < 0)    {	eventlog(eventlog_level_error, __FUNCTION__, "could not delete team file \"%s\" (remove: %s)", (char *) tempname, pstrerror(errno));	xfree(tempname);	return -1;    }    xfree(tempname);        return 0;}

⌨️ 快捷键说明

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