1
// $Id: string_reader.cxx,v 1.7 2002/09/14 19:06:34 torangan Exp $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include <ClanLib/Core/System/system.h>
21
#include <ClanLib/Display/Input/inputbuffer.h>
22
#include <ClanLib/Display/Display/display.h>
23
#include <ClanLib/Display/Font/font.h>
24
#include "../console.hxx"
25
#include "../pingus_resource.hxx"
26
#include "string_reader.hxx"
28
StringReader::StringReader()
31
font = PingusResource::load_font("Fonts/courier_small", "fonts");
34
StringReader::StringReader(const std::string & d, const std::string & def)
39
font = PingusResource::load_font("Fonts/courier_small", "fonts");
42
StringReader::~StringReader()
47
StringReader::set_strings(std::list<std::string>* s)
53
StringReader::read_string()
59
CL_System::keep_alive();
61
current_string = default_string;
70
CL_System::keep_alive();
72
if (keys.peek_key().state != CL_Key::NoKey)
76
if (key.state == CL_Key::Pressed)
88
case CL_KEY_BACKSPACE:
89
if (!current_string.empty())
90
current_string = current_string.substr(0, current_string.size() - 1);
98
current_string += key.ascii;
106
return current_string;
110
StringReader::complete_string()
112
int completions_counter = 0;
113
std::string* completion;
117
console << "\nCompletions:\n" << "~~~~~~~~~~~~" << std::endl;
119
for(std::list<std::string>::iterator i = strings->begin(); i != strings->end(); ++i)
121
if (i->find(current_string) == 0)
123
console << *i << std::endl;
124
++completions_counter;
126
completions.push_back(&(*i));
130
if (completions_counter >= 1)
132
current_string = find_uniq();
134
// std::cout << "Searching finished" << std::endl;
138
StringReader::find_uniq()
140
std::list<std::string*>::iterator i = completions.begin();
141
std::string ret_string = **(completions.begin());
143
while (i != completions.end())
145
ret_string = while_eq(ret_string, **i);
153
StringReader::while_eq(const std::string& a, const std::string& b)
155
std::string ret_string;
157
for(std::string::size_type i = 0;
158
i < a.size() && i < b.size() && a[i] == b[i];
170
CL_Display::clear_display();
171
font->print_left(20, 20, description.c_str());
172
font->print_left(20, 40, current_string.c_str());
173
Display::flip_display();