Files
pvvmud/common/lib/utils/namedb.C
2025-03-05 08:37:43 +01:00

159 lines
3.4 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* 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 the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "namedb.H"
#include "crossindex.H"
#include "exception.H"
CNameDBItem::CNameDBItem(int id,char *name) {
next=NULL;
this->id=id;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}
int CNameDB::insert(char * name) {
CNameDBItem *newItem,*currItem,*nextItem;
if (first == NULL){
first = new CNameDBItem(++currentId,name);
return currentId;
}
currItem = NULL;
nextItem = first;
while ((nextItem != NULL) && (strcmp(nextItem->getName(),name)<=0)) {
currItem = nextItem;
nextItem = currItem->getNext();
}
if ((currItem != NULL) && (strcmp(currItem->getName(),name)==0))
return currItem->getId();
newItem = new CNameDBItem(++currentId,name);
newItem->setNext(nextItem);
if (currItem == NULL) first = newItem;
else currItem->setNext(newItem);
return currentId;
}
int CNameDB::add(int id, char * name) {
//NameDBItem_t *newItem,*currItem,*nextItem;
CNameDBItem *newItem;
newItem = new CNameDBItem(id,name);
if (currentId < id ) currentId = id;
newItem->setNext(first);
first = newItem;
return id;
}
char * CNameDB::findName(int id){
CNameDBItem * currItem;
currItem = first;
while (currItem != NULL){
if (currItem->getId() == id) return currItem->getName();
currItem = currItem->getNext();
}
return NULL;
}
int CNameDB::findId(char * name){
CNameDBItem * currItem;
currItem = first;
while (currItem != NULL){
if (strcmp(currItem->getName(),name) == 0) return currItem->getId();
currItem = currItem->getNext();
}
return -1;
}
void CNameDB::save(char *fileName){
FILE * ndbFile;
ndbFile = fopen(fileName,"w+");
if (ndbFile == NULL) throw new CException();
writeNDB(ndbFile);
fclose(ndbFile);
}
void CNameDB::load(char *fileName){
FILE * ndbFile;
ndbFile = fopen(fileName,"r");
if (ndbFile == NULL) throw new CException();
readNDB(ndbFile);
fclose(ndbFile);
}
void CNameDB::writeNDB(FILE * ndbfile){
CNameDBItem * currItem;
currItem = first;
while(currItem != NULL){
fprintf(ndbfile,"%i : %s\n",currItem->getId(),currItem->getName());
currItem = currItem->getNext();
}
}
void CNameDB::readNDB(FILE * ndbfile) {
char name[256];
int id;
while (fscanf(ndbfile,"%i : %255s\n",&id,name)==2 ){
add(id,name);
}
}
CCrossIndex * CNameDB::merge(CNameDB * mergeDB){
CCrossIndex * crossIndex;
CNameDBItem * mergeItem;
crossIndex = new CCrossIndex;
mergeItem = mergeDB->getFirst();
while (mergeItem != NULL){
crossIndex->add(insert(mergeItem->getName()),mergeItem->getId());
mergeItem = mergeItem->getNext();
}
return crossIndex;
}