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

kcachegrind

  • sources
  • kde-4.12
  • kdesdk
  • kcachegrind
  • libcore
utils.cpp
Go to the documentation of this file.
1 /* This file is part of KCachegrind.
2  Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
3 
4  KCachegrind is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public
6  License as published by the Free Software Foundation, version 2.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 /*
20  * Utility classes for KCachegrind
21  */
22 
23 #include "utils.h"
24 
25 #include <errno.h>
26 
27 #include <QIODevice>
28 #include <QFile>
29 
30 
31 
32 // class FixString
33 
34 FixString::FixString(const char* str, int len)
35 {
36  _str = str;
37  _len = len;
38 }
39 
40 bool FixString::stripFirst(char& c)
41 {
42  if (!_len) {
43  c = 0;
44  return false;
45  }
46 
47  c = *_str;
48  _str++;
49  _len--;
50  return true;
51  }
52 
53 bool FixString::stripPrefix(const char* p)
54 {
55  if (_len == 0) return false;
56  if (!p || (*p != *_str)) return false;
57 
58  const char* s = _str+1;
59  int l = _len-1;
60  p++;
61  while(*p) {
62  if (l==0) return false;
63  if (*s != *p) return false;
64  p++;
65  s++;
66  l--;
67  }
68  _str = s;
69  _len = l;
70  return true;
71 }
72 
73 
74 // this parses hexadecimal (with prefix '0x' too)
75 bool FixString::stripUInt(unsigned int& v, bool stripSpaces)
76 {
77  if (_len==0) {
78  v = 0;
79  return false;
80  }
81 
82  char c = *_str;
83  if (c<'0' || c>'9') {
84  v = 0;
85  return false;
86  }
87 
88  v = c-'0';
89  const char* s = _str+1;
90  int l = _len-1;
91  c = *s;
92 
93  if ((l>0) && (c == 'x') && (v==0)) {
94  // hexadecimal
95  s++;
96  c = *s;
97  l--;
98 
99  while(l>0) {
100  if (c>='0' && c<='9')
101  v = 16*v + (c-'0');
102  else if (c>='a' && c<='f')
103  v = 16*v + 10 + (c-'a');
104  else if (c>='A' && c<='F')
105  v = 16*v + 10 + (c-'A');
106  else
107  break;
108  s++;
109  c = *s;
110  l--;
111  }
112  }
113  else {
114  // decimal
115 
116  while(l>0) {
117  if (c<'0' || c>'9') break;
118  v = 10*v + (c-'0');
119  s++;
120  c = *s;
121  l--;
122  }
123  }
124 
125  if (stripSpaces)
126  while(l>0) {
127  if (c != ' ') break;
128  s++;
129  c = *s;
130  l--;
131  }
132 
133  _str = s;
134  _len = l;
135  return true;
136 }
137 
138 
139 void FixString::stripSurroundingSpaces()
140 {
141  if (_len==0) return;
142 
143  // leading spaces
144  while((_len>0) && (*_str==' ')) {
145  _len--;
146  _str++;
147  }
148 
149  // trailing spaces
150  while((_len>0) && (_str[_len-1]==' ')) {
151  _len--;
152  }
153 }
154 
155 void FixString::stripSpaces()
156 {
157  while((_len>0) && (*_str==' ')) {
158  _len--;
159  _str++;
160  }
161 }
162 
163 bool FixString::stripName(FixString& s)
164 {
165  if (_len==0) return false;
166 
167  // first char has to be a letter or "_"
168  if (!QChar(*_str).isLetter() && (*_str != '_')) return false;
169 
170  int newLen = 1;
171  const char* newStr = _str;
172 
173  _str++;
174  _len--;
175 
176  while(_len>0) {
177  if (!QChar(*_str).isLetterOrNumber()
178  && (*_str != '_')) break;
179 
180  newLen++;
181  _str++;
182  _len--;
183  }
184 
185  s.set(newStr, newLen);
186  return true;
187 }
188 
189 FixString FixString::stripUntil(char c)
190 {
191  if (_len == 0) return FixString();
192 
193  const char* newStr = _str;
194  int newLen = 0;
195 
196  while(_len>0) {
197  if (*_str == c) {
198  _str++;
199  _len--;
200  break;
201  }
202 
203  _str++;
204  _len--;
205  newLen++;
206  }
207  return FixString(newStr, newLen);
208 }
209 
210 bool FixString::stripUInt64(uint64& v, bool stripSpaces)
211 {
212  if (_len==0) {
213  v = 0;
214  return false;
215  }
216 
217  char c = *_str;
218  if (c<'0' || c>'9') {
219  v = 0;
220  return false;
221  }
222 
223  v = c-'0';
224  const char* s = _str+1;
225  int l = _len-1;
226  c = *s;
227 
228  if ((l>0) && (c == 'x') && (v==0)) {
229  // hexadecimal
230  s++;
231  c = *s;
232  l--;
233 
234  while(l>0) {
235  if (c>='0' && c<='9')
236  v = 16*v + (c-'0');
237  else if (c>='a' && c<='f')
238  v = 16*v + 10 + (c-'a');
239  else if (c>='A' && c<='F')
240  v = 16*v + 10 + (c-'A');
241  else
242  break;
243  s++;
244  c = *s;
245  l--;
246  }
247  }
248  else {
249  // decimal
250  while(l>0) {
251  if (c<'0' || c>'9') break;
252  v = 10*v + (c-'0');
253  s++;
254  c = *s;
255  l--;
256  }
257  }
258 
259  if (stripSpaces)
260  while(l>0) {
261  if (c != ' ') break;
262  s++;
263  c = *s;
264  l--;
265  }
266 
267  _str = s;
268  _len = l;
269  return true;
270 }
271 
272 
273 bool FixString::stripInt64(int64& v, bool stripSpaces)
274 {
275  if (_len==0) {
276  v = 0;
277  return false;
278  }
279 
280  char c = *_str;
281  if (c<'0' || c>'9') {
282  v = 0;
283  return false;
284  }
285 
286  v = c-'0';
287  const char* s = _str+1;
288  int l = _len-1;
289  c = *s;
290 
291  if ((l>0) && (c == 'x') && (v==0)) {
292  // hexadecimal
293  s++;
294  c = *s;
295  l--;
296 
297  while(l>0) {
298  if (c>='0' && c<='9')
299  v = 16*v + (c-'0');
300  else if (c>='a' && c<='f')
301  v = 16*v + 10 + (c-'a');
302  else if (c>='A' && c<='F')
303  v = 16*v + 10 + (c-'A');
304  else
305  break;
306  s++;
307  c = *s;
308  l--;
309  }
310  }
311  else {
312  // decimal
313 
314  while(l>0) {
315  if (c<'0' || c>'9') break;
316  v = 10*v + (c-'0');
317  s++;
318  c = *s;
319  l--;
320  }
321  }
322 
323  if (stripSpaces)
324  while(l>0) {
325  if (c != ' ') break;
326  s++;
327  c = *s;
328  l--;
329  }
330 
331  _str = s;
332  _len = l;
333  return true;
334 }
335 
336 
337 
338 // class FixFile
339 
340 FixFile::FixFile(QIODevice* file, const QString& filename)
341 {
342  _file = file;
343 
344  if (!file) {
345  _len = 0;
346  _currentLeft = 0;
347  _openError = true;
348  return;
349  }
350 
351  _filename = filename;
352  if (!file->isOpen() && !file->open( QIODevice::ReadOnly ) ) {
353  qWarning( "%s: %s", (const char*)QFile::encodeName(_filename),
354  strerror( errno ) );
355  _len = 0;
356  _currentLeft = 0;
357  _openError = true;
358  return;
359  }
360 
361  _openError = false;
362  _used_mmap = false;
363 
364  uchar* addr = 0;
365 
366 #if QT_VERSION >= 0x040400
367  // QFile::map was introduced with Qt 4.4
368  if (file->size() >0) {
369  QFile* mappableDevice = dynamic_cast<QFile*>(file);
370  if (mappableDevice) {
371  addr = mappableDevice->map( 0, file->size() );
372  }
373  }
374 #endif
375 
376  if (addr) {
377  // map succeeded
378  _base = (char*) addr;
379  _len = file->size();
380  _used_mmap = true;
381 
382  if (0) qDebug("Mapped '%s'", qPrintable( _filename ));
383  }
384  else {
385  // try reading the data into memory instead
386  file->seek(0);
387  _data = file->readAll();
388  _base = _data.data();
389  _len = _data.size();
390  }
391 
392  _current = _base;
393  _currentLeft = _len;
394 }
395 
396 FixFile::~FixFile()
397 {
398  // if the file was read into _data, it will be deleted automatically
399 
400  if (_used_mmap && _file) {
401  if (0) qDebug("Unmapping '%s'", qPrintable( _filename ));
402 #if QT_VERSION >= 0x040400
403  QFile* mappableDevice = dynamic_cast<QFile*>(_file);
404  Q_ASSERT(mappableDevice);
405  if (!mappableDevice->unmap( (uchar*) _base ))
406  qWarning( "munmap: %s", strerror( errno ) );
407 #endif
408  }
409 }
410 
411 bool FixFile::nextLine(FixString& str)
412 {
413  if (_currentLeft == 0) return false;
414 
415  unsigned left = _currentLeft;
416  char* current = _current;
417 
418  while(left>0) {
419  if (*current == 0 || *current == '\n') break;
420  current++;
421  left--;
422  }
423 
424  if (0) {
425  char tmp[200];
426  int l = _currentLeft-left;
427  if (l>199) l = 199;
428  strncpy(tmp, _current, l);
429  tmp[l] = 0;
430  qDebug("[FixFile::nextLine] At %lu, len %u: '%s'",
431  (unsigned long) (_current - _base), _currentLeft-left, tmp);
432  }
433 
434  int len = _currentLeft-left;
435  // get rid of any carriage return at end
436  if ((len>0) && (*(current-1) == '\r')) len--;
437  str.set(_current, len);
438 
439  if (*current == '\n') {
440  current++;
441  left--;
442  }
443  _current = current;
444  _currentLeft = left;
445 
446  return true;
447 }
448 
449 bool FixFile::setCurrent(unsigned pos)
450 {
451  if (pos > _len) return false;
452 
453  _current = _base + pos;
454  _currentLeft = _len - pos;
455  return true;
456 }
457 
458 
459 #if 0
460 
461 // class AppendList
462 
463 
464 AppendList::AppendList()
465 {
466  _next = 0;
467  _current = 0;
468  _last = 0;
469 
470  _count = 0;
471  _currentIndex = 0;
472  _lastIndex = 0;
473  _autoDelete = false;
474 }
475 
476 
477 void AppendList::clear()
478 {
479  int count = _count;
480  int i;
481 
482  if (count <= firstLen) {
483  if (_autoDelete)
484  for (i=0;i<count;i++)
485  delete _first[i];
486  }
487 }
488 
489 #endif
490 
AppendList::count
unsigned count() const
Definition: utils.h:141
FixString::stripSpaces
void stripSpaces()
Strip leading spaces.
Definition: utils.cpp:155
FixString::stripUInt
bool stripUInt(uint &, bool stripSpaces=true)
Definition: utils.cpp:75
FixFile::setCurrent
bool setCurrent(unsigned pos)
Definition: utils.cpp:449
uint64
unsigned long long uint64
Definition: subcost.h:27
FixFile::~FixFile
~FixFile()
Definition: utils.cpp:396
utils.h
FixString::stripSurroundingSpaces
void stripSurroundingSpaces()
Strip leading and trailing spaces.
Definition: utils.cpp:139
FixString::stripInt64
bool stripInt64(int64 &, bool stripSpaces=true)
Definition: utils.cpp:273
FixString::FixString
FixString()
Definition: utils.h:42
FixString::stripFirst
bool stripFirst(char &)
Definition: utils.cpp:40
FixString
A simple, constant string class.
Definition: utils.h:38
FixFile::len
unsigned len()
Definition: utils.h:111
FixFile::FixFile
FixFile(QIODevice *, const QString &)
Definition: utils.cpp:340
FixString::len
int len()
Definition: utils.h:51
AppendList::AppendList
AppendList()
FixString::stripName
bool stripName(FixString &)
Strip name: [A-Za-z_][0-9A_Za-z_]*.
Definition: utils.cpp:163
FixString::stripPrefix
bool stripPrefix(const char *)
Definition: utils.cpp:53
FixString::set
void set(const char *s, int l)
Definition: utils.h:60
FixString::stripUntil
FixString stripUntil(char)
Strip string until char appears or end.
Definition: utils.cpp:189
int64
long long int64
Definition: utils.h:31
AppendList::clear
void clear()
FixFile::nextLine
bool nextLine(FixString &str)
Read next line into .
Definition: utils.cpp:411
FixString::stripUInt64
bool stripUInt64(uint64 &, bool stripSpaces=true)
Definition: utils.cpp:210
FixFile::current
unsigned current()
Definition: utils.h:112
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcachegrind

Skip menu "kcachegrind"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

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