File indexing completed on 2024-04-28 09:36:50

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 /*
0010  * Utility classes for KCachegrind
0011  */
0012 
0013 #ifndef UTILS_H
0014 #define UTILS_H
0015 
0016 #include <qstring.h>
0017 
0018 class QIODevice;
0019 
0020 typedef unsigned long long uint64;
0021 typedef long long int64;
0022 
0023 /**
0024  * A simple, constant string class
0025  *
0026  * For use with zero-copy strings from mapped files.
0027  */
0028 class FixString {
0029 
0030 public:
0031     // constructor for an invalid string
0032     FixString() { _len = 0; _str = nullptr; }
0033 
0034     /**
0035      * FixString never does a deep copy! You have to make sure that
0036      * the string starting at the char pointer is valid through the
0037      * lifetime of FixString.
0038      */
0039     FixString(const char*, int len);
0040 
0041     int len() { return _len; }
0042     const char* ascii() { return _str; }
0043     bool isEmpty() { return _len == 0; }
0044     bool isValid() { return _str != nullptr; }
0045 
0046     // sets <c> to first character and returns true if length >0
0047     bool first(char& c)
0048     { if (_len==0) return false; c=_str[0]; return true; }
0049 
0050     void set(const char* s, int l) { _str=s; _len=l; }
0051     bool stripFirst(char&);
0052     bool stripPrefix(const char*);
0053 
0054     /**
0055      * Strip leading and trailing spaces
0056      */
0057     void stripSurroundingSpaces();
0058 
0059     /**
0060      * Strip leading spaces
0061      */
0062     void stripSpaces();
0063 
0064     /**
0065      * Strip name: [A-Za-z_][0-9A_Za-z_]*
0066      */
0067     bool stripName(FixString&);
0068 
0069     /**
0070      * Strip string until char appears or end. Strips char, too.
0071      */
0072     FixString stripUntil(char);
0073 
0074     bool stripUInt(uint&, bool stripSpaces = true);
0075     bool stripUInt64(uint64&, bool stripSpaces = true);
0076     bool stripInt64(int64&, bool stripSpaces = true);
0077 
0078     operator QString() const
0079     { return QString::fromLocal8Bit(_str,_len); }
0080 
0081 private:
0082     const char* _str;
0083     int _len;
0084 };
0085 
0086 
0087 /**
0088  * A class for fast line by line reading of a read-only ASCII file
0089  */
0090 class FixFile {
0091 
0092 public:
0093     FixFile(QIODevice*, const QString&);
0094     ~FixFile();
0095 
0096     /**
0097      * Read next line into @p str.
0098      * @param str is the target string
0099      * @returns false on error or EOF.
0100      */
0101     bool nextLine(FixString& str);
0102     bool exists() { return !_openError; }
0103     unsigned len() { return _len; }
0104     unsigned current() { return _current - _base; }
0105     bool setCurrent(unsigned pos);
0106     void rewind() { setCurrent(0); }
0107 
0108 private:
0109     char *_base, *_current;
0110     QByteArray _data;
0111     unsigned _len, _currentLeft;
0112     bool _used_mmap, _openError;
0113     QIODevice* _file;
0114     QString _filename;
0115 };
0116 
0117 
0118 /**
0119  * A list of pointers, only able to append items.
0120  * Optimized for speed, not space.
0121  */
0122 template<class type>
0123 class AppendList {
0124 
0125 public:
0126     AppendList();
0127     ~AppendList() { clear(); }
0128 
0129     void setAutoDelete(bool);
0130     void clear();
0131     void append(const type*);
0132 
0133     unsigned count() const { return _count; }
0134     unsigned containsRef(const type*) const;
0135 
0136     type* current();
0137     type* first();
0138     type* next();
0139 
0140 private:
0141     static const int firstLen = 8;
0142     static const int maxLen = 256;
0143 
0144     struct AppendListChunk {
0145         int size;
0146         struct AppendListChunk* next;
0147         type* data[1];
0148     };
0149 
0150     struct AppendListChunk *_next, *_current, *_last;
0151     int _count, _currentIndex, _lastIndex;
0152     bool _autoDelete;
0153     type* _first[firstLen];
0154 };
0155 
0156 
0157 #endif