• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ExtendedCharTable.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Konsole, an X terminal.
3 
4  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
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.
15 
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., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "ExtendedCharTable.h"
24 
25 // KDE
26 #include <KDebug>
27 
28 // Konsole
29 #include "TerminalDisplay.h"
30 #include "SessionManager.h"
31 #include "Session.h"
32 #include "Screen.h"
33 
34 using namespace Konsole;
35 
36 ExtendedCharTable::ExtendedCharTable()
37 {
38 }
39 
40 ExtendedCharTable::~ExtendedCharTable()
41 {
42  // free all allocated character buffers
43  QHashIterator<ushort, ushort*> iter(extendedCharTable);
44  while (iter.hasNext()) {
45  iter.next();
46  delete[] iter.value();
47  }
48 }
49 
50 // global instance
51 ExtendedCharTable ExtendedCharTable::instance;
52 
53 ushort ExtendedCharTable::createExtendedChar(const ushort* unicodePoints , ushort length)
54 {
55  // look for this sequence of points in the table
56  ushort hash = extendedCharHash(unicodePoints, length);
57  const ushort initialHash = hash;
58  bool triedCleaningSolution = false;
59 
60  // check existing entry for match
61  while (extendedCharTable.contains(hash) && hash != 0) { // 0 has a special meaning for chars so we don't use it
62  if (extendedCharMatch(hash, unicodePoints, length)) {
63  // this sequence already has an entry in the table,
64  // return its hash
65  return hash;
66  } else {
67  // if hash is already used by another, different sequence of unicode character
68  // points then try next hash
69  hash++;
70 
71  if (hash == initialHash) {
72  if (!triedCleaningSolution) {
73  triedCleaningSolution = true;
74  // All the hashes are full, go to all Screens and try to free any
75  // This is slow but should happen very rarely
76  QSet<ushort> usedExtendedChars;
77  const SessionManager* sm = SessionManager::instance();
78  foreach(const Session * s, sm->sessions()) {
79  foreach(const TerminalDisplay * td, s->views()) {
80  usedExtendedChars += td->screenWindow()->screen()->usedExtendedChars();
81  }
82  }
83 
84  QHash<ushort, ushort*>::iterator it = extendedCharTable.begin();
85  QHash<ushort, ushort*>::iterator itEnd = extendedCharTable.end();
86  while (it != itEnd) {
87  if (usedExtendedChars.contains(it.key())) {
88  ++it;
89  } else {
90  it = extendedCharTable.erase(it);
91  }
92  }
93  } else {
94  kWarning() << "Using all the extended char hashes, going to miss this extended character";
95  return 0;
96  }
97  }
98  }
99  }
100 
101  // add the new sequence to the table and
102  // return that index
103  ushort* buffer = new ushort[length + 1];
104  buffer[0] = length;
105  for (int i = 0 ; i < length ; i++)
106  buffer[i + 1] = unicodePoints[i];
107 
108  extendedCharTable.insert(hash, buffer);
109 
110  return hash;
111 }
112 
113 ushort* ExtendedCharTable::lookupExtendedChar(ushort hash , ushort& length) const
114 {
115  // look up index in table and if found, set the length
116  // argument and return a pointer to the character sequence
117 
118  ushort* buffer = extendedCharTable[hash];
119  if (buffer) {
120  length = buffer[0];
121  return buffer + 1;
122  } else {
123  length = 0;
124  return 0;
125  }
126 }
127 
128 ushort ExtendedCharTable::extendedCharHash(const ushort* unicodePoints , ushort length) const
129 {
130  ushort hash = 0;
131  for (ushort i = 0 ; i < length ; i++) {
132  hash = 31 * hash + unicodePoints[i];
133  }
134  return hash;
135 }
136 
137 bool ExtendedCharTable::extendedCharMatch(ushort hash , const ushort* unicodePoints , ushort length) const
138 {
139  ushort* entry = extendedCharTable[hash];
140 
141  // compare given length with stored sequence length ( given as the first ushort in the
142  // stored buffer )
143  if (entry == 0 || entry[0] != length)
144  return false;
145  // if the lengths match, each character must be checked. the stored buffer starts at
146  // entry[1]
147  for (int i = 0 ; i < length ; i++) {
148  if (entry[i + 1] != unicodePoints[i])
149  return false;
150  }
151  return true;
152 }
153 
Session.h
Konsole::SessionManager::instance
static SessionManager * instance()
Returns the session manager instance.
Definition: SessionManager.cpp:69
Konsole::Session
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: Session.h:78
QHash::insert
iterator insert(const Key &key, const T &value)
ExtendedCharTable.h
QHash::key
const Key key(const T &value) const
Konsole::ExtendedCharTable::~ExtendedCharTable
~ExtendedCharTable()
Definition: ExtendedCharTable.cpp:40
Konsole::ExtendedCharTable::createExtendedChar
ushort createExtendedChar(const ushort *unicodePoints, ushort length)
Adds a sequences of unicode characters to the table and returns a hash code which can be used later t...
Definition: ExtendedCharTable.cpp:53
Konsole::ExtendedCharTable::instance
static ExtendedCharTable instance
The global ExtendedCharTable instance.
Definition: ExtendedCharTable.h:68
QHashIterator::hasNext
bool hasNext() const
Screen.h
Konsole::SessionManager::sessions
const QList< Session * > sessions() const
Returns a list of active sessions.
Definition: SessionManager.cpp:83
Konsole::Session::views
QList< TerminalDisplay * > views() const
Returns the views connected to this session.
Definition: Session.cpp:303
Konsole::ExtendedCharTable
A table which stores sequences of unicode characters, referenced by hash keys.
Definition: ExtendedCharTable.h:36
TerminalDisplay.h
QHash
QHashIterator
Konsole::ExtendedCharTable::lookupExtendedChar
ushort * lookupExtendedChar(ushort hash, ushort &length) const
Looks up and returns a pointer to a sequence of unicode characters which was added to the table using...
Definition: ExtendedCharTable.cpp:113
QHash::begin
iterator begin()
QSet
QHash::erase
iterator erase(iterator pos)
QHashIterator::next
Item next()
QSet::contains
bool contains(const T &value) const
Konsole::ExtendedCharTable::ExtendedCharTable
ExtendedCharTable()
Constructs a new character table.
Definition: ExtendedCharTable.cpp:36
Konsole::ScreenWindow::screen
Screen * screen() const
Returns the screen which this window looks onto.
Definition: ScreenWindow.cpp:53
Konsole::Screen::usedExtendedChars
QSet< ushort > usedExtendedChars() const
Definition: Screen.h:573
Konsole::TerminalDisplay::screenWindow
ScreenWindow * screenWindow() const
Returns the terminal screen section which is displayed in this widget.
Definition: TerminalDisplay.cpp:103
SessionManager.h
QHash::contains
bool contains(const Key &key) const
QHash::end
iterator end()
QHashIterator::value
const T & value() const
Konsole::TerminalDisplay
A widget which displays output from a terminal emulation and sends input keypresses and mouse activit...
Definition: TerminalDisplay.h:63
Konsole::SessionManager
Manages running terminal sessions.
Definition: SessionManager.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal