Пример #1
0
/* Subscribe returned */
int Array_PushBack(Array *a, void *Data, void *Boundary /* Only used by grow down array */)
{
	if( a -> Allocated >= 0 )
	{
		if( a -> Used == a -> Allocated )
		{
			int NewCount = (a -> Allocated) < 2 ? 2 : (a -> Allocated) + (a -> Allocated) / 2;

			if( SafeRealloc((void **)&(a -> Data), NewCount * (a -> DataLength)) != 0 )
				return -1;

			a -> Allocated = NewCount;
		}

		if( Data != NULL )
			memcpy((a -> Data) + (a -> DataLength) * (a -> Used), Data, a -> DataLength);

		return (a -> Used)++;

	} else {
		if( Boundary != NULL && ((a -> Data) + (-1) * (a -> DataLength) * (a -> Used)) < (char *)Boundary )
		{
			return -1;
		} else {
			if( Data != NULL )
			{
				memcpy((a -> Data) + (-1) * (a -> DataLength) * (a -> Used), Data, a -> DataLength);
			}
			return (a -> Used)++;
		}
	}
}
Пример #2
0
void PopulateEdgelist(EDGELIST *tree) {
	int level;
	POSITION parent, child;
	MOVELIST *childMoves;
	MOVE theMove;
	OPEN_POS_DATA pdata, cdata;
	int resizeCount = 0;
	int resizeLevelCount = 0;

	if(!kLoopy || !gUseOpen)
		level = 0;

	for(parent=0; parent < gNumberOfPositions; parent++) {
		if(GetValueOfPosition(parent) == undecided &&
		   Remoteness(parent) != REMOTENESS_MAX) {
			continue;
		}

		if(Primitive(parent) != undecided) {
			continue;
		} else {
			childMoves = GenerateMoves(parent);
		}

		while(1) {
			theMove = childMoves->move;
			child = DoMove(parent, theMove);

			if(kLoopy && gUseOpen) {
				pdata = GetOpenData(parent);
				cdata = GetOpenData(child);
				level = GetLevelNumber(pdata);
			}

			/* If the level is more than the number in tree, resize to handle more levels */

			if(level > (GameTree.NumberOfLevels - 1)) {
				resizeCount++;
				ResizeTree(&GameTree, level);
			}

			(((GameTree.edges)[level])[(GameTree.nextEdgeInLevel)[level]]).Parent = parent;
			(((GameTree.edges)[level])[(GameTree.nextEdgeInLevel)[level]]).Child = child;
			(GameTree.nextEdgeInLevel)[level] += 1;

			/* Resize an individual level if no room left */

			if(((GameTree.edgesPerLevel)[level] - 2) <= (GameTree.nextEdgeInLevel)[level]) {
				resizeLevelCount++;
				(GameTree.edges)[level] = SafeRealloc((GameTree.edges)[level], (GameTree.edgesPerLevel)[level] * 2 * sizeof(EDGE));
				(GameTree.edgesPerLevel)[level] = (GameTree.edgesPerLevel)[level] * 2;
			}

			if((childMoves = childMoves->next) == NULL) {
				break;
			}
		}
	}
	//printf("Levels resized: %d, tree resized: %d\n", resizeLevelCount, resizeCount);
}
Пример #3
0
void *Array_SetToSubscript(Array *a, int Subscript, void *Data)
{
	if( a -> Allocated >= 0 )
	{
		if( Subscript >= a -> Allocated )
		{
			if( SafeRealloc((void **)&(a -> Data), (Subscript + 1) * (a -> DataLength)) != 0 )
				return NULL;

			a -> Allocated = Subscript + 1;
		}

		memcpy((a -> Data) + (a -> DataLength) * Subscript, Data, a -> DataLength);

		if( a -> Used < Subscript + 1 )
			a -> Used = Subscript + 1;

		return (a -> Data) + (a -> DataLength) * Subscript;
	} else {
		if( a -> Used < Subscript + 1 )
		{
			a -> Used = Subscript + 1;
		}

		memcpy((a -> Data) + (-1) * (a -> DataLength) * Subscript, Data, a -> DataLength);

		return (a -> Data) + (-1) * (a -> DataLength) * Subscript;
	}
}
Пример #4
0
void ResizeTree(EDGELIST *tree, int newSize) {
	//printf("\nResizing tree\n");
	//printf("Current size: %d, new size: %d\n", tree->NumberOfLevels, newSize+1);
	int level;
	tree->edges = (EDGE **) SafeRealloc(tree->edges, (newSize+1) * sizeof(EDGE *));
	tree->edgesPerLevel = (POSITION *) SafeRealloc(tree->edgesPerLevel, (newSize+1) * sizeof(POSITION));
	tree->nextEdgeInLevel = (POSITION *) SafeRealloc(tree->nextEdgeInLevel, (newSize+1) * sizeof(POSITION));

	for(level = tree->NumberOfLevels; level < (newSize+1); level++) {
		tree->edges[level] = (EDGE *) SafeMalloc(INI_EDGES_PER_LEVEL*sizeof(EDGE));
		tree->edgesPerLevel[level] = INI_EDGES_PER_LEVEL;
		tree->nextEdgeInLevel[level] = 0;
	}

	tree->NumberOfLevels = newSize+1;
	//printf("Done resizing tree\n");
}
Пример #5
0
void UpdateRankList(EDGELIST *tree, POSITION node, REMOTENESS nodeRemoteness) {
	tree->nodeRanks[nodeRemoteness][tree->nextNodeInRank[nodeRemoteness]] = node;
	tree->nextNodeInRank[nodeRemoteness] += 1;

	if(tree->nextNodeInRank[nodeRemoteness] >= tree->nodesPerRank[nodeRemoteness]) {
		if(tree->nodesPerRank[nodeRemoteness] < 1000) {
			tree->nodeRanks[nodeRemoteness] = SafeRealloc(tree->nodeRanks[nodeRemoteness],1000*sizeof(POSITION));
			tree->nodesPerRank[nodeRemoteness] = 1000;
		} else {
			tree->nodeRanks[nodeRemoteness] = SafeRealloc(tree->nodeRanks[nodeRemoteness],tree->nodesPerRank[nodeRemoteness]*2*sizeof(POSITION));
			tree->nodesPerRank[nodeRemoteness] = tree->nodesPerRank[nodeRemoteness]*2;
		}
	}

	if(nodeRemoteness > tree->maxRank && nodeRemoteness < REMOTENESS_MAX) {
		tree->maxRank = nodeRemoteness;
	}
}
Пример #6
0
/* SafeStrcpy: Allocate or reallocate target to fit result; assumes ptr is NULL if not allocated */
char *SafeStrcpy (char **target, const char *source)
{

    *target = (char *) SafeRealloc ((void *)*target, (size_t)(strlen(source)+1)*sizeof(char));

    if (*target)
        strcpy(*target,source);

    return (*target);
}
Пример #7
0
/* SafeStrcat: Allocate or reallocate target to fit result; assumes ptr is NULL if not allocated */
char *SafeStrcat (char **target, const char *source)
{
    if (*target == NULL)
        *target = (char *) SafeCalloc (strlen(source)+1, sizeof(char));
    else
        *target = (char *) SafeRealloc ((void *)*target, (size_t)(strlen(source)+strlen(*target)+1)*sizeof(char));

    if (*target)
        strcat(*target,source);

    return (*target);
}
Пример #8
0
void RTS_AddFile (char *filename)
   {
   wadinfo_t  header;
   lumpinfo_t *lump_p;
   uint32     i;
   int32      handle, length;
   int32      startlump;
   filelump_t *fileinfo;

//
// read the entire file in
//      FIXME: shared opens

   handle = SafeOpenRead( filename, filetype_binary );

   startlump = numlumps;

   // WAD file
   printf("    Adding %s.\n",filename);
   SafeRead( handle, &header, sizeof( header ) );
   if (strncmp(header.identification,"IWAD",4))
      Error ("RTS file %s doesn't have IWAD id\n",filename);
   header.numlumps = IntelLong(header.numlumps);
   header.infotableofs = IntelLong(header.infotableofs);
   length = header.numlumps*sizeof(filelump_t);
   fileinfo = alloca (length);
   if (!fileinfo)
      Error ("RTS file could not allocate header info on stack");
   lseek (handle, header.infotableofs, SEEK_SET);
   SafeRead (handle, fileinfo, length);
   numlumps += header.numlumps;

//
// Fill in lumpinfo
//
   SafeRealloc(&lumpinfo,numlumps*sizeof(lumpinfo_t));
   lump_p = &lumpinfo[startlump];

   for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
      {
      lump_p->handle = handle;
      lump_p->position = IntelLong(fileinfo->filepos);
      lump_p->size = IntelLong(fileinfo->size);
      strncpy (lump_p->name, fileinfo->name, 8);
      }
   }
Пример #9
0
/* AddBitfield: Add bitfield to list of bitfields */
int AddBitfield (SafeLong ***list, int listLen, int *set, int setLen)
{
    int     i, nLongsNeeded;

    nLongsNeeded = (setLen - 1) / nBitsInALong + 1;

    (*list) = (SafeLong **) SafeRealloc ((void *)(*list), (size_t)((listLen+1)*sizeof(SafeLong *)));
    if (!(*list))
        return ERROR;
    
    (*list)[listLen] = (SafeLong *) SafeMalloc ((size_t)(nLongsNeeded*sizeof(SafeLong)));
    if (!(*list)[listLen])
        return ERROR;

    ClearBits ((*list)[listLen], nLongsNeeded);
    for (i=0; i<setLen; i++)
        if (set[i] == YES)
            SetBit(i, (*list)[listLen]);

    return NO_ERROR;
}
Пример #10
0
int ConfigAddOption(ConfigFileInfo *Info, char *KeyName, MultilineStrategy Strategy, OptionType Type, VType Initial, char *Caption)
{
	int loop;

	if( strlen(KeyName) > sizeof(Info -> Options -> KeyName) - 1 )
	{
		return -1;
	}

	for(loop = 0; loop != Info -> NumOfOptions; ++loop)
	{
		if(Info -> Options[loop].Status == STATUS_UNUSED)
			break;
	}

	if(loop == Info -> NumOfOptions)
	{
		int loop2;

		if( SafeRealloc((void *)&(Info -> Options), (Info -> NumOfOptions + 10) * sizeof(ConfigOption)) != 0)
		{
			return 1;
		}

		(Info -> NumOfOptions) += 10;

		for(loop2 = loop; loop2 != Info -> NumOfOptions; ++loop2)
			Info -> Options[loop2].Status = STATUS_UNUSED;
	}

	strcpy(Info -> Options[loop].KeyName, KeyName);
	Info -> Options[loop].Type = Type;
	Info -> Options[loop].Status = STATUS_DEFAULT_VALUE;
	if( Caption != NULL )
	{
		strncpy(Info -> Options[loop].Caption, Caption, CAPTION_MAX_SIZE);
		Info -> Options[loop].Caption[CAPTION_MAX_SIZE] = '\0';
	} else {
		*(Info -> Options[loop].Caption) = '\0';
	}

	switch( Type )
	{
		case TYPE_INT32:
		case TYPE_BOOLEAN:
			Info -> Options[loop].Holder = Initial;
			break;

		case TYPE_STRING:
			if(Initial.str != NULL)
			{
				Info -> Options[loop].Holder.str = SafeMalloc(strlen(Initial.str) + 1);
				strcpy(Info -> Options[loop].Holder.str, Initial.str);
			} else {
				Info -> Options[loop].Holder.str = NULL;
			}
			Info -> Options[loop].Strategy = Strategy;
			break;

		default:
			break;
	}

	return 0;
}
Пример #11
0
int ConfigRead(ConfigFileInfo *Info)
{
	int				NumOfRead	=	0;

	char			Buffer[3072];
	char			*ValuePos;
	ReadLineStatus	ReadStatus;

	char			KeyName[KEY_NAME_MAX_SIZE + 1];
	ConfigOption	*Option;

	while(TRUE){
		ReadStatus = ReadLine(Info -> fp, Buffer, sizeof(Buffer));
		if( ReadStatus == READ_FAILED_OR_END )
			return NumOfRead;

		if( GetKeyNameFromLine(Buffer, KeyName) == NULL )
			continue;

		Option = GetOptionOfAInfo(Info, KeyName);
		if( Option == NULL )
			continue;

		ValuePos = (char *)GetValuePosition(Buffer);
		if( ValuePos == NULL )
			continue;

		switch( Option -> Type )
		{
			case TYPE_INT32:
				switch (Option -> Strategy)
				{
					case STRATEGY_APPEND_DISCARD_DEFAULT:
						if( Option -> Status == STATUS_DEFAULT_VALUE )
						{
							Option -> Strategy = STRATEGY_APPEND;
						}
						/* No break */

					case STRATEGY_DEFAULT:
					case STRATEGY_REPLACE:
						sscanf(ValuePos, "%d", &(Option -> Holder.INT32));
						Option -> Status = STATUS_SPECIAL_VALUE;
						break;

					case STRATEGY_APPEND:
						{
							_32BIT_INT SpecifiedValue;

							sscanf(ValuePos, "%d", &SpecifiedValue);
							Option -> Holder.INT32 += SpecifiedValue;

							Option -> Status = STATUS_SPECIAL_VALUE;
						}
						break;

					default:
						continue;
						break;
				}
				break;

			case TYPE_BOOLEAN:
				switch (Option -> Strategy)
				{
					case STRATEGY_APPEND_DISCARD_DEFAULT:
						if( Option -> Status == STATUS_DEFAULT_VALUE )
						{
							Option -> Strategy = STRATEGY_APPEND;
						}
						/* No break */

					case STRATEGY_DEFAULT:
					case STRATEGY_REPLACE:

						Option -> Holder.boolean = GetBoolealValueFromString(ValuePos);

						Option -> Status = STATUS_SPECIAL_VALUE;
						break;

					case STRATEGY_APPEND:
						{
							BOOL SpecifiedValue;

							SpecifiedValue = GetBoolealValueFromString(ValuePos);
							Option -> Holder.boolean |= SpecifiedValue;

							Option -> Status = STATUS_SPECIAL_VALUE;
						}
						break;

						default:
							continue;
							break;

				}
				break;

			case TYPE_STRING:
				{
					char *result;

					switch (Option -> Strategy)
					{
						case STRATEGY_APPEND_DISCARD_DEFAULT:
							if( Option -> Status == STATUS_DEFAULT_VALUE )
							{
								Option -> Strategy = STRATEGY_APPEND;
							}
							/* No break */

						case STRATEGY_DEFAULT:
						case STRATEGY_REPLACE:
							if( Option -> Holder.str != NULL )
							{
								SafeFree(Option -> Holder.str);
							}

							result = SafeMalloc(strlen(ValuePos) + 1);
							if( result == NULL )
							{
								continue;
							}

							strcpy(result, ValuePos);
							Option -> Status = STATUS_SPECIAL_VALUE;
							break;

						case STRATEGY_APPEND:
							if( Option -> Holder.str != NULL )
							{
								result = SafeMalloc(strlen(Option -> Holder.str) + strlen(ValuePos) + 2);
								if( result == NULL )
								{
									continue;
								}
								strcpy(result, Option -> Holder.str);
								strcat(result, ",");
								strcat(result, ValuePos);
								SafeFree(Option -> Holder.str);
							} else {
								result = SafeMalloc(strlen(ValuePos) + 1);
								if( result == NULL )
								{
									continue;
								}
								strcpy(result, ValuePos);
							}
							Option -> Status = STATUS_SPECIAL_VALUE;
							break;

						default:
							continue;
							break;
					}

					while( ReadStatus != READ_DONE ){

						ReadStatus = ReadLine(Info -> fp, Buffer, sizeof(Buffer));
						if( ReadStatus == READ_FAILED_OR_END )
							break;
						if( SafeRealloc((void *)&result, strlen(result) + strlen(Buffer) + 1) != 0 )
							break;
						strcat(result, Buffer);
					}

					if( strlen(result) != 0 )
					{
						int loop = strlen(result) - 1;

						while( result[loop] == '\n' || result[loop] == '\r' )
						{
							result[loop] = '\0';
							--loop;
						}
					}

					Option -> Holder.str = result;
				}
				break;

			default:
				break;
		}
		++NumOfRead;
	}
	return NumOfRead;
}
Пример #12
0
/*
 * AddVerbAlias:  Set up given alias to given command verb.
 */
BOOL AddVerbAlias(char* pszVerb, char* pszCommand)
{
   VerbAlias* pAlias = NULL;
   int i;

   if (!_apVerbAliases)
   {
      _nVerbAliases = 0;
      _nAllocated = CHUNKSIZE;
      _apVerbAliases = (VerbAlias *) SafeMalloc(_nAllocated * sizeof(VerbAlias));
      memset(_apVerbAliases, 0, _nAllocated * sizeof(VerbAlias));
   }

   if (!pszVerb || !*pszVerb)
      return FALSE;

   // Unique verbs only.
   RemoveVerbAlias(pszVerb);

   // Find a free structure if there is one.
   //
   pAlias = NULL;
   for (i = 0; i < _nVerbAliases; i++)
   {
      if (!_apVerbAliases[i].verb[0])
      {
	 pAlias = &_apVerbAliases[i];
	 break;
      }
   }
   if (!pAlias)
   {
      if (_nVerbAliases < _nAllocated)
      {
	 pAlias = &_apVerbAliases[_nVerbAliases];
	 _nVerbAliases++;
      }
   }

   // SafeReallocate another CHUNKSIZE verb structures if necessary.
   //
   if (!pAlias)
   {
      pAlias = (VerbAlias *)
         SafeRealloc(_apVerbAliases, (_nAllocated+CHUNKSIZE)*sizeof(VerbAlias));
      if (pAlias)
      {
         _apVerbAliases = pAlias;
         
         for (i = _nAllocated; i < _nAllocated+CHUNKSIZE; i++)
            memset(&_apVerbAliases[i], 0, sizeof(*_apVerbAliases));
         pAlias = _apVerbAliases + _nVerbAliases;
         
         _nAllocated += CHUNKSIZE;
         _nVerbAliases++;
      }
   }
   if (!pAlias)
      return FALSE;
   
   // Prepare the verb/pattern structure.
   //
   strncpy(pAlias->verb, pszVerb, MAX_VERBLEN);
   pAlias->verb[MAX_VERBLEN] = '\0';

   strncpy(pAlias->text, pszCommand, MAX_ALIASLEN);
   pAlias->text[MAX_ALIASLEN] = '\0';

   return TRUE;
}