GnuCash  5.6-150-g038405b370+
csv-export-helpers.cpp
1 /*******************************************************************\
2  * csv-export-helpers.c -- Functions to assist csv export *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20 \********************************************************************/
25 #include <config.h>
26 
27 #include <cstring>
28 #include <cstdio>
29 #include <fstream>
30 #include <vector>
31 
32 #include "gnc-ui-util.h"
33 #include "csv-export-helpers.hpp"
34 
35 /* This static indicates the debugging module that this .o belongs to. */
36 [[maybe_unused]] static QofLogModule log_module = GNC_MOD_ASSISTANT;
37 
38 /* CSV spec requires CRLF line endings. Tweak the end-of-line string so this
39  * true for each platform */
40 #ifdef G_OS_WIN32
41 # define EOLSTR "\n"
42 #else
43 # define EOLSTR "\r\n"
44 #endif
45 
46 #define QUOTE '"'
47 
48 bool
49 gnc_csv_add_line (std::ostream& ss, const StringVec& str_vec,
50  bool use_quotes, const char* sep)
51 {
52  auto first{true};
53  auto sep_view{std::string_view (sep ? sep : "")};
54  for (const auto& str : str_vec)
55  {
56  auto need_quote = use_quotes
57  || (!sep_view.empty() && str.find (sep_view) != std::string::npos)
58  || str.find_first_of ("\"\n\r") != std::string::npos;
59 
60  if (first)
61  first = false;
62  else
63  ss << sep_view;
64 
65  if (need_quote)
66  ss << QUOTE;
67 
68  for (const char& p : str)
69  {
70  ss << p;
71  if (p == QUOTE)
72  ss << QUOTE;
73  }
74 
75  if (need_quote)
76  ss << QUOTE;
77 
78  if (ss.fail())
79  return false;
80  }
81  ss << EOLSTR;
82 
83  return !ss.fail();
84 }
85 
86 std::string
87 account_get_fullname_str (Account *account)
88 {
89  auto name{gnc_account_get_full_name (account)};
90  auto rv{std::string(name)};
91  g_free (name);
92  return rv;
93 }
utility functions for the GnuCash UI
STRUCTS.
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition: Account.cpp:3275