Guidance
指路人
g.yi.org
software / RapidQ / System / Win32 / Rapid-Q++ / rqstring.h

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
#if !defined RQSTRING_H
#define RQSTRING_H

#include <iostream.h>
#include <stdlib.h>
#include "rqdefs.h"


// BASIC String class by William Yu
// Feel free to use it and modify it anyway you want
// There's already a GNU string class, but I just wanted something that's
// more generic (ie. for Rapid-Q) and cross compilable.
//------------------------------------------------------------------------
class RQString
{
protected:
    char *str;

public:
    RQString();
    RQString(const char*);
    RQString(const RQString&);
    RQString(const char*, const char*);      // Sum strings
    ~RQString();

    int32 length() { return strlen(str); }
    char* toString() { return str; }
    RQString& operator=(const char*);
    RQString& operator=(const RQString&);
    friend RQString operator+(const char*, const RQString&);
    friend RQString operator+(const RQString&, const char*);
    friend RQString operator+(const RQString&, const RQString&);
    friend ostream& operator<<(ostream& os, const RQString&);
    friend int operator==(const RQString&, const RQString&);
    friend int operator>=(const RQString&, const RQString&);
    friend int operator<=(const RQString&, const RQString&);
    friend int operator!=(const RQString&, const RQString&);
};


//-- Implementation

RQString::RQString() {
    str = (char*)calloc(1,1);
}

RQString::~RQString() {
    free(str);
}

RQString::RQString(const char* s) {
    str = strdup(s);
}

RQString::RQString(const RQString& s) {
    str = strdup(s.str);
}

RQString::RQString(const char* s1, const char* s2) {
    str = (char*)malloc(strlen(s1)+strlen(s2)+1);
    strcpy(str, s1);
    strcat(str, s2);
}

RQString& RQString::operator=(const char* s) {
    str = (char*)realloc(str, strlen(s)+1);
    strcpy(str, s);
    return *this;
}

RQString& RQString::operator=(const RQString& s) {
    str = (char*)realloc(str, strlen(s.str)+1);
    strcpy(str, s.str);
    return *this;
}

RQString operator+(const char* s, const RQString& rqs) {
    RQString *rstr = new RQString(s, rqs.str);
    return *rstr;
}

RQString operator+(const RQString& rqs, const char* s) {
    RQString *rstr = new RQString(rqs.str, s);
    return *rstr;
}

RQString operator+(const RQString& rqs1, const RQString& rqs2) {
    RQString *rstr = new RQString(rqs1.str, rqs2.str);
    return *rstr;
}

ostream& operator<<(ostream& os, const RQString& s) {
    return os << s.str;
}

int operator==(const RQString& s1, const RQString& s2) {
    if (strcmp(s1.str, s2.str) == 0) return -1;
    return 0;
}

int operator>=(const RQString& s1, const RQString& s2) {
    if (strcmp(s1.str, s2.str) >= 0) return -1;
    return 0;
}

int operator<=(const RQString& s1, const RQString& s2) {
    if (strcmp(s1.str, s2.str) <= 0) return -1;
    return 0;
}

int operator!=(const RQString& s1, const RQString& s2) {
    if (strcmp(s1.str, s2.str) != 0) return -1;
    return 0;
}

#endif
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-4-25  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2013-08-20 12:34:44