Explorar el Código

Remove responses size craziness

Bryan Drewery hace 11 años
padre
commit
135edc5bca
Se han modificado 4 ficheros con 12 adiciones y 47 borrados
  1. 9 7
      build/makeres.sh
  2. 0 1
      src/main.cc
  3. 1 31
      src/response.cc
  4. 2 8
      src/response.h

+ 9 - 7
build/makeres.sh

@@ -3,15 +3,13 @@
 echo "/* Generated by $0 */"
 needcomma=0
 types=
-echo "typedef const char * res_t;"
-echo
 while read -r line; do
 	[ -z "${line%%#*}" ] && continue # skip comments
 	if [ -z "${line%%:*}" ]; then
-		[ ${needcomma} -eq 1 ] && printf ",\n\tNULL\n};\n\n"
+		[ ${needcomma} -eq 1 ] && printf ",\n};\n\n"
 		type="${line#:}"
 		[ "${type}" = "end" ] && break
-		printf "static res_t res_%s[] = {\n" "${type}"
+		printf "static const char* res_%s[] = {\n" "${type}"
 		needcomma=0
 		types="${types}${types:+ }${type}"
 	else
@@ -21,12 +19,16 @@ while read -r line; do
 	fi
 done
 
-echo "static res_t *res[] = {"
-printf "\tNULL,\n"
+echo "typedef struct {"
+echo "  const char** res;"
+echo "  const size_t size;"
+echo "} res_t;"
+echo
+echo "static const res_t res[] = {"
 needcomma=0
 for type in ${types}; do
 	[ ${needcomma} -eq 1 ] && printf ",\n"
-	printf "\tres_%s" "${type}"
+	printf "\t{res_%s, sizeof(res_%s)/sizeof(res_%s[0])}" "${type}" "${type}" "${type}"
 	needcomma=1
 done
 echo

+ 0 - 1
src/main.cc

@@ -827,7 +827,6 @@ int main(int argc, char **argv)
   Auth::InitTimer();
   init_vars();			/* needed for cfg */
   init_botcmd();
-  init_responses();		/* zeros out response[] */
 
   egg_dns_init();
   channels_init();

+ 1 - 31
src/response.cc

@@ -5,44 +5,14 @@
  *
  */
 
-
 #include "common.h"
 #include "response.h"
-#include "main.h"
 #include "responses.cc"
 
-static response_t response_totals[RES_TYPES + 1];
-
-void
-init_responses()
-{
-  for (response_t i = 0; i <= RES_TYPES; i++)
-    response_totals[i] = 0;
-}
-
-static void
-count_responses(response_t type)
-{
-  response_t total = 0;
-
-  for (total = 0; res[type][total]; total++) 
-    ;
-
-  response_totals[type] = total;
-  /* printf("Type: %d has %d total responses!\n", type, response_totals[type]); */
-}
-
 const char *
 response(response_t type)
 {
-  if (!type)
-    type = randint(RES_TYPES) + 1;
-
-  /* wait to count the totals until it's used for the first time */
-  if (!response_totals[type])
-    count_responses(type);
-
-  return res[type][randint(response_totals[type])];
+  return res[type].res[randint(res[type].size)];
 }
 
 const char *

+ 2 - 8
src/response.h

@@ -1,10 +1,8 @@
 #ifndef _RESPONSE_H
 #define _RESPONSE_H
 
-typedef unsigned int response_t;
-
-enum {
-	RES_BANNED = 1,
+enum response_t {
+	RES_BANNED = 0,
 	RES_KICKBAN,
 	RES_MASSDEOP,
 	RES_BADOP,
@@ -24,13 +22,9 @@ enum {
 	RES_MIRCVER,
 	RES_MIRCSCRIPT,
 	RES_OTHERSCRIPT,
-	RES_END
 };
 
-#define RES_TYPES 20
-
 const char *response(response_t);
-void init_responses();
 const char *r_banned(struct chanset_t* chan);
 
 #endif /* !_RESPONSE_H */