kumu merge
authorjhurst <jhurst@cinecert.com>
Fri, 21 Apr 2006 17:32:06 +0000 (17:32 +0000)
committerjhurst <>
Fri, 21 Apr 2006 17:32:06 +0000 (17:32 +0000)
src/AS_DCP.h
src/KM_error.h
src/KM_fileio.cpp
src/KM_fileio.h
src/KM_prng.cpp
src/KM_util.cpp
src/KM_util.h

index f6574ac154febf218c110acf9b98233f53037c30..deccbbba2bd6fab767e15f15f6e9a539e09b389b 100755 (executable)
@@ -143,7 +143,7 @@ namespace ASDCP {
   // 1.0.1. If changes were also required in AS_DCP.h, the new version would be 1.1.1.
   const ui32_t VERSION_MAJOR = 1;
   const ui32_t VERSION_APIMINOR = 1;
-  const ui32_t VERSION_IMPMINOR = 7;
+  const ui32_t VERSION_IMPMINOR = 8;
   const char* Version();
 
   // UUIDs are passed around as strings of UUIDlen bytes
index 612a92c7dc5452540574025a682701052123c8cd..ed171be988e94c3b30f5ca17539a3cb58efa8a4e 100755 (executable)
@@ -48,7 +48,11 @@ namespace Kumu
       Result_t();
 
     public:
-      Result_t(long v, const char* l) : value(v), label(l) {}
+      static const Result_t& Find(long);
+
+      Result_t(long v, const char* l);
+      ~Result_t();
+
       inline bool        operator==(const Result_t& rhs) const { return value == rhs.value; }
       inline bool        operator!=(const Result_t& rhs) const { return value != rhs.value; }
       inline bool        Success() { return ( value >= 0 ); }
index 1ed32fb7d462a96669ca4edbad6075142dc2af6e..4453e89d59ad48b6ec09c3046370d7ba5725e7cc 100644 (file)
@@ -38,6 +38,8 @@ using namespace Kumu;
 
 #ifdef KM_WIN32
 typedef struct _stati64 fstat_t;
+#define S_IFLNK 0
+
 
 // AFAIK, there is no iovec equivalent in the win32 API
 struct iovec {
@@ -107,7 +109,7 @@ Kumu::PathIsFile(const char* pathname)
 
   if ( KM_SUCCESS(do_stat(pathname, &info)) )
     {
-      if ( info.st_mode & S_IFREG )
+      if ( info.st_mode & ( S_IFREG|S_IFLNK ) )
         return true;
     }
 
@@ -141,7 +143,7 @@ Kumu::FileSize(const char* pathname)
 
   if ( KM_SUCCESS(do_stat(pathname, &info)) )
     {
-      if ( info.st_mode & S_IFREG )
+      if ( info.st_mode & ( S_IFREG|S_IFLNK ) )
         return(info.st_size);
     }
 
@@ -175,7 +177,7 @@ Kumu::FileReader::Size() const
 
   if ( KM_SUCCESS(do_fstat(m_Handle, &info)) )
     {
-      if ( info.st_mode & S_IFREG )
+      if ( info.st_mode & ( S_IFREG|S_IFLNK ) )
         return(info.st_size);
     }
 #endif
@@ -612,7 +614,7 @@ Kumu::Result_t
 Kumu::ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size)
 {
   fsize_t    fsize = 0;
-  ui32_t     read_size;
+  ui32_t     read_size = 0;
   FileReader File;
   ByteString ReadBuf;
 
@@ -624,7 +626,7 @@ Kumu::ReadFileIntoString(const char* filename, std::string& outString, ui32_t ma
     {
       fsize = File.Size();
 
-      if ( fsize > max_size )
+      if ( fsize > (Kumu::fpos_t)max_size )
        return RESULT_ALLOC;
 
       result = ReadBuf.Capacity((ui32_t)fsize);
@@ -640,6 +642,26 @@ Kumu::ReadFileIntoString(const char* filename, std::string& outString, ui32_t ma
 }
 
 
+//
+Kumu::Result_t
+Kumu::WriteStringIntoFile(const char* filename, const std::string& inString)
+{
+  FileWriter File;
+  ui32_t write_count = 0;
+  KM_TEST_NULL_STR(filename);
+
+  Result_t result = File.OpenWrite(filename);
+
+  if ( KM_SUCCESS(result) )
+    result = File.Write((byte_t*)inString.c_str(), inString.length(), &write_count);
+
+  if ( KM_SUCCESS(result) && write_count != inString.length() )
+    return RESULT_WRITEFAIL;
+
+  return RESULT_OK;
+}
+
+
 //------------------------------------------------------------------------------------------
 //
 
index 9c79d4992b5831fb0860747bb47ebd893bd5c526..b268ad79f2e3c042e4424921e3ff62a16ff659bd 100755 (executable)
@@ -113,9 +113,12 @@ namespace Kumu
   bool     PathIsDirectory(const char* pathname);
   fsize_t  FileSize(const char* pathname);
 
-  // reads an entire file into a string
+  // Reads an entire file into a string.
   Result_t ReadFileIntoString(const char* filename, std::string& outString, ui32_t max_size = 256 * Kilobyte);
 
+  // Writes a string to a file, overwrites the existing file if present.
+  Result_t WriteStringIntoFile(const char* filename, const std::string& inString);
+
   //
   class FileReader
     {
index 5e3dd48a2107219dca6fb3d2281c0510874e2880..c51b2dbddf294b1460a41ce9d96eeeb45f913e69 100755 (executable)
@@ -43,18 +43,24 @@ using namespace Kumu;
 #ifdef KM_WIN32
 
 //  make up a byte by sampling the perf counter LSB
-static byte_t get_perf_byte()
+static byte_t get_perf_byte(byte_t carry)
 {
   LARGE_INTEGER ticks;
-  byte_t retval;
-  
-  for ( int i = 0; i < 8; i++ )
+  byte_t sha_buf[20];
+  SHA_CTX SHA;
+  SHA1_Init(&SHA);
+  SHA1_Update(&SHA, &carry, sizeof(byte_t));
+
+  for ( int i = 0; i < 128; i++ )
     {
       QueryPerformanceCounter(&ticks);
-      retval |= (ticks.LowPart & 0x00000001) << i;
+      SHA1_Update(&SHA, &ticks.LowPart, sizeof(ticks.LowPart));
     }
   
-  return retval;
+  SHA1_Final(sha_buf, &SHA);
+
+  fprintf(stderr, "0x%02x ", sha_buf[0]);
+  return sha_buf[0];
 }
 
 #else // KM_WIN32
@@ -92,8 +98,10 @@ public:
 
 #ifdef KM_WIN32
       for ( ui32_t i = 0; i < RNG_KEY_SIZE; i++ )
-       rng_key[i] = get_perf_byte();
-
+       {
+         byte_t carry = ( i == 0 ) ? 0xa3 : rng_key[i-1];
+         rng_key[i] = get_perf_byte(carry);
+       }
 #else // KM_WIN32
       // on POSIX systems we simply read some seed from /dev/urandom
       FileReader URandom;
index c8cbb1c649901656f413e8fc0dfe80adcca8bd0c..bf61f94165242c3686910a118bfe2579b6da06fc 100755 (executable)
@@ -36,8 +36,73 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <KM_log.h>
 #include <ctype.h>
 #include <list>
+#include <map>
 #include <string>
 
+//------------------------------------------------------------------------------------------
+
+// Result_t Internals
+
+struct map_entry_t
+{
+  long            rcode;
+  Kumu::Result_t* result;
+};
+
+const ui32_t MapMax = 512;
+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)
+{
+  for ( ui32_t i = 0; s_ResultMap[i].result != 0 && i < MapMax; i++ )
+    {
+      if ( s_ResultMap[i].rcode == v )
+       return *s_ResultMap[i].result;
+    }
+
+  DefaultLogSink().Error("Unknown result code: %ld\n", v);
+  return RESULT_FAIL;
+}
+
+//
+Kumu::Result_t::Result_t(long v, const char* l) : value(v), label(l)
+{
+  assert(l);
+
+  if ( ! s_MapInit )
+    {
+      s_MapInit = true;
+      s_ResultMap[0].rcode = v;
+      s_ResultMap[0].result = this;
+      s_ResultMap[1].rcode = 0;
+      s_ResultMap[1].result = 0;
+      return;
+    }
+
+  ui32_t i = 0;
+  while ( s_ResultMap[i].result != 0 && i < MapMax )
+    {
+      i++;
+      if ( s_ResultMap[i].rcode == v )
+       return;
+    }
+
+  assert(i+2 < MapMax);
+
+  s_ResultMap[i].rcode = v;
+  s_ResultMap[i].result = this;
+  s_ResultMap[i+1].rcode = 0;
+  s_ResultMap[i+1].result = 0;
+  return;
+}
+
+Kumu::Result_t::~Result_t() {}
+
+
 //------------------------------------------------------------------------------------------
 
 
@@ -720,6 +785,49 @@ Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
   return str_buf;
 }
 
+//
+bool
+Kumu::Timestamp::DecodeString(const char* datestr)
+{
+  if ( ! ( isdigit(datestr[0]) && isdigit(datestr[1]) && isdigit(datestr[2]) && isdigit(datestr[3]) )
+       || datestr[4] != '-'
+       || ! ( isdigit(datestr[5]) && isdigit(datestr[6]) )
+       || datestr[7] != '-'
+       || ! ( isdigit(datestr[8]) && isdigit(datestr[9]) )
+       || datestr[10] != 'T'
+       || ! ( isdigit(datestr[11]) && isdigit(datestr[12]) )
+       || datestr[13] != ':'
+       || ! ( isdigit(datestr[14]) && isdigit(datestr[15]) )
+       || datestr[16] != ':'
+       || ! ( isdigit(datestr[17]) && isdigit(datestr[18]) )
+       || ! ( datestr[19] == '-' || datestr[19] == '+' )
+       || ! ( isdigit(datestr[20]) && isdigit(datestr[21]) )
+       || datestr[22] != ':'
+       || ! ( isdigit(datestr[23]) && isdigit(datestr[24]) ) )
+    return false;
+
+  // TODO -- test this!
+  Year = atoi(datestr);
+  Month = atoi(datestr + 5);
+  Day = atoi(datestr + 8);
+  Hour = atoi(datestr + 11);
+  Minute = atoi(datestr + 14);
+  Second = atoi(datestr + 17);
+
+  ui32_t TZ_hh = atoi(datestr + 20);
+  ui32_t TZ_mm = atoi(datestr + 23);
+
+  if ( TZ_mm != 0 )
+    DefaultLogSink().Error("Ignoring sub-hours timezone offset: %lu\n", TZ_mm);
+  if ( TZ_hh > 12 )
+    DefaultLogSink().Error("Ignoring large timezone offset: %s\n", (datestr+19));
+  else 
+    AddHours(TZ_hh);
+
+  return true;
+}
+
 //
 bool
 Kumu::Timestamp::HasValue() const
index a27296bc694409dcd4b838e8a7282f171d1859ae..900962459933fb18d86d635e37ab598072fd5fa3 100755 (executable)
@@ -327,7 +327,7 @@ namespace Kumu
       const char* EncodeString(char* str_buf, ui32_t buf_len) const;
 
       // decode and set value from string formatted by EncodeString
-      Result_t    SetFromString(const char* datestr);
+      bool        DecodeString(const char* datestr);
 
       // add the given number of days or hours to the timestamp value. Values less than zero
       // will cause the value to decrease