mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
[csv-tree-export.cpp] convert to cpp, use fstream
This commit is contained in:
parent
e6352f5497
commit
cb8cdd1b99
@ -5,7 +5,7 @@ set(csv_export_SOURCES
|
|||||||
gnc-plugin-csv-export.c
|
gnc-plugin-csv-export.c
|
||||||
csv-export-helpers.cpp
|
csv-export-helpers.cpp
|
||||||
assistant-csv-export.c
|
assistant-csv-export.c
|
||||||
csv-tree-export.c
|
csv-tree-export.cpp
|
||||||
csv-transactions-export.c
|
csv-transactions-export.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,265 +0,0 @@
|
|||||||
/*******************************************************************\
|
|
||||||
* csv-tree-export.c -- Export Account Tree to a file *
|
|
||||||
* *
|
|
||||||
* Copyright (C) 2012 Robert Fewell *
|
|
||||||
* *
|
|
||||||
* This program is free software; you can redistribute it and/or *
|
|
||||||
* modify it under the terms of the GNU General Public License as *
|
|
||||||
* published by the Free Software Foundation; either version 2 of *
|
|
||||||
* the License, or (at your option) any later version. *
|
|
||||||
* *
|
|
||||||
* This program is distributed in the hope that it will be useful, *
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
||||||
* GNU General Public License for more details. *
|
|
||||||
* *
|
|
||||||
* You should have received a copy of the GNU General Public License*
|
|
||||||
* along with this program; if not, contact: *
|
|
||||||
* *
|
|
||||||
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
||||||
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
||||||
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
||||||
\********************************************************************/
|
|
||||||
/** @file csv-tree-export.c
|
|
||||||
@brief CSV Export Account Tree
|
|
||||||
@author Copyright (c) 2012 Robert Fewell
|
|
||||||
*/
|
|
||||||
#include <config.h>
|
|
||||||
|
|
||||||
#include <gtk/gtk.h>
|
|
||||||
#include <glib/gi18n.h>
|
|
||||||
#include <glib/gstdio.h>
|
|
||||||
|
|
||||||
#include "gnc-commodity.h"
|
|
||||||
#include "gnc-ui-util.h"
|
|
||||||
|
|
||||||
#include "csv-tree-export.h"
|
|
||||||
|
|
||||||
/* This static indicates the debugging module that this .o belongs to. */
|
|
||||||
static QofLogModule log_module = GNC_MOD_ASSISTANT;
|
|
||||||
|
|
||||||
/* CSV spec requires CRLF line endings. Tweak the end-of-line string so this
|
|
||||||
* true for each platform */
|
|
||||||
#ifdef G_OS_WIN32
|
|
||||||
# define EOLSTR "\n"
|
|
||||||
#else
|
|
||||||
# define EOLSTR "\r\n"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/******************************************************************/
|
|
||||||
|
|
||||||
/*******************************************************
|
|
||||||
* write_line_to_file
|
|
||||||
*
|
|
||||||
* write a text string to a file pointer, return TRUE if
|
|
||||||
* successful.
|
|
||||||
*******************************************************/
|
|
||||||
static
|
|
||||||
gboolean write_line_to_file (FILE *fh, char * line)
|
|
||||||
{
|
|
||||||
int len, written;
|
|
||||||
DEBUG("Account String: %s", line);
|
|
||||||
|
|
||||||
/* Write account line */
|
|
||||||
len = strlen (line);
|
|
||||||
written = fwrite (line, 1, len, fh);
|
|
||||||
|
|
||||||
if (written != len)
|
|
||||||
return FALSE;
|
|
||||||
else
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*******************************************************
|
|
||||||
* csv_test_field_string
|
|
||||||
*
|
|
||||||
* Test the field string for ," and new lines
|
|
||||||
*******************************************************/
|
|
||||||
static
|
|
||||||
gchar *csv_test_field_string (CsvExportInfo *info, const gchar *string_in)
|
|
||||||
{
|
|
||||||
gboolean need_quote = FALSE;
|
|
||||||
gchar **parts;
|
|
||||||
gchar *string_parts;
|
|
||||||
gchar *string_out;
|
|
||||||
|
|
||||||
/* Check for " and then "" them */
|
|
||||||
parts = g_strsplit (string_in, "\"", -1);
|
|
||||||
string_parts = g_strjoinv ("\"\"", parts);
|
|
||||||
g_strfreev (parts);
|
|
||||||
|
|
||||||
/* Check for separator string and \n and " in field,
|
|
||||||
if so quote field if not already quoted */
|
|
||||||
if (g_strrstr (string_parts, info->separator_str) != NULL)
|
|
||||||
need_quote = TRUE;
|
|
||||||
if (g_strrstr (string_parts, "\n") != NULL)
|
|
||||||
need_quote = TRUE;
|
|
||||||
if (g_strrstr (string_parts, "\"") != NULL)
|
|
||||||
need_quote = TRUE;
|
|
||||||
|
|
||||||
if (!info->use_quotes && need_quote)
|
|
||||||
string_out = g_strconcat ("\"", string_parts, "\"", NULL);
|
|
||||||
else
|
|
||||||
string_out = g_strdup (string_parts);
|
|
||||||
|
|
||||||
g_free (string_parts);
|
|
||||||
return string_out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*******************************************************
|
|
||||||
* csv_tree_export
|
|
||||||
*
|
|
||||||
* write a list of accounts settings to a text file
|
|
||||||
*******************************************************/
|
|
||||||
void csv_tree_export (CsvExportInfo *info)
|
|
||||||
{
|
|
||||||
FILE *fh;
|
|
||||||
Account *root;
|
|
||||||
Account *acc;
|
|
||||||
GList *accts, *ptr;
|
|
||||||
|
|
||||||
ENTER("");
|
|
||||||
DEBUG("File name is : %s", info->file_name);
|
|
||||||
|
|
||||||
/* Get list of Accounts */
|
|
||||||
root = gnc_book_get_root_account (gnc_get_current_book());
|
|
||||||
accts = gnc_account_get_descendants_sorted (root);
|
|
||||||
info->failed = FALSE;
|
|
||||||
|
|
||||||
/* Open File for writing */
|
|
||||||
fh = g_fopen (info->file_name, "w");
|
|
||||||
if (fh != NULL)
|
|
||||||
{
|
|
||||||
gchar *header;
|
|
||||||
gchar *part1;
|
|
||||||
gchar *part2;
|
|
||||||
const gchar *currentSel;
|
|
||||||
gchar *end_sep;
|
|
||||||
gchar *mid_sep;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
|
|
||||||
/* Set up separators */
|
|
||||||
if (info->use_quotes)
|
|
||||||
{
|
|
||||||
end_sep = "\"";
|
|
||||||
mid_sep = g_strconcat ("\"", info->separator_str, "\"", NULL);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
end_sep = "";
|
|
||||||
mid_sep = g_strconcat (info->separator_str, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Header string, 'eol = end of line marker' */
|
|
||||||
header = g_strconcat (end_sep, _("Type"), mid_sep, _("Full Account Name"), mid_sep, _("Account Name"), mid_sep,
|
|
||||||
_("Account Code"), mid_sep, _("Description"), mid_sep, _("Account Color"), mid_sep,
|
|
||||||
_("Notes"), mid_sep, _("Symbol"), mid_sep, _("Namespace"), mid_sep,
|
|
||||||
_("Hidden"), mid_sep, _("Tax Info"), mid_sep, _("Placeholder"), end_sep, EOLSTR, NULL);
|
|
||||||
DEBUG("Header String: %s", header);
|
|
||||||
|
|
||||||
/* Write header line */
|
|
||||||
if (!write_line_to_file (fh, header))
|
|
||||||
{
|
|
||||||
info->failed = TRUE;
|
|
||||||
g_free (mid_sep);
|
|
||||||
g_free (header);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
g_free (header);
|
|
||||||
|
|
||||||
/* Go through list of accounts */
|
|
||||||
for (ptr = accts, i = 0; ptr; ptr = g_list_next (ptr), i++)
|
|
||||||
{
|
|
||||||
gchar *fullname = NULL;
|
|
||||||
gchar *str_temp = NULL;
|
|
||||||
acc = ptr->data;
|
|
||||||
DEBUG("Account being processed is : %s", xaccAccountGetName (acc));
|
|
||||||
/* Type */
|
|
||||||
currentSel = xaccAccountTypeEnumAsString (xaccAccountGetType (acc));
|
|
||||||
part1 = g_strconcat (end_sep, currentSel, mid_sep, NULL);
|
|
||||||
/* Full Name */
|
|
||||||
fullname = gnc_account_get_full_name (acc);
|
|
||||||
str_temp = csv_test_field_string (info, fullname);
|
|
||||||
part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (fullname);
|
|
||||||
g_free (part1);
|
|
||||||
/* Name */
|
|
||||||
currentSel = xaccAccountGetName (acc);
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part2);
|
|
||||||
/* Code */
|
|
||||||
currentSel = xaccAccountGetCode (acc) ? xaccAccountGetCode (acc) : "";
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part1);
|
|
||||||
/* Description */
|
|
||||||
currentSel = xaccAccountGetDescription (acc) ? xaccAccountGetDescription (acc) : "";
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part2);
|
|
||||||
/* Color */
|
|
||||||
currentSel = xaccAccountGetColor (acc) ? xaccAccountGetColor (acc) : "" ;
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part1);
|
|
||||||
/* Notes */
|
|
||||||
currentSel = xaccAccountGetNotes (acc) ? xaccAccountGetNotes (acc) : "" ;
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part2);
|
|
||||||
/* Commodity Symbol */
|
|
||||||
currentSel = gnc_commodity_get_mnemonic (xaccAccountGetCommodity (acc));
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part1);
|
|
||||||
/* Commodity Namespace */
|
|
||||||
currentSel = gnc_commodity_get_namespace (xaccAccountGetCommodity (acc));
|
|
||||||
str_temp = csv_test_field_string (info, currentSel);
|
|
||||||
part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
|
|
||||||
g_free (str_temp);
|
|
||||||
g_free (part2);
|
|
||||||
/* Hidden */
|
|
||||||
currentSel = xaccAccountGetHidden (acc) ? "T" : "F" ;
|
|
||||||
part2 = g_strconcat (part1, currentSel, mid_sep, NULL);
|
|
||||||
g_free (part1);
|
|
||||||
/* Tax */
|
|
||||||
currentSel = xaccAccountGetTaxRelated (acc) ? "T" : "F" ;
|
|
||||||
part1 = g_strconcat (part2, currentSel, mid_sep, NULL);
|
|
||||||
g_free (part2);
|
|
||||||
/* Place Holder / end of line marker */
|
|
||||||
currentSel = xaccAccountGetPlaceholder (acc) ? "T" : "F" ;
|
|
||||||
part2 = g_strconcat (part1, currentSel, end_sep, EOLSTR, NULL);
|
|
||||||
g_free (part1);
|
|
||||||
|
|
||||||
DEBUG("Account String: %s", part2);
|
|
||||||
|
|
||||||
/* Write to file */
|
|
||||||
if (!write_line_to_file (fh, part2))
|
|
||||||
{
|
|
||||||
info->failed = TRUE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
g_free (part2);
|
|
||||||
}
|
|
||||||
g_free (mid_sep);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
info->failed = TRUE;
|
|
||||||
if (fh)
|
|
||||||
fclose (fh);
|
|
||||||
|
|
||||||
g_list_free (accts);
|
|
||||||
LEAVE("");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
112
gnucash/import-export/csv-exp/csv-tree-export.cpp
Normal file
112
gnucash/import-export/csv-exp/csv-tree-export.cpp
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/*******************************************************************\
|
||||||
|
* csv-tree-export.cpp -- Export Account Tree to a file *
|
||||||
|
* *
|
||||||
|
* Copyright (C) 2012 Robert Fewell *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or *
|
||||||
|
* modify it under the terms of the GNU General Public License as *
|
||||||
|
* published by the Free Software Foundation; either version 2 of *
|
||||||
|
* the License, or (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License*
|
||||||
|
* along with this program; if not, contact: *
|
||||||
|
* *
|
||||||
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
||||||
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
||||||
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
||||||
|
\********************************************************************/
|
||||||
|
/** @file csv-tree-export.c
|
||||||
|
@brief CSV Export Account Tree
|
||||||
|
@author Copyright (c) 2012 Robert Fewell
|
||||||
|
*/
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "gnc-commodity.h"
|
||||||
|
#include "gnc-ui-util.h"
|
||||||
|
#include "csv-tree-export.h"
|
||||||
|
#include "csv-export-helpers.hpp"
|
||||||
|
|
||||||
|
/* This static indicates the debugging module that this .o belongs to. */
|
||||||
|
static QofLogModule log_module = GNC_MOD_ASSISTANT;
|
||||||
|
|
||||||
|
static std::string
|
||||||
|
account_get_fullname_str (Account *account)
|
||||||
|
{
|
||||||
|
auto name{gnc_account_get_full_name (account)};
|
||||||
|
auto rv{std::string(name)};
|
||||||
|
g_free (name);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************
|
||||||
|
* csv_tree_export
|
||||||
|
*
|
||||||
|
* write a list of accounts settings to a text file
|
||||||
|
*******************************************************/
|
||||||
|
void
|
||||||
|
csv_tree_export (CsvExportInfo *info)
|
||||||
|
{
|
||||||
|
ENTER("");
|
||||||
|
DEBUG("File name is : %s", info->file_name);
|
||||||
|
|
||||||
|
/* Open File for writing */
|
||||||
|
auto ss{std::ofstream (info->file_name, std::ofstream::out)};
|
||||||
|
|
||||||
|
/* Header string */
|
||||||
|
StringVec headervec = {
|
||||||
|
_("Type"), _("Full Account Name"), _("Account Name"),
|
||||||
|
_("Account Code"), _("Description"), _("Account Color"),
|
||||||
|
_("Notes"), _("Symbol"), _("Namespace"),
|
||||||
|
_("Hidden"), _("Tax Info"), _("Placeholder")
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Write header line */
|
||||||
|
info->failed = ss.fail() ||
|
||||||
|
!gnc_csv_add_line (ss, headervec, info->use_quotes, info->separator_str);
|
||||||
|
|
||||||
|
/* Get list of Accounts */
|
||||||
|
auto root{gnc_book_get_root_account (gnc_get_current_book())};
|
||||||
|
auto accts{gnc_account_get_descendants_sorted (root)};
|
||||||
|
auto str_or_empty = [](const char* a){ return a ? a : ""; };
|
||||||
|
auto bool_to_char = [](bool b){ return b ? "T" : "F"; };
|
||||||
|
|
||||||
|
/* Go through list of accounts */
|
||||||
|
for (GList *ptr = accts; !info->failed && ptr; ptr = g_list_next (ptr))
|
||||||
|
{
|
||||||
|
auto acc{static_cast<Account*>(ptr->data)};
|
||||||
|
DEBUG("Account being processed is : %s", xaccAccountGetName (acc));
|
||||||
|
|
||||||
|
StringVec line = {
|
||||||
|
xaccAccountTypeEnumAsString (xaccAccountGetType (acc)),
|
||||||
|
account_get_fullname_str (acc),
|
||||||
|
xaccAccountGetName (acc),
|
||||||
|
str_or_empty (xaccAccountGetCode (acc)),
|
||||||
|
str_or_empty (xaccAccountGetDescription (acc)),
|
||||||
|
str_or_empty (xaccAccountGetColor (acc)),
|
||||||
|
str_or_empty (xaccAccountGetNotes (acc)),
|
||||||
|
gnc_commodity_get_mnemonic (xaccAccountGetCommodity (acc)),
|
||||||
|
gnc_commodity_get_namespace (xaccAccountGetCommodity (acc)),
|
||||||
|
bool_to_char (xaccAccountGetHidden (acc)),
|
||||||
|
bool_to_char (xaccAccountGetTaxRelated (acc)),
|
||||||
|
bool_to_char (xaccAccountGetPlaceholder (acc)),
|
||||||
|
};
|
||||||
|
info->failed = !gnc_csv_add_line (ss, line, info->use_quotes, info->separator_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_list_free (accts);
|
||||||
|
LEAVE("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,10 +29,18 @@
|
|||||||
|
|
||||||
#include "assistant-csv-export.h"
|
#include "assistant-csv-export.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
/** The csv_tree_export() will let the user export the
|
/** The csv_tree_export() will let the user export the
|
||||||
* account tree to a delimited file.
|
* account tree to a delimited file.
|
||||||
*/
|
*/
|
||||||
void csv_tree_export (CsvExportInfo *info);
|
void csv_tree_export (CsvExportInfo *info);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -328,7 +328,7 @@ gnucash/import-export/bi-import/gnc-plugin-bi-import.c
|
|||||||
gnucash/import-export/csv-exp/assistant-csv-export.c
|
gnucash/import-export/csv-exp/assistant-csv-export.c
|
||||||
gnucash/import-export/csv-exp/csv-export-helpers.cpp
|
gnucash/import-export/csv-exp/csv-export-helpers.cpp
|
||||||
gnucash/import-export/csv-exp/csv-transactions-export.c
|
gnucash/import-export/csv-exp/csv-transactions-export.c
|
||||||
gnucash/import-export/csv-exp/csv-tree-export.c
|
gnucash/import-export/csv-exp/csv-tree-export.cpp
|
||||||
gnucash/import-export/csv-exp/gnc-plugin-csv-export.c
|
gnucash/import-export/csv-exp/gnc-plugin-csv-export.c
|
||||||
gnucash/import-export/csv-imp/assistant-csv-account-import.c
|
gnucash/import-export/csv-imp/assistant-csv-account-import.c
|
||||||
gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
|
gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
|
||||||
|
Loading…
Reference in New Issue
Block a user