--- deliantra/server/common/init.C 2010/04/11 17:27:51 1.56 +++ deliantra/server/common/init.C 2010/04/15 06:05:52 1.57 @@ -195,101 +195,67 @@ void init_attackmess () { - char buf[MAX_BUF]; char filename[MAX_BUF]; - char *cp, *p; - FILE *fp; - static int has_been_done = 0; - int msgnum, level, comp; - int mode = 0, total = 0; - - if (has_been_done) - return; - else - has_been_done = 1; sprintf (filename, "%s/attackmess", settings.datadir); LOG (llevDebug, "Reading attack messages from %s...\n", filename); - if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) + + object_thawer thawer (filename); + + if (!thawer) { LOG (llevError, "Can't open %s.\n", filename); return; } - level = 0; - while (fgets (buf, MAX_BUF, fp) != NULL) - { - if (*buf == '#') - continue; + int msgnum = -1; + int total = 0; - if ((cp = strchr (buf, '\n')) != NULL) - *cp = '\0'; - - cp = buf; - while (*cp == ' ') /* Skip blanks */ - cp++; + while (thawer.kw) + { + if (thawer.kw != KW_type) + if (!thawer.parse_error ("attackmess file")) + break; - if (strncmp (cp, "TYPE:", 5) == 0) - { - p = strtok (buf, ":"); - p = strtok (NULL, ":"); + thawer.get (msgnum); + thawer.next (); - if (mode == 1) - { - attack_mess[msgnum][level].level = -1; - attack_mess[msgnum][level].buf1 = NULL; - attack_mess[msgnum][level].buf2 = NULL; - attack_mess[msgnum][level].buf3 = NULL; - } + int level = 0; - level = 0; - msgnum = atoi (p); - mode = 1; - } - else if (mode == 1) + while (thawer.kw == KW_hp) { - p = strtok (buf, "="); - attack_mess[msgnum][level].level = atoi (buf); - p = strtok (NULL, "="); - - if (p != NULL) - attack_mess[msgnum][level].buf1 = strdup (p); + // our old friend, dog-slow sscanf + int hp; + char buf1[1024]; + char buf2[1024]; + char buf3[1024]; + + if (4 != sscanf (thawer.value_nn, "%d %1023[^|]|%1023[^|]|%1023[^|]", + &hp, buf1, buf2, buf3)) + thawer.parse_error ("attackmess file"); else - attack_mess[msgnum][level].buf1 = strdup (""); + { + if (*buf2 == '-') + *buf2 = 0; // sscanf can't parse empty fields, so use "-" as marker - mode = 2; - } - else if (mode == 2) - { - p = strtok (buf, "="); - attack_mess[msgnum][level].level = atoi (buf); - p = strtok (NULL, "="); + attack_mess[msgnum][level].level = hp; + attack_mess[msgnum][level].buf1 = strdup (buf1); + attack_mess[msgnum][level].buf2 = strdup (buf2); + attack_mess[msgnum][level].buf3 = strdup (buf3); - if (p != NULL) - attack_mess[msgnum][level].buf2 = strdup (p); - else - attack_mess[msgnum][level].buf2 = strdup (""); + ++level; + ++total; + } - mode = 3; + thawer.next (); } - else if (mode == 3) - { - p = strtok (buf, "="); - attack_mess[msgnum][level].level = atoi (buf); - p = strtok (NULL, "="); - - if (p != NULL) - attack_mess[msgnum][level].buf3 = strdup (p); - else - attack_mess[msgnum][level].buf3 = strdup (""); - mode = 1; - level++; - total++; - } + attack_mess[msgnum][level].level = -1; + attack_mess[msgnum][level].buf1 = 0; + attack_mess[msgnum][level].buf2 = 0; + attack_mess[msgnum][level].buf3 = 0; } LOG (llevDebug, "got %d messages in %d categories.\n", total, msgnum + 1); - close_and_delete (fp, comp); }