From 1c3714926d213c6c6639d1da65b64116b881f18f Mon Sep 17 00:00:00 2001 From: Thomas Arp Date: Wed, 1 Dec 2010 14:41:47 +0000 Subject: [PATCH] new rebuild Index utility --- src/util/Makefile.in | 6 ++ src/util/rebuildAsciiIndex.c | 154 +++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 src/util/rebuildAsciiIndex.c diff --git a/src/util/Makefile.in b/src/util/Makefile.in index 29a4ffb..5b2f65b 100755 --- a/src/util/Makefile.in +++ b/src/util/Makefile.in @@ -29,6 +29,7 @@ all: $(BINDIR)/asciipasswd \ $(BINDIR)/shopconv \ $(BINDIR)/sign \ $(BINDIR)/split \ + $(BINDIR)/rebuildIndex \ $(BINDIR)/wld2html \ $(BINDIR)/webster @@ -48,6 +49,11 @@ wld2html: $(BINDIR)/wld2html webster: $(BINDIR)/webster +rebuildIndex: $(BINDIR)/rebuildIndex + +$(BINDIR)/rebuildIndex: rebuildAsciiIndex.c + $(CC) $(CFLAGS) -o $(BINDIR)/rebuildIndex rebuildAsciiIndex.c + $(BINDIR)/asciipasswd: asciipasswd.c $(CC) $(CFLAGS) -o $(BINDIR)/asciipasswd asciipasswd.c @CRYPTLIB@ diff --git a/src/util/rebuildAsciiIndex.c b/src/util/rebuildAsciiIndex.c new file mode 100644 index 0000000..6954c67 --- /dev/null +++ b/src/util/rebuildAsciiIndex.c @@ -0,0 +1,154 @@ +/* ************************************************************************ +* file: rebuildAsciiIndex.c Part of tbaMUD * +* Copyright (C) 1990, 2010 - see 'license.doc' for complete information. * +* All Rights Reserved * +************************************************************************* */ + +#include +#include +#include +#include +#include + +#define READ_SIZE 256 + +int atoi(const char *str); +long atol(const char *str); + + +void walkdir(FILE* index_file, char *dir); +int get_line(FILE *fl, char *buf); + +int main(int argc, char** argv) +{ + FILE *index_file; + if ( argc == 1 ) { + printf("Usage: %s indexfile\n",argv[0]); + return 0; + } + if (!(index_file = fopen(argv[1], "w"))) { + perror("error opening index file"); + return 1; + } + walkdir(index_file, "."); + + fprintf(index_file, "~\n"); + fclose(index_file); + return 0; +} + +char *parsename(char *filename) { + static char copy[1024]; + strcpy(copy, filename); + char *extension = strchr(copy, '.'); + if (extension == NULL) { + return NULL; + } + if (strcmp(".plr", extension)) { + return NULL; + } + *extension = '\0'; + return copy; +} + +char *findLine(FILE *plr_file, char *tag) { + static char line[5000]; + rewind(plr_file); + + while (get_line(plr_file, line)) { + if(!strncmp(tag, line, strlen(tag))) { + return line+strlen(tag); + } + } + return NULL; +} + +long parseid(FILE *plr_file) { + return atol(findLine(plr_file, "Id :")); +} + +int parselevel(FILE *plr_file) { + return atoi(findLine(plr_file, "Levl:")); +} + +int parseadminlevel(FILE *plr_file, int level) { + char *fromFile = findLine(plr_file, "Admn:"); + if (fromFile != NULL) + return atoi(fromFile); + + if (level >= 30) + return level-30; + else + return 0; +} + +long parselast(FILE *plr_file) { + return atol(findLine(plr_file, "Last:")); +} + + +void walkdir(FILE *index_file, char *dir) { + char filename_qfd[1000] ; + struct dirent *dp; + DIR *dfd; + + if ((dfd = opendir(dir)) == NULL) + { + fprintf(stderr, "Can't open %s\n", dir); + return; + } + while ((dp = readdir(dfd)) != NULL) + { + struct stat stbuf ; + sprintf( filename_qfd , "%s/%s",dir,dp->d_name) ; + if( stat(filename_qfd,&stbuf ) == -1 ) { + fprintf(stdout, "Unable to stat file: %s\n",filename_qfd) ; + continue ; + } + + if ( ( stbuf.st_mode & S_IFMT ) == S_IFDIR ) { + if (!strcmp(".", dp->d_name) || !strcmp("..", dp->d_name)) + continue; + + walkdir(index_file, filename_qfd); + } else { + char *name = parsename(dp->d_name); + + if (name != NULL) { + FILE *plr_file = fopen(filename_qfd, "r"); + long id = parseid(plr_file); + + int level = parselevel(plr_file); + int adminlevel = parseadminlevel(plr_file, level); + if (level > 30) + level = 30; + long last = parselast(plr_file); + + fprintf(index_file, "%ld %s %d %d 0 %ld\n", id, name, level, adminlevel, last); + + fclose(plr_file); + } + } + } +} + +int get_line(FILE *fl, char *buf) +{ + char temp[READ_SIZE]; + int lines = 0; + int sl; + + do { + if (!fgets(temp, READ_SIZE, fl)) + return (0); + lines++; + } while (*temp == '*' || *temp == '\n' || *temp == '\r'); + + /* Last line of file doesn't always have a \n, but it should. */ + sl = strlen(temp); + while (sl > 0 && (temp[sl - 1] == '\n' || temp[sl - 1] == '\r')) + temp[--sl] = '\0'; + + strcpy(buf, temp); /* strcpy: OK, if buf >= READ_SIZE (256) */ + return (lines); +}