Logo
~Sockets~
~Examples~
~Contact~

Editor.cpp

Go to the documentation of this file.
00001 // Editor.cpp
00002 // released 2006-09-25
00003 /*
00004 Copyright (C) 2006  Anders Hedstrom (grymse@alhem.net)
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program 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 this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 */
00020 #include "Editor.h"
00021 #include "InSocket.h"
00022 #include "Player.h"
00023 #include "Area.h"
00024 #include "RoomDescription.h"
00025 #include "Chunk.h"
00026 
00027 
00028 Editor::Editor(InSocket *from,editor_t type) : Command(), m_from(from), m_type(type), m_line(1)
00029 {
00030 }
00031 
00032 
00033 Editor::~Editor()
00034 {
00035 }
00036 
00037 
00038 void Editor::Init()
00039 {
00040         Database& db = m_from -> GetDatabase();
00041         switch (m_type)
00042         {
00043         case EDIT_ROOM_DESCRIPTION:
00044                 {
00045                         Player pl(db, m_from -> GetAccountName());
00046                         Area area(db, pl.GetArea());
00047                         Chunk chunk(db, area, pl.GetChunk());
00048                         RoomDescription rd(db, chunk, pl.GetChunkX(), pl.GetChunkY());
00049                         if (rd.Exists())
00050                         {
00051                                 std::string tmp;
00052                                 for (size_t i = 0; i < rd.GetData().size(); i++)
00053                                 {
00054                                         if (rd.GetData()[i] == 13)
00055                                         {
00056                                                 // ignore
00057                                         }
00058                                         else
00059                                         if (rd.GetData()[i] == 10)
00060                                         {
00061                                                 AddLine(tmp);
00062                                                 tmp = "";
00063                                         }
00064                                         else
00065                                         {
00066                                                 tmp += rd.GetData()[i];
00067                                         }
00068                                 }
00069                                 if (tmp.size())
00070                                 {
00071                                         AddLine(tmp);
00072                                 }
00073                         }
00074                 }
00075                 break;
00076         }
00077         //
00078         if (m_line <= m_edit.size())
00079                 m_from -> Send(m_edit[m_line - 1] + "\n");
00080         SetPrompt(0);
00081 }
00082 
00083 
00084 void Editor::Save()
00085 {
00086         Database& db = m_from -> GetDatabase();
00087         switch (m_type)
00088         {
00089         case EDIT_ROOM_DESCRIPTION:
00090                 {
00091                         Player pl(db, m_from -> GetAccountName());
00092                         Area area(db, pl.GetArea());
00093                         Chunk chunk(db, area, pl.GetChunk());
00094                         RoomDescription rd(db, chunk, pl.GetChunkX(), pl.GetChunkY());
00095                         std::string tmp;
00096                         for (size_t i = 0; i < m_edit.size(); i++)
00097                         {
00098                                 tmp += m_edit[i] + "\n";
00099                         }
00100                         rd.SetData(tmp);
00101                         rd.Save();
00102                 }
00103                 break;
00104         }
00105 }
00106 
00107 
00108 void Editor::SetPrompt(int state)
00109 {
00110         char slask[100];
00111         if (m_line <= m_edit.size())
00112                 sprintf(slask, "%03d>", m_line);
00113         else
00114                 strcpy(slask, "<END>");
00115         m_from -> SetPrompt(this, state, slask);
00116 }
00117 
00118 
00119 void Editor::OnLine(InSocket *from,const std::string& line_in,int state)
00120 {
00121         std::vector<std::string>::iterator it = m_edit.begin();
00122         {
00123                 size_t i = 1;
00124                 while (i++ < m_line && it != m_edit.end())
00125                         it++;
00126         }
00127         std::string line = line_in;
00128         switch (state)
00129         {
00130         case 0:
00131                 if (!line.size())
00132                 {
00133                         if (m_line <= m_edit.size())
00134                                 m_line++;
00135                         if (m_line <= m_edit.size())
00136                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00137                         SetPrompt(0);
00138                         return;
00139                 }
00140                 if (!strcasecmp(line.c_str(), "a"))
00141                         line = m_prev;
00142                 m_prev = line;
00143                 if (!strcasecmp(line.c_str(), "e"))
00144                 {
00145                         m_line = m_edit.size() + 1;
00146                         SetPrompt(0);
00147                 }
00148                 else
00149                 if (!strcasecmp(line.c_str(), "b"))
00150                 {
00151                         m_line = 1;
00152                         if (m_line <= m_edit.size())
00153                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00154                         SetPrompt(0);
00155                 }
00156                 else
00157                 if (!strcasecmp(line.c_str(), "i"))
00158                 {
00159                         m_from -> SetPrompt(this, 1, "   .");
00160                 }
00161                 else
00162                 if (!strcasecmp(line.c_str(), "d"))
00163                 {
00164                         if (it != m_edit.end())
00165                                 m_edit.erase(it);
00166                         if (m_line <= m_edit.size())
00167                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00168                         SetPrompt(0);
00169                 }
00170                 else
00171                 if (!strcasecmp(line.c_str(), "qu"))
00172                 {
00173                         m_from -> Send("Exit without save\n");
00174                         m_from -> SetPrompt();
00175                         delete this;
00176                 }
00177                 else
00178                 if (!strcasecmp(line.c_str(), "ex"))
00179                 {
00180                         Save();
00181                         m_from -> Send("Ok.\n");
00182                         m_from -> SetPrompt();
00183                         delete this;
00184                 }
00185                 else
00186                 if (!strcasecmp(line.c_str(), "ec"))
00187                 {
00188                         Save();
00189                         m_from -> Send("Ok.\n");
00190                         m_from -> SetPrompt();
00191                         delete this;
00192                 }
00193                 else
00194                 if (!strcasecmp(line.substr(0, 1).c_str(), "s"))
00195                 {
00196                 }
00197                 else
00198                 if (!strcmp(line.substr(0, 1).c_str(), "+"))
00199                 {
00200                         int x = line.substr(1).size() ? atoi(line.substr(1).c_str()) : 1;
00201                         m_line += x;
00202                         if (m_line > m_edit.size())
00203                                 m_line = m_edit.size() + 1;
00204                         if (m_line <= m_edit.size())
00205                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00206                         SetPrompt(0);
00207                 }
00208                 else
00209                 if (!strcmp(line.substr(0, 1).c_str(), "-"))
00210                 {
00211                         int x = line.substr(1).size() ? atoi(line.substr(1).c_str()) : 1;
00212                         m_line -= x;
00213                         if (m_line < 1)
00214                                 m_line = 1;
00215                         if (m_line <= m_edit.size())
00216                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00217                         SetPrompt(0);
00218                 }
00219                 else
00220                 if (isdigit(line[0]))
00221                 {
00222                         m_line = atoi(line.c_str());
00223                         if (m_line < 1)
00224                                 m_line = 1;
00225                         if (m_line > m_edit.size())
00226                                 m_line = m_edit.size() + 1;
00227                         if (m_line <= m_edit.size())
00228                                 m_from -> Send(m_edit[m_line - 1] + "\n");
00229                         SetPrompt(0);
00230                 }
00231                 else
00232                 if (!strcasecmp(line.substr(0, 1).c_str(), "l"))
00233                 {
00234                         for (size_t i = 0; i < m_edit.size(); i++)
00235                         {
00236                                 char slask[100];
00237                                 sprintf(slask, "%03d>", i + 1);
00238                                 m_from -> Send(slask + m_edit[i] + "\n");
00239                         }
00240                         SetPrompt(0);
00241                 }
00242                 else
00243                 if (!strcmp(line.substr(0, 1).c_str(), ">"))
00244                 {
00245                 }
00246                 else
00247                 if (!strcmp(line.substr(0, 1).c_str(), "<"))
00248                 {
00249                 }
00250                 else
00251                 {
00252                         m_from -> Send(
00253                                 "&GE   &ngå till slutet av texten&n\n"
00254                                 "&GB   &ngå till början av texten&n\n"
00255                                 "&GI   &nskjut in rader före aktuell rad, avsluta med tom rad&n\n"
00256                                 "&GD   &nta bort aktuell rad&n\n"
00257                                 "&GQU  &navsluta utan att spara&n\n"
00258                                 "&GEX  &navsluta och spara&n\n"
00259                                 "&GEC  &navsluta, spara, och kompilera&n\n"
00260                                 "&GS/xx/yy    &nbyt ut xx mot yy på aktuell rad&n\n"
00261                                 "&G+x  &ngå fram x rader&n\n"
00262                                 "&G-x  &ngå bak x rader&n\n"
00263                                 "&G#   &ngå till rad nummer #&n\n"
00264                                 "&GL   &nlista hela texten&n\n"
00265                                 "&GL#  &nlista texten från rad #&n\n"
00266                                 "&G>   &nskjut in 2 mellanslag&n\n"
00267                                 "&G<   &nta bort 2 mellanslag&n\n"
00268                                 "&GA   &nupprepa föregående kommando&n\n");
00269                         m_from -> SendPrompt();
00270                 }
00271                 break;
00272         case 1: // insert lines
00273                 if (!line.size())
00274                 {
00275                         SetPrompt(0);
00276                         return;
00277                 }
00278                 m_edit.insert(it, line);
00279                 m_line++;
00280                 m_from -> SetPrompt(this, 1, "   .");
00281                 break;
00282         }
00283 }
00284 
00285 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4