Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

ResDialogPack.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "ResDialogPack.h"
00010 
00011 #include "Log.h"
00012 #include "Dialog.h"
00013 #include "OptionAgent.h"
00014 #include "minmax.h"
00015 
00016 //-----------------------------------------------------------------
00017     void
00018 ResDialogPack::unloadRes(Dialog *res)
00019 {
00020     delete res;
00021 }
00022 //-----------------------------------------------------------------
00023 /**
00024  * Return match score.
00025  * One string must excatly be starting substring of the other.
00026  */
00027 int
00028 ResDialogPack::matchScore(const std::string &first,
00029         const std::string &second) const
00030 {
00031     int score = 0;
00032     int minSize = min(first.size(), second.size());
00033     for (int i = 0; i < minSize; ++i) {
00034         if (first[i] == second[i]) {
00035             score++;
00036         }
00037         else {
00038             return 0;
00039         }
00040     }
00041     return score;
00042 }
00043 //-----------------------------------------------------------------
00044 /**
00045  * Return dialog or NULL.
00046  * Compare dialog names and lang codes.
00047  * The best lang match is selected, at least two characters must equals.
00048  */
00049    const Dialog *
00050 ResDialogPack::findDialog(const std::string &name,
00051         const std::string &lang)
00052 {
00053     int bestScore = 0;
00054     Dialog *bestDialog = NULL;
00055 
00056     t_range range = getRange(name);
00057     t_range::iterator end = range.end();
00058     for (t_range::iterator i = range.begin(); i != end; ++i) {
00059         int score = matchScore(lang, (*i)->getLang());
00060         if (bestScore < score) {
00061             bestScore = score;
00062             bestDialog = *i;
00063         }
00064     }
00065 
00066     if (bestScore < 2) {
00067         bestDialog = NULL;
00068     }
00069     return bestDialog;
00070 }
00071 //-----------------------------------------------------------------
00072 /**
00073  * Try find dialog for current lang or default lang.
00074  * @return dialog or NULL
00075  */
00076     const Dialog *
00077 ResDialogPack::findDialogHard(const std::string &name)
00078 {
00079     std::string lang = OptionAgent::agent()->getParam("lang");
00080     const Dialog *dialog = findDialog(name, lang);
00081     if (NULL == dialog) {
00082         dialog = findDialog(name, Dialog::DEFAULT_LANG);
00083         if (NULL == dialog) {
00084             LOG_WARNING(ExInfo("cannot find dialog")
00085                     .addInfo("name", name)
00086                     .addInfo("lang", lang)
00087                     .addInfo("pack", toString()));
00088         }
00089     }
00090 
00091     return dialog;
00092 }
00093 //-----------------------------------------------------------------
00094 /**
00095  * Try find dialog for lang=speech or default lang.
00096  * @return dialog or NULL
00097  */
00098     const Dialog *
00099 ResDialogPack::findDialogSpeech(const std::string &name)
00100 {
00101     std::string speech = OptionAgent::agent()->getParam("speech",
00102             OptionAgent::agent()->getParam("lang"));
00103     const Dialog *dialog = findDialog(name, speech);
00104     if (NULL == dialog || dialog->isSpeechless()) {
00105         dialog = findDialog(name, Dialog::DEFAULT_LANG);
00106         if (NULL == dialog) {
00107             LOG_WARNING(ExInfo("cannot find speech")
00108                     .addInfo("name", name)
00109                     .addInfo("speech", speech)
00110                     .addInfo("pack", toString()));
00111         }
00112     }
00113 
00114     return dialog;
00115 }
00116 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2