⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptypes.h

📁 使用stl技术,(还没看,是听说的)
💻 H
📖 第 1 页 / 共 3 页
字号:
/*
 *
 *  C++ Portable Types Library (PTypes)
 *  Version 1.8.3  Released 25-Aug-2003
 *
 *  Copyright (c) 2001, 2002, 2003 Hovik Melikyan
 *
 *  http://www.melikyan.com/ptypes/
 *  http://ptypes.sourceforge.net/
 *
 */

#ifndef __PTYPES_H__
#define __PTYPES_H__


#ifndef _INC_STRING
#include <string.h>
#endif

#ifndef __PPORT_H__
#include "pport.h"
#endif


PTYPES_BEGIN


#ifdef _MSC_VER
#pragma pack(push, 4)
#endif


#ifdef WIN32
#  define __PFASTCALL __fastcall
#else
#  define __PFASTCALL
#endif

ptpublic int   __PFASTCALL pincrement(int* target);
ptpublic int   __PFASTCALL pdecrement(int* target);
ptpublic int   __PFASTCALL pexchange(int* target, int value);
ptpublic void* __PFASTCALL pexchange(void** target, void* value);

template <class T> inline T* tpexchange(T** target, T* value)
    { return (T*)pexchange((void**)target, (void*)value); }


// -------------------------------------------------------------------- //
// ---  string class -------------------------------------------------- //
// -------------------------------------------------------------------- //


struct _strrec 
{
    int refcount;
    int length;
};
typedef _strrec* _pstrrec;


#define STR_BASE(x)      (_pstrrec(x)-1)
#define STR_REFCOUNT(x)  (STR_BASE(x)->refcount)
#define STR_LENGTH(x)    (STR_BASE(x)->length)

#define PTR_TO_PSTRING(p)   (pstring(&(p)))
#define PTR_TO_STRING(p)    (*PTR_TO_PSTRING(p))

#if (__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)
#  define VARIANT_TYPECAST_HACK
#endif


const int strrecsize = sizeof(_strrec);

ptpublic extern char* emptystr;

ptpublic extern int stralloc;


class ptpublic string 
{
    friend class variant;

protected:
    char* data;

    void _alloc(int);
    void _realloc(int);
    void _free();

    void initialize()                             { data = emptystr; }
    void initialize(const char*, int);
    void initialize(const char*);
    void initialize(char);
    void initialize(const string& s);
    void initialize(const char*, int, const char*, int);
    void initialize(const variant&);
    void finalize();

    void assign(const char*, int);
    void assign(const char*);
    void assign(const string&);
    void assign(char);

    string(const char* s1, int len1, const char* s2, int len2)  { initialize(s1, len1, s2, len2); }

public:
    ptpublic friend int  length(const string& s);
    ptpublic friend int  refcount(const string& s);
    ptpublic friend void assign(string& s, const char* buf, int len);
    ptpublic friend void clear(string& s);
    ptpublic friend bool isempty(const string& s);

    ptpublic friend void   setlength(string&, int);
    ptpublic friend char*  unique(string&);
    ptpublic friend void   concat(string& s, const char* sc, int catlen);
    ptpublic friend void   concat(string& s, const char* s1);
    ptpublic friend void   concat(string& s, char s1);
    ptpublic friend void   concat(string& s, const string& s1);
    ptpublic friend string copy(const string& s, int from, int cnt);
    ptpublic friend void   ins(const char* s1, int s1len, string& s, int at);
    ptpublic friend void   ins(const char* s1, string& s, int at);
    ptpublic friend void   ins(char s1, string& s, int at);
    ptpublic friend void   ins(const string& s1, string& s, int at);
    ptpublic friend void   del(string& s, int at, int cnt);
    ptpublic friend int    pos(const char* s1, const string& s);
    ptpublic friend int    pos(char s1, const string& s);
    ptpublic friend int    rpos(char s1, const string& s);
    ptpublic friend bool   contains(const char* s1, int len, const string& s, int at);
    ptpublic friend bool   contains(const char* s1, const string& s, int at);
    ptpublic friend bool   contains(char s1, const string& s, int at);
    ptpublic friend bool   contains(const string& s1, const string& s, int at);
    ptpublic friend int    pos(const string& s1, const string& s);
    ptpublic friend string dup(const string& s);

    string()                                      { initialize(); }
    string(const char* sc, int initlen)           { initialize(sc, initlen); }
    string(const char* sc)                        { initialize(sc); }
    string(char c)                                { initialize(c); }
    string(const string& s)                       { initialize(s); }
    ~string()                                     { finalize(); }

#ifdef VARIANT_TYPECAST_HACK
    string(const variant& v)                      { initialize(v); }
#endif

    string& operator=  (const char* sc)           { assign(sc); return *this; }
    string& operator=  (char c)                   { assign(c); return *this; }
    string& operator=  (const string& s)          { assign(s); return *this; }
    string& operator+= (const char* sc)           { concat(*this, sc); return *this; }
    string& operator+= (char c)                   { concat(*this, c); return *this; }
    string& operator+= (const string& s)          { concat(*this, s); return *this; }

    string  operator+  (const char* sc) const;
    string  operator+  (char c) const;
    string  operator+  (const string& s) const;

    ptpublic friend string operator+ (const char* sc, const string& s);
    ptpublic friend string operator+ (char c, const string& s);

    bool    operator== (const char* sc) const     { return strcmp(data, sc) == 0; }
    bool    operator== (char) const;
    bool    operator== (const string&) const;
    bool    operator!= (const char* sc) const     { return !(*this == sc); }
    bool    operator!= (char c) const             { return !(*this == c); }
    bool    operator!= (const string& s) const    { return !(*this == s); }
    operator const char*() const                  { return data; }
    operator const uchar*() const                 { return (uchar*)data; }

    char&   operator[] (int i);

    ptpublic friend void initialize(string& s);
    ptpublic friend void initialize(string& s, const string& s1);
    ptpublic friend void initialize(string& s, const char* s1);
    ptpublic friend void finalize(string& s);
};


typedef string* pstring;


inline int length(const string& s)                      { return STR_LENGTH(s.data); }
inline int refcount(const string& s)                    { return STR_REFCOUNT(s.data); }
inline void assign(string& s, const char* buf, int len) { s.assign(buf, len); }
inline void clear(string& s)                            { s.finalize(); }
inline bool isempty(const string& s)                    { return length(s) == 0; }
inline int pos(const string& s1, const string& s)       { return pos(s1.data, s); }
inline void initialize(string& s)                       { s.initialize(); }
inline void initialize(string& s, const string& s1)     { s.initialize(s1); }
inline void initialize(string& s, const char* s1)       { s.initialize(s1); }
inline void finalize(string& s)                         { s.finalize(); }

#ifndef CHECK_BOUNDS
inline char& string::operator[] (int i)                 { return unique(*this)[i]; }
#endif



ptpublic extern string nullstring;


// -------------------------------------------------------------------- //
// ---  string utilities ---------------------------------------------- //
// -------------------------------------------------------------------- //


ptpublic string fill(int width, char pad);
ptpublic string pad(const string& s, int width, char c, bool left = true);

ptpublic string itostring(large value, int base, int width = 0, char pad = 0);
ptpublic string itostring(ularge value, int base, int width = 0, char pad = 0);
ptpublic string itostring(int value, int base, int width = 0, char pad = 0);
ptpublic string itostring(unsigned value, int base, int width = 0, char pad = 0);
ptpublic string itostring(large v);
ptpublic string itostring(ularge v);
ptpublic string itostring(int v);
ptpublic string itostring(unsigned v);

ptpublic large  stringtoi(const char*);
ptpublic large  stringtoie(const char*);
ptpublic ularge stringtoue(const char*, int base);

ptpublic string lowercase(const char* s);
ptpublic string lowercase(const string& s);

// itobase is now alias to itostring, deprecated
ptpublic string itobase(large value, int base, int width = 0, char pad = 0);


char hex4(char c);

inline char locase(char c) 
    { if (c >= 'A' && c <= 'Z') return char(c + 32); return c; }

inline char upcase(char c) 
    { if (c >= 'a' && c <= 'z') return char(c - 32); return c; }

inline int hstrlen(const char* p) // some Unix systems do not accept NULL
    { return p == nil ? 0 : strlen(p); }




// -------------------------------------------------------------------- //
// ---  character set class ------------------------------------------- //
// -------------------------------------------------------------------- //


const int  _csetbits = 256;
const int  _csetbytes = _csetbits / 8;
const int  _csetwords = _csetbytes / sizeof(int);
const char _csetesc = '~';


class ptpublic cset 
{
protected:
    char data[_csetbytes];

    void assign(const cset& s)                  { memcpy(data, s.data, _csetbytes); }
    void assign(const char* setinit);
    void clear()                                { memset(data, 0, _csetbytes); }
    void fill()                                 { memset(data, -1, _csetbytes); }
    void include(char b)                        { data[uchar(b) / 8] |= uchar(1 << (uchar(b) % 8)); }
    void include(char min, char max);
    void exclude(char b)                        { data[uchar(b) / 8] &= uchar(~(1 << (uchar(b) % 8))); }
    void unite(const cset& s);
    void subtract(const cset& s);
    void intersect(const cset& s);
    void invert();
    bool contains(char b) const                 { return (data[uchar(b) / 8] & (1 << (uchar(b) % 8))) != 0; }
    bool eq(const cset& s) const                { return memcmp(data, s.data, _csetbytes) == 0; }
    bool le(const cset& s) const;

public:
    cset()                                      { clear(); }
    cset(const cset& s)                         { assign(s); }
    cset(const char* setinit)                   { assign(setinit); }

    cset& operator=  (const cset& s)            { assign(s); return *this; }
    cset& operator+= (const cset& s)            { unite(s); return *this; }
    cset& operator+= (char b)                   { include(b); return *this; }
    cset  operator+  (const cset& s) const      { cset t = *this; return t += s; }
    cset  operator+  (char b) const             { cset t = *this; return t += b; }
    cset& operator-= (const cset& s)            { subtract(s); return *this; }
    cset& operator-= (char b)                   { exclude(b); return *this; }
    cset  operator-  (const cset& s) const      { cset t = *this; return t -= s; }
    cset  operator-  (char b) const             { cset t = *this; return t -= b; }
    cset& operator*= (const cset& s)            { intersect(s); return *this; }
    cset  operator*  (const cset& s) const      { cset t = *this; return t *= s; }
    cset  operator!  () const                   { cset t = *this; t.invert(); return t; }
    bool  operator== (const cset& s) const      { return eq(s); }
    bool  operator!= (const cset& s) const      { return !eq(s); }
    bool  operator<= (const cset& s) const      { return le(s); }
    bool  operator>= (const cset& s) const      { return s.le(*this); }

    ptpublic friend cset operator+ (char b, const cset& s);
    ptpublic friend bool operator& (char b, const cset& s);
    ptpublic friend void assign(cset& s, const char* setinit);
    ptpublic friend void clear(cset& s);
    ptpublic friend void fill(cset& s);
    ptpublic friend void include(cset& s, char b);
    ptpublic friend void exclude(cset& s, char b);
    ptpublic friend void include(cset& s, char min, char max);

    ptpublic friend string asstring(const cset& s);
};


inline cset operator+ (char b, const cset& s)     { return s + b; }
inline bool operator& (char b, const cset& s)     { return s.contains(b); }
inline void assign(cset& s, const char* setinit)  { s.assign(setinit); }
inline void clear(cset& s)                        { s.clear(); }
inline void fill(cset& s)                         { s.fill(); }
inline void include(cset& s, char b)              { s.include(b); }
inline void exclude(cset& s, char b)              { s.exclude(b); }
inline void include(cset& s, char min, char max)  { s.include(min, max); }


// -------------------------------------------------------------------- //
// ---  basic abstract class ------------------------------------------ //
// -------------------------------------------------------------------- //


class ptpublic unknown 
{
public:
    unknown();
    virtual ~unknown();
};

typedef unknown* punknown;


ptpublic extern int objalloc;


// -------------------------------------------------------------------- //
// ---  component ----------------------------------------------------- //
// -------------------------------------------------------------------- //


//
// class ID's for all basic types: the first byte (least significant)
// contains the base ID, the next is for the second level of inheritance,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -