command_groups.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * This program is free software; you can redistribute it and/or
  3.  * modify it under the terms of the GNU General Public License
  4.  * as published by the Free Software Foundation; either version 2
  5.  * of the License, or (at your option) any later version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License
  13.  * along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  15.  */
  16. #define COMMAND_GROUPS_INTERNAL_ACCESS
  17. #include "common/setup_before.h"
  18. #include <math.h>
  19. #include <stdio.h>
  20. #ifdef HAVE_STDDEF_H
  21. # include <stddef.h>
  22. #else
  23. # ifndef NULL
  24. #  define NULL ((void *)0)
  25. # endif
  26. #endif
  27. #ifdef STDC_HEADERS
  28. # include <stdlib.h>
  29. #else
  30. # ifdef HAVE_MALLOC_H
  31. #  include <malloc.h>
  32. # endif
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. # include <string.h>
  36. #else
  37. # ifdef HAVE_STRINGS_H
  38. #  include <strings.h>
  39. # endif
  40. #endif
  41. #include <errno.h>
  42. #include "common/eventlog.h"
  43. #include "common/list.h"
  44. #include "prefs.h"
  45. #include "common/util.h"
  46. #include "command_groups.h"
  47. #include "common/setup_after.h"
  48. //#define COMMANDGROUPSDEBUG 1
  49. static t_list * command_groups_head=NULL;
  50. static FILE * fp = NULL;
  51. extern int command_groups_load(char const * filename)
  52. {
  53.     unsigned int line;
  54.     unsigned int pos;
  55.     char * buff;
  56.     char * temp;
  57.     char const * command;
  58.     unsigned int group;
  59.     t_command_groups * entry;
  60.     
  61.     if (!filename) {
  62.         eventlog(eventlog_level_error,"command_groups_load","got NULL filename");
  63.         return -1;
  64.     }
  65.     if (!(command_groups_head = list_create())) {
  66.         eventlog(eventlog_level_error,"command_groups_load","could not create list");
  67.         return -1;
  68.     }
  69.     if (!(fp = fopen(filename,"r"))) {
  70.         eventlog(eventlog_level_error,"command_groups_load","could not open file "%s" for reading (fopen: %s)",filename,strerror(errno));
  71. list_destroy(command_groups_head);
  72. command_groups_head = NULL;
  73.         return -1;
  74.     }
  75.     for (line=1; (buff = file_get_line(fp)); line++) {
  76.         for (pos=0; buff[pos]=='t' || buff[pos]==' '; pos++);
  77. if (buff[pos]=='' || buff[pos]=='#') {
  78.             free(buff);
  79.             continue;
  80.         }
  81. if ((temp = strrchr(buff,'#'))) {
  82.     unsigned int len;
  83.     unsigned int endpos;
  84.     
  85.             *temp = '';
  86.     len = strlen(buff)+1;
  87.             for (endpos=len-1; buff[endpos]=='t' || buff[endpos]==' '; endpos--);
  88.             buff[endpos+1] = '';
  89.         }
  90. if (!(temp = strtok(buff," t"))) { /* strtok modifies the string it is passed */
  91.     eventlog(eventlog_level_error,"command_groups_load","missing group on line %u of file "%s"",line,filename);
  92.     free(buff);
  93.     continue;
  94. }
  95. if (str_to_uint(temp,&group)<0) {
  96.     eventlog(eventlog_level_error,"command_groups_load","group '%s' not a valid group (1-8)",temp);
  97.     free(buff);
  98.     continue;
  99. }
  100. if (group == 0 || group > 8) {
  101.     eventlog(eventlog_level_error,"command_groups_load","group '%u' not within groups limits (1-8)",group);
  102.     free(buff);
  103.     continue;
  104. while ((command = strtok(NULL," t"))) {
  105.     if (!(entry = malloc(sizeof(t_command_groups)))) {
  106. eventlog(eventlog_level_error,"command_groups_load","could not allocate memory for entry");
  107. continue;
  108.     }
  109.     if (!(entry->group = pow(2,group-1))) {
  110. eventlog(eventlog_level_error,"command_groups_load","could not allocate memory for group");
  111. free(entry);
  112. continue;
  113.     }
  114.     if (!(entry->command = strdup(command))) {
  115. eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for client address");
  116. free(entry);
  117. continue;
  118.     }
  119.     if (list_append_data(command_groups_head,entry)<0) {
  120. eventlog(eventlog_level_error,"command_groups_load","could not append item");
  121. free(entry->command);
  122. free(entry);
  123.     }
  124. #ifdef COMMANDGROUPSDEBUG
  125.     else {
  126. eventlog(eventlog_level_info,"command_groups_load","Added command: %s - with group %u",entry->command,entry->group);
  127.     }
  128. #endif
  129. }
  130. free(buff);
  131.     }
  132.     fclose(fp);
  133.     return 0;
  134. }
  135. extern int command_groups_unload(void)
  136. {
  137.     t_elem * curr;
  138.     t_command_groups * entry;
  139.     
  140.     if (command_groups_head) {
  141. LIST_TRAVERSE(command_groups_head,curr) {
  142.     if (!(entry = elem_get_data(curr)))
  143. eventlog(eventlog_level_error,"command_groups_unload","found NULL entry in list");
  144.     else {
  145. free(entry->command);
  146. free(entry);
  147.     }
  148.     list_remove_elem(command_groups_head,curr);
  149. }
  150. list_destroy(command_groups_head);
  151. command_groups_head = NULL;
  152.     }
  153.     return 0;
  154. }
  155. extern unsigned int command_get_group(char const * command)
  156. {
  157.     t_elem const * curr;
  158.     t_command_groups * entry;
  159.     if (command_groups_head) {
  160. LIST_TRAVERSE(command_groups_head,curr) {
  161.     if (!(entry = elem_get_data(curr)))
  162. eventlog(eventlog_level_error,"command_get_group","found NULL entry in list");
  163.     else if (!(strcmp(entry->command,command)))
  164. return entry->group;
  165. }
  166.     }
  167.     return 0;
  168. }
  169. extern int command_groups_reload(char const * filename)
  170. {
  171.     command_groups_unload();
  172.     return command_groups_load(filename);
  173. }