Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Core / Editors / EditorString.cpp
blobd418a2487ddbe2a733bfb991d12a798fd3ef744e
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorString.h"
22 #include "StringUtilities.h"
23 #include "UBSocket.h"
24 #include "Account.h"
26 EditorString::EditorString(UBSocket* sock, std::string& target) :
27 Editor(sock),
28 m_state(M_FIRST),
29 m_target(target)
31 OnLine(Global::Get()->EmptyString);
34 EditorString::~EditorString()
36 std::string result = String::Get()->unlines(m_strings, " ");
37 m_target = result;
40 EditorString::E_STATE EditorString::ParseMode(char mode, bool silent)
42 E_STATE choice = M_FIRST;
44 switch(mode)
46 default:
47 if(!silent)
48 m_sock->Sendf("Unknown mode '%c'.\n", mode);
49 break;
51 case 'a': // append
52 m_sock->Send("Switching to 'append' mode.\n");
53 choice = M_APPEND;
54 break;
56 case 'd': // done
57 m_sock->Send("Allright, done!\n");
58 choice = M_WAITING_FOR_INPUT;
59 break;
61 case 'r': // replace
62 m_sock->Send("Switching to 'replacement' mode.\n");
63 choice = M_REPLACE;
64 break;
66 case 's': // show
67 case 'v': // view
68 m_sock->Send("Switching to 'viewing' mode.\n");
69 choice = M_VIEW;
70 break;
73 return choice;
76 void EditorString::ParseDot(const std::string& line)
78 std::string command = line;
79 command.erase(0, 1);
81 E_STATE changedstate = ParseMode(command[0]);
82 if(changedstate != M_FIRST)
84 m_state = changedstate;
85 return;
88 if(!command.compare("c"))
90 m_sock->Send("Text cleared.\n");
91 m_strings.clear();
92 return;
95 if(!command.compare("q"))
97 m_sock->Send("Quitting now.\n");
98 m_state = M_DONE;
99 return;
102 if(!command.compare("h"))
104 m_sock->Send("Some descriptive help text goes here.\n");
105 return;
108 m_sock->Sendf("Unknown dot command '%s'.\n", command.c_str());
109 return;
112 void EditorString::OnLine(const std::string& line)
114 if(!line.compare("~"))
116 m_sock->Send("Allright, done!\n");
117 m_state = M_DONE;
120 if(line[0] == '.')
122 ParseDot(line);
123 return;
126 switch(m_state)
128 default:
130 Global::Get()->bug("EditorString::OnLine(), default m_state!\n");
131 m_sock->Send("BUG! Disconnecting you now...\n");
132 m_sock->SetCloseAndDelete();
133 return;
134 } /* default */
136 case M_FIRST:
138 m_state = M_WAITING_FOR_INPUT;
139 m_sock->Send("Welcome to the string editor.\n");
140 // fallthrough
141 } /* case M_FIRST: */
143 case M_WAITING_FOR_INPUT:
145 if(line.size() == 0)
147 m_sock->Send("Please choose an editing mode.\n");
148 m_sock->Send("If you need help please type '.h'.\n");
149 return;
152 char mode;
154 if(line[0] == '.')
155 mode = line[1];
156 else
157 mode = line[0];
159 E_STATE choice = ParseMode(mode);
161 if(choice == M_FIRST)
162 m_state = M_WAITING_FOR_INPUT;
163 else
164 m_state = choice;
166 OnLine(Global::Get()->EmptyString);
167 return;
168 } /* case M_WAITING_FOR_INPUT: */
170 case M_APPEND:
172 if(line.size() == 0)
174 m_sock->Sendf("If you want to append a newline type '.n' on an empty line.\n");
175 return;
178 m_strings.push_back(line);
179 return;
180 } /* case M_APPEND: */
182 case M_REPLACE:
184 if(line.size() == 0)
186 m_sock->Send("Not yet implemented - Alturin 30-12-2007.\n");
187 return;
190 return;
192 case M_VIEW:
194 if(line.size() != 0)
196 m_sock->Send("At the moment all you can do here is hit enter to show the entire string.\n");
197 return;
200 m_sock->Send(String::Get()->unlines(m_strings, " "));
201 return;
205 case M_DONE:
207 m_sock->PopEditor();
208 return;
209 } /* case M_DONE: */
211 } /* switch(state) */