summaryrefslogtreecommitdiff
path: root/asdcplib/src/KM_util.cpp
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-01-04 14:48:18 +0000
committerCarl Hetherington <cth@carlh.net>2016-01-04 14:48:18 +0000
commit086b83c4132189120b7f1685cb39efba56d71c2c (patch)
treed42475c7e133c4ad078eb8da3acd0ee2b6206942 /asdcplib/src/KM_util.cpp
parent5c92b6dbf3bc7d905938ad9972aec5f0ee106935 (diff)
Copy asdcplib 2.5.11 into the tree.
Diffstat (limited to 'asdcplib/src/KM_util.cpp')
-rwxr-xr-xasdcplib/src/KM_util.cpp190
1 files changed, 150 insertions, 40 deletions
diff --git a/asdcplib/src/KM_util.cpp b/asdcplib/src/KM_util.cpp
index b1814840..f738814f 100755
--- a/asdcplib/src/KM_util.cpp
+++ b/asdcplib/src/KM_util.cpp
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2005-2012, John Hurst
+Copyright (c) 2005-2015, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file KM_util.cpp
- \version $Id: KM_util.cpp,v 1.40 2012/02/22 19:20:33 jhurst Exp $
+ \version $Id: KM_util.cpp,v 1.47 2015/10/12 15:30:46 jhurst Exp $
\brief Utility functions
*/
@@ -40,8 +40,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <map>
#include <string>
-bool Kumu::libdcp_test = false;
-
const char*
Kumu::Version()
{
@@ -131,10 +129,10 @@ Kumu::Result_t::Get(unsigned int i)
}
//
-Kumu::Result_t::Result_t(int v, const char* s, const char* l) : value(v), label(l), symbol(s)
+Kumu::Result_t::Result_t(int v, const std::string& s, const std::string& l) : value(v), symbol(s), label(l)
{
- assert(l);
- assert(s);
+ assert(!l.empty());
+ assert(!s.empty());
if ( v == 0 )
return;
@@ -164,8 +162,65 @@ Kumu::Result_t::Result_t(int v, const char* s, const char* l) : value(v), label(
return;
}
+
+Kumu::Result_t::Result_t(const Result_t& rhs)
+{
+ value = rhs.value;
+ symbol = rhs.symbol;
+ label = rhs.label;
+ message = rhs.message;
+}
+
Kumu::Result_t::~Result_t() {}
+//
+const Kumu::Result_t&
+Kumu::Result_t::operator=(const Result_t& rhs)
+{
+ value = rhs.value;
+ symbol = rhs.symbol;
+ label = rhs.label;
+ message = rhs.message;
+ return *this;
+}
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const std::string& message) const
+{
+ Result_t result = *this;
+ result.message = message;
+ return result;
+}
+
+static int const MESSAGE_BUF_MAX = 2048;
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const int& line, const char* filename) const
+{
+ assert(filename);
+ char buf[MESSAGE_BUF_MAX];
+ snprintf(buf, MESSAGE_BUF_MAX-1, "%s, line %d", filename, line);
+
+ Result_t result = *this;
+ result.message = buf;
+ return result;
+}
+
+//
+const Kumu::Result_t
+Kumu::Result_t::operator()(const std::string& message, const int& line, const char* filename) const
+{
+ assert(filename);
+ char buf[MESSAGE_BUF_MAX];
+ snprintf(buf, MESSAGE_BUF_MAX-1, "%s, line %d", filename, line);
+
+ Result_t result = *this;
+ result.message = message + buf;
+ return result;
+}
+
//------------------------------------------------------------------------------------------
// DTrace internals
@@ -534,15 +589,6 @@ Kumu::GenRandomValue(UUID& ID)
ID.Set(tmp_buf);
}
-#ifdef LIBDCP_POSIX
-void
-Kumu::ResetTestRNG()
-{
- FortunaRNG RNG;
- RNG.Reset();
-}
-#endif
-
//
void
Kumu::GenRandomUUID(byte_t* buf)
@@ -699,14 +745,7 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
//
Kumu::Timestamp::Timestamp() : m_TZOffsetMinutes(0) {
- if (libdcp_test)
- {
- m_Timestamp.x = 42;
- }
- else
- {
- m_Timestamp.now();
- }
+ m_Timestamp.now();
}
Kumu::Timestamp::Timestamp(const Timestamp& rhs) {
@@ -814,8 +853,8 @@ Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
tmp_t.AddMinutes(m_TZOffsetMinutes);
tmp_t.GetComponents(year, month, day, hour, minute, second);
- ofst_hours = abs(m_TZOffsetMinutes) / 60;
- ofst_minutes = abs(m_TZOffsetMinutes) % 60;
+ ofst_hours = Kumu::xabs(m_TZOffsetMinutes) / 60;
+ ofst_minutes = Kumu::xabs(m_TZOffsetMinutes) % 60;
if ( m_TZOffsetMinutes < 0 )
direction = '-';
@@ -823,14 +862,14 @@ Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
// 2004-05-01T13:20:00+00:00
snprintf(str_buf, buf_len,
- "%04hu-%02hu-%02huT%02hu:%02hu:%02hu%c%02hu:%02hu",
+ "%04hu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu%c%02u:%02u",
year, month, day, hour, minute, second,
direction, ofst_hours, ofst_minutes);
return str_buf;
}
-//
+// ^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(?:([+-]\d{2}):(\d{2}))?)?$
bool
Kumu::Timestamp::DecodeString(const char* datestr)
{
@@ -843,10 +882,13 @@ Kumu::Timestamp::DecodeString(const char* datestr)
ui32_t char_count = 10;
TAI::caltime YMDhms;
+ YMDhms.hour = 0;
+ YMDhms.minute = 0;
+ YMDhms.second = 0;
YMDhms.offset = 0;
- YMDhms.date.year = atoi(datestr);
- YMDhms.date.month = atoi(datestr + 5);
- YMDhms.date.day = atoi(datestr + 8);
+ YMDhms.date.year = strtol(datestr, 0, 10);
+ YMDhms.date.month = strtol(datestr + 5, 0, 10);
+ YMDhms.date.day = strtol(datestr + 8, 0, 10);
if ( datestr[10] == 'T' )
{
@@ -856,8 +898,8 @@ Kumu::Timestamp::DecodeString(const char* datestr)
return false;
char_count += 6;
- YMDhms.hour = atoi(datestr + 11);
- YMDhms.minute = atoi(datestr + 14);
+ YMDhms.hour = strtol(datestr + 11, 0, 10);
+ YMDhms.minute = strtol(datestr + 14, 0, 10);
if ( datestr[16] == ':' )
{
@@ -865,16 +907,23 @@ Kumu::Timestamp::DecodeString(const char* datestr)
return false;
char_count += 3;
- YMDhms.second = atoi(datestr + 17);
+ YMDhms.second = strtol(datestr + 17, 0, 10);
}
if ( datestr[19] == '.' )
{
- if ( ! ( isdigit(datestr[20]) && isdigit(datestr[21]) && isdigit(datestr[22]) ) )
- return false;
-
+ if ( ! isdigit(datestr[20]) )
+ {
+ return false;
+ }
+
// we don't carry the ms value
- datestr += 4;
+ while ( isdigit(datestr[20]) )
+ {
+ ++datestr;
+ }
+
+ ++datestr;
}
if ( datestr[19] == '-' || datestr[19] == '+' )
@@ -886,8 +935,8 @@ Kumu::Timestamp::DecodeString(const char* datestr)
char_count += 6;
- ui32_t TZ_hh = atoi(datestr + 20);
- ui32_t TZ_mm = atoi(datestr + 23);
+ ui32_t TZ_hh = strtol(datestr + 20, 0, 10);
+ ui32_t TZ_mm = strtol(datestr + 23, 0, 10);
if ((TZ_hh > 14) || (TZ_mm > 59) || ((TZ_hh == 14) && (TZ_mm > 0)))
return false;
@@ -972,6 +1021,15 @@ Kumu::Timestamp::GetCTime() const
return m_Timestamp.x - ui64_C(4611686018427387914);
}
+//
+void
+Kumu::Timestamp::SetCTime(const ui64_t& ctime)
+{
+ m_Timestamp.x = ctime + ui64_C(4611686018427387914);
+}
+
+
+
//------------------------------------------------------------------------------------------
@@ -1135,6 +1193,58 @@ Kumu::ByteString::Append(const byte_t* buf, ui32_t buf_len)
return result;
}
+//------------------------------------------------------------------------------------------
+
+//
+const char*
+Kumu::km_strnstr(const char *s, const char *find, size_t slen)
+{
+ char c, sc;
+ size_t len;
+
+ if ( ( c = *find++ ) != '\0' )
+ {
+ len = strlen(find);
+ do
+ {
+ do
+ {
+ if ( slen-- < 1 || ( sc = *s++ ) == '\0' )
+ return 0;
+ }
+ while ( sc != c );
+
+ if ( len > slen )
+ return 0;
+ }
+ while ( strncmp(s, find, len) != 0 );
+ --s;
+ }
+
+ return s;
+}
+
+//
+std::list<std::string>
+Kumu::km_token_split(const std::string& str, const std::string& separator)
+{
+ std::list<std::string> components;
+ const char* pstr = str.c_str();
+ const char* r = strstr(pstr, separator.c_str());
+
+ while ( r != 0 )
+ {
+ assert(r >= pstr);
+ std::string tmp_str;
+ tmp_str.assign(pstr, r - pstr);
+ components.push_back(tmp_str);
+ pstr = r + separator.size();
+ r = strstr(pstr, separator.c_str());
+ }
+
+ components.push_back(std::string(pstr));
+ return components;
+}
//
// end KM_util.cpp