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

📄 str.h

📁 linux下实现视频播放的播放器
💻 H
字号:
/* *  Copyright (C) 2008  gulikoza * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* $Id$ */#ifndef __STR_H__#define __STR_H__#ifndef IPTV_H#define C_HAVE_WXGUI 0#include <stdio.h>#include <stdlib.h>#include <string.h>#include <malloc.h>#include <string>#endif#define MODULE "CStr"#include "log.h"class CStr {protected:    // Data block    int    m_nLength;    char * m_pString;    MUTEX * mutex;    void mutex_lock() {	if(mutex == NULL) {	    mutex = MUTEX_CREATE();	}	MUTEX_LOCK(mutex);    }    void mutex_unlock() {	if(mutex) {	    MUTEX_UNLOCK(mutex);	}    }    void append(const char * str, int l) {	if(l == 0) return;	mutex_lock();	char * pNew = (char*)realloc(m_pString, m_nLength + l + 1);	if(pNew != NULL) {	    if(l == 1) {		pNew[m_nLength] = *str; pNew[m_nLength + 1] = '\0';	    } else if(m_nLength > 0) {		strcat(pNew, str);	    } else strcpy(pNew, str);	    m_pString = pNew;	    m_nLength += l;	}	mutex_unlock();    }    void assign(const char * str, int l) {	mutex_lock();	if(l == 0) {	    if(m_pString) free(m_pString);	    m_pString = NULL;	    m_nLength = 0;	} else {	    m_nLength = l;	    char * pNew = (char*)realloc(m_pString, m_nLength + 1);	    if(pNew == NULL) {		if(m_pString != NULL) free(m_pString);		m_nLength = 0;	    } else strcpy(pNew, str);	    m_pString = pNew;	}	mutex_unlock();    }    // strlen helper function    int length(const char * str) {	if(str) return strlen(str);	return 0;    }public:    class iterator {    private:	const CStr * cstr;	int pos;    public:	iterator(const CStr * const c, int p):cstr(c),pos(p) { }	iterator& operator ++() { if(pos < cstr->m_nLength) pos++; return *this; }	void operator ++(int) { ++(*this); }	char operator *() const { if(cstr->m_pString && (pos <= cstr->m_nLength)) return cstr->m_pString[pos]; return 0; }	iterator& operator =(iterator& itr) { cstr = itr.cstr; pos = itr.pos; return *this; }	bool operator ==(const iterator& itr) const { return ((itr.pos == pos) && (itr.cstr == cstr)); }	bool operator !=(const iterator& itr) const { return ((itr.pos != pos) || (itr.cstr != cstr)); }    };    friend class iterator;    // Constructors and destructor    CStr():m_nLength(0),m_pString(NULL),mutex(NULL) { }    CStr(const CStr& str):m_nLength(0),m_pString(NULL),mutex(NULL) { assign(str.m_pString, str.m_nLength); }    CStr(const std::string& str):m_nLength(0),m_pString(NULL),mutex(NULL) { assign(str.c_str(), str.length()); }    CStr(const char* str):m_nLength(0),m_pString(NULL),mutex(NULL) { assign(str, length(str)); }#if (C_HAVE_WXGUI)    CStr(const wxString& str):m_nLength(0),m_pString(NULL),mutex(NULL) { assign(str.c_str(), str.length()); }#endif    ~CStr() { if(m_pString) free(m_pString); if(mutex) { MUTEX_DESTROY(mutex); } }    // Operator overloading    CStr& operator  =(const char* str) { assign(str, length(str)); return *this; }    CStr& operator  =(const CStr& str) { assign(str.m_pString, str.m_nLength); return *this; }    CStr& operator  =(const std::string& str) { assign(str.c_str(), str.length()); return *this; }#if (C_HAVE_WXGUI)    CStr& operator  =(const wxString& str) { assign(str.c_str(), str.length()); return *this; }#endif    CStr& operator +=(const char c) { append(&c, 1); return *this; }    CStr& operator +=(const char* str) { append(str, length(str)); return *this; }    CStr& operator +=(const CStr& str) { append(str.m_pString, str.m_nLength); return *this;}    bool operator ==(char* str) {	mutex_lock();	bool ret = (strcmp(str, m_pString) == 0);	mutex_unlock();	return ret;    }    bool operator !=(char* str) {	mutex_lock();	bool ret = (strcmp(str, m_pString) != 0);	mutex_unlock();	return ret;    }    bool operator ==(CStr& str) {	mutex_lock();	bool ret = (strcmp(str.m_pString, m_pString) == 0);	mutex_unlock();	return ret;    }    bool operator !=(CStr& str) {	mutex_lock();	bool ret = (strcmp(str.m_pString, m_pString) != 0);	mutex_unlock();	return ret;    }    // C type string conversion    operator const char * () const { if(m_pString) return m_pString; return (char*)&m_pString;}    const char * c_str() const { if(m_pString) return m_pString; return (char*)&m_pString; }    void append(const char * str) {	append(str, length(str));    }    void assign(const char * str) {	assign(str, length(str));    }    bool empty() const {	return (m_nLength == 0);    }    int length() const {	return m_nLength;    }    const char * replace(char s, char r) {	char * ret = strrchr(m_pString, s);	if(ret == NULL) return ret;	*ret = r;	return ++ret;    }    void clear() {	mutex_lock();	if(m_pString) free(m_pString);	m_pString = NULL;	m_nLength = 0;	mutex_unlock();    }    iterator begin() const {	iterator tmp(this, 0);	return tmp;    }    iterator end() const {	iterator tmp(this, m_nLength);	return tmp;    }    iterator last() const {	iterator tmp(this, m_nLength-1);	return tmp;    }    // format string    int Format(const char* fmt, ...) {	int n = 0;	char * np;	va_list ap;	if(fmt == NULL) return 0;	mutex_lock();	if(m_pString == NULL) {	    m_nLength = 15;	    if((m_pString = (char*)malloc(m_nLength + 1)) == NULL)		m_nLength = 0;	}	while(m_pString) {	    /* Try to print in the allocated space. */	    va_start(ap, fmt);	    n = vsnprintf(m_pString, m_nLength + 1, fmt, ap);	    va_end(ap);	    /* If that worked, return the string. */	    if (n > -1 && n <= m_nLength) {		m_nLength = n;		break;	    }	    /* Else try again with more space. */	    if(n > -1) /* glibc 2.1 */		m_nLength  = n; /* precisely what is needed */	    else /* glibc 2.0 */		m_nLength *= 2; /* twice the old size */	    if((np = (char*)realloc(m_pString, m_nLength + 1)) == NULL) {		free(m_pString); m_pString = NULL;		m_nLength = 0; n = 0;		break;	    }	    m_pString = np;	}	mutex_unlock();	return n;    }};#undef MODULE#endif

⌨️ 快捷键说明

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