]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdbsupport/filestuff.cc
Move unrelocated_addr to common-types.h
[thirdparty/binutils-gdb.git] / gdbsupport / filestuff.cc
index 2dfae5a48c5c85385b96263e5ce6b9e627b45359..9e61fea1195121d302553516f668d779d941647e 100644 (file)
@@ -1,5 +1,5 @@
 /* Low-level file-handling.
-   Copyright (C) 2012-2022 Free Software Foundation, Inc.
+   Copyright (C) 2012-2023 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -501,3 +501,40 @@ mkdir_recursive (const char *dir)
       component_start = component_end;
     }
 }
+
+/* See gdbsupport/filestuff.h.  */
+
+gdb::optional<std::string>
+read_text_file_to_string (const char *path)
+{
+  gdb_file_up file = gdb_fopen_cloexec (path, "r");
+  if (file == nullptr)
+    return {};
+
+  std::string res;
+  for (;;)
+    {
+      std::string::size_type start_size = res.size ();
+      constexpr int chunk_size = 1024;
+
+      /* Resize to accommodate CHUNK_SIZE bytes.  */
+      res.resize (start_size + chunk_size);
+
+      int n = fread (&res[start_size], 1, chunk_size, file.get ());
+      if (n == chunk_size)
+       continue;
+
+      gdb_assert (n < chunk_size);
+
+      /* Less than CHUNK means EOF or error.  If it's an error, return
+        no value.  */
+      if (ferror (file.get ()))
+       return {};
+
+      /* Resize the string according to the data we read.  */
+      res.resize (start_size + n);
+      break;
+    }
+
+  return res;
+}