![]() |
Editor Class ReferenceLine-based Text Editor class.
More...
|
Public Types | |
enum | editor_t { EDIT_ROOM_DESCRIPTION = 1 } |
Public Member Functions | |
Editor (InSocket *from, editor_t type) | |
~Editor () | |
void | Init () |
void | Save () |
void | Execute (InSocket *, const std::string ¶ms, Parse &) |
Execute command. | |
void | OnLine (InSocket *, const std::string &line, int state) |
Additional callback, reply to command prompt. | |
std::string | Category () |
Get command category. | |
void | SetPrompt (int state) |
Private Member Functions | |
void | AddLine (const std::string &x) |
Private Attributes | |
InSocket * | m_from |
editor_t | m_type |
std::vector< std::string > | m_edit |
size_t | m_line |
std::string | m_prev |
Definition at line 32 of file Editor.h.
enum Editor::editor_t |
Editor::~Editor | ( | ) |
void Editor::Init | ( | ) |
Definition at line 38 of file Editor.cpp.
References AddLine(), EDIT_ROOM_DESCRIPTION, Player::GetArea(), Player::GetChunk(), Player::GetChunkX(), Player::GetChunkY(), m_edit, m_from, m_line, m_type, and SetPrompt().
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 }
void Editor::Save | ( | ) |
Definition at line 84 of file Editor.cpp.
References EDIT_ROOM_DESCRIPTION, Player::GetArea(), Player::GetChunk(), Player::GetChunkX(), Player::GetChunkY(), m_edit, m_from, and m_type.
Referenced by OnLine().
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 }
void Editor::Execute | ( | InSocket * | , | |
const std::string & | arg, | |||
Parse & | ||||
) | [inline, virtual] |
void Editor::OnLine | ( | InSocket * | , | |
const std::string & | line, | |||
int | state | |||
) | [virtual] |
Additional callback, reply to command prompt.
Reimplemented from Command.
Definition at line 119 of file Editor.cpp.
References m_edit, m_from, m_line, m_prev, Save(), and SetPrompt().
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 }
std::string Editor::Category | ( | ) | [inline, virtual] |
void Editor::SetPrompt | ( | int | state | ) |
Definition at line 108 of file Editor.cpp.
References m_edit, m_from, and m_line.
Referenced by Init(), and OnLine().
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 }
void Editor::AddLine | ( | const std::string & | x | ) | [inline, private] |
InSocket* Editor::m_from [private] |
editor_t Editor::m_type [private] |
std::vector<std::string> Editor::m_edit [private] |
size_t Editor::m_line [private] |
std::string Editor::m_prev [private] |