This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gutlgrp.cpp

268 lines
7.4 KiB
C++
Raw Normal View History

2000-02-25 10:15:17 +00:00
// hey, emacs, treat this like -*- C++ -*- file
// ------------------------------------------------------------------
// The Goldware Library.
// Copyright (C) 1990-1999 Odinn Sorensen
// Copyright (C) 2000 Alex. S. Aganichev
// ------------------------------------------------------------------
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Group/Random System.
// ------------------------------------------------------------------
#ifdef __GNUG__
#pragma implementation "gutlgrp.h"
#endif
#include <cstdlib>
#include <gmemdbg.h>
#include <string>
#include <gwildmat.h>
#include <gutlgrp.h>
// ------------------------------------------------------------------
// Constructor
Grp::Grp() {
currgrp = container.end();
currgrpno = -1;
}
// ------------------------------------------------------------------
// Destructors
Grp::~Grp() {
std::multimap<int, grp_stock>::iterator i;
2000-02-25 10:15:17 +00:00
for(currgrp = container.begin(); currgrp != container.end(); currgrp++)
for(i = currgrp->second.begin(); i != currgrp->second.end(); i++) {
2000-02-25 10:15:17 +00:00
if(i->second.type == TYPE_OBJECT)
throw_free(i->second.data.object_item);
else if(i->second.type == TYPE_STRING)
throw_delete(i->second.data.string_item);
}
}
// ------------------------------------------------------------------
// Add a new group
void Grp::AddGrp(const char* id) {
std::string sid(id);
std::multimap<int, grp_stock> m;
m.insert(std::pair<int, grp_stock>(GRP_MEMBER, sid));
container.push_back(std::pair<std::string, std::multimap<int, grp_stock> >(sid, m));
2000-02-25 10:15:17 +00:00
currgrp = container.end()-1;
currgrpno = container.size()-1;
}
// ------------------------------------------------------------------
// Add a new group member
void Grp::AddMbr(const char* id) {
currgrp->second.insert(std::pair<int, grp_stock>(GRP_MEMBER, std::string(id)));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
const char* Grp::SetGrp(const char* id) {
std::multimap<int, grp_stock>::iterator i;
2000-02-25 10:15:17 +00:00
for(currgrp = container.begin(), currgrpno = 0; currgrp != container.end(); currgrp++, currgrpno++)
for(i = currgrp->second.find(GRP_MEMBER); (i != currgrp->second.end()) and (i->first == GRP_MEMBER); i++)
if(strwild(id, i->second.data.string_item->c_str()))
return i->second.data.string_item->c_str();
currgrpno = -1;
return NULL;
}
// ------------------------------------------------------------------
void Grp::AddItm(int __type, bool __data) {
currgrp->second.insert(std::pair<int, grp_stock>(__type, __data));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
void Grp::AddItm(int __type, char __data) {
currgrp->second.insert(std::pair<int, grp_stock>(__type, __data));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
void Grp::AddItm(int __type, int __data) {
currgrp->second.insert(std::pair<int, grp_stock>(__type, __data));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
void Grp::AddItm(int __type, const std::string& __data) {
2000-02-25 10:15:17 +00:00
currgrp->second.insert(std::pair<int, grp_stock>(__type, __data));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
void Grp::AddItm(int __type, void* __data, int __size) {
2000-06-16 23:30:48 +00:00
void *data = throw_malloc(__size+sizeof(int));
*((int *)data) = __size;
memcpy((char *)data+sizeof(int), __data, __size);
currgrp->second.insert(std::pair<int, grp_stock>(__type, data));
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
int Grp::GetItm(int __type, bool& __data, int __no) {
// Return error if a current group is not set
if(currgrpno == -1)
return -1;
// Pointer to item type index for this group and type
int rv = currgrp->second.count(__type);
if(rv) {
if((__no >= rv) or (__no == -1))
__no = rand() % rv;
std::multimap<int, grp_stock>::iterator i = currgrp->second.find(__type);
2000-02-25 10:15:17 +00:00
while(__no--) i++;
__data = i->second.data.bool_item;
}
// Return number of items of this type in the group
return rv;
}
// ------------------------------------------------------------------
int Grp::GetItm(int __type, char& __data, int __no) {
// Return error if a current group is not set
if(currgrpno == -1)
return -1;
// Pointer to item type index for this group and type
int rv = currgrp->second.count(__type);
if(rv) {
if((__no >= rv) or (__no == -1))
__no = rand() % rv;
std::multimap<int, grp_stock>::iterator i = currgrp->second.find(__type);
2000-02-25 10:15:17 +00:00
while(__no--) i++;
__data = i->second.data.char_item;
}
// Return number of items of this type in the group
return rv;
}
// ------------------------------------------------------------------
int Grp::GetItm(int __type, int& __data, int __no) {
// Return error if a current group is not set
if(currgrpno == -1)
return -1;
// Pointer to item type index for this group and type
int rv = currgrp->second.count(__type);
if(rv) {
if((__no >= rv) or (__no == -1))
__no = rand() % rv;
std::multimap<int, grp_stock>::iterator i = currgrp->second.find(__type);
2000-02-25 10:15:17 +00:00
while(__no--) i++;
__data = i->second.data.int_item;
}
// Return number of items of this type in the group
return rv;
}
// ------------------------------------------------------------------
int Grp::GetItm(int __type, std::string& __data, int __no) {
2000-02-25 10:15:17 +00:00
// Return error if a current group is not set
if(currgrpno == -1)
return -1;
// Pointer to item type index for this group and type
int rv = currgrp->second.count(__type);
if(rv) {
if((__no >= rv) or (__no == -1))
__no = rand() % rv;
std::multimap<int, grp_stock>::iterator i = currgrp->second.find(__type);
2000-02-25 10:15:17 +00:00
while(__no--) i++;
__data = *(i->second.data.string_item);
}
// Return number of items of this type in the group
return rv;
}
// ------------------------------------------------------------------
int Grp::GetItm(int __type, void* __data, int __size, int __no) {
// Return error if a current group is not set
if(currgrpno == -1)
return -1;
// Pointer to item type index for this group and type
int rv = currgrp->second.count(__type);
if(rv) {
if((__no >= rv) or (__no == -1))
__no = rand() % rv;
std::multimap<int, grp_stock>::iterator i = currgrp->second.find(__type);
2000-02-25 10:15:17 +00:00
while(__no--) i++;
2000-06-16 23:30:48 +00:00
void *data = i->second.data.object_item;
int local_size = *((int *)data);
__size = minimum_of_two (local_size, __size);
memcpy(__data, (char *)data+sizeof(int), __size);
2000-02-25 10:15:17 +00:00
}
// Return number of items of this type in the group
return rv;
}
// ------------------------------------------------------------------