Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 206 lines (175 sloc) 4.954 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
// Dictionary.cpp: implementation of the CDictionary class.
//
//////////////////////////////////////////////////////////////////////

#include "uox3.h"
#include "Dictionary.h"
#include "speech.h"
#include "scriptc.h"
#include "ssection.h"

namespace UOX
{

CDictionaryContainer *Dictionary;

const SI32 dictCANTOPEN = -1;
const SI32 dictDUPESECTION = -2;
const SI32 dictDUPEOPENBRACE = -3;
const SI32 dictNOOPENBRACE = -4;


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDictionary::CDictionary() : IsValid( false )
{
Language = "ZRO";
PathToDictionary = "./dictionary.ZRO";
}

CDictionary::~CDictionary()
{
}

CDictionary::CDictionary( const std::string filepath, const std::string language )
{
CDictionary(); // Call default constructor for this Class

if( language.empty() )
Language = "ZRO";
else
Language = language;

// Load the path
if( filepath.empty() )
PathToDictionary = "dictionary.ZRO";
else
PathToDictionary = filepath;
}

const UI16 DICT_MAX_TEXTBUFFERSIZE = 1024;

SI32 CDictionary::LoadDictionary( void )
{
SI32 count = 0;
IsValid = false;
if( FileExists( PathToDictionary ) )
{
Script *dictData = new Script( PathToDictionary, NUM_DEFS, false );
if( dictData != NULL )
{
ScriptSection *dictSect = NULL;
UString tag, data;
for( dictSect = dictData->FirstEntry(); dictSect != NULL; dictSect = dictData->NextEntry() )
{
if( dictSect != NULL )
{
if( dictData->EntryName().substr( 0, 10 ) != "DICTIONARY" ) // verify it is a dictionary entry
continue;
for( tag = dictSect->First(); !dictSect->AtEnd(); tag = dictSect->Next() )
{
data = dictSect->GrabData();
Text2[tag.toULong()] = data.stripWhiteSpace();
++count;
}
}
}
delete dictData;
dictData = NULL;
}
IsValid = true;
Console.Print( " " );
Console.MoveTo( 15 );
Console << "Dictionary." << Language;
Console.PrintSpecial( CGREEN, "loaded" );
}
else
count = dictCANTOPEN;

return count;
}

void CDictionary::ShowList( void )
{
Console << "[Testing]" << myendl;
std::map< long, std::string >::const_iterator toFind = Text2.begin();
while( toFind != Text2.end() )
{
Console << (SI32)(toFind->first) << ") " << toFind->second << myendl;
++toFind;
}
}

std::string CDictionary::operator[]( SI32 Num )
{
return GetEntry( Num );
}

void CDictionary::SetValid( const bool newVal )
{
IsValid = newVal;
}

bool CDictionary::GetValid( void ) const
{
return IsValid;
}

size_t CDictionary::NumberOfEntries( void ) const
{
return Text2.size();
}

std::string CDictionary::GetEntry( const SI32 Num )
{
std::string rvalue;
if( IsValid )
{
std::map< long, std::string >::const_iterator toFind = Text2.find( Num );
if( toFind != Text2.end() )
rvalue = toFind->second;
else
Console.Warning( "Dictionary Reference %i not found in \"%s\"", Num, PathToDictionary.c_str() );
}
return rvalue;
}

CDictionaryContainer::CDictionaryContainer() : defaultLang( ZERO )
{
std::string buildName;
for( UI16 i = (UI16)DL_UNKNOWN; i < (UI16)DL_COUNT; ++i )
{
buildName = cwmWorldState->ServerData()->Directory( CSDDP_DICTIONARIES ) + "dictionary." + DistinctLanguageNames[i];
dictList[i] = new CDictionary( buildName, DistinctLanguageNames[i] );
}
}

CDictionaryContainer::CDictionaryContainer( const std::string filepath ) : defaultLang( ZERO )
{
std::string buildName;
for( UI16 i = (UI16)DL_UNKNOWN; i < (UI16)DL_COUNT; ++i )
{
buildName = filepath + "dictionary." + DistinctLanguageNames[i];
dictList[i] = new CDictionary( buildName, DistinctLanguageNames[i] );
}
}

CDictionaryContainer::~CDictionaryContainer()
{
for( UI16 i = (UI16)DL_UNKNOWN; i < (UI16)DL_COUNT; ++i )
delete dictList[i];
}

SI32 CDictionaryContainer::LoadDictionary( void )
{
SI32 rvalue = 0;
for( UI16 i = (UI16)DL_UNKNOWN; i < (UI16)DL_COUNT; ++i )
{
dictList[i]->LoadDictionary();
}
if( !dictList[LanguageCodesLang[ZERO]]->GetValid() )
{
Console.Error( "Dictionary.ZRO is bad or nonexistant" );
Shutdown( FATAL_UOX3_BAD_DEF_DICT );
rvalue = -1;
}
return rvalue;
}

std::string CDictionaryContainer::operator[]( const SI32 Num )
{
return GetEntry( Num );
}

std::string CDictionaryContainer::GetEntry( const SI32 Num, const UnicodeTypes toDisp )
{
std::string RetVal;
const DistinctLanguage mLanguage = LanguageCodesLang[toDisp];

if( mLanguage < DL_COUNT )
RetVal = dictList[mLanguage]->GetEntry( Num );
if( RetVal.empty() && toDisp != defaultLang )//if we are using a diff language, and the entry wasn't found, try the ZERO before we flat out return null
RetVal = dictList[LanguageCodesLang[defaultLang]]->GetEntry( Num );

return RetVal;
}

void CDictionaryContainer::SetDefaultLang( const UnicodeTypes newType )
{
if( dictList[LanguageCodesLang[newType]]->GetValid() )
        defaultLang = newType;
}

}
Something went wrong with that request. Please try again.