ServerTextFilter.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <vector>
00022 #include <server/ServerTextFilter.h>
00023 #include <server/ScorchedServer.h>
00024 #include <common/OptionsScorched.h>
00025 #include <common/Defines.h>
00026 
00027 #define IS_SPACE(c) ((c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || c > 'z'))
00028 
00029 ServerTextFilter::ServerTextFilter() : lastReadTime_(0)
00030 {
00031 }
00032 
00033 ServerTextFilter::~ServerTextFilter()
00034 {
00035 }
00036 
00037 void ServerTextFilter::filterString(LangString &inputText)
00038 {
00039         loadFile();
00040 
00041         if (words_.empty()) return;
00042         std::vector<TextPart> parts;
00043 
00044         // Split the string into parts (words) each seperated by the 
00045         // seperating characters
00046     LangString current;
00047         const unsigned int *pos = 0;
00048         for (const unsigned int *c = inputText.c_str(); *c; c++)
00049         {
00050                 if (IS_SPACE(*c))
00051                 {
00052                         if (current.c_str()[0])
00053                         {
00054                                 TextPart part;
00055                                 part.part = current;
00056                                 part.pos = (unsigned int *) pos;
00057                                 parts.push_back(part);
00058                         }
00059                         current.clear();
00060                         pos = 0;
00061                 }
00062                 else
00063                 {
00064                         if (!pos) pos = c;
00065                         current += *c;
00066                 }
00067         }
00068         // The first/last part
00069         if (current.c_str()[0])
00070         {
00071                 TextPart part;
00072                 part.part = current;
00073                 part.pos = (unsigned int *) pos;
00074                 parts.push_back(part);
00075         }
00076 
00077         // For each part 
00078         std::vector<TextPart>::iterator itor;
00079         for (itor = parts.begin(); itor != parts.end(); itor++)
00080         {
00081                 TextPart &part = (*itor);
00082                 const unsigned int *text = part.part.c_str();
00083                 
00084                 // Check that they don't contain the words
00085                 std::list<LangString>::iterator witor;
00086                 for (witor = words_.begin(); witor != words_.end(); witor++)
00087                 {
00088                         const unsigned int *word = (*witor).c_str();
00089                         unsigned int *pos = LangStringUtil::strstr(text, word);
00090                         if (pos)
00091                         {
00092                                 // Only filter out if the words is at the start or end of the word
00093                                 if (pos == text || (pos - text) + LangStringUtil::strlen(word) == LangStringUtil::strlen(text))
00094                                 {
00095                                         // If they do then * out the word
00096                                         for (int i=0; i<LangStringUtil::strlen(word); i++)
00097                                         {
00098                                                 pos[i] = '*';
00099                                         }
00100                                 }
00101                         }
00102                 }
00103         }
00104 
00105         // For each combination of parts check that they don't add up to the words
00106         for (int i=0; i<(int) parts.size(); i++)
00107         {
00108                 LangString sofar;
00109                 for (int j=i; j<(int) parts.size(); j++)
00110                 {
00111                         sofar += parts[j].part;
00112                         const unsigned int *text = sofar.c_str();
00113 
00114                         // Check each word against the parts so far
00115                         std::list<LangString>::iterator witor;
00116                         for (witor = words_.begin(); witor != words_.end(); witor++)
00117                         {
00118                                 const unsigned int *word = (*witor).c_str();
00119                                 if (LangStringUtil::strcasecmp(text, word) == 0)
00120                                 {
00121                                         // If they match, * out all parts
00122                                         for (int k=i; k<=j; k++)
00123                                         {
00124                                                 for (unsigned int *pos = (unsigned int *) parts[k].part.c_str();
00125                                                         *pos; 
00126                                                         pos++)
00127                                                 {
00128                                                         *pos = '*';
00129                                                 }
00130                                         }
00131 
00132                                         break;
00133                                 }
00134                         }
00135                 }
00136         }
00137 
00138         // Re-form the words
00139         for (itor = parts.begin(); itor != parts.end(); itor++)
00140         {
00141                 TextPart &part = (*itor);
00142                 const unsigned int *text = part.part.c_str();
00143 
00144                 for (int i=0; i<(int) part.part.size(); i++)
00145                 {
00146                         part.pos[i] = text[i];
00147                 }
00148         }
00149 }
00150 
00151 void ServerTextFilter::loadFile()
00152 {
00153         std::string filename = 
00154                 S3D::getSettingsFile(S3D::formatStringBuffer("filter-%i.txt", 
00155                         ScorchedServer::instance()->getOptionsGame().getPortNo()));
00156         if (!::S3D::fileExists(filename)) return;
00157 
00158         time_t fileTime = S3D::fileModTime(filename);
00159         if (fileTime == lastReadTime_) return;
00160 
00161     FileLines lines;
00162         if (!lines.readFile(filename)) return;
00163 
00164         lastReadTime_ = fileTime;
00165 
00166         words_.clear();
00167         std::vector<std::string>::iterator itor;
00168         for (itor = lines.getLines().begin();
00169                 itor != lines.getLines().end();
00170                 itor++)
00171         {
00172                 if ((*itor).c_str()[0])
00173                 {
00174                         words_.push_back(LANG_STRING(*itor));
00175                 }
00176         }
00177 }

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3