From 4cae141a9c22d6dc7933cd293923ba703a6132d5 Mon Sep 17 00:00:00 2001 From: Dan Olson Date: Thu, 4 May 2000 01:01:57 +0000 Subject: [PATCH] Umm... someone else that was committing this patch forgot to add these... :) --- common/cl_slist.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++ common/cl_slist.h | 17 ++++++ 2 files changed, 149 insertions(+) create mode 100644 common/cl_slist.c create mode 100644 common/cl_slist.h diff --git a/common/cl_slist.c b/common/cl_slist.c new file mode 100644 index 0000000..849af3c --- /dev/null +++ b/common/cl_slist.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include + +//Better watch out for buffer overflows +server_entry_t slist[MAX_SERVER_LIST]; + +void Server_List_Init(void) { // Do this or everything else will sig11 + int i; + + for(i=0;i < MAX_SERVER_LIST;i++) { + slist[i].server = '\0'; + slist[i].description = '\0'; + slist[i].ping = 0; + } +} + + +void Server_List_Shutdown(void) { // I am the liberator of memory. + int i; + + for(i=0;i < MAX_SERVER_LIST;i++) { + if (slist[i].server) + free(slist[i].server); + if (slist[i].description) + free(slist[i].description); + } +} + + +int Server_List_Set(int i,char *addr,char *desc) { + int len; + if (i < MAX_SERVER_LIST && i >= 0) { + if (slist[i].server) // (Re)allocate memory first + free(slist[i].server); + if (slist[i].description) + free(slist[i].description); + len = strlen(desc); + slist[i].server = malloc(strlen(addr) + 1); + slist[i].description = malloc(len + 1); + strcpy(slist[i].server,addr); + strncpy(slist[i].description,desc,len); + slist[i].description[len] = '\0'; //In case it got cut off + return 0; // Yay, we haven't segfaulted yet. + } + return 1; // Out of range +} + +int Server_List_Load (QFile *f) { // This could get messy + int serv = 0; + char line[256]; // Long lines get truncated. + char c = ' '; + char *start; + int len; + int i; + char *addr; + + // Init again to clear the list + Server_List_Shutdown(); + Server_List_Init(); + while (serv < MAX_SERVER_LIST) { + //First, get a line + i = 0; + c = ' '; + while (c != '\n' && c != EOF) { + c = Qgetc(f); + if (i < 255) { + line[i] = c; + i++; + } + } + line[i - 1] = '\0'; // Now we can parse it + if ((start = gettokstart(line,1,' ')) != NULL) { + len = gettoklen(line,1,' '); + addr = malloc(len + 1); + strncpy(addr,&line[0],len); + addr[len] = '\0'; + if ((start = gettokstart(line,2,' '))) { + Server_List_Set(serv,addr,start); + } + else { + Server_List_Set(serv,addr,"Unknown"); + } + serv++; + } + if (c == EOF) // We're done + return 0; + } + return 0; //I'd love to see the day when there are enough servers +} //for it to break out of the first while loop + +char *gettokstart (char *str, int req, char delim) { + char *start = str; + + int tok = 1; + + while (*start == delim) { + start++; + } + if (*start == '\0') + return '\0'; + while (tok < req) { //Stop when we get to the requested token + if (*++start == delim) { //Increment pointer and test + while (*start == delim) { //Get to next token + start++; + } + tok++; + } + if (*start == '\0') { + return '\0'; + } + } + return start; +} + +int gettoklen (char *str, int req, char delim) { + char *start = 0; + + int len = 0; + + start = gettokstart(str,req,delim); + if (start == '\0') { + return 0; + } + while (*start != delim && *start != '\0') { + start++; + len++; + } + return len; +} diff --git a/common/cl_slist.h b/common/cl_slist.h new file mode 100644 index 0000000..abdd54c --- /dev/null +++ b/common/cl_slist.h @@ -0,0 +1,17 @@ +#include +#define MAX_SERVER_LIST 256 + +typedef struct { + char *server; + char *description; + int ping; +} server_entry_t; + +extern server_entry_t slist[MAX_SERVER_LIST]; + +void Server_List_Init(void); +void Server_List_Shutdown(void); +int Server_List_Set(int i,char *addr,char *desc); +int Server_List_Load(QFile *f); +char *gettokstart (char *str, int req, char delim); +int gettoklen(char *str, int req, char delim);