Browse Source

split file_utils into files matching functions/classes

Alec Jacobson 3 years ago
parent
commit
0b5148f646

+ 63 - 0
include/igl/FileMemoryStream.h

@@ -0,0 +1,63 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2020 Jérémie Dumas <[email protected]>
+// Copyright (C) 2021 Alec Jacobson <[email protected]>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_FILEMEMORYSTREAM_H
+#define IGL_FILEMEMORYSTREAM_H
+
+#include "igl_inline.h"
+
+#include <streambuf>
+#include <istream>
+#include <string>
+
+namespace igl {
+  struct FileMemoryBuffer : public std::streambuf 
+  {
+    char *p_start{nullptr};
+    char *p_end{nullptr};
+    size_t size;
+  
+    FileMemoryBuffer(char const *first_elem, size_t size)
+      : p_start(const_cast<char *>(first_elem)), p_end(p_start + size),
+        size(size) 
+    {
+      setg(p_start, p_start, p_end);
+    }
+  
+    pos_type seekoff(
+      off_type off, 
+      std::ios_base::seekdir dir,
+      std::ios_base::openmode which) override 
+    {
+      if (dir == std::ios_base::cur) 
+      {
+        gbump(static_cast<int>(off));
+      }else
+      {
+        setg(p_start,(dir==std::ios_base::beg ? p_start : p_end) + off,p_end);
+      }
+      return gptr() - p_start;
+    }
+  
+    pos_type seekpos(pos_type pos, std::ios_base::openmode which) override 
+    {
+      return seekoff(pos, std::ios_base::beg, which);
+    }
+  };
+  
+  struct FileMemoryStream : virtual FileMemoryBuffer, public std::istream 
+  {
+    FileMemoryStream( char const *first_elem, size_t size)
+      : FileMemoryBuffer(first_elem, size), 
+        std::istream( static_cast<std::streambuf *>(this))
+    {}
+  };
+} 
+
+#endif
+

+ 0 - 62
include/igl/file_utils.h

@@ -1,62 +0,0 @@
-// This file is part of libigl, a simple c++ geometry processing library.
-//
-// Copyright (C) 2020 Jérémie Dumas <[email protected]>
-//
-// This Source Code Form is subject to the terms of the Mozilla Public License
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can
-// obtain one at http://mozilla.org/MPL/2.0/.
-#ifndef IGL_FILE_UTILS_H
-#define IGL_FILE_UTILS_H
-
-#include "igl_inline.h"
-
-#include <streambuf>
-#include <istream>
-#include <string>
-#include <vector>
-
-namespace igl {
-
-IGL_INLINE void read_file_binary(FILE *fp,
-                                 std::vector<uint8_t> &fileBufferBytes);
-
-struct file_memory_buffer : public std::streambuf {
-  char *p_start{nullptr};
-  char *p_end{nullptr};
-  size_t size;
-
-  file_memory_buffer(char const *first_elem, size_t size)
-      : p_start(const_cast<char *>(first_elem)), p_end(p_start + size),
-        size(size) {
-    setg(p_start, p_start, p_end);
-  }
-
-  pos_type seekoff(off_type off, std::ios_base::seekdir dir,
-                   std::ios_base::openmode which) override {
-    if (dir == std::ios_base::cur) {
-      gbump(static_cast<int>(off));
-    } else {
-      setg(p_start, (dir == std::ios_base::beg ? p_start : p_end) + off, p_end);
-    }
-    return gptr() - p_start;
-  }
-
-  pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
-    return seekoff(pos, std::ios_base::beg, which);
-  }
-};
-
-struct file_memory_stream : virtual file_memory_buffer, public std::istream {
-  file_memory_stream(char const *first_elem, size_t size)
-      : file_memory_buffer(first_elem, size), std::istream(
-                                                  static_cast<std::streambuf *>(
-                                                      this)) {}
-};
-
-} // namespace igl
-
-#ifndef IGL_STATIC_LIBRARY
-#include "file_utils.cpp"
-#endif
-
-#endif

+ 3 - 2
include/igl/readPLY.cpp

@@ -6,7 +6,8 @@
 #include <Eigen/Core>
 
 #include "tinyply.h"
-#include "file_utils.h"
+#include "read_file_binary.h"
+#include "FileMemoryStream.h"
 
 
 namespace igl
@@ -171,7 +172,7 @@ IGL_INLINE bool readPLY(
   {
     std::vector<uint8_t> fileBufferBytes;
     read_file_binary(fp,fileBufferBytes);
-    file_memory_stream stream((char*)fileBufferBytes.data(), fileBufferBytes.size());
+    FileMemoryStream stream((char*)fileBufferBytes.data(), fileBufferBytes.size());
     return readPLY(stream,V,F,E,N,UV,VD,Vheader,FD,Fheader,ED,Eheader,comments);
   }
   catch(const std::exception& e)

+ 3 - 2
include/igl/readSTL.cpp

@@ -10,7 +10,8 @@
 #include "readSTL.h"
 #include "list_to_matrix.h"
 #include "string_utils.h"
-#include "file_utils.h"
+#include "read_file_binary.h"
+#include "FileMemoryStream.h"
 
 #include <iostream>
 
@@ -304,7 +305,7 @@ IGL_INLINE bool readSTL(
 {
   std::vector<uint8_t> fileBufferBytes;
   read_file_binary(fp,fileBufferBytes);
-  file_memory_stream stream((char*)fileBufferBytes.data(), fileBufferBytes.size());
+  FileMemoryStream stream((char*)fileBufferBytes.data(), fileBufferBytes.size());
   return readSTL(stream, V, F, N);
 }
 

+ 10 - 11
include/igl/file_utils.cpp → include/igl/read_file_binary.cpp

@@ -1,31 +1,30 @@
 // This file is part of libigl, a simple c++ geometry processing library.
 //
 // Copyright (C) 2020 Jérémie Dumas <[email protected]>
+// Copyright (C) 2021 Alec Jacobson <[email protected]>
 //
 // This Source Code Form is subject to the terms of the Mozilla Public License
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
-#include "file_utils.h"
+#include "read_file_binary.h"
 
 #include <cstring>
 
-namespace igl {
-
-IGL_INLINE void read_file_binary(FILE *fp,
-                                 std::vector<uint8_t> &fileBufferBytes) {
-  if (!ferror(fp)) {
+IGL_INLINE void igl::read_file_binary(
+  FILE *fp,
+  std::vector<uint8_t> &fileBufferBytes) 
+{
+  if (!ferror(fp)) 
+  {
     fseek(fp, 0, SEEK_END);
     size_t sizeBytes = ftell(fp);
     fseek(fp, 0, SEEK_SET);
     fileBufferBytes.resize(sizeBytes);
-
-    if (fread((char*)fileBufferBytes.data(), 1, sizeBytes, fp) == sizeBytes) {
+    if(fread((char*)fileBufferBytes.data(), 1, sizeBytes, fp) == sizeBytes) 
+    {
       fclose(fp);
       return;
     }
   }
-
   throw std::runtime_error("error reading from file");
 }
-
-}

+ 40 - 0
include/igl/read_file_binary.h

@@ -0,0 +1,40 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2020 Jérémie Dumas <[email protected]>
+// Copyright (C) 2021 Alec Jacobson <[email protected]>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_READ_FILE_BINARY_H
+#define IGL_READ_FILE_BINARY_H
+
+#include "igl_inline.h"
+
+#include <streambuf>
+#include <istream>
+#include <string>
+#include <vector>
+
+namespace igl {
+
+  // Read contents of file into a buffer of uint8_t bytes.
+  //
+  // Input:
+  //   fp  pointer to open File
+  // Outputs:
+  //   fileBufferBytes  contents of file as vector of bytes
+  // Side effects:
+  //   closes fp
+  // Throws runtime_error on error
+  IGL_INLINE void read_file_binary(
+    FILE *fp,
+    std::vector<uint8_t> &fileBufferBytes);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#include "read_file_binary.cpp"
+#endif
+
+#endif
+