2000-05-19 04:33:12 +00:00
|
|
|
/*
|
|
|
|
cl_slist.c
|
|
|
|
|
|
|
|
serverlist addressbook
|
|
|
|
|
2000-05-22 06:46:35 +00:00
|
|
|
Copyright (C) 2000 Brian Koropoff <brian.hk@home.com>
|
2000-05-19 04:33:12 +00:00
|
|
|
|
|
|
|
Author: Brian Koropoff
|
|
|
|
Date: 03 May 2000
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
2000-05-21 08:24:45 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
#include "cl_slist.h"
|
|
|
|
#include "bothdefs.h"
|
|
|
|
#include "console.h"
|
2000-05-21 20:14:09 +00:00
|
|
|
#include "commdef.h"
|
|
|
|
#include "zone.h"
|
2000-05-22 01:37:19 +00:00
|
|
|
#include "quakefs.h"
|
2000-05-19 04:33:12 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2000-05-21 20:14:09 +00:00
|
|
|
server_entry_t *slist;
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
server_entry_t *SL_Add (server_entry_t *start, char *ip, char *desc) {
|
2000-05-21 20:14:09 +00:00
|
|
|
server_entry_t *p;
|
|
|
|
p = start;
|
|
|
|
if (!start) { //Nothing at beginning of list, create it
|
|
|
|
start = Z_Malloc(sizeof(server_entry_t));
|
|
|
|
start->prev = 0;
|
|
|
|
start->next = 0;
|
|
|
|
start->server = Z_Malloc(strlen(ip) + 1);
|
|
|
|
start->desc = Z_Malloc(strlen(desc) + 1);
|
|
|
|
strcpy(start->server,ip);
|
|
|
|
strcpy(start->desc,desc);
|
|
|
|
return (start);
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-21 20:14:09 +00:00
|
|
|
for(p=start;p->next;p=p->next); //Get to end of list
|
2000-05-19 04:33:12 +00:00
|
|
|
|
2000-05-21 20:14:09 +00:00
|
|
|
p->next = Z_Malloc(sizeof(server_entry_t));
|
|
|
|
p->next->prev = p;
|
|
|
|
p->next->server = Z_Malloc(strlen(ip) + 1);
|
|
|
|
p->next->desc = Z_Malloc(strlen(desc) + 1);
|
|
|
|
|
|
|
|
strcpy(p->next->server,ip);
|
|
|
|
strcpy(p->next->desc,desc);
|
|
|
|
p->next->next = 0;
|
|
|
|
|
|
|
|
return (start);
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
server_entry_t *SL_Del(server_entry_t *start, server_entry_t *del) {
|
2000-05-21 20:14:09 +00:00
|
|
|
server_entry_t *n;
|
|
|
|
if (del == start) {
|
|
|
|
Z_Free(start->server);
|
|
|
|
Z_Free(start->desc);
|
|
|
|
n = start->next;
|
|
|
|
if (n)
|
|
|
|
n->prev = 0;
|
|
|
|
Z_Free(start);
|
|
|
|
return (n);
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
2000-05-21 20:14:09 +00:00
|
|
|
|
|
|
|
Z_Free(del->server);
|
|
|
|
Z_Free(del->desc);
|
|
|
|
if (del->prev)
|
|
|
|
del->prev->next = del->next;
|
|
|
|
if (del->next)
|
|
|
|
del->next->prev = del->prev;
|
|
|
|
Z_Free(del);
|
|
|
|
return (start);
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
server_entry_t *SL_InsB (server_entry_t *start, server_entry_t *place, char *ip, char *desc) {
|
2000-05-21 20:14:09 +00:00
|
|
|
server_entry_t *new;
|
|
|
|
server_entry_t *other;
|
|
|
|
|
|
|
|
new = Z_Malloc(sizeof(server_entry_t));
|
|
|
|
new->server = Z_Malloc(strlen(ip) + 1);
|
|
|
|
new->desc = Z_Malloc(strlen(desc) + 1);
|
|
|
|
strcpy(new->server,ip);
|
|
|
|
strcpy(new->desc,desc);
|
|
|
|
other = place->prev;
|
|
|
|
if (other)
|
|
|
|
other->next = new;
|
|
|
|
place->prev = new;
|
|
|
|
new->next = place;
|
|
|
|
new->prev = other;
|
|
|
|
if (!other)
|
|
|
|
return new;
|
|
|
|
return start;
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
void SL_Swap (server_entry_t *swap1, server_entry_t *swap2) {
|
2000-05-21 20:14:09 +00:00
|
|
|
char *p;
|
|
|
|
p = swap1->server;
|
|
|
|
swap1->server = swap2->server;
|
|
|
|
swap2->server = p;
|
|
|
|
p = swap1->desc;
|
|
|
|
swap1->desc = swap2->desc;
|
|
|
|
swap2->desc = p;
|
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
server_entry_t *SL_Get_By_Num (server_entry_t *start, int n) {
|
2000-05-19 04:33:12 +00:00
|
|
|
int i;
|
2000-05-21 20:14:09 +00:00
|
|
|
for (i=0;i < n;i++)
|
|
|
|
start = start->next;
|
2000-05-25 01:38:52 +00:00
|
|
|
if (!start)
|
|
|
|
return (0);
|
2000-05-21 20:14:09 +00:00
|
|
|
return (start);
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
2000-05-21 20:14:09 +00:00
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
int SL_Len (server_entry_t *start) {
|
2000-05-19 04:33:12 +00:00
|
|
|
int i;
|
2000-05-21 20:14:09 +00:00
|
|
|
for (i=0;start;i++)
|
|
|
|
start=start->next;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
server_entry_t *SL_LoadF (FILE *f,server_entry_t *start) { // This could get messy
|
2000-05-21 20:14:09 +00:00
|
|
|
char line[256]; /* Long lines get truncated. */
|
|
|
|
int c = ' '; /* int so it can be compared to EOF properly*/
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
char *st;
|
|
|
|
char *addr;
|
|
|
|
|
|
|
|
while (1) {
|
2000-05-19 04:33:12 +00:00
|
|
|
//First, get a line
|
|
|
|
i = 0;
|
|
|
|
c = ' ';
|
|
|
|
while (c != '\n' && c != EOF) {
|
2000-05-23 22:43:36 +00:00
|
|
|
c = getc(f);
|
2000-05-19 04:33:12 +00:00
|
|
|
if (i < 255) {
|
|
|
|
line[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line[i - 1] = '\0'; // Now we can parse it
|
2000-05-21 20:14:09 +00:00
|
|
|
if ((st = gettokstart(line,1,' ')) != NULL) {
|
|
|
|
len = gettoklen(line,1,' ');
|
|
|
|
addr = Z_Malloc(len + 1);
|
|
|
|
strncpy(addr,&line[0],len);
|
|
|
|
addr[len] = '\0';
|
|
|
|
if ((st = gettokstart(line,2,' '))) {
|
2000-05-25 01:38:52 +00:00
|
|
|
start = SL_Add(start,addr,st);
|
2000-05-21 20:14:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2000-05-25 01:38:52 +00:00
|
|
|
start = SL_Add(start,addr,"Unknown");
|
2000-05-21 20:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == EOF) // We're done
|
|
|
|
return start;
|
|
|
|
}
|
2000-05-19 04:33:12 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
void SL_SaveF (FILE *f,server_entry_t *start) {
|
2000-05-21 20:14:09 +00:00
|
|
|
do {
|
2000-05-23 22:43:36 +00:00
|
|
|
fprintf(f,"%s %s\n",start->server,start->desc);
|
2000-05-21 20:14:09 +00:00
|
|
|
start = start->next;
|
|
|
|
|
|
|
|
} while (start);
|
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
void SL_Shutdown (server_entry_t *start) {
|
2000-05-24 22:36:59 +00:00
|
|
|
FILE *f;
|
|
|
|
char e_path[MAX_OSPATH];
|
|
|
|
|
2000-05-21 20:14:09 +00:00
|
|
|
if (start) {
|
2000-05-24 22:36:59 +00:00
|
|
|
Qexpand_squiggle(fs_userpath->string, e_path);
|
|
|
|
if ((f = fopen(va("%s/servers.txt", e_path),"w"))) {
|
2000-05-25 01:38:52 +00:00
|
|
|
SL_SaveF(f,start);
|
2000-05-23 22:43:36 +00:00
|
|
|
fclose(f);
|
2000-05-21 20:14:09 +00:00
|
|
|
}
|
2000-05-25 01:38:52 +00:00
|
|
|
SL_Del_All (start);
|
2000-05-21 20:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-25 01:38:52 +00:00
|
|
|
void SL_Del_All (server_entry_t *start) {
|
2000-05-21 20:14:09 +00:00
|
|
|
server_entry_t *n;
|
|
|
|
while (start) {
|
|
|
|
n = start->next;
|
|
|
|
Z_Free(start->server);
|
|
|
|
Z_Free(start->desc);
|
|
|
|
Z_Free(start);
|
|
|
|
start = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-19 04:33:12 +00:00
|
|
|
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;
|
|
|
|
}
|