From fc3ee97f1ee4d24c2918761d5072abecb84724df Mon Sep 17 00:00:00 2001 From: nnhy Date: Sat, 4 Jun 2016 11:09:44 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Array.Expand=EF=BC=8C?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E6=95=B0=E7=BB=84=E6=98=AF=E5=90=A6=E5=8F=AF?= =?UTF-8?q?=E6=89=A9=E5=AE=B9=E3=80=82=20=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E6=9E=84=E9=80=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=85=81=E8=AE=B8=E5=A4=96=E9=83=A8=E8=AE=BE?= =?UTF-8?q?=E5=AE=9A=E4=B8=80=E4=B8=AA=E7=BC=93=E5=86=B2=E5=8C=BA=E4=B8=94?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E6=89=A9=E5=AE=B9=EF=BC=8C=E9=80=82=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E4=BB=A4=E7=89=8C=E9=85=8D=E7=BD=AE=E3=80=82=20?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Array.cpp | 4 ++++ Core/Array.h | 2 ++ Core/SString.h | 3 ++- Core/String.cpp | 20 ++++++++++++++------ Core/Type.cpp | 1 + Drivers/Esp8266.cpp | 2 +- TokenNet/TokenConfig.cpp | 4 +++- TokenNet/TokenConfig.h | 10 +++++----- TokenNet/TokenController.cpp | 1 + 9 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Core/Array.cpp b/Core/Array.cpp index 6a8a7f94..b1f4e08b 100644 --- a/Core/Array.cpp +++ b/Core/Array.cpp @@ -43,6 +43,7 @@ void Array::move(Array& rval) _Capacity = rval._Capacity; _needFree = rval._needFree; _canWrite = rval._canWrite; + Expand = rval.Expand; rval._Capacity = 0; rval._needFree = false; @@ -51,6 +52,7 @@ void Array::move(Array& rval) void Array::Init() { + Expand = true; _Size = 1; _Capacity = _Length; @@ -276,6 +278,8 @@ bool Array::CheckCapacity(int len, int bak) // 是否超出容量 // 如果不是可写,在扩容检查时,也要进行扩容,避免内部不可写数据被修改 if(_Arr && len <= _Capacity && _canWrite) return true; + // 是否可以扩容 + if(!Expand) return false; // 自动计算合适的容量 int sz = 0x40; diff --git a/Core/Array.h b/Core/Array.h index dc8c6dc9..ebdde735 100644 --- a/Core/Array.h +++ b/Core/Array.h @@ -7,6 +7,8 @@ class Array : public Buffer { public: + bool Expand; // 是否可扩容 + // 数组最大容量。初始化时决定,后面不允许改变 inline int Capacity() const { return _Capacity; } diff --git a/Core/SString.h b/Core/SString.h index 6256d105..9ef2c090 100644 --- a/Core/SString.h +++ b/Core/SString.h @@ -14,8 +14,9 @@ public: String(cstring cstr = ""); String(const String& str); String(String&& rval); - // 外部传入缓冲区供内部使用,注意长度减去零结束符 + // 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符 String(char* str, int length); + String(char* str, int length, bool expand); // 包装静态字符串,直接使用,修改时扩容 String(cstring str, int length); explicit String(char c); diff --git a/Core/String.cpp b/Core/String.cpp index 2afc0d81..87d31ee5 100644 --- a/Core/String.cpp +++ b/Core/String.cpp @@ -120,15 +120,23 @@ String::String(double value, byte decimalPlaces) : Array(Arr, ArrayLength(Arr)) //dtostrf(value, (decimalPlaces + 2), decimalPlaces, _Arr); } -// 外部传入缓冲区供内部使用,注意长度减去零结束符 +// 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符 String::String(char* str, int length) : Array(str, length) { - //init(); - - _Arr = str; + _Arr = str; _Capacity = length - 1; - _Length = 0; - _Arr[0] = '\0'; + + // 计算外部字符串长度 + int len = strlen(str); + if(len >= length) len = length - 1; + _Length = len; + _Arr[_Length] = '\0'; +} + +// 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符 +String::String(char* str, int length, bool expand) : String(str, length) +{ + Expand = expand; } // 包装静态字符串,直接使用,修改时扩容 diff --git a/Core/Type.cpp b/Core/Type.cpp index a22c5b8a..af3f223e 100644 --- a/Core/Type.cpp +++ b/Core/Type.cpp @@ -36,6 +36,7 @@ void Object::Show(bool newLine) const // 为了减少堆分配,采用较大的栈缓冲区 char cs[0x200]; String str(cs, ArrayLength(cs)); + str.SetLength(0); ToStr(str); str.Show(newLine); } diff --git a/Drivers/Esp8266.cpp b/Drivers/Esp8266.cpp index 1897d622..9cefe717 100644 --- a/Drivers/Esp8266.cpp +++ b/Drivers/Esp8266.cpp @@ -50,7 +50,7 @@ public: // 收到数据 virtual void OnProcess(const Buffer& bs, const IPEndPoint& remote); - + protected: bool SendData(const String& cmd, const Buffer& bs); }; diff --git a/TokenNet/TokenConfig.cpp b/TokenNet/TokenConfig.cpp index ee6f60b8..ae0cfb65 100644 --- a/TokenNet/TokenConfig.cpp +++ b/TokenNet/TokenConfig.cpp @@ -54,7 +54,9 @@ TokenConfig* TokenConfig::Create(cstring vendor, ProtocolType protocol, ushort s tc.Init(); tc.Protocol = protocol; tc.Load(); - if(tc.Protocol == 0x00)tc.Protocol = ProtocolType::Udp; // 默认 UDP 不允许 unknown + + // 默认 UDP 不允许 unknown + if(tc.Protocol == 0x00) tc.Protocol = ProtocolType::Udp; bool rs = tc.New; auto vnd = tc.Vendor(); diff --git a/TokenNet/TokenConfig.h b/TokenNet/TokenConfig.h index 4aeb9135..1678ae03 100644 --- a/TokenNet/TokenConfig.h +++ b/TokenNet/TokenConfig.h @@ -36,11 +36,11 @@ public: virtual void Init(); virtual void Show() const; - String User() { return String(_User); } - String Pass() { return String(_Pass); } - String Token() { return String(_Token); } - String Server() { return String(_Server); } - String Vendor() { return String(_Vendor); } + String User() { return String(_User, sizeof(_User), false); } + String Pass() { return String(_Pass, sizeof(_Pass), false); } + String Token() { return String(_Token, sizeof(_Token), false); } + String Server() { return String(_Server, sizeof(_Server), false); } + String Vendor() { return String(_Vendor, sizeof(_Vendor), false); } static TokenConfig* Current; static TokenConfig* Create(cstring vendor, ProtocolType protocol, ushort sport, ushort port); diff --git a/TokenNet/TokenController.cpp b/TokenNet/TokenController.cpp index 485b2963..17165e7f 100644 --- a/TokenNet/TokenController.cpp +++ b/TokenNet/TokenController.cpp @@ -589,6 +589,7 @@ void StatTask(void* param) char cs[128]; String str(cs, ArrayLength(cs)); + str.SetLength(0); st->ToStr(str); str.Show(true);