str.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef STR_HPP
00013 #define STR_HPP
00014
00015 #include <errno.h>
00016 #include <stdarg.h>
00017 #include "../string_func.h"
00018
00020 struct CStrA : public CBlobT<char>
00021 {
00022 typedef CBlobT<char> base;
00023
00025 FORCEINLINE CStrA()
00026 {
00027 }
00028
00030 FORCEINLINE CStrA(const OnTransfer& ot)
00031 : base(ot)
00032 {
00033 }
00034
00036 FORCEINLINE char *GrowSizeNC(bsize_t count)
00037 {
00038 char *ret = base::GrowSizeNC(count);
00039 base::FixTail();
00040 return ret;
00041 }
00042
00044 FORCEINLINE void AppendStr(const char *str)
00045 {
00046 if (!StrEmpty(str)) {
00047 base::Append(str, strlen(str));
00048 base::FixTail();
00049 }
00050 }
00051
00053 FORCEINLINE CStrA& operator = (const char *src)
00054 {
00055 base::Clear();
00056 AppendStr(src);
00057 return *this;
00058 }
00059
00061 FORCEINLINE bool operator < (const CStrA &other) const
00062 {
00063 return strcmp(base::Data(), other.Data()) < 0;
00064 }
00065
00067 int AddFormatL(const char *format, va_list args)
00068 {
00069 bsize_t addSize = max<size_t>(strlen(format), 16);
00070 addSize += addSize / 2;
00071 int ret;
00072 int err = 0;
00073 for (;;) {
00074 char *buf = MakeFreeSpace(addSize);
00075 ret = vsnprintf(buf, base::GetReserve(), format, args);
00076 if (ret >= base::GetReserve()) {
00077
00078 addSize = ret + 1;
00079 continue;
00080 }
00081 if (ret >= 0) {
00082
00083 break;
00084 }
00085 err = errno;
00086 if (err != ERANGE && err != ENOENT && err != 0) {
00087
00088 break;
00089 }
00090
00091 addSize *= 2;
00092 }
00093 if (ret > 0) {
00094 GrowSizeNC(ret);
00095 } else {
00096 base::FixTail();
00097 }
00098 return ret;
00099 }
00100
00102 int AddFormat(const char *format, ...)
00103 {
00104 va_list args;
00105 va_start(args, format);
00106 int ret = AddFormatL(format, args);
00107 va_end(args);
00108 return ret;
00109 }
00110
00112 int Format(const char *format, ...)
00113 {
00114 base::Free();
00115 va_list args;
00116 va_start(args, format);
00117 int ret = AddFormatL(format, args);
00118 va_end(args);
00119 return ret;
00120 }
00121 };
00122
00123 #endif