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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • config
bufferfragment_p.h
Go to the documentation of this file.
1 /*
2  This file is part of the KDE libraries
3  Copyright (c) 2008 Jakub Stachowski <qbast@go2.pl>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library 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 GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef BUFFERFRAGMENT_H
22 #define BUFFERFRAGMENT_H
23 
24 #define bf_isspace(str) ((str == ' ') || (str == '\t') || (str == '\r'))
25 
26 // This class provides wrapper around fragment of existing buffer (array of bytes).
27 // If underlying buffer gets deleted, all BufferFragment objects referencing it become invalid.
28 // Use toByteArray() to make deep copy of the buffer fragment.
29 //
30 // API is designed to subset of QByteArray methods with some changes:
31 // - trim() is like QByteArray.trimmed(), but it modifies current object
32 // - truncateLeft() provides way to cut off beginning of the buffer
33 // - split() works more like strtok_r than QByteArray.split()
34 // - truncateLeft() and mid() require position argument to be valid
35 
36 class KConfigIniBackend::BufferFragment
37 {
38 
39 public:
40 
41  BufferFragment() : d(0), len(0)
42  {
43  }
44 
45  BufferFragment(char* buf, int size) : d(buf), len(size)
46  {
47  }
48 
49  int length() const
50  {
51  return len;
52  }
53 
54  char at(unsigned int i) const
55  {
56  Q_ASSERT(i < len);
57  return d[i];
58  }
59 
60  void clear()
61  {
62  len = 0;
63  }
64 
65  const char* constData() const
66  {
67  return d;
68  }
69 
70  char* data() const
71  {
72  return d;
73  }
74 
75  void trim()
76  {
77  while (bf_isspace(*d) && len > 0) {
78  d++;
79  len--;
80  }
81  while (len > 0 && bf_isspace(d[len - 1]))
82  len--;
83  }
84 
85  // similar to strtok_r . On first call variable pointed by start should be set to 0.
86  // Each call will update *start to new starting position.
87  BufferFragment split(char c, unsigned int* start)
88  {
89  while (*start < len) {
90  int end = indexOf(c, *start);
91  if (end == -1) end = len;
92  BufferFragment line(d + (*start), end - (*start));
93  *start = end + 1;
94  return line;
95  }
96  return BufferFragment();
97  }
98 
99  bool isEmpty() const
100  {
101  return (len == 0);
102  }
103 
104  BufferFragment left(unsigned int size) const
105  {
106  return BufferFragment(d, qMin(size,len));
107  }
108 
109  void truncateLeft(unsigned int size)
110  {
111  Q_ASSERT(size <= len);
112  d += size;
113  len -= size;
114  }
115 
116  void truncate(unsigned int pos)
117  {
118  if (pos < len) len = pos;
119  }
120 
121  bool isNull() const
122  {
123  return (d == 0);
124  }
125 
126  BufferFragment mid(unsigned int pos, int length=-1) const
127  {
128  Q_ASSERT(pos < len);
129  int size = length;
130  if (length == -1 || (pos + length) > len)
131  size = len - pos;
132  return BufferFragment(d + pos, size);
133  }
134 
135  bool operator==(const QByteArray& other) const
136  {
137  return (other.size() == (int)len && memcmp(d,other.constData(),len) == 0);
138  }
139 
140  bool operator!=(const QByteArray& other) const
141  {
142  return (other.size() != (int)len || memcmp(d,other.constData(),len) != 0);
143  }
144 
145  bool operator==(const BufferFragment& other) const
146  {
147  return other.len == len && !memcmp(d, other.d, len);
148  }
149 
150  int indexOf(char c, unsigned int from = 0) const
151  {
152  const char* cursor = d + from - 1;
153  const char* end = d + len;
154  while ( ++cursor < end)
155  if (*cursor ==c )
156  return cursor - d;
157  return -1;
158  }
159 
160  int lastIndexOf(char c) const
161  {
162  int from = len - 1;
163  while (from >= 0)
164  if (d[from] == c)
165  return from;
166  else
167  from--;
168  return -1;
169  }
170 
171  QByteArray toByteArray() const {
172  return QByteArray(d,len);
173  }
174 
175  // this is faster than toByteArray, but returned QByteArray becomes invalid
176  // when buffer for this BufferFragment disappears
177  QByteArray toVolatileByteArray() const {
178  return QByteArray::fromRawData(d, len);
179  }
180 
181 private:
182  char* d;
183  unsigned int len;
184 };
185 
186 uint qHash(const KConfigIniBackend::BufferFragment& fragment)
187 {
188  const uchar *p = reinterpret_cast<const uchar*>(fragment.constData());
189  const int len = fragment.length();
190 
191  // This algorithm is copied from qhash.cpp (Qt5 version).
192  // Sadly this code is not accessible from the outside without going through abstraction
193  // layers. Even QByteArray::fromRawData would do an allocation internally...
194  uint h = 0;
195 
196  for (int i = 0; i < len; ++i) {
197  h = 31 * h + p[i];
198  }
199 
200  return h;
201 }
202 
203 #endif
KConfigIniBackend::BufferFragment::constData
const char * constData() const
Definition: bufferfragment_p.h:65
KConfigIniBackend::BufferFragment::trim
void trim()
Definition: bufferfragment_p.h:75
KConfigIniBackend::BufferFragment::left
BufferFragment left(unsigned int size) const
Definition: bufferfragment_p.h:104
KConfigIniBackend::BufferFragment
Definition: bufferfragment_p.h:36
QByteArray
KConfigBackend::size
qint64 size() const
Definition: kconfigbackend.cpp:113
KConfigIniBackend::BufferFragment::data
char * data() const
Definition: bufferfragment_p.h:70
qHash
uint qHash(const KConfigIniBackend::BufferFragment &fragment)
Definition: bufferfragment_p.h:186
KConfigIniBackend::BufferFragment::at
char at(unsigned int i) const
Definition: bufferfragment_p.h:54
QByteArray::fromRawData
QByteArray fromRawData(const char *data, int size)
KConfigIniBackend::BufferFragment::truncate
void truncate(unsigned int pos)
Definition: bufferfragment_p.h:116
KConfigIniBackend::BufferFragment::toByteArray
QByteArray toByteArray() const
Definition: bufferfragment_p.h:171
KConfigIniBackend::BufferFragment::mid
BufferFragment mid(unsigned int pos, int length=-1) const
Definition: bufferfragment_p.h:126
KConfigIniBackend::BufferFragment::operator==
bool operator==(const QByteArray &other) const
Definition: bufferfragment_p.h:135
KConfigIniBackend::BufferFragment::operator==
bool operator==(const BufferFragment &other) const
Definition: bufferfragment_p.h:145
QByteArray::constData
const char * constData() const
KConfigIniBackend::BufferFragment::BufferFragment
BufferFragment()
Definition: bufferfragment_p.h:41
KConfigIniBackend::BufferFragment::isNull
bool isNull() const
Definition: bufferfragment_p.h:121
KConfigIniBackend::BufferFragment::split
BufferFragment split(char c, unsigned int *start)
Definition: bufferfragment_p.h:87
bf_isspace
#define bf_isspace(str)
Definition: bufferfragment_p.h:24
KConfigIniBackend::BufferFragment::truncateLeft
void truncateLeft(unsigned int size)
Definition: bufferfragment_p.h:109
KConfigIniBackend::BufferFragment::indexOf
int indexOf(char c, unsigned int from=0) const
Definition: bufferfragment_p.h:150
KConfigIniBackend::BufferFragment::clear
void clear()
Definition: bufferfragment_p.h:60
KConfigIniBackend::BufferFragment::toVolatileByteArray
QByteArray toVolatileByteArray() const
Definition: bufferfragment_p.h:177
KConfigIniBackend::BufferFragment::lastIndexOf
int lastIndexOf(char c) const
Definition: bufferfragment_p.h:160
KConfigIniBackend::BufferFragment::length
int length() const
Definition: bufferfragment_p.h:49
KConfigIniBackend::BufferFragment::operator!=
bool operator!=(const QByteArray &other) const
Definition: bufferfragment_p.h:140
QByteArray::size
int size() const
KConfigIniBackend::BufferFragment::isEmpty
bool isEmpty() const
Definition: bufferfragment_p.h:99
KConfigIniBackend::BufferFragment::BufferFragment
BufferFragment(char *buf, int size)
Definition: bufferfragment_p.h:45
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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