src/helpers.cpp
author Darkvater
Sun, 18 Feb 2007 22:50:51 +0000
changeset 6400 7b98825c5cb0
parent 5884 be0c8467aeb4
child 6285 187e3ef04cc9
permissions -rw-r--r--
(svn r8807) -Feature: Add support for loading (some of) the TTDP extra chunks mainly list of GRFIDs and TTDP version the game was saved with.
GRFID list is read and active GRFs are loaded with the game if present in the data/ directory.
TTDP versioning information only works for newer versions, old versions will only give rough information but it's not worth the effort to precisely figure out what bit means what where and when.
/* $Id$ */
#include "stdafx.h"

#include "openttd.h"
#include "engine.h"

#include <new>
#include "misc/blob.hpp"

/* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */

/* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
#define B (*(CBlobT<EngineID>*)el)

/** Create Engine List (and initialize it to empty) */
void EngList_Create(EngineList *el)
{
	// call CBlobT constructor explicitly
	new (&B) CBlobT<EngineID>();
}

/** Destroy Engine List (and free its contents) */
void EngList_Destroy(EngineList *el)
{
	// call CBlobT destructor explicitly
	B.~CBlobT<EngineID>();
}

/** Return number of items stored in the Engine List */
uint EngList_Count(const EngineList *el)
{
	return B.Size();
}

/** Add new item at the end of Engine List */
void EngList_Add(EngineList *el, EngineID eid)
{
	B.Append(eid);
}

/** Return pointer to the items array held by Engine List */
EngineID* EngList_Items(EngineList *el)
{
	return B.Data();
}

/** Clear the Engine List (by invalidating all its items == reseting item count to zero) */
void EngList_RemoveAll(EngineList *el)
{
	B.Clear();
}

/** Sort all items using qsort() and given 'CompareItems' function */
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
{
	qsort(B.Data(), B.Size(), sizeof(**el), compare);
}

/** Sort selected range of items (on indices @ <begin, begin+num_items-1>) */
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
{
	assert(begin <= (uint)B.Size());
	assert(begin + num_items <= (uint)B.Size());
	qsort(B.Data() + begin, num_items, sizeof(**el), compare);
}

#undef B