File: utils.h

package info (click to toggle)
kcachegrind 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,964 kB
  • sloc: cpp: 32,149; xml: 403; perl: 325; python: 235; sh: 8; makefile: 6
file content (157 lines) | stat: -rw-r--r-- 3,351 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
    This file is part of KCachegrind.

    SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

    SPDX-License-Identifier: GPL-2.0-only
*/

/*
 * Utility classes for KCachegrind
 */

#ifndef UTILS_H
#define UTILS_H

#include <qstring.h>

class QIODevice;

typedef unsigned long long uint64;
typedef long long int64;

/**
 * A simple, constant string class
 *
 * For use with zero-copy strings from mapped files.
 */
class FixString {

public:
    // constructor for an invalid string
    FixString() { _len = 0; _str = nullptr; }

    /**
     * FixString never does a deep copy! You have to make sure that
     * the string starting at the char pointer is valid through the
     * lifetime of FixString.
     */
    FixString(const char*, int len);

    int len() { return _len; }
    const char* ascii() { return _str; }
    bool isEmpty() { return _len == 0; }
    bool isValid() { return _str != nullptr; }

    // sets <c> to first character and returns true if length >0
    bool first(char& c)
    { if (_len==0) return false; c=_str[0]; return true; }

    void set(const char* s, int l) { _str=s; _len=l; }
    bool stripFirst(char&);
    bool stripPrefix(const char*);

    /**
     * Strip leading and trailing spaces
     */
    void stripSurroundingSpaces();

    /**
     * Strip leading spaces
     */
    void stripSpaces();

    /**
     * Strip name: [A-Za-z_][0-9A_Za-z_]*
     */
    bool stripName(FixString&);

    /**
     * Strip string until char appears or end. Strips char, too.
     */
    FixString stripUntil(char);

    bool stripUInt(uint&, bool stripSpaces = true);
    bool stripUInt64(uint64&, bool stripSpaces = true);
    bool stripInt64(int64&, bool stripSpaces = true);

    operator QString() const
    { return QString::fromLocal8Bit(_str,_len); }

private:
    const char* _str;
    int _len;
};


/**
 * A class for fast line by line reading of a read-only ASCII file
 */
class FixFile {

public:
    FixFile(QIODevice*, const QString&);
    ~FixFile();

    /**
     * Read next line into @p str.
     * @param str is the target string
     * @returns false on error or EOF.
     */
    bool nextLine(FixString& str);
    bool exists() { return !_openError; }
    unsigned len() { return _len; }
    unsigned current() { return _current - _base; }
    bool setCurrent(unsigned pos);
    void rewind() { setCurrent(0); }

private:
    char *_base, *_current;
    QByteArray _data;
    unsigned _len, _currentLeft;
    bool _used_mmap, _openError;
    QIODevice* _file;
    QString _filename;
};


/**
 * A list of pointers, only able to append items.
 * Optimized for speed, not space.
 */
template<class type>
class AppendList {

public:
    AppendList();
    ~AppendList() { clear(); }

    void setAutoDelete(bool);
    void clear();
    void append(const type*);

    unsigned count() const { return _count; }
    unsigned containsRef(const type*) const;

    type* current();
    type* first();
    type* next();

private:
    static const int firstLen = 8;
    static const int maxLen = 256;

    struct AppendListChunk {
        int size;
        struct AppendListChunk* next;
        type* data[1];
    };

    struct AppendListChunk *_next, *_current, *_last;
    int _count, _currentIndex, _lastIndex;
    bool _autoDelete;
    type* _first[firstLen];
};


#endif