From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/5] Introduce new overload of read_text_file_to_string
Date: Fri, 16 Feb 2024 11:37:49 -0700 [thread overview]
Message-ID: <20240216-py-simple-exc-v1-1-c275bcfb5e4a@adacore.com> (raw)
In-Reply-To: <20240216-py-simple-exc-v1-0-c275bcfb5e4a@adacore.com>
This patch adds a new overload of read_text_file_to_string. This one
reads from an existing 'FILE *'. This will be used in a subsequent
patch.
---
gdbsupport/filestuff.cc | 24 ++++++++++++++++--------
gdbsupport/filestuff.h | 4 ++++
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc
index c67e650c1a6..70bcc3faf60 100644
--- a/gdbsupport/filestuff.cc
+++ b/gdbsupport/filestuff.cc
@@ -504,13 +504,9 @@ mkdir_recursive (const char *dir)
/* See gdbsupport/filestuff.h. */
-std::optional<std::string>
-read_text_file_to_string (const char *path)
+std::string
+read_text_file_to_string (FILE *file)
{
- gdb_file_up file = gdb_fopen_cloexec (path, "r");
- if (file == nullptr)
- return {};
-
std::string res;
for (;;)
{
@@ -520,7 +516,7 @@ read_text_file_to_string (const char *path)
/* Resize to accommodate CHUNK_SIZE bytes. */
res.resize (start_size + chunk_size);
- int n = fread (&res[start_size], 1, chunk_size, file.get ());
+ int n = fread (&res[start_size], 1, chunk_size, file);
if (n == chunk_size)
continue;
@@ -528,7 +524,7 @@ read_text_file_to_string (const char *path)
/* Less than CHUNK means EOF or error. If it's an error, return
no value. */
- if (ferror (file.get ()))
+ if (ferror (file))
return {};
/* Resize the string according to the data we read. */
@@ -538,3 +534,15 @@ read_text_file_to_string (const char *path)
return res;
}
+
+/* See gdbsupport/filestuff.h. */
+
+std::optional<std::string>
+read_text_file_to_string (const char *path)
+{
+ gdb_file_up file = gdb_fopen_cloexec (path, "r");
+ if (file == nullptr)
+ return {};
+
+ return read_text_file_to_string (file.get ());
+}
diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h
index 1c43b7de0ca..4a90b08c5f3 100644
--- a/gdbsupport/filestuff.h
+++ b/gdbsupport/filestuff.h
@@ -133,4 +133,8 @@ extern bool mkdir_recursive (const char *dir);
extern std::optional<std::string> read_text_file_to_string (const char *path);
+/* Read the entire content of FILE into an std::string. */
+
+extern std::string read_text_file_to_string (FILE *file);
+
#endif /* COMMON_FILESTUFF_H */
--
2.43.0
next prev parent reply other threads:[~2024-02-16 18:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-16 18:37 [PATCH 0/5] Fix DAP pause bug Tom Tromey
2024-02-16 18:37 ` Tom Tromey [this message]
2024-02-19 15:36 ` [PATCH 1/5] Introduce new overload of read_text_file_to_string Tom de Vries
2024-02-20 14:08 ` Tom Tromey
2024-02-20 14:48 ` Tom de Vries
2024-02-16 18:37 ` [PATCH 2/5] Fix formatting buglet in python.c Tom Tromey
2024-02-16 18:37 ` [PATCH 3/5] kfail a FinishBreakpoint test Tom Tromey
2024-02-16 18:37 ` [PATCH 4/5] Rewrite "python" command exception handling Tom Tromey
2024-02-16 18:37 ` [PATCH 5/5] Use the .py file in gdb.dap/pause.exp Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240216-py-simple-exc-v1-1-c275bcfb5e4a@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).