/home/coin/SVN-release/CoinAll-1.1.0/Bcp/src/include/BCP_string.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef _BCP_STRING_H
00004 #define _BCP_STRING_H
00005 
00006 // This file is fully docified.
00007 
00008 #include <cstring>
00009 
00013 class BCP_string {
00014 public:
00015    /* Return the length of the string. */
00016    int length() const         { return _len; }
00017    /* Return a pointer to the data stored in the string. I.e., return a C
00018       style string. */
00019    const char * c_str() const { return _data; }
00020 private:
00021    /* the length of the string */
00022    int    _len;
00023    /* the data in the string */
00024    char * _data;
00025 public:
00026    /* The default constructor creates an empty sting. */
00027    BCP_string() : _len(0), _data(0) {};
00028    /* Create a <code>BCP_string</code> from a C style string. */
00029    BCP_string(const char * str) {
00030       _len = strlen(str);
00031       _data = new char[_len+1];
00032       memcpy(_data, str, _len);
00033       _data[_len] = 0;
00034    }
00035    /* Make a copy of the argument string. */
00036    BCP_string(const BCP_string& str) {
00037       _len = str.length();
00038       _data = new char[_len+1];
00039       memcpy(_data, str.c_str(), _len);
00040       _data[_len] = 0;
00041    }
00042    /* Delete the data members. */
00043    ~BCP_string() {
00044       delete[] _data;
00045    }
00046    /* This methods replaces the current <code>BCP_string</code> with one
00047       create from the first <code>len</code> bytes in <code>source</code>. */
00048    BCP_string& assign(const char * source, const int len) {
00049       delete[] _data;
00050       _len = len;
00051       _data = new char[_len+1];
00052       memcpy(_data, source, _len);
00053       _data[_len] = 0;
00054       return *this;
00055    }
00056    /* replace the current <code>BCP_string</code> with a copy of the argument
00057     */ 
00058    BCP_string& operator= (const BCP_string& str) {
00059       return assign(str.c_str(), str.length());
00060    }
00061    /* replace the current <code>BCP_string</code> with a copy of the argument
00062       C style string. */ 
00063    BCP_string& operator= (const char * str) {
00064       return assign(str, strlen(str));
00065    }
00066 
00067 };
00068 
00070 inline bool operator==(const BCP_string& s0, const char* s1) {
00071    if (s0.c_str() == 0)
00072       return s1 == 0;
00073    return s1 == 0 ? false : (strcmp(s0.c_str(), s1) == 0);
00074 }
00075 
00077 inline bool
00078 operator==(const BCP_string& s0, const BCP_string& s1) {
00079    return s0 == s1.c_str();
00080 }
00081 
00083 inline bool
00084 operator==(const char* s0, const BCP_string& s1) {
00085    return s1 == s0;
00086 }
00087 
00088 #endif

Generated on Sun Nov 14 14:06:29 2010 for Coin-All by  doxygen 1.4.7