- 1 名前:デフォルトの名無しさん [2006/12/26(火) 20:24:15 ]
- CString , string , wstringに負けないものをみんなで作ろうね。
前スレ: pc8.2ch.net/test/read.cgi/tech/1044098312/
- 57 名前:デフォルトの名無しさん mailto:sage [2007/07/30(月) 18:52:29 ]
- #include <windows.h>
#include <stdio.h> // simple string class class TSTR { private: char* Memory; int buff_len; size_t CalcBuffSize(const char* s, const char* a=NULL) { size_t size = s? strlen(s)+1: 0; return a ? size+1 + strlen(a)+1: size; } void Copy(const char* s, const size_t length) { if (!s) return; size_t new_size = CalcBuffSize(s); if (length>0 && length < new_size) new_size = length + 1; if (Memory!=NULL) free(Memory); Memory = (char*)malloc(new_size); if (length==0) strcpy(Memory, s); else { strncpy(Memory, s, length); Memory[length+1] = 0; } buff_len = strlen(Memory); } void Copy(const char* s){ Copy(s, 0); } void Add(const char* s) { if (!s) return; size_t new_size = CalcBuffSize(Memory, s); Memory = (char*)realloc(Memory, new_size); strcat(Memory, s); buff_len = strlen(Memory); } char* MakeBuff(size_t size) { if (Memory!=NULL) free(Memory); Memory = (char*)calloc(size, 1); return Memory; }
- 58 名前:デフォルトの名無しさん mailto:sage [2007/07/30(月) 18:53:59 ]
- public:
__declspec(property(get=buff_len)) int len; __declspec(property(get=Memory,put=Copy)) char* str; TSTR() { buff_len = 0; Memory = NULL; } TSTR(const TSTR &s) { buff_len = 0; Memory = NULL; Copy(s.Memory); } TSTR(const char* s) { buff_len = 0; Memory = NULL; Copy(s); } TSTR(const char* s, size_t length) { buff_len = 0; Memory = NULL; Copy(s, length); } ~TSTR() { if (Memory!=NULL) free(Memory); } char* Sprintf(const char* fom, ...) { va_list ap; va_start(ap, fom); vsprintf(MakeBuff(strlen(fom)+1024), fom, ap); va_end(ap); return Memory; } char operator [](const int idx) { return (char)(Memory ? Memory[idx]: 0); } bool operator==(const char* s) const { return (strcmp(Memory, s)==0); } bool operator >(const char* s) const { return (strcmp(Memory, s)>0); } bool operator <(const char* s) const { return (strcmp(Memory, s)<0); } bool operator >=(const char* s) const { return (strcmp(Memory, s)>=0); } bool operator <=(const char* s) const { return (strcmp(Memory, s)<=0); } bool operator !=(const char* s) const { return (strcmp(Memory, s)!=0); }
- 59 名前:デフォルトの名無しさん mailto:sage [2007/07/30(月) 18:54:15 ]
- bool operator==(const TSTR &s) const { return (strcmp(Memory, s.Memory)==0); }
bool operator >(const TSTR &s) const { return (strcmp(Memory, s.Memory)>0); } bool operator <(const TSTR &s) const { return (strcmp(Memory, s.Memory)<0); } bool operator >=(const TSTR &s) const { return (strcmp(Memory, s.Memory)>=0); } bool operator <=(const TSTR &s) const { return (strcmp(Memory, s.Memory)<=0); } bool operator !=(const TSTR &s) const { return (strcmp(Memory, s.Memory)!=0); } char* operator =(const char* s) { Copy(s); return this->Memory; } TSTR& operator =(const TSTR& s) { Copy(s.Memory); return *this; } TSTR& operator +=(const TSTR& s) { Add(s.Memory); return *this; } TSTR& operator +=(const char* s) { Add(s); return *this; } friend const TSTR operator + (const char* ls, const TSTR& rs) { TSTR l(ls); l.Add(rs.Memory); return l; } friend const TSTR operator + (const TSTR& ls, const char* rs) { TSTR l(ls); l.Add(rs); return l; } TSTR operator +(const TSTR& rs) const { TSTR a(Memory); a.Add(rs.Memory); return a; } char* operator +(const char* rs) const { TSTR a(Memory); a.Add(rs); return a.Memory; } };
|

|