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

KDECore

  • sources
  • kde-4.12
  • 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  int indexOf(char c, unsigned int from = 0) const
146  {
147  const char* cursor = d + from - 1;
148  const char* end = d + len;
149  while ( ++cursor < end)
150  if (*cursor ==c )
151  return cursor - d;
152  return -1;
153  }
154 
155  int lastIndexOf(char c) const
156  {
157  int from = len - 1;
158  while (from >= 0)
159  if (d[from] == c)
160  return from;
161  else
162  from--;
163  return -1;
164  }
165 
166  QByteArray toByteArray() const {
167  return QByteArray(d,len);
168  }
169 
170  // this is faster than toByteArray, but returned QByteArray becomes invalid
171  // when buffer for this BufferFragment disappears
172  QByteArray toVolatileByteArray() const {
173  return QByteArray::fromRawData(d, len);
174  }
175 
176 private:
177  char* d;
178  unsigned int len;
179 };
180 
181 #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
KConfigBackend::size
qint64 size() const
Definition: kconfigbackend.cpp:113
KConfigIniBackend::BufferFragment::data
char * data() const
Definition: bufferfragment_p.h:70
KConfigIniBackend::BufferFragment::at
char at(unsigned int i) const
Definition: bufferfragment_p.h:54
KConfigIniBackend::BufferFragment::truncate
void truncate(unsigned int pos)
Definition: bufferfragment_p.h:116
KConfigIniBackend::BufferFragment::toByteArray
QByteArray toByteArray() const
Definition: bufferfragment_p.h:166
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::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:145
KConfigIniBackend::BufferFragment::clear
void clear()
Definition: bufferfragment_p.h:60
KConfigIniBackend::BufferFragment::toVolatileByteArray
QByteArray toVolatileByteArray() const
Definition: bufferfragment_p.h:172
KConfigIniBackend::BufferFragment::lastIndexOf
int lastIndexOf(char c) const
Definition: bufferfragment_p.h:155
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
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:07 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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