added identifier list type
[asdcplib.git] / src / KM_fileio.cpp
index ef1c9dc1c05a1be5f9a431bc64b0cd67ded1fb9f..e01b01ffee0973e47618b03c379d3b792ab78ce3 100644 (file)
@@ -57,8 +57,8 @@ typedef struct stat     fstat_t;
 static Kumu::Result_t
 do_stat(const char* path, fstat_t* stat_info)
 {
-  KM_TEST_NULL_STR(path);
-  KM_TEST_NULL(stat_info);
+  KM_TEST_NULL_STR_L(path);
+  KM_TEST_NULL_L(stat_info);
 
   Kumu::Result_t result = Kumu::RESULT_OK;
 
@@ -86,7 +86,7 @@ do_stat(const char* path, fstat_t* stat_info)
 static Kumu::Result_t
 do_fstat(HANDLE handle, fstat_t* stat_info)
 {
-  KM_TEST_NULL(stat_info);
+  KM_TEST_NULL_L(stat_info);
 
   Kumu::Result_t result = Kumu::RESULT_OK;
 
@@ -106,7 +106,9 @@ do_fstat(HANDLE handle, fstat_t* stat_info)
 bool
 Kumu::PathIsFile(const char* pathname)
 {
-  assert(pathname);
+  if ( pathname == 0 || *pathname == 0 )
+    return false;
+
   fstat_t info;
 
   if ( KM_SUCCESS(do_stat(pathname, &info)) )
@@ -123,7 +125,9 @@ Kumu::PathIsFile(const char* pathname)
 bool
 Kumu::PathIsDirectory(const char* pathname)
 {
-  assert(pathname);
+  if ( pathname == 0 || *pathname == 0 )
+    return false;
+
   fstat_t info;
 
   if ( KM_SUCCESS(do_stat(pathname, &info)) )
@@ -140,7 +144,9 @@ Kumu::PathIsDirectory(const char* pathname)
 Kumu::fsize_t
 Kumu::FileSize(const char* pathname)
 {
-  assert(pathname);
+  if ( pathname == 0 || *pathname == 0 )
+    return false;
+
   fstat_t info;
 
   if ( KM_SUCCESS(do_stat(pathname, &info)) )
@@ -198,13 +204,13 @@ Kumu::FileWriter::Writev(const byte_t* buf, ui32_t buf_len)
 {
   assert( ! m_IOVec.empty() );
   register h__iovec* iov = m_IOVec;
-  KM_TEST_NULL(buf);
+  KM_TEST_NULL_L(buf);
 
   if ( iov->m_Count >= IOVecMaxEntries )
     {
       DefaultLogSink().Error("The iovec is full! Only %u entries allowed before a flush.\n",
                             IOVecMaxEntries);
-      return RESULT_FAIL;
+      return RESULT_WRITEFAIL;
     }
 
   iov->m_iovec[iov->m_Count].iov_base = (char*)buf; // stupid iovec uses char*
@@ -222,7 +228,7 @@ Kumu::FileWriter::Writev(const byte_t* buf, ui32_t buf_len)
 Kumu::Result_t
 Kumu::FileReader::OpenRead(const char* filename) const
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
   const_cast<FileReader*>(this)->m_Filename = filename;
   
   // suppress popup window on error
@@ -285,7 +291,7 @@ Kumu::FileReader::Seek(Kumu::fpos_t position, SeekPos_t whence) const
 Kumu::Result_t
 Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
 {
-  KM_TEST_NULL(pos);
+  KM_TEST_NULL_L(pos);
 
   if ( m_Handle == (HANDLE)-1L )
     return Kumu::RESULT_FILEOPEN;
@@ -310,7 +316,7 @@ Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
 Kumu::Result_t
 Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
 {
-  KM_TEST_NULL(buf);
+  KM_TEST_NULL_L(buf);
   Result_t result = Kumu::RESULT_OK;
   DWORD    tmp_count;
   ui32_t tmp_int;
@@ -347,7 +353,7 @@ Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
 Kumu::Result_t
 Kumu::FileWriter::OpenWrite(const char* filename)
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
   m_Filename = filename;
   
   // suppress popup window on error
@@ -399,13 +405,12 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
                                   (DWORD*)&tmp_count,
                                   NULL);
 
-      if ( wr_result == 0 )
+      if ( wr_result == 0 || tmp_count != iov->m_iovec[i].iov_len)
        {
          result = Kumu::RESULT_WRITEFAIL;
          break;
        }
 
-      assert(iov->m_iovec[i].iov_len == tmp_count);
       *bytes_written += tmp_count;
     }
 
@@ -419,7 +424,7 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
 Kumu::Result_t
 Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written)
 {
-  KM_TEST_NULL(buf);
+  KM_TEST_NULL_L(buf);
   ui32_t tmp_int;
 
   if ( bytes_written == 0 )
@@ -433,7 +438,10 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
   BOOL result = ::WriteFile(m_Handle, buf, buf_len, (DWORD*)bytes_written, NULL);
   ::SetErrorMode(prev);
 
-  return ( result == 0 ) ? Kumu::RESULT_WRITEFAIL : Kumu::RESULT_OK;
+  if ( result == 0 || bytes_written != buf_len )
+    return Kumu::RESULT_WRITEFAIL;
+
+  return Kumu::RESULT_OK;
 }
 
 #else // KM_WIN32
@@ -444,7 +452,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
 Kumu::Result_t
 Kumu::FileReader::OpenRead(const char* filename) const
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
   const_cast<FileReader*>(this)->m_Filename = filename;
   const_cast<FileReader*>(this)->m_Handle = open(filename, O_RDONLY, 0);
   return ( m_Handle == -1L ) ? RESULT_FILEOPEN : RESULT_OK;
@@ -479,7 +487,7 @@ Kumu::FileReader::Seek(Kumu::fpos_t position, SeekPos_t whence) const
 Kumu::Result_t
 Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
 {
-  KM_TEST_NULL(pos);
+  KM_TEST_NULL_L(pos);
 
   if ( m_Handle == -1L )
     return RESULT_FILEOPEN;
@@ -497,7 +505,7 @@ Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
 Kumu::Result_t
 Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
 {
-  KM_TEST_NULL(buf);
+  KM_TEST_NULL_L(buf);
   i32_t  tmp_count = 0;
   ui32_t tmp_int = 0;
 
@@ -524,7 +532,7 @@ Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
 Kumu::Result_t
 Kumu::FileWriter::OpenWrite(const char* filename)
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
   m_Filename = filename;
   m_Handle = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0664);
 
@@ -542,7 +550,7 @@ Kumu::FileWriter::OpenWrite(const char* filename)
 Kumu::Result_t
 Kumu::FileWriter::OpenModify(const char* filename)
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
   m_Filename = filename;
   m_Handle = open(filename, O_RDWR|O_CREAT, 0664);
 
@@ -570,13 +578,17 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
   if ( m_Handle == -1L )
     return RESULT_STATE;
 
-  int read_size = writev(m_Handle, iov->m_iovec, iov->m_Count);
+  int total_size = 0;
+  for ( int i = 0; i < iov->m_Count; i++ )
+    total_size += iov->m_iovec[i].iov_len;
+
+  int write_size = writev(m_Handle, iov->m_iovec, iov->m_Count);
   
-  if ( read_size == -1L )
+  if ( write_size == -1L || write_size != total_size )
     return RESULT_WRITEFAIL;
 
   iov->m_Count = 0;
-  *bytes_written = read_size;  
+  *bytes_written = write_size;  
   return RESULT_OK;
 }
 
@@ -584,24 +596,21 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
 Kumu::Result_t
 Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written)
 {
-  KM_TEST_NULL(buf);
+  KM_TEST_NULL_L(buf);
   ui32_t tmp_int;
 
   if ( bytes_written == 0 )
     bytes_written = &tmp_int;
 
-  // TODO: flush iovec
-
-
   if ( m_Handle == -1L )
     return RESULT_STATE;
 
-  int read_size = write(m_Handle, buf, buf_len);
-  
-  if ( read_size == -1L )
+  int write_size = write(m_Handle, buf, buf_len);
+
+  if ( write_size == -1L || (ui32_t)write_size != buf_len )
     return RESULT_WRITEFAIL;
 
-  *bytes_written = read_size;  
+  *bytes_written = write_size;
   return RESULT_OK;
 }
 
@@ -620,7 +629,7 @@ Kumu::ReadFileIntoString(const char* filename, std::string& outString, ui32_t ma
   FileReader File;
   ByteString ReadBuf;
 
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
 
   Result_t result = File.OpenRead(filename);
 
@@ -630,10 +639,16 @@ Kumu::ReadFileIntoString(const char* filename, std::string& outString, ui32_t ma
 
       if ( fsize > (Kumu::fpos_t)max_size )
        {
-         DefaultLogSink().Error("%s: exceeds available buffer size (%u)", filename, max_size);
+         DefaultLogSink().Error("%s: exceeds available buffer size (%u)\n", filename, max_size);
          return RESULT_ALLOC;
        }
 
+      if ( fsize == 0 )
+       {
+         DefaultLogSink().Error("%s: zero file size\n", filename);
+         return RESULT_READFAIL;
+       }
+
       result = ReadBuf.Capacity((ui32_t)fsize);
     }
 
@@ -653,17 +668,14 @@ Kumu::WriteStringIntoFile(const char* filename, const std::string& inString)
 {
   FileWriter File;
   ui32_t write_count = 0;
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(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;
+  return result;
 }
 
 
@@ -679,7 +691,7 @@ Kumu::WriteStringIntoFile(const char* filename, const std::string& inString)
 Result_t
 Kumu::DirScanner::Open(const char* filename)
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
 
   // we need to append a '*' to read the entire directory
   ui32_t fn_len = strlen(filename); 
@@ -732,7 +744,7 @@ Kumu::DirScanner::Close()
 Result_t
 Kumu::DirScanner::GetNext(char* filename)
 {
-  KM_TEST_NULL(filename);
+  KM_TEST_NULL_L(filename);
 
   if ( m_Handle == -1 )
     return RESULT_FILEOPEN;
@@ -763,7 +775,7 @@ Kumu::DirScanner::GetNext(char* filename)
 Result_t
 Kumu::DirScanner::Open(const char* filename)
 {
-  KM_TEST_NULL_STR(filename);
+  KM_TEST_NULL_STR_L(filename);
 
   Result_t result = RESULT_OK;
 
@@ -799,7 +811,7 @@ Kumu::DirScanner::Close()
 Result_t
 Kumu::DirScanner::GetNext(char* filename)
 {
-  KM_TEST_NULL(filename);
+  KM_TEST_NULL_L(filename);
 
   if ( m_Handle == NULL )
     return RESULT_FILEOPEN;