X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2FKM_util.cpp;h=0f8976a159a8ad9cb58a40034b3903d75af5572b;hb=6159b54a6ea46408a71c74b7c0a999c9ff5449e5;hp=82287390f609f3e0db40d6900928a3690ce087fb;hpb=49e1347e14c2ea6ee69d7f879d2d05448da50403;p=asdcplib.git diff --git a/src/KM_util.cpp b/src/KM_util.cpp index 8228739..0f8976a 100755 --- a/src/KM_util.cpp +++ b/src/KM_util.cpp @@ -45,18 +45,18 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. struct map_entry_t { - long rcode; + int rcode; Kumu::Result_t* result; }; -const ui32_t MapMax = 512; +const ui32_t MapMax = 1024; const ui32_t MapSize = MapMax * (sizeof(struct map_entry_t)); static bool s_MapInit = false; static struct map_entry_t s_ResultMap[MapSize]; // const Kumu::Result_t& -Kumu::Result_t::Find(long v) +Kumu::Result_t::Find(int v) { if ( v == 0 ) return RESULT_OK; @@ -72,9 +72,38 @@ Kumu::Result_t::Find(long v) } // -Kumu::Result_t::Result_t(long v, const char* l) : value(v), label(l) +Kumu::Result_t +Kumu::Result_t::Delete(int v) +{ + if ( v >= RESULT_NOTAFILE.Value() ) + { + DefaultLogSink().Error("Cannot delete core result code: %ld\n", v); + return RESULT_FAIL; + } + + for ( ui32_t i = 0; s_ResultMap[i].result != 0 && i < MapMax; i++ ) + { + if ( s_ResultMap[i].rcode == v ) + { + s_ResultMap[i].rcode = 0; + s_ResultMap[i++].result = 0; + + for ( ; s_ResultMap[i].result != 0 && i < MapMax; i++ ) + s_ResultMap[i-1] = s_ResultMap[i]; + + return RESULT_OK; + } + } + + return RESULT_FALSE; +} + + +// +Kumu::Result_t::Result_t(int v, const char* l) : value(v), label(l) { assert(l); + assert(value < (int)MapMax); if ( v == 0 ) return; @@ -110,6 +139,26 @@ Kumu::Result_t::Result_t(long v, const char* l) : value(v), label(l) Kumu::Result_t::~Result_t() {} +//------------------------------------------------------------------------------------------ +// DTrace internals + +static int s_DTraceSequence = 0; + +Kumu::DTrace_t::DTrace_t(const char* Label, Kumu::Result_t* Watch, int Line, const char* File) + : m_Label(Label), m_Watch(Watch), m_Line(Line), m_File(File) +{ + m_Sequence = s_DTraceSequence++; + DefaultLogSink().Debug("@enter %s[%d] (%s at %d)\n", m_Label, m_Sequence, m_File, m_Line); +} + +Kumu::DTrace_t::~DTrace_t() +{ + if ( m_Watch != 0 ) + DefaultLogSink().Debug("@exit %s[%d]: %s\n", m_Label, m_Sequence, m_Watch->Label()); + else + DefaultLogSink().Debug("@exit %s[%d]\n", m_Label, m_Sequence); +} + //------------------------------------------------------------------------------------------ @@ -829,6 +878,7 @@ Kumu::Timestamp::DecodeString(const char* datestr) || ! ( isdigit(datestr[8]) && isdigit(datestr[9]) ) ) return false; + ui32_t char_count = 10; TmpStamp.Year = atoi(datestr); TmpStamp.Month = atoi(datestr + 5); TmpStamp.Day = atoi(datestr + 8); @@ -841,6 +891,7 @@ Kumu::Timestamp::DecodeString(const char* datestr) || ! ( isdigit(datestr[14]) && isdigit(datestr[15]) ) ) return false; + char_count += 6; TmpStamp.Hour = atoi(datestr + 11); TmpStamp.Minute = atoi(datestr + 14); @@ -849,28 +900,46 @@ Kumu::Timestamp::DecodeString(const char* datestr) if ( ! ( isdigit(datestr[17]) && isdigit(datestr[18]) ) ) return false; + char_count += 3; TmpStamp.Second = atoi(datestr + 17); } - } - if ( datestr[19] == '-' || datestr[19] == '+' ) - { - if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) ) - || datestr[22] != ':' - || ! ( isdigit(datestr[23]) && isdigit(datestr[24]) ) ) - return false; + if ( datestr[19] == '.' ) + { + if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) && isdigit(datestr[22]) ) ) + return false; + + // we don't carry the ms value + datestr += 4; + } - ui32_t TZ_hh = atoi(datestr + 20); - ui32_t TZ_mm = atoi(datestr + 23); + if ( datestr[19] == '-' || datestr[19] == '+' ) + { + if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) ) + || datestr[22] != ':' + || ! ( isdigit(datestr[23]) && isdigit(datestr[24]) ) ) + return false; + + char_count += 6; + ui32_t TZ_hh = atoi(datestr + 20); + ui32_t TZ_mm = atoi(datestr + 23); - if ( TZ_mm != 0 ) - DefaultLogSink().Error("Ignoring minutes in timezone offset: %u\n", TZ_mm); + if ( TZ_mm != 0 ) + DefaultLogSink().Warn("Ignoring minutes in timezone offset: %u\n", TZ_mm); + + if ( TZ_hh > 12 ) + return false; - if ( TZ_hh > 12 ) - return false; + else + AddHours( (datestr[19] == '-' ? (0 - TZ_hh) : TZ_hh)); + } + } - else - AddHours( (datestr[19] == '-' ? (-TZ_hh) : TZ_hh)); + if ( datestr[char_count] != 0 ) + { + DefaultLogSink().Error("Unexpected extra characters in string: %s (%ld)\n", + datestr, char_count); + return false; } #ifdef KM_WIN32 @@ -921,31 +990,6 @@ Kumu::Timestamp::Archive(MemIOWriter* Writer) const return true; } -#if 0 -// -bool -Kumu::UnarchiveString(MemIOReader* Reader, std::string&) -{ - assert(Reader); - ui32_t str_length; - if ( ! Reader->ReadUi32BE(&str_length) ) return false; - assign((const char*)Reader->CurrentData(), str_length); - if ( ! Reader->SkipOffset(str_length) ) return false; - return true; -} - -// -bool -Kumu::String::Archive(MemIOWriter* Writer) const -{ - assert(Writer); - if ( ! Writer->WriteUi32BE(length()) ) return false; - if ( ! Writer->WriteRaw((const byte_t*)c_str(), length()) ) return false; - - return true; -} -#endif - //------------------------------------------------------------------------------------------ Kumu::MemIOWriter::MemIOWriter(ByteString* Buf) @@ -1041,24 +1085,32 @@ Kumu::ByteString::Set(const ByteString& Buf) // Sets the size of the internally allocate buffer. -// Resets content length to zero. Kumu::Result_t Kumu::ByteString::Capacity(ui32_t cap_size) { - if ( m_Capacity < cap_size ) + if ( m_Capacity >= cap_size ) + return RESULT_OK; + + byte_t* tmp_data = 0; + if ( m_Data != 0 ) { - if ( m_Data != 0 ) + if ( m_Length > 0 ) + tmp_data = m_Data; + else free(m_Data); + } - m_Data = (byte_t*)malloc(cap_size); - - if ( m_Data == 0 ) - return RESULT_ALLOC; - - m_Capacity = cap_size; - m_Length = 0; + if ( ( m_Data = (byte_t*)malloc(cap_size) ) == 0 ) + return RESULT_ALLOC; + + if ( tmp_data != 0 ) + { + assert(m_Length > 0); + memcpy(m_Data, tmp_data, m_Length); + free(tmp_data); } - + + m_Capacity = cap_size; return RESULT_OK; }