2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2011-02-25 09:45:27 -08:00
|
|
|
Copyright (C) 2005-2011 Warzone 2100 Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 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.
|
|
|
|
|
|
|
|
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
/*
|
|
|
|
* StrRes.c
|
|
|
|
*
|
|
|
|
* String storage an manipulation functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2007-04-15 11:48:13 -07:00
|
|
|
#include <stdlib.h>
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Allow frame header files to be singly included */
|
|
|
|
#define FRAME_LIB_INCLUDE
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "treap.h"
|
|
|
|
#include "strres.h"
|
|
|
|
#include "strresly.h"
|
|
|
|
|
2008-07-22 15:36:23 -07:00
|
|
|
/* A String Resource */
|
2011-02-25 12:30:13 -08:00
|
|
|
struct STR_RES
|
2008-07-22 15:36:23 -07:00
|
|
|
{
|
|
|
|
struct TREAP_NODE** psIDTreap; ///< The treap to store string identifiers
|
2011-02-25 12:30:13 -08:00
|
|
|
};
|
2008-07-22 15:36:23 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
/* Initialise the string system */
|
2008-07-24 11:57:09 -07:00
|
|
|
STR_RES* strresCreate()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-07-22 15:33:10 -07:00
|
|
|
STR_RES* const psRes = (STR_RES*)malloc(sizeof(*psRes));
|
2007-06-28 10:47:08 -07:00
|
|
|
if (!psRes)
|
|
|
|
{
|
2009-10-30 20:57:44 -07:00
|
|
|
debug(LOG_FATAL, "Out of memory");
|
2006-08-22 07:28:49 -07:00
|
|
|
abort();
|
2008-07-22 15:33:10 -07:00
|
|
|
return NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-07-20 17:23:36 -07:00
|
|
|
psRes->psIDTreap = treapCreate();
|
|
|
|
if (!psRes->psIDTreap)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-10-30 20:57:44 -07:00
|
|
|
debug(LOG_FATAL, "Out of memory");
|
2006-08-22 07:28:49 -07:00
|
|
|
abort();
|
2007-04-15 03:43:05 -07:00
|
|
|
free(psRes);
|
2008-07-22 15:33:10 -07:00
|
|
|
return NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-07-22 15:33:10 -07:00
|
|
|
return psRes;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Shutdown the string system */
|
|
|
|
void strresDestroy(STR_RES *psRes)
|
|
|
|
{
|
|
|
|
// Release the treap and free the final memory
|
2008-07-14 13:22:19 -07:00
|
|
|
treapDestroy(psRes->psIDTreap);
|
2007-04-15 03:43:05 -07:00
|
|
|
free(psRes);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Store a string */
|
2010-02-14 13:31:09 -08:00
|
|
|
bool strresStoreString(STR_RES *psRes, const char* pID, const char* pString)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-07-25 13:27:54 -07:00
|
|
|
ASSERT(psRes != NULL, "Invalid string res pointer");
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-07-25 13:27:54 -07:00
|
|
|
// Make sure that this ID string hasn't been used before
|
|
|
|
if (treapFind(psRes->psIDTreap, pID) != NULL)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-10-30 20:57:44 -07:00
|
|
|
debug(LOG_FATAL, "Duplicate string for id: \"%s\"", pID);
|
2008-07-25 13:27:54 -07:00
|
|
|
abort();
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-07-24 11:57:09 -07:00
|
|
|
|
2008-07-27 16:58:49 -07:00
|
|
|
return treapAdd(psRes->psIDTreap, pID, pString);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-07-27 16:58:49 -07:00
|
|
|
const char* strresGetString(const STR_RES* psRes, const char* ID)
|
2008-07-24 13:10:16 -07:00
|
|
|
{
|
2008-07-25 13:27:54 -07:00
|
|
|
const char * string;
|
2008-07-24 13:10:16 -07:00
|
|
|
|
|
|
|
ASSERT(psRes != NULL, "Invalid string resource pointer");
|
|
|
|
|
2008-07-25 13:27:54 -07:00
|
|
|
string = treapFind(psRes->psIDTreap, ID);
|
2008-08-07 08:40:21 -07:00
|
|
|
ASSERT(string, "Could not find string for id \"%s\"", ID);
|
2008-07-24 13:10:16 -07:00
|
|
|
|
2008-07-25 13:27:54 -07:00
|
|
|
return string;
|
2008-07-24 13:10:16 -07:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Load a string resource file */
|
2010-02-14 13:31:09 -08:00
|
|
|
bool strresLoad(STR_RES* psRes, const char* fileName)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-05-12 11:35:36 -07:00
|
|
|
bool retval;
|
2008-05-12 11:22:05 -07:00
|
|
|
lexerinput_t input;
|
2008-05-12 11:16:30 -07:00
|
|
|
|
2008-05-12 11:22:05 -07:00
|
|
|
input.type = LEXINPUT_PHYSFS;
|
|
|
|
input.input.physfsfile = PHYSFS_openRead(fileName);
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Reading...[directory %s] %s", PHYSFS_getRealDir(fileName), fileName);
|
2008-05-12 11:22:05 -07:00
|
|
|
if (!input.input.physfsfile)
|
2007-05-29 11:12:02 -07:00
|
|
|
{
|
2007-06-03 06:46:50 -07:00
|
|
|
debug(LOG_ERROR, "strresLoadFile: PHYSFS_openRead(\"%s\") failed with error: %s\n", fileName, PHYSFS_getLastError());
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-05-29 11:12:02 -07:00
|
|
|
}
|
|
|
|
|
2008-05-12 11:22:05 -07:00
|
|
|
strres_set_extra(&input);
|
2008-07-24 07:16:27 -07:00
|
|
|
retval = (strres_parse(psRes) == 0);
|
2008-05-12 11:16:30 -07:00
|
|
|
|
2008-03-26 05:49:51 -07:00
|
|
|
strres_lex_destroy();
|
2008-05-12 11:22:05 -07:00
|
|
|
PHYSFS_close(input.input.physfsfile);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-05-12 11:16:30 -07:00
|
|
|
return retval;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the ID number for a string*/
|
2008-07-27 16:58:49 -07:00
|
|
|
const char* strresGetIDfromString(STR_RES *psRes, const char *pString)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-07-25 13:27:54 -07:00
|
|
|
ASSERT(psRes != NULL, "Invalid string res pointer");
|
2008-07-24 11:57:09 -07:00
|
|
|
|
2008-07-27 16:58:49 -07:00
|
|
|
return treapFindKey(psRes->psIDTreap, pString);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|