KDECore
bufferfragment_p.h
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2008 Jakub Stachowski <qbast@go2.pl> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #ifndef BUFFERFRAGMENT_H 00022 #define BUFFERFRAGMENT_H 00023 00024 #define bf_isspace(str) ((str == ' ') || (str == '\t') || (str == '\r')) 00025 00026 // This class provides wrapper around fragment of existing buffer (array of bytes). 00027 // If underlying buffer gets deleted, all BufferFragment objects referencing it become invalid. 00028 // Use toByteArray() to make deep copy of the buffer fragment. 00029 // 00030 // API is designed to subset of QByteArray methods with some changes: 00031 // - trim() is like QByteArray.trimmed(), but it modifies current object 00032 // - truncateLeft() provides way to cut off beginning of the buffer 00033 // - split() works more like strtok_r than QByteArray.split() 00034 // - truncateLeft() and mid() require position argument to be valid 00035 00036 class KConfigIniBackend::BufferFragment 00037 { 00038 00039 public: 00040 00041 BufferFragment() : d(0), len(0) 00042 { 00043 } 00044 00045 BufferFragment(char* buf, int size) : d(buf), len(size) 00046 { 00047 } 00048 00049 int length() const 00050 { 00051 return len; 00052 } 00053 00054 char at(unsigned int i) const 00055 { 00056 Q_ASSERT(i < len); 00057 return d[i]; 00058 } 00059 00060 void clear() 00061 { 00062 len = 0; 00063 } 00064 00065 const char* constData() const 00066 { 00067 return d; 00068 } 00069 00070 char* data() const 00071 { 00072 return d; 00073 } 00074 00075 void trim() 00076 { 00077 while (bf_isspace(*d) && len > 0) { 00078 d++; 00079 len--; 00080 } 00081 while (len > 0 && bf_isspace(d[len - 1])) 00082 len--; 00083 } 00084 00085 // similar to strtok_r . On first call variable pointed by start should be set to 0. 00086 // Each call will update *start to new starting position. 00087 BufferFragment split(char c, unsigned int* start) 00088 { 00089 while (*start < len) { 00090 int end = indexOf(c, *start); 00091 if (end == -1) end = len; 00092 BufferFragment line(d + (*start), end - (*start)); 00093 *start = end + 1; 00094 return line; 00095 } 00096 return BufferFragment(); 00097 } 00098 00099 bool isEmpty() const 00100 { 00101 return (len == 0); 00102 } 00103 00104 BufferFragment left(unsigned int size) const 00105 { 00106 return BufferFragment(d, qMin(size,len)); 00107 } 00108 00109 void truncateLeft(unsigned int size) 00110 { 00111 Q_ASSERT(size <= len); 00112 d += size; 00113 len -= size; 00114 } 00115 00116 void truncate(unsigned int pos) 00117 { 00118 if (pos < len) len = pos; 00119 } 00120 00121 bool isNull() const 00122 { 00123 return (d == 0); 00124 } 00125 00126 BufferFragment mid(unsigned int pos, int length=-1) const 00127 { 00128 Q_ASSERT(pos < len); 00129 int size = length; 00130 if (length == -1 || (pos + length) > len) 00131 size = len - pos; 00132 return BufferFragment(d + pos, size); 00133 } 00134 00135 bool operator==(const QByteArray& other) const 00136 { 00137 return (other.size() == (int)len && memcmp(d,other.constData(),len) == 0); 00138 } 00139 00140 bool operator!=(const QByteArray& other) const 00141 { 00142 return (other.size() != (int)len || memcmp(d,other.constData(),len) != 0); 00143 } 00144 00145 int indexOf(char c, unsigned int from = 0) const 00146 { 00147 const char* cursor = d + from - 1; 00148 const char* end = d + len; 00149 while ( ++cursor < end) 00150 if (*cursor ==c ) 00151 return cursor - d; 00152 return -1; 00153 } 00154 00155 int lastIndexOf(char c) const 00156 { 00157 int from = len - 1; 00158 while (from >= 0) 00159 if (d[from] == c) 00160 return from; 00161 else 00162 from--; 00163 return -1; 00164 } 00165 00166 QByteArray toByteArray() const { 00167 return QByteArray(d,len); 00168 } 00169 00170 // this is faster than toByteArray, but returned QByteArray becomes invalid 00171 // when buffer for this BufferFragment disappears 00172 QByteArray toVolatileByteArray() const { 00173 return QByteArray::fromRawData(d, len); 00174 } 00175 00176 private: 00177 char* d; 00178 unsigned int len; 00179 }; 00180 00181 #endif
KDE 4.6 API Reference