From patchwork Tue Feb 20 19:46:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 86076 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9BCC83858C42 for ; Tue, 20 Feb 2024 19:47:05 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by sourceware.org (Postfix) with ESMTPS id 5E5B53858D35 for ; Tue, 20 Feb 2024 19:46:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E5B53858D35 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5E5B53858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708458378; cv=none; b=j7Wa8ve5JgoVTto+LIUsP9rERpmKd+KAG1LwQkloTDfRTt3R9+/EdBlQqDQDZJ14ssMvHrXPupB6M/klThEG40tuq9bqzxD2BinLHigFfi2q/ESxD9ek4ssJDffYqKHzITYInLueW/n7bEZCK9Ykap20KivaI4V5720T0o0DCVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708458378; c=relaxed/simple; bh=288PPjDq8MyQ5VpXDiC00p9XiotRt0mCpUo8+oG9A3Q=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=Oh9RSkzNji4994xGWoYCqd0PTpmn5A0lawqjDBiufnXo5FP6hn05B83JeB7U4WqCPcZOQ4kjJB0VMEUjs24q+bNrIlcUciGh/WITId9SwYqNsE1X0g78qnN/TiOte3CxQedN2rIq+G/7ne6qvIJF4Z24+56pWdEG2s6ojZ0u+Y8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd35.google.com with SMTP id ca18e2360f4ac-7c48fc56752so244793739f.3 for ; Tue, 20 Feb 2024 11:46:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1708458374; x=1709063174; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/VkWtmVn6mxT1Itq8Qby4I6dWMtHK9cTxgw8NSx9Qds=; b=Fzhy77nYppw2rxlNQb9WPqX8KPeQijDnaDWcJuq2h6vz6WEWq07RHtnk1hjsM4wBff TKtIbAe3V/YKLVV2qVmTMHlmiTnqbZWU681ruIgEu+S+saojY7waLh0psmHv3AjwPGtp pu69NIMNay8Q9zovEYiaFRhgopsLTCiOGMDXV/Xe7oQ/HoZv6aUURc+VtM8odHzujKAM VWY+ToR35U2oXZ25lu8X41dPp791gwNRB4Uo2jvzSwhx4Qvx+aWy8jlCiijRpYqTzQbh tVbbcCmoeZKAcLEBy416yxgwmNYYauB59wwN0QDhlhg+NZDknEYcsL27mTF3PQ3fcHMJ afNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708458374; x=1709063174; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/VkWtmVn6mxT1Itq8Qby4I6dWMtHK9cTxgw8NSx9Qds=; b=ZhvVlePBBEm5NorZ3d1T4OsakNS/9u6AEJ8qNT+II3ws34Dq+xQKZ5sOdqcIxhef6A 4+lqty2atOHKF3yvwDU817BuzqAHT2ZTDe7OdKpeYy/q+8ExuAMGUB8M4qfAr7zlQp4N /umPGtolyhZBQrqYBXCOU3BoYYGCpo0AgeZYv/FZwOEjS+QXniit13lDvYN/lZA9Ulqc 4vErxpiqA3r6jwlVMt8zGmhwCcNIlzETbWKWVfUcU00C4j3Z4R7V5smTjXs+B7WjTUMC Oxe+ZcGL4NQc1tiF+KszBK2KDK/1CCZWVet3SqtQWiUDfHKMSWG1sIIC44yDhtJXn2Cc VZ8Q== X-Gm-Message-State: AOJu0YwZK+WMBEhIxjYAAZNuSbd1sA8RAKNn9iSLcoma6GUF3sem8+k+ ixAfaPuf7Y/8Oe3N7Sa59U7QjpLitITZTlXSIToUIErsoXoeztu+IRfSPS0ko4s/W/ZgCzfg8sA = X-Google-Smtp-Source: AGHT+IHOuoa/FGK9ktvS1OzZjvYJWqLKVJzTtp4vT5rDA+kslf1GSDEmZX0lEcLFX98h6ZBJxs7l/Q== X-Received: by 2002:a5d:9308:0:b0:7c7:5d61:8050 with SMTP id l8-20020a5d9308000000b007c75d618050mr4001842ion.21.1708458374527; Tue, 20 Feb 2024 11:46:14 -0800 (PST) Received: from localhost.localdomain (71-211-170-195.hlrn.qwest.net. [71.211.170.195]) by smtp.gmail.com with ESMTPSA id q10-20020a02c8ca000000b00474284a0228sm1261811jao.61.2024.02.20.11.46.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 11:46:14 -0800 (PST) From: Tom Tromey Date: Tue, 20 Feb 2024 12:46:12 -0700 Subject: [PATCH v3 1/5] Introduce read_remainder_of_file MIME-Version: 1.0 Message-Id: <20240220-py-simple-exc-v3-1-a2825caa4b74@adacore.com> References: <20240220-py-simple-exc-v3-0-a2825caa4b74@adacore.com> In-Reply-To: <20240220-py-simple-exc-v3-0-a2825caa4b74@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This patch adds a new function, read_remainder_of_file. This is like read_text_file_to_string, but reads from an existing 'FILE *'. This will be used in a subsequent patch. Reviewed-By: Tom de Vries --- 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..9d3b417359b 100644 --- a/gdbsupport/filestuff.cc +++ b/gdbsupport/filestuff.cc @@ -504,13 +504,9 @@ mkdir_recursive (const char *dir) /* See gdbsupport/filestuff.h. */ -std::optional -read_text_file_to_string (const char *path) +std::string +read_remainder_of_file (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 +read_text_file_to_string (const char *path) +{ + gdb_file_up file = gdb_fopen_cloexec (path, "r"); + if (file == nullptr) + return {}; + + return read_remainder_of_file (file.get ()); +} diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h index 1c43b7de0ca..e2ee141d46f 100644 --- a/gdbsupport/filestuff.h +++ b/gdbsupport/filestuff.h @@ -133,4 +133,8 @@ extern bool mkdir_recursive (const char *dir); extern std::optional read_text_file_to_string (const char *path); +/* Read the remaining content from FILE into an std::string. */ + +extern std::string read_remainder_of_file (FILE *file); + #endif /* COMMON_FILESTUFF_H */