| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "ResDialogPack.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Dialog.h" |
| 13 |
#include "OptionAgent.h" |
| 14 |
#include "minmax.h" |
| 15 |
|
| 16 |
//----------------------------------------------------------------- |
| 17 |
void |
| 18 |
ResDialogPack::unloadRes(Dialog *res) |
| 19 |
{ |
| 20 |
delete res; |
| 21 |
} |
| 22 |
//----------------------------------------------------------------- |
| 23 |
/** |
| 24 |
* Return match score. |
| 25 |
* One string must excatly be starting substring of the other. |
| 26 |
*/ |
| 27 |
int |
| 28 |
ResDialogPack::matchScore(const std::string &first, |
| 29 |
const std::string &second) const |
| 30 |
{ |
| 31 |
int score = 0; |
| 32 |
int minSize = min(first.size(), second.size()); |
| 33 |
for (int i = 0; i < minSize; ++i) { |
| 34 |
if (first[i] == second[i]) { |
| 35 |
score++; |
| 36 |
} |
| 37 |
else { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
} |
| 41 |
return score; |
| 42 |
} |
| 43 |
//----------------------------------------------------------------- |
| 44 |
/** |
| 45 |
* Return dialog or NULL. |
| 46 |
* Compare dialog names and lang codes. |
| 47 |
* The best lang match is selected, at least two characters must equals. |
| 48 |
*/ |
| 49 |
const Dialog * |
| 50 |
ResDialogPack::findDialog(const std::string &name, |
| 51 |
const std::string &lang) |
| 52 |
{ |
| 53 |
int bestScore = 0; |
| 54 |
Dialog *bestDialog = NULL; |
| 55 |
|
| 56 |
t_range range = getRange(name); |
| 57 |
t_range::iterator end = range.end(); |
| 58 |
for (t_range::iterator i = range.begin(); i != end; ++i) { |
| 59 |
int score = matchScore(lang, (*i)->getLang()); |
| 60 |
if (bestScore < score) { |
| 61 |
bestScore = score; |
| 62 |
bestDialog = *i; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if (bestScore < 2) { |
| 67 |
bestDialog = NULL; |
| 68 |
} |
| 69 |
return bestDialog; |
| 70 |
} |
| 71 |
//----------------------------------------------------------------- |
| 72 |
/** |
| 73 |
* Try find dialog for current lang or default lang. |
| 74 |
* @return dialog or NULL |
| 75 |
*/ |
| 76 |
const Dialog * |
| 77 |
ResDialogPack::findDialogHard(const std::string &name) |
| 78 |
{ |
| 79 |
std::string lang = OptionAgent::agent()->getParam("lang"); |
| 80 |
const Dialog *dialog = findDialog(name, lang); |
| 81 |
if (NULL == dialog) { |
| 82 |
dialog = findDialog(name, Dialog::DEFAULT_LANG); |
| 83 |
if (NULL == dialog) { |
| 84 |
LOG_WARNING(ExInfo("cannot find dialog") |
| 85 |
.addInfo("name", name) |
| 86 |
.addInfo("lang", lang) |
| 87 |
.addInfo("pack", toString())); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return dialog; |
| 92 |
} |
| 93 |
//----------------------------------------------------------------- |
| 94 |
/** |
| 95 |
* Try find dialog for lang=speech or default lang. |
| 96 |
* @return dialog or NULL |
| 97 |
*/ |
| 98 |
const Dialog * |
| 99 |
ResDialogPack::findDialogSpeech(const std::string &name) |
| 100 |
{ |
| 101 |
std::string speech = OptionAgent::agent()->getParam("speech", |
| 102 |
OptionAgent::agent()->getParam("lang")); |
| 103 |
const Dialog *dialog = findDialog(name, speech); |
| 104 |
if (NULL == dialog || dialog->isSpeechless()) { |
| 105 |
dialog = findDialog(name, Dialog::DEFAULT_LANG); |
| 106 |
if (NULL == dialog) { |
| 107 |
LOG_WARNING(ExInfo("cannot find speech") |
| 108 |
.addInfo("name", name) |
| 109 |
.addInfo("speech", speech) |
| 110 |
.addInfo("pack", toString())); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return dialog; |
| 115 |
} |
| 116 |
|